forked from XianyuTech/aspectd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0001-aspectd.patch
260 lines (254 loc) · 10.4 KB
/
0001-aspectd.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
From 9a94fddaf7ceedfcbef267b293634e5fbcfb0996 Mon Sep 17 00:00:00 2001
From: "kylewong.wk" <[email protected]>
Date: Wed, 23 Jun 2021 01:42:43 +0800
Subject: [PATCH] aspectd
---
packages/flutter_tools/lib/src/aspectd.dart | 206 ++++++++++++++++++
.../lib/src/build_system/targets/common.dart | 9 +
2 files changed, 215 insertions(+)
create mode 100644 packages/flutter_tools/lib/src/aspectd.dart
diff --git a/packages/flutter_tools/lib/src/aspectd.dart b/packages/flutter_tools/lib/src/aspectd.dart
new file mode 100644
index 0000000000..773caaa1c9
--- /dev/null
+++ b/packages/flutter_tools/lib/src/aspectd.dart
@@ -0,0 +1,206 @@
+// 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 'dart:io';
+
+import 'package:package_config/package_config.dart';
+
+import 'artifacts.dart';
+import 'base/common.dart';
+import 'base/file_system.dart';
+import 'build_system/build_system.dart';
+import 'build_system/targets/common.dart';
+import 'cache.dart';
+import 'compile.dart';
+import 'dart/package_map.dart';
+import 'globals.dart' as globals;
+
+const String aspectdImplPackageRelPath = '..';
+const String aspectdImplPackageName = 'aspectd_impl';
+const String frontendServerDartSnapshot = 'frontend_server.dart.snapshot';
+
+class AspectdHook {
+ static String? aspectdTransformerSnapshot;
+
+ static Directory getAspectdImplDirectory(Directory rootProjectDir) {
+ return globals.fs.directory(globals.fs.path.normalize(globals.fs.path.join(
+ rootProjectDir.path,
+ aspectdImplPackageRelPath,
+ aspectdImplPackageName)));
+ }
+
+ static Future<Directory> getPackagePathFromConfig(
+ String packageConfigPath, String packageName) async {
+ final PackageConfig packageConfig = await loadPackageConfigWithLogging(
+ globals.fs.file(packageConfigPath),
+ logger: globals.logger,
+ );
+ if ((packageConfig?.packages?.length ?? 0) > 0) {
+ final Package aspectdPackage = packageConfig.packages
+ .toList()
+ .firstWhere((Package element) => element.name == packageName);
+ return globals.fs.directory(aspectdPackage.root.toFilePath());
+ }
+ return Future.value(null);
+ }
+
+ static Future<Directory> getFlutterFrontendServerDirectory(
+ String packagesPath) async {
+ return globals.fs.directory(globals.fs.path.join(
+ (await getPackagePathFromConfig(packagesPath, 'aspectd')).absolute.path,
+ 'lib',
+ 'src',
+ 'flutter_frontend_server'));
+ }
+
+ static Future<bool> isAspectdEnabled() async {
+ final Directory currentDirectory = globals.fs.currentDirectory;
+ final Directory aspectdDirectory =
+ getAspectdImplDirectory(currentDirectory);
+ if (!aspectdDirectory.existsSync()) {
+ return false;
+ }
+ final String aspectdImplPackagesPath =
+ globals.fs.path.join(aspectdDirectory.absolute.path, '.packages');
+ final Directory flutterFrontendServerDirectory =
+ await getFlutterFrontendServerDirectory(aspectdImplPackagesPath);
+ if (!(aspectdDirectory.existsSync() &&
+ flutterFrontendServerDirectory.existsSync() &&
+ currentDirectory.absolute.path != aspectdDirectory.absolute.path &&
+ globals.fs
+ .file(globals.fs.path.join(aspectdDirectory.path, 'pubspec.yaml'))
+ .existsSync() &&
+ globals.fs
+ .file(globals.fs.path.join(aspectdDirectory.path, '.packages'))
+ .existsSync() &&
+ globals.fs
+ .file(globals.fs.path.join(
+ aspectdDirectory.path, 'lib', aspectdImplPackageName + '.dart'))
+ .existsSync())) {
+ return false;
+ }
+ return await checkAspectdFlutterFrontendServerSnapshot(
+ aspectdImplPackagesPath);
+ }
+
+ static Future<bool> checkAspectdFlutterFrontendServerSnapshot(
+ String packagesPath) async {
+ final Directory flutterFrontendServerDirectory =
+ await getFlutterFrontendServerDirectory(packagesPath);
+ final String aspectdFlutterFrontendServerSnapshot = globals.fs.path.join(
+ flutterFrontendServerDirectory.absolute.path,
+ frontendServerDartSnapshot);
+ final String defaultFlutterFrontendServerSnapshot = globals.artifacts
+ .getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk);
+ if (!globals.fs.file(aspectdFlutterFrontendServerSnapshot).existsSync()) {
+ final String dartSdkDir = await getDartSdkDependency(
+ (await getPackagePathFromConfig(packagesPath, 'aspectd'))
+ .absolute
+ .path);
+
+ final String frontendServerPackageConfigJsonFile =
+ '${flutterFrontendServerDirectory.absolute.path}/package_config.json';
+ final String rebasedFrontendServerPackageConfigJsonFile =
+ '${flutterFrontendServerDirectory.absolute.path}/rebased_package_config.json';
+ String frontendServerPackageConfigJson = globals.fs
+ .file(frontendServerPackageConfigJsonFile)
+ .readAsStringSync();
+ frontendServerPackageConfigJson = frontendServerPackageConfigJson
+ .replaceAll('../../../third_party/dart/', dartSdkDir);
+ globals.fs
+ .file(rebasedFrontendServerPackageConfigJsonFile)
+ .writeAsStringSync(frontendServerPackageConfigJson);
+
+ final List<String> commands = <String>[
+ globals.artifacts.getArtifactPath(Artifact.engineDartBinary),
+ '--deterministic',
+ '--packages=$rebasedFrontendServerPackageConfigJsonFile',
+ '--snapshot=$aspectdFlutterFrontendServerSnapshot',
+ '--snapshot-kind=kernel',
+ '${flutterFrontendServerDirectory.absolute.path}/starter.dart'
+ ];
+ final ProcessResult processResult =
+ await globals.processManager.run(commands);
+ globals.fs.file(rebasedFrontendServerPackageConfigJsonFile).deleteSync();
+ if (processResult.exitCode != 0 ||
+ globals.fs.file(aspectdFlutterFrontendServerSnapshot).existsSync() ==
+ false) {
+ throwToolExit(
+ 'Aspectd unexpected error: ${processResult.stderr.toString()}');
+ }
+ }
+ if (globals.fs.file(defaultFlutterFrontendServerSnapshot).existsSync()) {
+ globals.fs.file(defaultFlutterFrontendServerSnapshot).deleteSync();
+ }
+ globals.fs
+ .file(aspectdFlutterFrontendServerSnapshot)
+ .copySync(defaultFlutterFrontendServerSnapshot);
+ return true;
+ }
+
+ static Future<String> getDartSdkDependency(String aspectdDir) async {
+ final ProcessResult processResult = await globals.processManager.run(
+ <String>[
+ globals.fs.path.join(
+ globals.artifacts.getArtifactPath(Artifact.engineDartSdkPath),
+ 'bin',
+ 'pub'),
+ 'get',
+ '--verbosity=warning'
+ ],
+ workingDirectory: aspectdDir,
+ environment: <String, String>{'FLUTTER_ROOT': Cache.flutterRoot ?? ""});
+ if (processResult.exitCode != 0) {
+ throwToolExit(
+ 'Aspectd unexpected error: ${processResult.stderr.toString()}');
+ }
+ final Directory kernelDir = await getPackagePathFromConfig(
+ globals.fs.path.join(aspectdDir, '.packages'), 'kernel');
+ return kernelDir.parent.parent.uri.toString();
+ }
+
+ Future<void> runBuildDillCommand(Environment environment) async {
+ final Directory aspectdDir =
+ getAspectdImplDirectory(globals.fs.currentDirectory);
+ final Directory previousDirectory = globals.fs.currentDirectory;
+ globals.fs.currentDirectory = aspectdDir;
+
+ String relativeDir = environment.outputDir.absolute.path
+ .substring(environment.projectDir.absolute.path.length + 1);
+ final String outputDir = globals.fs.path.join(aspectdDir.path, relativeDir);
+
+ final String buildDir =
+ globals.fs.path.join(aspectdDir.path, '.dart_tool', 'flutter_build');
+
+ final Map<String, String> defines = environment.defines;
+ defines[kTargetFile] = globals.fs.path
+ .join(aspectdDir.path, 'lib', aspectdImplPackageName + '.dart');
+
+ final Environment auxEnvironment = Environment(
+ projectDir: aspectdDir,
+ outputDir: globals.fs.directory(outputDir),
+ cacheDir: environment.cacheDir,
+ flutterRootDir: environment.flutterRootDir,
+ fileSystem: environment.fileSystem,
+ logger: environment.logger,
+ artifacts: environment.artifacts,
+ processManager: environment.processManager,
+ engineVersion: environment.engineVersion,
+ buildDir: globals.fs.directory(buildDir),
+ defines: defines,
+ inputs: environment.inputs);
+ const KernelSnapshot auxKernelSnapshot = KernelSnapshot();
+ final CompilerOutput compilerOutput =
+ await auxKernelSnapshot.buildImpl(auxEnvironment);
+
+ final String aspectdDill = compilerOutput.outputFilename;
+ final File originalDillFile = globals.fs.file(
+ globals.fs.path.join(environment.buildDir.absolute.path, 'app.dill'));
+ if (originalDillFile.existsSync()) {
+ originalDillFile.renameSync(originalDillFile.absolute.path + '.bak');
+ }
+ globals.fs.file(aspectdDill).copySync(originalDillFile.absolute.path);
+ globals.fs.currentDirectory = previousDirectory;
+ }
+}
diff --git a/packages/flutter_tools/lib/src/build_system/targets/common.dart b/packages/flutter_tools/lib/src/build_system/targets/common.dart
index f22100901d..f514b11893 100644
--- a/packages/flutter_tools/lib/src/build_system/targets/common.dart
+++ b/packages/flutter_tools/lib/src/build_system/targets/common.dart
@@ -7,6 +7,7 @@
import 'package:package_config/package_config.dart';
import '../../artifacts.dart';
+import '../../aspectd.dart';
import '../../base/build.dart';
import '../../base/file_system.dart';
import '../../build_info.dart';
@@ -213,6 +214,13 @@ class KernelSnapshot extends Target {
@override
Future<void> build(Environment environment) async {
+ await buildImpl(environment);
+ if (await AspectdHook.isAspectdEnabled()) {
+ await AspectdHook().runBuildDillCommand(environment);
+ }
+ }
+
+ Future<CompilerOutput> buildImpl(Environment environment) async {
final KernelCompiler compiler = KernelCompiler(
fileSystem: environment.fileSystem,
logger: environment.logger,
@@ -290,6 +298,7 @@ class KernelSnapshot extends Target {
if (output == null || output.errorCount != 0) {
throw Exception();
}
+ return output;
}
}
--
2.30.1 (Apple Git-130)