From 7755353821d23dd4b3d851c6ad6e437c46b56d9b Mon Sep 17 00:00:00 2001
Date: Tue, 5 Sep 2023 15:08:35 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20aop=20=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 packages/flutter_tools/bin/flutter_tools.dart |  2 +
 packages/flutter_tools/lib/src/compile.dart   |  9 +++-
 packages/flutter_tools/lib/src/hook.dart      | 44 +++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 packages/flutter_tools/lib/src/hook.dart

diff --git a/packages/flutter_tools/bin/flutter_tools.dart b/packages/flutter_tools/bin/flutter_tools.dart
index 2caa134a35..91f2351cfa 100644
--- a/packages/flutter_tools/bin/flutter_tools.dart
+++ b/packages/flutter_tools/bin/flutter_tools.dart
@@ -3,7 +3,9 @@
 // found in the LICENSE file.
 
 import 'package:flutter_tools/executable.dart' as executable;
+import 'package:flutter_tools/src/hook.dart';
 
 void main(List<String> args) {
+  print('TDFlutterAop version:${TransformHook.flutterVersion}, execute command: ${args.join(' ')}');
   executable.main(args);
 }
diff --git a/packages/flutter_tools/lib/src/compile.dart b/packages/flutter_tools/lib/src/compile.dart
index 5b1422b8ba..2758bd1374 100644
--- a/packages/flutter_tools/lib/src/compile.dart
+++ b/packages/flutter_tools/lib/src/compile.dart
@@ -2,6 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+import 'hook.dart';
 import 'dart:async';
 import 'dart:typed_data';
 
@@ -245,7 +246,12 @@ class KernelCompiler {
     required PackageConfig packageConfig,
   }) async {
     final TargetPlatform? platform = targetModel == TargetModel.dartdevc ? TargetPlatform.web_javascript : null;
-    final String frontendServer = _artifacts.getArtifactPath(
+    /// 插桩工程，有限
+    String? frontendServer = await TransformHook.fetchTransformSnapshot();
+    print('TDFlutterAop isEnable: ${frontendServer != null}');
+
+    /// 默认
+    frontendServer ??= _artifacts.getArtifactPath(
       Artifact.frontendServerSnapshotForEngineDartSdk,
       platform: platform,
     );
@@ -341,6 +347,7 @@ class KernelCompiler {
       mainUri,
     ];
 
+    print('TDFlutterAop compile command: ${command.join(' ')}');
     _logger.printTrace(command.join(' '));
     final Process server = await _processManager.start(command);
 
diff --git a/packages/flutter_tools/lib/src/hook.dart b/packages/flutter_tools/lib/src/hook.dart
new file mode 100644
index 0000000000..02385332c4
--- /dev/null
+++ b/packages/flutter_tools/lib/src/hook.dart
@@ -0,0 +1,44 @@
+// Copyright 2018 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'base/common.dart';
+import 'base/file_system.dart';
+import 'globals.dart' as globals;
+
+// ignore: avoid_classes_with_only_static_members
+/// transform
+class TransformHook {
+  //snapshot名
+  static const String frontendServerDartSnapshot = 'frontend_server.dart.snapshot';
+  static const String flutterVersion = '3.10.0';
+
+  //获取frontedServer路径，位于主工程../demo_tool/snapshot/$flutterVersion/
+  static Future<Directory> getFlutterFrontendServerDirectory(String path) async {
+    return globals.fs.directory(globals.fs.path.join(path, '..', 'demo_tool', 'aop', flutterVersion));
+  }
+
+  //获取FlutterAop的frontend_server snapshot路径
+  static Future<String?> fetchTransformSnapshot() async {
+    final Directory currentDirectory = globals.fs.currentDirectory;
+    if (!currentDirectory.existsSync()) {
+      return null;
+    }
+    final String packagesPath = currentDirectory.absolute.path;
+
+    final Directory flutterFrontendServerDirectory = await getFlutterFrontendServerDirectory(packagesPath);
+
+    if (!flutterFrontendServerDirectory.existsSync()) {
+      return null;
+    }
+
+    final String hookFlutterFrontendServerSnapshot = globals.fs.path.join(flutterFrontendServerDirectory.absolute.path, frontendServerDartSnapshot);
+
+    //每次都重新生成FrontendServerSnapshot
+    if (globals.fs.file(hookFlutterFrontendServerSnapshot).existsSync()) {
+      return hookFlutterFrontendServerSnapshot;
+    }
+    throwToolExit('TDFlutterAop execute snapshot not exist, path: $hookFlutterFrontendServerSnapshot');
+    return null;
+  }
+}
-- 
2.23.0

