Add/use addMachineOutputFlag/outputsMachineFormat instead of strings (#171459)

Prepares to make changes such as
https://github.com/flutter/flutter/issues/10621 easier.
This commit is contained in:
Matan Lurey 2025-07-08 11:25:22 -07:00 committed by GitHub
parent 70f2ca1965
commit 8449573549
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 30 additions and 57 deletions

View File

@ -87,14 +87,7 @@ class AnalyzeCommand extends FlutterCommand {
hide: !verboseHelp,
);
argParser.addFlag('suggestions', help: 'Show suggestions about the current flutter project.');
argParser.addFlag(
'machine',
negatable: false,
help:
'Dumps a JSON with a subset of relevant data about the tool, project, '
'and environment.',
hide: !verboseHelp,
);
addMachineOutputFlag(verboseHelp: verboseHelp);
// Hidden option to enable a benchmarking mode.
argParser.addFlag(
@ -245,7 +238,7 @@ class AnalyzeCommand extends FlutterCommand {
}
// Don't run pub if asking for machine output.
if (boolArg('machine')) {
if (outputMachineFormat) {
return false;
}
@ -370,7 +363,7 @@ class AnalyzeCommand extends FlutterCommand {
allProjectValidators: _allProjectValidators,
userPath: directoryPath,
processManager: _processManager,
machine: boolArg('machine'),
machine: outputMachineFormat,
).run();
} else if (boolArg('watch')) {
await AnalyzeContinuously(

View File

@ -130,15 +130,8 @@ class AttachCommand extends FlutterCommand {
'using "--machine" instead.',
hide: !verboseHelp,
)
..addOption('project-root', hide: !verboseHelp, help: 'Normally used only in run target.')
..addFlag(
'machine',
hide: !verboseHelp,
negatable: false,
help:
'Handle machine structured JSON command input and provide output '
'and progress in machine-friendly format.',
);
..addOption('project-root', hide: !verboseHelp, help: 'Normally used only in run target.');
addMachineOutputFlag(verboseHelp: verboseHelp);
usesTrackWidgetCreation(verboseHelp: verboseHelp);
addDdsOptions(verboseHelp: verboseHelp);
addDevToolsOptions(verboseHelp: verboseHelp);

View File

@ -61,12 +61,7 @@ class ConfigCommand extends FlutterCommand {
help: 'The relative path to override a projects build directory.',
valueHelp: 'out/',
);
argParser.addFlag(
'machine',
negatable: false,
hide: !verboseHelp,
help: 'Print config values as json.',
);
addMachineOutputFlag(verboseHelp: verboseHelp);
for (final Feature feature in featureFlags.allFeatures) {
final String? configSetting = feature.configSetting;
if (configSetting == null) {
@ -126,7 +121,7 @@ class ConfigCommand extends FlutterCommand {
return FlutterCommandResult.success();
}
if (boolArg('machine')) {
if (outputMachineFormat) {
await handleMachine();
return FlutterCommandResult.success();
}

View File

@ -14,11 +14,7 @@ import '../runner/flutter_command.dart';
class DevicesCommand extends FlutterCommand {
DevicesCommand({bool verboseHelp = false}) {
argParser.addFlag(
'machine',
negatable: false,
help: 'Output device information in machine readable structured JSON format.',
);
addMachineOutputFlag(verboseHelp: verboseHelp);
argParser.addOption(
'timeout',
abbr: 't',
@ -78,7 +74,7 @@ class DevicesCommand extends FlutterCommand {
deviceConnectionInterface: deviceConnectionInterface,
);
await output.findAndOutputAllTargetDevices(machine: boolArg('machine'));
await output.findAndOutputAllTargetDevices(machine: outputMachineFormat);
return FlutterCommandResult.success();
}

View File

@ -402,6 +402,7 @@ class RunCommand extends RunCommandBase {
// without needing to know the port.
addPublishPort(verboseHelp: verboseHelp);
addIgnoreDeprecationOption();
addMachineOutputFlag(verboseHelp: verboseHelp);
argParser
..addFlag(
'await-first-frame-when-tracing',
@ -424,14 +425,6 @@ class RunCommand extends RunCommandBase {
)
..addFlag('build', defaultsTo: true, help: 'If necessary, build the app before running.')
..addOption('project-root', hide: !verboseHelp, help: 'Specify the project root directory.')
..addFlag(
'machine',
hide: !verboseHelp,
negatable: false,
help:
'Handle machine structured JSON command input and provide output '
'and progress in machine friendly format.',
)
..addFlag(
'hot',
defaultsTo: kHotReloadDefault,
@ -760,7 +753,7 @@ class RunCommand extends RunCommandBase {
final bool hotMode = shouldUseHotMode(buildInfo);
final String? applicationBinaryPath = stringArg(FlutterOptions.kUseApplicationBinary);
if (boolArg('machine')) {
if (outputMachineFormat) {
if (devices!.length > 1) {
throwToolExit('"--machine" does not support "-d all".');
}

View File

@ -78,6 +78,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
usesDeviceUserOption();
usesFlavorOption();
addEnableImpellerFlag(verboseHelp: verboseHelp);
addMachineOutputFlag(verboseHelp: verboseHelp);
addEnableFlutterGpuFlag(verboseHelp: verboseHelp);
argParser
@ -165,14 +166,6 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
valueHelp: 'package-name-regexp',
splitCommas: false,
)
..addFlag(
'machine',
hide: !verboseHelp,
negatable: false,
help:
'Handle machine structured JSON command input '
'and provide output and progress in machine friendly format.',
)
..addFlag(
'update-goldens',
negatable: false,
@ -568,7 +561,6 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
);
}
final bool machine = boolArg('machine');
CoverageCollector? collector;
if (boolArg('coverage') || boolArg('merge-coverage') || boolArg('branch-coverage')) {
final Set<String> packagesToInclude = _getCoveragePackages(
@ -577,7 +569,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
buildInfo.packageConfig,
);
collector = CoverageCollector(
verbose: !machine,
verbose: !outputMachineFormat,
libraryNames: packagesToInclude,
packagesPath: buildInfo.packageConfigPath,
resolver: await CoverageCollector.getResolver(buildInfo.packageConfigPath),
@ -587,7 +579,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
}
TestWatcher? watcher;
if (machine) {
if (outputMachineFormat) {
watcher = EventPrinter(parent: collector, out: globals.stdio.stdout);
} else if (collector != null) {
watcher = collector;
@ -649,7 +641,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
plainNames: plainNames,
tags: tags,
excludeTags: excludeTags,
machine: machine,
machine: outputMachineFormat,
updateGoldens: boolArg('update-goldens'),
concurrency: jobs,
testAssetDirectory: testAssetPath,
@ -677,7 +669,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
excludeTags: excludeTags,
watcher: watcher,
enableVmService: collector != null || startPaused || enableVmService,
machine: machine,
machine: outputMachineFormat,
updateGoldens: boolArg('update-goldens'),
concurrency: jobs,
testAssetDirectory: testAssetPath,

View File

@ -241,6 +241,8 @@ abstract class FlutterCommand extends Command<void> {
bool get shouldRunPub => _usesPubOption && boolArg('pub');
bool get outputMachineFormat => boolArg('machine');
bool get shouldUpdateCache => true;
bool get deprecated => false;
@ -1280,6 +1282,15 @@ abstract class FlutterCommand extends Command<void> {
);
}
void addMachineOutputFlag({required bool verboseHelp}) {
argParser.addFlag(
FlutterGlobalOptions.kMachineFlag,
negatable: false,
help: 'Outputs in a machine readable structured JSON format.',
hide: !verboseHelp,
);
}
/// Returns a [FlutterProject] view of the current directory or a ToolExit error,
/// if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
FlutterProject get project => FlutterProject.current();

View File

@ -97,7 +97,7 @@ void main() {
'does not check that Flutter installation is up-to-date with --machine flag present anywhere',
() async {
final FlutterCommandRunner runner =
createTestCommandRunner(_FlutterCommandWithItsOwnMachineFlag())
createTestCommandRunner(_FlutterCommandWithItsOwnMachineFlag(verboseHelp: false))
as FlutterCommandRunner;
final FakeFlutterVersion version = globals.flutterVersion as FakeFlutterVersion;
@ -446,8 +446,8 @@ class FakeStdio extends Stdio {
}
final class _FlutterCommandWithItsOwnMachineFlag extends FlutterCommand {
_FlutterCommandWithItsOwnMachineFlag() {
argParser.addFlag('machine', negatable: false);
_FlutterCommandWithItsOwnMachineFlag({required bool verboseHelp}) {
addMachineOutputFlag(verboseHelp: verboseHelp);
}
@override