Skip to content

Commit 9301753

Browse files
authored
refactor(flutter_tools): share FlutterCommand options and OptionBundles (flutter#194356)
Moves the remaining shared CLI options on `FlutterCommand` into declarative option descriptors and bundles, and replaces untyped argument lookups in the command runner with typed descriptor reads. - **Shared option bundles for leaf commands**: Groups common CLI flags—device discovery, VM service/DevTools ports, filesystem overrides, resident compiler settings, engine rendering flags, and build mode configuration—into reusable bundles. This sets the stage for follow-up PRs migrating `assemble` (flutter#194358), `attach` (flutter#194359), `drive` (flutter#194360), `test` (flutter#194361), and `run` (flutter#194362). - **Deduplicated build flags**: Consolidates duplicate definitions and drifted help text for `--config-only`, `--target-platform` (`build apk`, `build aar`, `build appbundle`), `--static` (`build ios-framework`, `build macos-framework`, `build swift-package`), and `--performance-measurement-file`. Part of flutter#194338 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
1 parent 2f04e08 commit 9301753

10 files changed

Lines changed: 694 additions & 367 deletions

File tree

‎packages/flutter_tools/lib/src/commands/build_aar.dart‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BuildAarCommand extends BuildSubCommand {
4545
BuildInfoOptions.obfuscate,
4646
BuildInfoOptions.extraFrontEndOptions,
4747
BuildInfoOptions.extraGenSnapshotOptions,
48-
_targetPlatform,
48+
AndroidBuildOptionsBundle.targetPlatformWithDefaults,
4949
BuildInfoOptions.trackWidgetCreation,
5050
], verboseHelp: verboseHelp);
5151
}
@@ -68,13 +68,6 @@ class BuildAarCommand extends BuildSubCommand {
6868
help: 'Build a release version of the current project.',
6969
);
7070

71-
static const _targetPlatform = MultiOptionDescriptor(
72-
name: 'target-platform',
73-
defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
74-
allowed: <String>['android-arm', 'android-arm64', 'android-x64'],
75-
help: 'The target platform for which the project is compiled.',
76-
);
77-
7871
final AndroidBuilder _androidBuilder;
7972
final AndroidContext _androidContext;
8073
final BuildSystem _buildSystem;
@@ -126,7 +119,8 @@ class BuildAarCommand extends BuildSubCommand {
126119
workflow: commandPath,
127120
commandHasTerminal: hasTerminal,
128121
buildAarProjectType: projectType,
129-
buildAarTargetPlatform: getValue(_targetPlatform).join(','),
122+
buildAarTargetPlatform: getValue(AndroidBuildOptionsBundle.targetPlatformWithDefaults)
123+
.join(','),
130124
// TODO(gmackall): Consider collecting hcpp analytics, see https://github.com/flutter/flutter/issues/184541.
131125
);
132126
}
@@ -158,8 +152,9 @@ class BuildAarCommand extends BuildSubCommand {
158152
}
159153
final androidBuildInfo = <AndroidBuildInfo>{};
160154

161-
final Iterable<CpuArch> targetArchitectures = getValue(_targetPlatform)
162-
.map<CpuArch>(getCpuArchForName);
155+
final Iterable<CpuArch> targetArchitectures = getValue(
156+
AndroidBuildOptionsBundle.targetPlatformWithDefaults,
157+
).map<CpuArch>(getCpuArchForName);
163158

164159
final String? buildNumberArg = getValue(CommonOptions.buildNumber);
165160
final String buildNumber = (buildNumberArg != null && buildNumberArg.isNotEmpty)

‎packages/flutter_tools/lib/src/commands/build_apk.dart‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class BuildApkCommand extends BuildSubCommand {
3535
]);
3636
argParser.addDescriptors(const <OptionDescriptor<Object?>>[
3737
_splitPerAbi,
38-
_configOnly,
39-
_targetPlatform,
38+
BuildInfoOptions.configOnly,
39+
AndroidBuildOptionsBundle.targetPlatform,
4040
]);
4141
}
4242

@@ -48,19 +48,6 @@ class BuildApkCommand extends BuildSubCommand {
4848
'To learn more, see: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',
4949
);
5050

51-
static const _configOnly = FlagOptionDescriptor(
52-
name: 'config-only',
53-
help:
54-
'Generate build files used by flutter but '
55-
'do not build any artifacts.',
56-
);
57-
58-
static const _targetPlatform = MultiOptionDescriptor(
59-
name: 'target-platform',
60-
allowed: <String>['android-arm', 'android-arm64', 'android-x64'],
61-
help: 'The target platform for which the app is compiled.',
62-
);
63-
6451
final AndroidBuilder _androidBuilder;
6552
final AndroidContext _androidContext;
6653
final BuildSystem _buildSystem;
@@ -93,7 +80,7 @@ class BuildApkCommand extends BuildSubCommand {
9380
static const _kDefaultJitArchs = <String>['android-arm', 'android-arm64', 'android-x64'];
9481
static const _kDefaultAotArchs = <String>['android-arm', 'android-arm64', 'android-x64'];
9582
List<String> get _targetArchs {
96-
final List<String> targetPlatform = getValue(_targetPlatform);
83+
final List<String> targetPlatform = getValue(AndroidBuildOptionsBundle.targetPlatform);
9784
return targetPlatform.isEmpty
9885
? switch (_buildMode) {
9986
BuildMode.release || BuildMode.profile => _kDefaultAotArchs,
@@ -129,7 +116,7 @@ class BuildApkCommand extends BuildSubCommand {
129116
? DeprecationBehavior.ignore
130117
: DeprecationBehavior.exit;
131118

132-
bool get configOnly => getValue(_configOnly);
119+
bool get configOnly => getValue(BuildInfoOptions.configOnly);
133120

134121
@override
135122
Future<Set<DevelopmentArtifact>> get requiredArtifacts async => <DevelopmentArtifact>{

‎packages/flutter_tools/lib/src/commands/build_appbundle.dart‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,12 @@ class BuildAppBundleCommand extends BuildSubCommand {
4040
AndroidBuildOptionsBundle(),
4141
]);
4242
argParser.addDescriptors(const <OptionDescriptor<Object?>>[
43-
_targetPlatform,
43+
AndroidBuildOptionsBundle.targetPlatformWithDefaults,
4444
_deferredComponents,
4545
_validateDeferredComponents,
4646
]);
4747
}
4848

49-
static const _targetPlatform = MultiOptionDescriptor(
50-
name: 'target-platform',
51-
defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
52-
allowed: <String>['android-arm', 'android-arm64', 'android-x64'],
53-
help: 'The target platform for which the app is compiled.',
54-
);
55-
5649
static const _deferredComponents = FlagOptionDescriptor(
5750
name: 'deferred-components',
5851
defaultsTo: true,
@@ -141,7 +134,8 @@ class BuildAppBundleCommand extends BuildSubCommand {
141134
return Event.commandUsageValues(
142135
workflow: commandPath,
143136
commandHasTerminal: hasTerminal,
144-
buildAppBundleTargetPlatform: getValue(_targetPlatform).join(','),
137+
buildAppBundleTargetPlatform: getValue(AndroidBuildOptionsBundle.targetPlatformWithDefaults)
138+
.join(','),
145139
buildAppBundleBuildMode: buildMode,
146140
buildBundleEnableHcpp:
147141
explicitEnableHcpp ?? project.android.computeHcppEnabled(ifAbsent: enableHcpp),
@@ -156,7 +150,8 @@ class BuildAppBundleCommand extends BuildSubCommand {
156150
}
157151
final androidBuildInfo = AndroidBuildInfo(
158152
await getBuildInfo(),
159-
targetArchs: getValue(_targetPlatform).map<CpuArch>(getCpuArchForName),
153+
targetArchs: getValue(AndroidBuildOptionsBundle.targetPlatformWithDefaults)
154+
.map<CpuArch>(getCpuArchForName),
160155
);
161156
// Do all setup verification that doesn't involve loading units. Checks that
162157
// require generated loading units are done after gen_snapshot in assemble.

‎packages/flutter_tools/lib/src/commands/build_ios_framework.dart‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ abstract class BuildFrameworkCommand extends BuildSubCommand {
4848
releaseMode,
4949
cocoapods,
5050
plugins,
51-
staticFrameworks,
5251
output,
5352
force,
5453
], verboseHelp: verboseHelp);
@@ -91,10 +90,7 @@ abstract class BuildFrameworkCommand extends BuildSubCommand {
9190
'This is intended for cases where plugins are already being built separately.',
9291
);
9392

94-
static const staticFrameworks = FlagOptionDescriptor(
95-
name: 'static',
96-
help: 'Build plugins as static frameworks. Link on, but do not embed these frameworks in the existing Xcode project.',
97-
);
93+
static const FlagOptionDescriptor staticFrameworks = DarwinAddToAppOptionsBundle.staticFrameworks;
9894

9995
static const output = StringOptionDescriptor(
10096
name: 'output',

‎packages/flutter_tools/lib/src/commands/build_swift_package.dart‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class BuildSwiftPackage extends BuildSubCommand {
8181
_output,
8282
_platformOption,
8383
_buildMode,
84-
_static,
8584
], verboseHelp: verboseHelp);
8685
}
8786

@@ -106,11 +105,6 @@ class BuildSwiftPackage extends BuildSubCommand {
106105
help: 'Build modes to include.',
107106
);
108107

109-
static const _static = FlagOptionDescriptor(
110-
name: 'static',
111-
help: 'Build CocoaPods plugins as static frameworks.',
112-
);
113-
114108
@override
115109
final name = 'swift-package';
116110

@@ -381,7 +375,7 @@ class BuildSwiftPackage extends BuildSubCommand {
381375

382376
await cocoapodDependencies.generateArtifacts(
383377
buildInfo: buildInfo,
384-
buildStatic: getValue(_static),
378+
buildStatic: getValue(DarwinAddToAppOptionsBundle.staticFrameworks),
385379
cacheDirectory: cacheDirectory,
386380
xcframeworkOutput: xcframeworkOutput,
387381
codesignIdentity: codesignIdentity,

0 commit comments

Comments
 (0)