@@ -41,6 +41,18 @@ abstract class CodeGenerator {
41
41
// Generates a synthetic package under .dart_tool/flutter_tool which is in turn
42
42
// used to generate a build script.
43
43
Future <void > generateBuildScript (FlutterProject flutterProject);
44
+
45
+ /// Create generated packages file which adds a multi-root scheme to the user's
46
+ /// project directory. Currently we only replace the root package with a multiroot
47
+ /// scheme. To support codegen on arbitrary packages we would need to do
48
+ /// this for each dependency.
49
+ void updatePackages (FlutterProject flutterProject) {
50
+ final String oldPackagesContents = fs.file (PackageMap .globalPackagesPath).readAsStringSync ();
51
+ final String appName = flutterProject.manifest.appName;
52
+ final String newPackagesContents = oldPackagesContents.replaceFirst ('$appName :lib/' , '$appName :$kMultiRootScheme :/' );
53
+ final String generatedPackagesPath = fs.path.setExtension (PackageMap .globalPackagesPath, '.generated' );
54
+ fs.file (generatedPackagesPath).writeAsStringSync (newPackagesContents);
55
+ }
44
56
}
45
57
46
58
class UnsupportedCodeGenerator extends CodeGenerator {
@@ -104,6 +116,7 @@ class CodeGeneratingKernelCompiler implements KernelCompiler {
104
116
'build* pipeline' );
105
117
}
106
118
final FlutterProject flutterProject = await FlutterProject .current ();
119
+ codeGenerator.updatePackages (flutterProject);
107
120
final CodegenDaemon codegenDaemon = await codeGenerator.daemon (flutterProject);
108
121
codegenDaemon.startBuild ();
109
122
await for (CodegenStatus codegenStatus in codegenDaemon.buildResults) {
@@ -141,7 +154,7 @@ class CodeGeneratingKernelCompiler implements KernelCompiler {
141
154
/// An implementation of a [ResidentCompiler] which runs a [BuildRunner] before
142
155
/// talking to the CFE.
143
156
class CodeGeneratingResidentCompiler implements ResidentCompiler {
144
- CodeGeneratingResidentCompiler ._(this ._residentCompiler, this ._codegenDaemon);
157
+ CodeGeneratingResidentCompiler ._(this ._residentCompiler, this ._codegenDaemon, this ._flutterProject );
145
158
146
159
/// Creates a new [ResidentCompiler] and configures a [BuildDaemonClient] to
147
160
/// run builds.
@@ -158,6 +171,7 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
158
171
String initializeFromDill,
159
172
bool runCold = false ,
160
173
}) async {
174
+ codeGenerator.updatePackages (flutterProject);
161
175
final ResidentCompiler residentCompiler = ResidentCompiler (
162
176
artifacts.getArtifactPath (Artifact .flutterPatchedSdkPath),
163
177
trackWidgetCreation: trackWidgetCreation,
@@ -182,11 +196,12 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
182
196
if (status == CodegenStatus .Failed ) {
183
197
printError ('Code generation failed, build may have compile errors.' );
184
198
}
185
- return CodeGeneratingResidentCompiler ._(residentCompiler, codegenDaemon);
199
+ return CodeGeneratingResidentCompiler ._(residentCompiler, codegenDaemon, flutterProject );
186
200
}
187
201
188
202
final ResidentCompiler _residentCompiler;
189
203
final CodegenDaemon _codegenDaemon;
204
+ final FlutterProject _flutterProject;
190
205
191
206
@override
192
207
void accept () {
@@ -206,7 +221,13 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
206
221
});
207
222
}
208
223
if (_codegenDaemon.lastStatus == CodegenStatus .Failed ) {
209
- printError ('Codegeneration failed, halting build.' );
224
+ printError ('Code generation failed, build may have compile errors.' );
225
+ }
226
+ // Update the generated packages file if the original packages file has changes.
227
+ if (fs.statSync (PackageMap .globalPackagesPath).modified.millisecondsSinceEpoch >
228
+ fs.statSync (PackageMap .globalGeneratedPackagesPath).modified.millisecondsSinceEpoch) {
229
+ codeGenerator.updatePackages (_flutterProject);
230
+ invalidatedFiles.add (fs.file (PackageMap .globalGeneratedPackagesPath).uri);
210
231
}
211
232
return _residentCompiler.recompile (
212
233
mainPath,
0 commit comments