Skip to content

Commit a2035c9

Browse files
stuartmorgan-gfotiDim
authored andcommitted
[flutter_plugin_tools] Migrate publish-check to the new base command (flutter#4119)
To support this command's --machine flag, which moves all normal output into a field in a JSON struct, adds a way of capturing output and providing it to the command subclass on completion. Part of flutter/flutter#83413
1 parent e2657a2 commit a2035c9

File tree

4 files changed

+209
-136
lines changed

4 files changed

+209
-136
lines changed

script/tool/lib/src/common/package_looping_command.dart

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:async';
6+
57
import 'package:colorize/colorize.dart';
68
import 'package:file/file.dart';
79
import 'package:git/git.dart';
@@ -90,6 +92,10 @@ abstract class PackageLoopingCommand extends PluginCommand {
9092
/// opportunity to do any cleanup of run-level state.
9193
Future<void> completeRun() async {}
9294

95+
/// If [captureOutput], this is called just before exiting with all captured
96+
/// [output].
97+
Future<void> handleCapturedOutput(List<String> output) async {}
98+
9399
/// Whether or not the output (if any) of [runForPackage] is long, or short.
94100
///
95101
/// This changes the logging that happens at the start of each package's
@@ -120,6 +126,14 @@ abstract class PackageLoopingCommand extends PluginCommand {
120126
/// context.
121127
String get failureListFooter => 'See above for full details.';
122128

129+
/// If true, all printing (including the summary) will be redirected to a
130+
/// buffer, and provided in a call to [handleCapturedOutput] at the end of
131+
/// the run.
132+
///
133+
/// Capturing output will disable any colorizing of output from this base
134+
/// class.
135+
bool get captureOutput => false;
136+
123137
// ----------------------------------------
124138

125139
/// Logs that a warning occurred, and prints `warningMessage` in yellow.
@@ -162,6 +176,26 @@ abstract class PackageLoopingCommand extends PluginCommand {
162176

163177
@override
164178
Future<void> run() async {
179+
bool succeeded;
180+
if (captureOutput) {
181+
final List<String> output = <String>[];
182+
final ZoneSpecification logSwitchSpecification = ZoneSpecification(
183+
print: (Zone self, ZoneDelegate parent, Zone zone, String message) {
184+
output.add(message);
185+
});
186+
succeeded = await runZoned<Future<bool>>(_runInternal,
187+
zoneSpecification: logSwitchSpecification);
188+
await handleCapturedOutput(output);
189+
} else {
190+
succeeded = await _runInternal();
191+
}
192+
193+
if (!succeeded) {
194+
throw ToolExit(exitCommandFoundErrors);
195+
}
196+
}
197+
198+
Future<bool> _runInternal() async {
165199
_packagesWithWarnings.clear();
166200
_otherWarningCount = 0;
167201
_currentPackage = null;
@@ -178,28 +212,39 @@ abstract class PackageLoopingCommand extends PluginCommand {
178212
_printPackageHeading(package);
179213
final PackageResult result = await runForPackage(package);
180214
if (result.state == RunState.skipped) {
181-
print(Colorize('${indentation}SKIPPING: ${result.details.first}')
182-
..darkGray());
215+
final String message =
216+
'${indentation}SKIPPING: ${result.details.first}';
217+
captureOutput ? print(message) : print(Colorize(message)..darkGray());
183218
}
184219
results[package] = result;
185220
}
186221
_currentPackage = null;
187222

188223
completeRun();
189224

225+
print('\n');
190226
// If there were any errors reported, summarize them and exit.
191227
if (results.values
192228
.any((PackageResult result) => result.state == RunState.failed)) {
193229
_printFailureSummary(packages, results);
194-
throw ToolExit(exitCommandFoundErrors);
230+
return false;
195231
}
196232

197233
// Otherwise, print a summary of what ran for ease of auditing that all the
198234
// expected tests ran.
199235
_printRunSummary(packages, results);
200236

201237
print('\n');
202-
printSuccess('No issues found!');
238+
_printSuccess('No issues found!');
239+
return true;
240+
}
241+
242+
void _printSuccess(String message) {
243+
captureOutput ? print(message) : printSuccess(message);
244+
}
245+
246+
void _printError(String message) {
247+
captureOutput ? print(message) : printError(message);
203248
}
204249

205250
/// Prints the status message indicating that the command is being run for
@@ -220,7 +265,7 @@ abstract class PackageLoopingCommand extends PluginCommand {
220265
} else {
221266
heading = '$heading...';
222267
}
223-
print(Colorize(heading)..cyan());
268+
captureOutput ? print(heading) : print(Colorize(heading)..cyan());
224269
}
225270

226271
/// Prints a summary of packges run, packages skipped, and warnings.
@@ -265,19 +310,21 @@ abstract class PackageLoopingCommand extends PluginCommand {
265310
print('Run overview:');
266311
for (final Directory package in packages) {
267312
final bool hadWarning = _packagesWithWarnings.contains(package);
268-
Colorize summary;
313+
Styles style;
314+
String summary;
269315
if (skipped.contains(package)) {
270-
if (hadWarning) {
271-
summary = Colorize('skipped (with warning)')..lightYellow();
272-
} else {
273-
summary = Colorize('skipped')..darkGray();
274-
}
316+
summary = 'skipped';
317+
style = hadWarning ? Styles.LIGHT_YELLOW : Styles.DARK_GRAY;
275318
} else {
276-
if (hadWarning) {
277-
summary = Colorize('ran (with warning)')..yellow();
278-
} else {
279-
summary = Colorize('ran')..green();
280-
}
319+
summary = 'ran';
320+
style = hadWarning ? Styles.YELLOW : Styles.GREEN;
321+
}
322+
if (hadWarning) {
323+
summary += ' (with warning)';
324+
}
325+
326+
if (!captureOutput) {
327+
summary = (Colorize(summary)..apply(style)).toString();
281328
}
282329
print(' ${getPackageDescription(package)} - $summary');
283330
}
@@ -288,7 +335,7 @@ abstract class PackageLoopingCommand extends PluginCommand {
288335
void _printFailureSummary(
289336
List<Directory> packages, Map<Directory, PackageResult> results) {
290337
const String indentation = ' ';
291-
printError(failureListHeader);
338+
_printError(failureListHeader);
292339
for (final Directory package in packages) {
293340
final PackageResult result = results[package]!;
294341
if (result.state == RunState.failed) {
@@ -298,10 +345,10 @@ abstract class PackageLoopingCommand extends PluginCommand {
298345
errorDetails =
299346
':\n$errorIndentation${result.details.join('\n$errorIndentation')}';
300347
}
301-
printError(
348+
_printError(
302349
'$indentation${getPackageDescription(package)}$errorDetails');
303350
}
304351
}
305-
printError(failureListFooter);
352+
_printError(failureListFooter);
306353
}
307354
}

0 commit comments

Comments
 (0)