Skip to content

Commit b8d0aad

Browse files
kevmoosuryaKommana2662
authored andcommitted
refactor(flutter_tools): migrate AssembleCommand to OptionDescriptor (flutter#194358)
Migrates `flutter assemble` CLI options to declarative option descriptors and shared bundles introduced in flutter#194356, replacing untyped string-keyed argument lookups with typed descriptor reads. Also addresses two follow-up cleanup nits from flutter#194356 (`FakeToolContext` + `testWithoutContext` in `option_descriptor_test.dart`, and `dartDefineConfigJsonMap.addAll(...)` in `flutter_command.dart`). 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 b178edb commit b8d0aad

3 files changed

Lines changed: 179 additions & 169 deletions

File tree

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

Lines changed: 94 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -100,74 +100,84 @@ class AssembleCommand extends FlutterCommand {
100100
required this._buildSystem,
101101
required this._featureFlags,
102102
required ToolContext super.toolContext,
103-
bool verboseHelp = false,
104-
}) : _toolContext = toolContext,
105-
_verboseHelp = verboseHelp {
103+
super.verboseHelp = false,
104+
}) : _toolContext = toolContext {
106105
requiresPubspecYaml();
107-
argParser.addMultiOption(
108-
'define',
109-
abbr: 'd',
110-
valueHelp: 'target=key=value',
111-
hide: !verboseHelp,
112-
help:
113-
'DEPRECATED. Use "--dart-define" or "-D" instead for consistency.\n'
114-
'\n'
115-
'Allows passing configuration to a target, as in "--define=target=key=value".',
116-
);
106+
argParser.addDescriptors(const <OptionDescriptor<Object?>>[
107+
_define,
108+
CommonOptions.dartDefines,
109+
CommonOptions.dartDefineFromFile,
110+
BuildInfoOptions.performanceMeasurementFile,
111+
_input,
112+
_depfile,
113+
_buildInputs,
114+
_buildOutputs,
115+
_output,
116+
BuildInfoOptions.extraFrontEndOptions,
117+
BuildInfoOptions.extraGenSnapshotOptions,
118+
_resourcePoolSize,
119+
], verboseHelp: verboseHelp);
120+
}
117121

118-
// New -D/--dart-define (consistent across app)
119-
usesDartDefineOption();
122+
static const _define = MultiOptionDescriptor(
123+
name: 'define',
124+
abbr: 'd',
125+
valueHelp: 'target=key=value',
126+
verboseOnly: true,
127+
help:
128+
'DEPRECATED. Use "--dart-define" or "-D" instead for consistency.\n'
129+
'\n'
130+
'Allows passing configuration to a target, as in "--define=target=key=value".',
131+
);
120132

121-
argParser.addOption(
122-
'performance-measurement-file',
123-
help: 'Output individual target performance to a JSON file.',
124-
);
125-
argParser.addMultiOption(
126-
'input',
127-
abbr: 'i',
128-
help:
129-
'Allows passing additional inputs with "--input=key=value". Unlike '
130-
'defines, additional inputs do not generate a new configuration; instead '
131-
'they are treated as dependencies of the targets that use them.',
132-
);
133-
argParser.addOption(
134-
'depfile',
135-
help:
136-
'A file path where a depfile will be written. '
137-
'This contains all build inputs and outputs in a Make-style syntax.',
138-
);
139-
argParser.addOption(
140-
'build-inputs',
141-
help:
142-
'A file path where a newline-separated '
143-
'file containing all inputs used will be written after a build. '
144-
'This file is not included as a build input or output. This file is not '
145-
'written if the build fails for any reason.',
146-
);
147-
argParser.addOption(
148-
'build-outputs',
149-
help:
150-
'A file path where a newline-separated '
151-
'file containing all outputs created will be written after a build. '
152-
'This file is not included as a build input or output. This file is not '
153-
'written if the build fails for any reason.',
154-
);
155-
argParser.addOption(
156-
'output',
157-
abbr: 'o',
158-
help:
159-
'A directory where output '
160-
'files will be written. Must be either absolute or relative from the '
161-
'root of the current Flutter project.',
162-
);
163-
usesExtraDartFlagOptions(verboseHelp: verboseHelp);
164-
argParser.addOption(
165-
'resource-pool-size',
166-
help: 'The maximum number of concurrent tasks the build system will run.',
167-
);
168-
}
133+
static const _input = MultiOptionDescriptor(
134+
name: 'input',
135+
abbr: 'i',
136+
help:
137+
'Allows passing additional inputs with "--input=key=value". Unlike '
138+
'defines, additional inputs do not generate a new configuration; instead '
139+
'they are treated as dependencies of the targets that use them.',
140+
);
141+
142+
static const _depfile = StringOptionDescriptor(
143+
name: 'depfile',
144+
help:
145+
'A file path where a depfile will be written. '
146+
'This contains all build inputs and outputs in a Make-style syntax.',
147+
);
148+
149+
static const _buildInputs = StringOptionDescriptor(
150+
name: 'build-inputs',
151+
help:
152+
'A file path where a newline-separated '
153+
'file containing all inputs used will be written after a build. '
154+
'This file is not included as a build input or output. This file is not '
155+
'written if the build fails for any reason.',
156+
);
157+
158+
static const _buildOutputs = StringOptionDescriptor(
159+
name: 'build-outputs',
160+
help:
161+
'A file path where a newline-separated '
162+
'file containing all outputs created will be written after a build. '
163+
'This file is not included as a build input or output. This file is not '
164+
'written if the build fails for any reason.',
165+
);
166+
167+
static const _output = StringOptionDescriptor(
168+
name: 'output',
169+
abbr: 'o',
170+
help:
171+
'A directory where output '
172+
'files will be written. Must be either absolute or relative from the '
173+
'root of the current Flutter project.',
174+
);
175+
176+
static const _resourcePoolSize = StringOptionDescriptor(
177+
name: 'resource-pool-size',
178+
help: 'The maximum number of concurrent tasks the build system will run.',
179+
);
169180

170-
final bool _verboseHelp;
171181
final BuildSystem _buildSystem;
172182
final FeatureFlags _featureFlags;
173183
final ToolContext _toolContext;
@@ -176,7 +186,7 @@ class AssembleCommand extends FlutterCommand {
176186
ToolContext get toolContext => _toolContext;
177187

178188
@override
179-
bool get hidden => !_verboseHelp;
189+
bool get hidden => !verboseHelp;
180190

181191
@override
182192
String get description => 'Assemble and build Flutter resources.';
@@ -254,7 +264,7 @@ class AssembleCommand extends FlutterCommand {
254264
Environment _createEnvironment() {
255265
final FileSystem fs = _toolContext.fs;
256266
final FlutterProject project = this.project;
257-
String? output = stringArg('output');
267+
String? output = getValue(_output);
258268
if (output == null) {
259269
throwToolExit('--output directory is required for assemble.');
260270
}
@@ -267,7 +277,7 @@ class AssembleCommand extends FlutterCommand {
267277
List<String> decodedDefines;
268278
try {
269279
decodedDefines = decodeDartDefines({
270-
'dart-define': stringsArg('dart-define').join(','),
280+
'dart-define': getValue(CommonOptions.dartDefines).join(','),
271281
}, 'dart-define');
272282
} on FormatException {
273283
throwToolExit(
@@ -281,8 +291,8 @@ class AssembleCommand extends FlutterCommand {
281291
buildDir: project.directory.childDirectory('.dart_tool').childDirectory('flutter_build'),
282292
projectDir: project.directory,
283293
packageConfigPath: packageConfigPath(),
284-
defines: _parseDefines([...stringsArg('define'), ...decodedDefines]),
285-
inputs: _parseDefines(stringsArg('input')),
294+
defines: _parseDefines([...getValue(_define), ...decodedDefines]),
295+
inputs: _parseDefines(getValue(_input)),
286296
cacheDir: _toolContext.cache.getRoot(),
287297
flutterRootDir: fs.directory(Cache.flutterRoot),
288298
artifacts: artifacts,
@@ -312,10 +322,9 @@ class AssembleCommand extends FlutterCommand {
312322
final String value = chunk.substring(indexEquals + 1);
313323
results[key] = value;
314324
}
315-
final ArgResults argumentResults = argResults!;
316-
if (argumentResults.wasParsed(FlutterOptions.kExtraGenSnapshotOptions)) {
317-
results[kExtraGenSnapshotOptions] =
318-
(argumentResults[FlutterOptions.kExtraGenSnapshotOptions] as List<String>).join(',');
325+
if (wasParsed(BuildInfoOptions.extraGenSnapshotOptions)) {
326+
results[kExtraGenSnapshotOptions] = getValue(BuildInfoOptions.extraGenSnapshotOptions)
327+
.join(',');
319328
}
320329

321330
final Map<String, Object?> defineConfigJsonMap = extractDartDefineConfigJsonMap();
@@ -330,9 +339,8 @@ class AssembleCommand extends FlutterCommand {
330339
!isDebug()) {
331340
results[kDeferredComponents] = 'true';
332341
}
333-
if (argumentResults.wasParsed(FlutterOptions.kExtraFrontEndOptions)) {
334-
results[kExtraFrontEndOptions] =
335-
(argumentResults[FlutterOptions.kExtraFrontEndOptions] as List<String>).join(',');
342+
if (wasParsed(BuildInfoOptions.extraFrontEndOptions)) {
343+
results[kExtraFrontEndOptions] = getValue(BuildInfoOptions.extraFrontEndOptions).join(',');
336344
}
337345
return results;
338346
}
@@ -377,13 +385,12 @@ class AssembleCommand extends FlutterCommand {
377385
} else if (targets.isNotEmpty) {
378386
target = targets.single;
379387
}
380-
final ArgResults argumentResults = argResults!;
381388
final BuildResult result = await _buildSystem.build(
382389
target!,
383390
_environment,
384391
buildSystemConfig: BuildSystemConfig(
385-
resourcePoolSize: argumentResults.wasParsed('resource-pool-size')
386-
? int.tryParse(stringArg('resource-pool-size')!)
392+
resourcePoolSize: wasParsed(_resourcePoolSize)
393+
? int.tryParse(getValue(_resourcePoolSize)!)
387394
: null,
388395
),
389396
);
@@ -400,18 +407,18 @@ class AssembleCommand extends FlutterCommand {
400407
}
401408
logger.printTrace('build succeeded.');
402409

403-
if (argumentResults.wasParsed('build-inputs')) {
404-
writeListIfChanged(result.inputFiles, stringArg('build-inputs')!, fs: fs);
410+
if (wasParsed(_buildInputs)) {
411+
writeListIfChanged(result.inputFiles, getValue(_buildInputs)!, fs: fs);
405412
}
406-
if (argumentResults.wasParsed('build-outputs')) {
407-
writeListIfChanged(result.outputFiles, stringArg('build-outputs')!, fs: fs);
413+
if (wasParsed(_buildOutputs)) {
414+
writeListIfChanged(result.outputFiles, getValue(_buildOutputs)!, fs: fs);
408415
}
409-
if (argumentResults.wasParsed('performance-measurement-file')) {
410-
final File outFile = fs.file(argumentResults['performance-measurement-file']);
416+
if (wasParsed(BuildInfoOptions.performanceMeasurementFile)) {
417+
final File outFile = fs.file(getValue(BuildInfoOptions.performanceMeasurementFile));
411418
writePerformanceData(result.performance.values, outFile);
412419
}
413-
if (argumentResults.wasParsed('depfile')) {
414-
final File depfileFile = fs.file(stringArg('depfile'));
420+
if (wasParsed(_depfile)) {
421+
final File depfileFile = fs.file(getValue(_depfile));
415422
final depfile = Depfile(result.inputFiles, result.outputFiles);
416423
_environment.depFileService.writeToFile(depfile, depfileFile);
417424
}

‎packages/flutter_tools/lib/src/runner/flutter_command.dart‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,10 +1237,7 @@ abstract class FlutterCommand extends Command<void> {
12371237
}
12381238

12391239
try {
1240-
// Fix json convert Object value :type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Object>' in type cast
1241-
(json.decode(configJsonRaw) as Map<String, dynamic>).forEach((String key, Object? value) {
1242-
dartDefineConfigJsonMap[key] = value;
1243-
});
1240+
dartDefineConfigJsonMap.addAll(json.decode(configJsonRaw) as Map<String, Object?>);
12441241
} on FormatException catch (err) {
12451242
throwToolExit(
12461243
'Unable to parse the file at path "$path" due to a formatting error. '

0 commit comments

Comments
 (0)