@@ -28,6 +28,7 @@ void main(List<String> args) async {
2828 destinationPath: parsedArguments.command! ['dest' ],
2929 includeAssets: parsedArguments.command! ['include-assets' ],
3030 branchName: parsedArguments.command! ['branch-name' ],
31+ fix: parsedArguments.command! ['fix' ],
3132 );
3233}
3334
@@ -46,6 +47,12 @@ ArgResults _parseArgs(List<String> args) {
4647 'for the first time or after updating assets.\n '
4748 'Downloading code without assets is typically much faster.' ,
4849 defaultsTo: false ,
50+ )
51+ ..addFlag (
52+ 'fix' ,
53+ negatable: true ,
54+ help: 'Run "dart fix" on the downloaded code.' ,
55+ defaultsTo: false ,
4956 );
5057
5158 final parser = ArgParser ()
@@ -94,6 +101,7 @@ Future _exportCode({
94101 required String projectId,
95102 required String destinationPath,
96103 required bool includeAssets,
104+ required bool fix,
97105 String ? branchName,
98106}) async {
99107 final endpointUrl = Uri .parse (endpoint);
@@ -112,13 +120,22 @@ Future _exportCode({
112120 final projectFolder = ZipDecoder ().decodeBytes (projectZipBytes);
113121 extractArchiveToDisk (projectFolder, destinationPath);
114122
115- // Download assets
116- if (includeAssets) {
117- await _downloadAssets (
118- client: client,
119- destinationPath: destinationPath,
120- assetDescriptions: result['assets' ],
121- );
123+ final postCodeGenerationFutures = < Future > [
124+ if (fix)
125+ _runFix (
126+ destinationPath: destinationPath,
127+ projectFolder: projectFolder,
128+ ),
129+ if (includeAssets)
130+ _downloadAssets (
131+ client: client,
132+ destinationPath: destinationPath,
133+ assetDescriptions: result['assets' ],
134+ ),
135+ ];
136+
137+ if (postCodeGenerationFutures.isNotEmpty) {
138+ await Future .wait (postCodeGenerationFutures);
122139 }
123140 } finally {
124141 client.close ();
@@ -194,3 +211,45 @@ Future _downloadAssets({
194211 });
195212 await Future .wait (futures);
196213}
214+
215+ Future _runFix ({
216+ required String destinationPath,
217+ required Archive projectFolder,
218+ }) async {
219+ try {
220+ if (projectFolder.isEmpty) {
221+ return ;
222+ }
223+ final firstFilePath = projectFolder.files.first.name;
224+ final directory = path_util.split (firstFilePath).first;
225+
226+ final pubGetResult = await Process .run (
227+ 'flutter' ,
228+ ['pub' , 'get' ],
229+ workingDirectory: path_util.join (destinationPath, directory),
230+ runInShell: true ,
231+ stdoutEncoding: utf8,
232+ stderrEncoding: utf8,
233+ );
234+ if (pubGetResult.exitCode != 0 ) {
235+ stderr.write (
236+ '"flutter pub get" failed with code ${pubGetResult .exitCode }, stderr:\n ${pubGetResult .stderr }\n ' );
237+ return ;
238+ }
239+
240+ final dartFixResult = await Process .run (
241+ 'dart' ,
242+ ['fix' , '--apply' , directory],
243+ workingDirectory: destinationPath,
244+ runInShell: true ,
245+ stdoutEncoding: utf8,
246+ stderrEncoding: utf8,
247+ );
248+ if (dartFixResult.exitCode != 0 ) {
249+ stderr.write (
250+ '"dart fix" failed with code ${dartFixResult .exitCode }, stderr:\n ${dartFixResult .stderr }\n ' );
251+ }
252+ } catch (e) {
253+ stderr.write ('Error running "dart fix": $e \n ' );
254+ }
255+ }
0 commit comments