@@ -48,114 +48,121 @@ const String _kVMSnapshotData = 'vm_snapshot_data';
48
48
const String _kIsolateSnapshotData = 'isolate_snapshot_data' ;
49
49
const String _kIsolateSnapshotInstr = 'isolate_snapshot_instr' ;
50
50
51
- Future <void > build ({
52
- TargetPlatform platform,
53
- BuildMode buildMode,
54
- String mainPath = defaultMainPath,
55
- String manifestPath = defaultManifestPath,
56
- String applicationKernelFilePath,
57
- String depfilePath,
58
- String privateKeyPath = defaultPrivateKeyPath,
59
- String assetDirPath,
60
- String packagesPath,
61
- bool precompiledSnapshot = false ,
62
- bool reportLicensedPackages = false ,
63
- bool trackWidgetCreation = false ,
64
- String compilationTraceFilePath,
65
- List <String > extraFrontEndOptions = const < String > [],
66
- List <String > extraGenSnapshotOptions = const < String > [],
67
- List <String > fileSystemRoots,
68
- String fileSystemScheme,
69
- }) async {
70
- depfilePath ?? = defaultDepfilePath;
71
- assetDirPath ?? = getAssetBuildDirectory ();
72
- packagesPath ?? = fs.path.absolute (PackageMap .globalPackagesPath);
73
- applicationKernelFilePath ?? = getDefaultApplicationKernelPath (trackWidgetCreation: trackWidgetCreation);
74
- final FlutterProject flutterProject = FlutterProject .current ();
51
+ /// Provides a `build` method that builds the bundle.
52
+ class BundleBuilder {
53
+ /// Builds the bundle for the given target platform.
54
+ ///
55
+ /// The default `mainPath` is `lib/main.dart` .
56
+ /// The default `manifestPath` is `pubspec.yaml`
57
+ Future <void > build ({
58
+ TargetPlatform platform,
59
+ BuildMode buildMode,
60
+ String mainPath = defaultMainPath,
61
+ String manifestPath = defaultManifestPath,
62
+ String applicationKernelFilePath,
63
+ String depfilePath,
64
+ String privateKeyPath = defaultPrivateKeyPath,
65
+ String assetDirPath,
66
+ String packagesPath,
67
+ bool precompiledSnapshot = false ,
68
+ bool reportLicensedPackages = false ,
69
+ bool trackWidgetCreation = false ,
70
+ String compilationTraceFilePath,
71
+ List <String > extraFrontEndOptions = const < String > [],
72
+ List <String > extraGenSnapshotOptions = const < String > [],
73
+ List <String > fileSystemRoots,
74
+ String fileSystemScheme,
75
+ }) async {
76
+ depfilePath ?? = defaultDepfilePath;
77
+ assetDirPath ?? = getAssetBuildDirectory ();
78
+ packagesPath ?? = fs.path.absolute (PackageMap .globalPackagesPath);
79
+ applicationKernelFilePath ?? = getDefaultApplicationKernelPath (trackWidgetCreation: trackWidgetCreation);
80
+ final FlutterProject flutterProject = FlutterProject .current ();
75
81
76
- if (compilationTraceFilePath != null ) {
77
- if (buildMode != BuildMode .dynamicProfile && buildMode != BuildMode .dynamicRelease) {
78
- // Silently ignore JIT snapshotting for those builds that don't support it.
79
- compilationTraceFilePath = null ;
80
-
81
- } else if (compilationTraceFilePath.isEmpty) {
82
- // Disable JIT snapshotting if flag is empty.
83
- printStatus ('Code snapshot will be disabled for this build.' );
84
- compilationTraceFilePath = null ;
85
-
86
- } else if (! fs.file (compilationTraceFilePath).existsSync ()) {
87
- // Be forgiving if compilation trace file is missing.
88
- printStatus ('No compilation trace available. To optimize performance, consider using --train.' );
89
- final File tmp = fs.systemTempDirectory.childFile ('flutterEmptyCompilationTrace.txt' );
90
- compilationTraceFilePath = (tmp..createSync (recursive: true )).path;
91
-
92
- } else {
93
- printStatus ('Code snapshot will use compilation training file $compilationTraceFilePath .' );
94
- }
95
- }
96
-
97
- DevFSContent kernelContent;
98
- if (! precompiledSnapshot) {
99
- if ((extraFrontEndOptions != null ) && extraFrontEndOptions.isNotEmpty)
100
- printTrace ('Extra front-end options: $extraFrontEndOptions ' );
101
- ensureDirectoryExists (applicationKernelFilePath);
102
- final KernelCompiler kernelCompiler = await kernelCompilerFactory.create (flutterProject);
103
- final CompilerOutput compilerOutput = await kernelCompiler.compile (
104
- sdkRoot: artifacts.getArtifactPath (Artifact .flutterPatchedSdkPath, mode: buildMode),
105
- incrementalCompilerByteStorePath: compilationTraceFilePath != null ? null :
106
- fs.path.absolute (getIncrementalCompilerByteStoreDirectory ()),
107
- mainPath: fs.file (mainPath).absolute.path,
108
- outputFilePath: applicationKernelFilePath,
109
- depFilePath: depfilePath,
110
- trackWidgetCreation: trackWidgetCreation,
111
- extraFrontEndOptions: extraFrontEndOptions,
112
- fileSystemRoots: fileSystemRoots,
113
- fileSystemScheme: fileSystemScheme,
114
- packagesPath: packagesPath,
115
- linkPlatformKernelIn: compilationTraceFilePath != null ,
116
- );
117
- if (compilerOutput? .outputFilename == null ) {
118
- throwToolExit ('Compiler failed on $mainPath ' );
82
+ if (compilationTraceFilePath != null ) {
83
+ if (buildMode != BuildMode .dynamicProfile && buildMode != BuildMode .dynamicRelease) {
84
+ // Silently ignore JIT snapshotting for those builds that don't support it.
85
+ compilationTraceFilePath = null ;
86
+
87
+ } else if (compilationTraceFilePath.isEmpty) {
88
+ // Disable JIT snapshotting if flag is empty.
89
+ printStatus ('Code snapshot will be disabled for this build.' );
90
+ compilationTraceFilePath = null ;
91
+
92
+ } else if (! fs.file (compilationTraceFilePath).existsSync ()) {
93
+ // Be forgiving if compilation trace file is missing.
94
+ printStatus ('No compilation trace available. To optimize performance, consider using --train.' );
95
+ final File tmp = fs.systemTempDirectory.childFile ('flutterEmptyCompilationTrace.txt' );
96
+ compilationTraceFilePath = (tmp..createSync (recursive: true )).path;
97
+
98
+ } else {
99
+ printStatus ('Code snapshot will use compilation training file $compilationTraceFilePath .' );
100
+ }
119
101
}
120
- kernelContent = DevFSFileContent (fs.file (compilerOutput.outputFilename));
121
-
122
- await fs.directory (getBuildDirectory ()).childFile ('frontend_server.d' )
123
- .writeAsString ('frontend_server.d: ${artifacts .getArtifactPath (Artifact .frontendServerSnapshotForEngineDartSdk )}\n ' );
124
102
125
- if (compilationTraceFilePath != null ) {
126
- final JITSnapshotter snapshotter = JITSnapshotter ();
127
- final int snapshotExitCode = await snapshotter.build (
128
- platform: platform,
129
- buildMode: buildMode,
130
- mainPath: applicationKernelFilePath,
131
- outputPath: getBuildDirectory (),
103
+ DevFSContent kernelContent;
104
+ if (! precompiledSnapshot) {
105
+ if ((extraFrontEndOptions != null ) && extraFrontEndOptions.isNotEmpty)
106
+ printTrace ('Extra front-end options: $extraFrontEndOptions ' );
107
+ ensureDirectoryExists (applicationKernelFilePath);
108
+ final KernelCompiler kernelCompiler = await kernelCompilerFactory.create (flutterProject);
109
+ final CompilerOutput compilerOutput = await kernelCompiler.compile (
110
+ sdkRoot: artifacts.getArtifactPath (Artifact .flutterPatchedSdkPath, mode: buildMode),
111
+ incrementalCompilerByteStorePath: compilationTraceFilePath != null ? null :
112
+ fs.path.absolute (getIncrementalCompilerByteStoreDirectory ()),
113
+ mainPath: fs.file (mainPath).absolute.path,
114
+ outputFilePath: applicationKernelFilePath,
115
+ depFilePath: depfilePath,
116
+ trackWidgetCreation: trackWidgetCreation,
117
+ extraFrontEndOptions: extraFrontEndOptions,
118
+ fileSystemRoots: fileSystemRoots,
119
+ fileSystemScheme: fileSystemScheme,
132
120
packagesPath: packagesPath,
133
- compilationTraceFilePath: compilationTraceFilePath,
134
- extraGenSnapshotOptions: extraGenSnapshotOptions,
121
+ linkPlatformKernelIn: compilationTraceFilePath != null ,
135
122
);
136
- if (snapshotExitCode != 0 ) {
137
- throwToolExit ('Snapshotting exited with non-zero exit code: $snapshotExitCode ' );
123
+ if (compilerOutput? .outputFilename == null ) {
124
+ throwToolExit ('Compiler failed on $mainPath ' );
125
+ }
126
+ kernelContent = DevFSFileContent (fs.file (compilerOutput.outputFilename));
127
+
128
+ await fs.directory (getBuildDirectory ()).childFile ('frontend_server.d' )
129
+ .writeAsString ('frontend_server.d: ${artifacts .getArtifactPath (Artifact .frontendServerSnapshotForEngineDartSdk )}\n ' );
130
+
131
+ if (compilationTraceFilePath != null ) {
132
+ final JITSnapshotter snapshotter = JITSnapshotter ();
133
+ final int snapshotExitCode = await snapshotter.build (
134
+ platform: platform,
135
+ buildMode: buildMode,
136
+ mainPath: applicationKernelFilePath,
137
+ outputPath: getBuildDirectory (),
138
+ packagesPath: packagesPath,
139
+ compilationTraceFilePath: compilationTraceFilePath,
140
+ extraGenSnapshotOptions: extraGenSnapshotOptions,
141
+ );
142
+ if (snapshotExitCode != 0 ) {
143
+ throwToolExit ('Snapshotting exited with non-zero exit code: $snapshotExitCode ' );
144
+ }
138
145
}
139
146
}
140
- }
141
147
142
- final AssetBundle assets = await buildAssets (
143
- manifestPath: manifestPath,
144
- assetDirPath: assetDirPath,
145
- packagesPath: packagesPath,
146
- reportLicensedPackages: reportLicensedPackages,
147
- );
148
- if (assets == null )
149
- throwToolExit ('Error building assets' , exitCode: 1 );
150
-
151
- await assemble (
152
- buildMode: buildMode,
153
- assetBundle: assets,
154
- kernelContent: kernelContent,
155
- privateKeyPath: privateKeyPath,
156
- assetDirPath: assetDirPath,
157
- compilationTraceFilePath: compilationTraceFilePath,
158
- );
148
+ final AssetBundle assets = await buildAssets (
149
+ manifestPath: manifestPath,
150
+ assetDirPath: assetDirPath,
151
+ packagesPath: packagesPath,
152
+ reportLicensedPackages: reportLicensedPackages,
153
+ );
154
+ if (assets == null )
155
+ throwToolExit ('Error building assets' , exitCode: 1 );
156
+
157
+ await assemble (
158
+ buildMode: buildMode,
159
+ assetBundle: assets,
160
+ kernelContent: kernelContent,
161
+ privateKeyPath: privateKeyPath,
162
+ assetDirPath: assetDirPath,
163
+ compilationTraceFilePath: compilationTraceFilePath,
164
+ );
165
+ }
159
166
}
160
167
161
168
Future <AssetBundle > buildAssets ({
0 commit comments