Skip to content

Commit acc840e

Browse files
authored
[tool] Proposal to multiple defines for --dart-define-from-file (#120878)
[tool] Proposal to multiple defines for --dart-define-from-file
1 parent efcc8b7 commit acc840e

File tree

4 files changed

+156
-73
lines changed

4 files changed

+156
-73
lines changed

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -258,24 +258,8 @@ class AssembleCommand extends FlutterCommand {
258258
results[kExtraGenSnapshotOptions] = (argumentResults[FlutterOptions.kExtraGenSnapshotOptions] as List<String>).join(',');
259259
}
260260

261-
List<String> dartDefines = <String>[];
262-
if (argumentResults.wasParsed(FlutterOptions.kDartDefinesOption)) {
263-
dartDefines = argumentResults[FlutterOptions.kDartDefinesOption] as List<String>;
264-
}
265-
if (argumentResults.wasParsed(FlutterOptions.kDartDefineFromFileOption)) {
266-
final String? configJsonPath = stringArg(FlutterOptions.kDartDefineFromFileOption);
267-
if (configJsonPath != null && globals.fs.isFileSync(configJsonPath)) {
268-
final String configJsonRaw = globals.fs.file(configJsonPath).readAsStringSync();
269-
try {
270-
(json.decode(configJsonRaw) as Map<String, dynamic>).forEach((String key, dynamic value) {
271-
dartDefines.add('$key=$value');
272-
});
273-
} on FormatException catch (err) {
274-
throwToolExit('Json config define file "--${FlutterOptions.kDartDefineFromFileOption}=$configJsonPath" format err, '
275-
'please fix first! format err:\n$err');
276-
}
277-
}
278-
}
261+
final Map<String, Object>? defineConfigJsonMap = extractDartDefineConfigJsonMap();
262+
final List<String> dartDefines = extractDartDefines(defineConfigJsonMap: defineConfigJsonMap);
279263
if(dartDefines.isNotEmpty){
280264
results[kDartDefines] = dartDefines.join(',');
281265
}

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

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -642,12 +642,14 @@ abstract class FlutterCommand extends Command<void> {
642642
}
643643

644644
void useDartDefineConfigJsonFileOption() {
645-
argParser.addOption(
645+
argParser.addMultiOption(
646646
FlutterOptions.kDartDefineFromFileOption,
647647
help: 'The path of a json format file where flutter define a global constant pool. '
648648
'Json entry will be available as constants from the String.fromEnvironment, bool.fromEnvironment, '
649-
'int.fromEnvironment, and double.fromEnvironment constructors; the key and field are json values.',
650-
valueHelp: 'use-define-config.json'
649+
'int.fromEnvironment, and double.fromEnvironment constructors; the key and field are json values.\n'
650+
'Multiple defines can be passed by repeating "--${FlutterOptions.kDartDefineFromFileOption}" multiple times.',
651+
valueHelp: 'use-define-config.json',
652+
splitCommas: false,
651653
);
652654
}
653655

@@ -1185,9 +1187,8 @@ abstract class FlutterCommand extends Command<void> {
11851187
? stringArgDeprecated(FlutterOptions.kPerformanceMeasurementFile)
11861188
: null;
11871189

1188-
List<String> dartDefines = argParser.options.containsKey(FlutterOptions.kDartDefinesOption)
1189-
? stringsArg(FlutterOptions.kDartDefinesOption)
1190-
: <String>[];
1190+
final Map<String, Object>? defineConfigJsonMap = extractDartDefineConfigJsonMap();
1191+
List<String> dartDefines = extractDartDefines(defineConfigJsonMap: defineConfigJsonMap);
11911192

11921193
WebRendererMode webRenderer = WebRendererMode.autoDetect;
11931194
if (argParser.options.containsKey(FlutterOptions.kWebRendererFlag)) {
@@ -1198,27 +1199,6 @@ abstract class FlutterCommand extends Command<void> {
11981199
dartDefines = updateDartDefines(dartDefines, webRenderer);
11991200
}
12001201

1201-
Map<String, Object>? defineConfigJsonMap;
1202-
if (argParser.options.containsKey(FlutterOptions.kDartDefineFromFileOption)) {
1203-
final String? configJsonPath = stringArg(FlutterOptions.kDartDefineFromFileOption);
1204-
if (configJsonPath != null && globals.fs.isFileSync(configJsonPath)) {
1205-
final String configJsonRaw = globals.fs.file(configJsonPath).readAsStringSync();
1206-
try {
1207-
defineConfigJsonMap = <String, Object>{};
1208-
// Fix json convert Object value :type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Object>' in type cast
1209-
(json.decode(configJsonRaw) as Map<String, dynamic>).forEach((String key, dynamic value) {
1210-
defineConfigJsonMap?[key]=value as Object;
1211-
});
1212-
defineConfigJsonMap.forEach((String key, Object value) {
1213-
dartDefines.add('$key=$value');
1214-
});
1215-
} on FormatException catch (err) {
1216-
throwToolExit('Json config define file "--${FlutterOptions.kDartDefineFromFileOption}=$configJsonPath" format err, '
1217-
'please fix first! format err:\n$err');
1218-
}
1219-
}
1220-
}
1221-
12221202
return BuildInfo(buildMode,
12231203
argParser.options.containsKey('flavor')
12241204
? stringArgDeprecated('flavor')
@@ -1334,6 +1314,56 @@ abstract class FlutterCommand extends Command<void> {
13341314
}
13351315
}
13361316

1317+
List<String> extractDartDefines({Map<String, Object>? defineConfigJsonMap}) {
1318+
final List<String> dartDefines = <String>[];
1319+
1320+
if (argParser.options.containsKey(FlutterOptions.kDartDefinesOption)) {
1321+
dartDefines.addAll(stringsArg(FlutterOptions.kDartDefinesOption));
1322+
}
1323+
1324+
if (defineConfigJsonMap == null) {
1325+
return dartDefines;
1326+
}
1327+
defineConfigJsonMap.forEach((String key, Object value) {
1328+
dartDefines.add('$key=$value');
1329+
});
1330+
1331+
return dartDefines;
1332+
}
1333+
1334+
Map<String, Object>? extractDartDefineConfigJsonMap() {
1335+
final Map<String, Object> dartDefineConfigJsonMap = <String, Object>{};
1336+
1337+
if (argParser.options.containsKey(FlutterOptions.kDartDefineFromFileOption)) {
1338+
final List<String> configJsonPaths = stringsArg(
1339+
FlutterOptions.kDartDefineFromFileOption,
1340+
);
1341+
1342+
for (final String path in configJsonPaths) {
1343+
if (!globals.fs.isFileSync(path)) {
1344+
throwToolExit('Json config define file "--${FlutterOptions
1345+
.kDartDefineFromFileOption}=$path" is not a file, '
1346+
'please fix first!');
1347+
}
1348+
1349+
final String configJsonRaw = globals.fs.file(path).readAsStringSync();
1350+
try {
1351+
// Fix json convert Object value :type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Object>' in type cast
1352+
(json.decode(configJsonRaw) as Map<String, dynamic>)
1353+
.forEach((String key, dynamic value) {
1354+
dartDefineConfigJsonMap[key] = value as Object;
1355+
});
1356+
} on FormatException catch (err) {
1357+
throwToolExit('Json config define file "--${FlutterOptions
1358+
.kDartDefineFromFileOption}=$path" format err, '
1359+
'please fix first! format err:\n$err');
1360+
}
1361+
}
1362+
}
1363+
1364+
return dartDefineConfigJsonMap;
1365+
}
1366+
13371367
/// Updates dart-defines based on [webRenderer].
13381368
@visibleForTesting
13391369
static List<String> updateDartDefines(List<String> dartDefines, WebRendererMode webRenderer) {

packages/flutter_tools/test/commands.shard/hermetic/assemble_test.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,24 @@ void main() {
291291
});
292292
});
293293

294+
testUsingContext('test --dart-define-from-file option with err file format', () {
295+
globals.fs.directory('config').createSync();
296+
final CommandRunner<void> commandRunner = createTestCommandRunner(AssembleCommand(
297+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
298+
));
299+
300+
expect(commandRunner.run(<String>['assemble',
301+
'-o Output',
302+
'debug_macos_bundle_flutter_assets',
303+
'--dart-define=k=v',
304+
'--dart-define-from-file=config']),
305+
throwsToolExit(message: 'Json config define file "--dart-define-from-file=config" is not a file, please fix first!'));
306+
}, overrides: <Type, Generator>{
307+
Cache: () => Cache.test(processManager: FakeProcessManager.any()),
308+
FileSystem: () => MemoryFileSystem.test(),
309+
ProcessManager: () => FakeProcessManager.any(),
310+
});
311+
294312
testUsingContext('test --dart-define-from-file option with err json format', () async {
295313
await globals.fs.file('config.json').writeAsString(
296314
'''
@@ -317,6 +335,4 @@ void main() {
317335
FileSystem: () => MemoryFileSystem.test(),
318336
ProcessManager: () => FakeProcessManager.any(),
319337
});
320-
321-
322338
}

0 commit comments

Comments
 (0)