Unverified Commit acc840e5 authored by Seiya Kokushi's avatar Seiya Kokushi Committed by GitHub

[tool] Proposal to multiple defines for --dart-define-from-file (#120878)

[tool] Proposal to multiple defines for --dart-define-from-file
parent efcc8b76
......@@ -258,24 +258,8 @@ class AssembleCommand extends FlutterCommand {
results[kExtraGenSnapshotOptions] = (argumentResults[FlutterOptions.kExtraGenSnapshotOptions] as List<String>).join(',');
}
List<String> dartDefines = <String>[];
if (argumentResults.wasParsed(FlutterOptions.kDartDefinesOption)) {
dartDefines = argumentResults[FlutterOptions.kDartDefinesOption] as List<String>;
}
if (argumentResults.wasParsed(FlutterOptions.kDartDefineFromFileOption)) {
final String? configJsonPath = stringArg(FlutterOptions.kDartDefineFromFileOption);
if (configJsonPath != null && globals.fs.isFileSync(configJsonPath)) {
final String configJsonRaw = globals.fs.file(configJsonPath).readAsStringSync();
try {
(json.decode(configJsonRaw) as Map<String, dynamic>).forEach((String key, dynamic value) {
dartDefines.add('$key=$value');
});
} on FormatException catch (err) {
throwToolExit('Json config define file "--${FlutterOptions.kDartDefineFromFileOption}=$configJsonPath" format err, '
'please fix first! format err:\n$err');
}
}
}
final Map<String, Object>? defineConfigJsonMap = extractDartDefineConfigJsonMap();
final List<String> dartDefines = extractDartDefines(defineConfigJsonMap: defineConfigJsonMap);
if(dartDefines.isNotEmpty){
results[kDartDefines] = dartDefines.join(',');
}
......
......@@ -642,12 +642,14 @@ abstract class FlutterCommand extends Command<void> {
}
void useDartDefineConfigJsonFileOption() {
argParser.addOption(
argParser.addMultiOption(
FlutterOptions.kDartDefineFromFileOption,
help: 'The path of a json format file where flutter define a global constant pool. '
'Json entry will be available as constants from the String.fromEnvironment, bool.fromEnvironment, '
'int.fromEnvironment, and double.fromEnvironment constructors; the key and field are json values.',
valueHelp: 'use-define-config.json'
'int.fromEnvironment, and double.fromEnvironment constructors; the key and field are json values.\n'
'Multiple defines can be passed by repeating "--${FlutterOptions.kDartDefineFromFileOption}" multiple times.',
valueHelp: 'use-define-config.json',
splitCommas: false,
);
}
......@@ -1185,9 +1187,8 @@ abstract class FlutterCommand extends Command<void> {
? stringArgDeprecated(FlutterOptions.kPerformanceMeasurementFile)
: null;
List<String> dartDefines = argParser.options.containsKey(FlutterOptions.kDartDefinesOption)
? stringsArg(FlutterOptions.kDartDefinesOption)
: <String>[];
final Map<String, Object>? defineConfigJsonMap = extractDartDefineConfigJsonMap();
List<String> dartDefines = extractDartDefines(defineConfigJsonMap: defineConfigJsonMap);
WebRendererMode webRenderer = WebRendererMode.autoDetect;
if (argParser.options.containsKey(FlutterOptions.kWebRendererFlag)) {
......@@ -1198,27 +1199,6 @@ abstract class FlutterCommand extends Command<void> {
dartDefines = updateDartDefines(dartDefines, webRenderer);
}
Map<String, Object>? defineConfigJsonMap;
if (argParser.options.containsKey(FlutterOptions.kDartDefineFromFileOption)) {
final String? configJsonPath = stringArg(FlutterOptions.kDartDefineFromFileOption);
if (configJsonPath != null && globals.fs.isFileSync(configJsonPath)) {
final String configJsonRaw = globals.fs.file(configJsonPath).readAsStringSync();
try {
defineConfigJsonMap = <String, Object>{};
// Fix json convert Object value :type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Object>' in type cast
(json.decode(configJsonRaw) as Map<String, dynamic>).forEach((String key, dynamic value) {
defineConfigJsonMap?[key]=value as Object;
});
defineConfigJsonMap.forEach((String key, Object value) {
dartDefines.add('$key=$value');
});
} on FormatException catch (err) {
throwToolExit('Json config define file "--${FlutterOptions.kDartDefineFromFileOption}=$configJsonPath" format err, '
'please fix first! format err:\n$err');
}
}
}
return BuildInfo(buildMode,
argParser.options.containsKey('flavor')
? stringArgDeprecated('flavor')
......@@ -1334,6 +1314,56 @@ abstract class FlutterCommand extends Command<void> {
}
}
List<String> extractDartDefines({Map<String, Object>? defineConfigJsonMap}) {
final List<String> dartDefines = <String>[];
if (argParser.options.containsKey(FlutterOptions.kDartDefinesOption)) {
dartDefines.addAll(stringsArg(FlutterOptions.kDartDefinesOption));
}
if (defineConfigJsonMap == null) {
return dartDefines;
}
defineConfigJsonMap.forEach((String key, Object value) {
dartDefines.add('$key=$value');
});
return dartDefines;
}
Map<String, Object>? extractDartDefineConfigJsonMap() {
final Map<String, Object> dartDefineConfigJsonMap = <String, Object>{};
if (argParser.options.containsKey(FlutterOptions.kDartDefineFromFileOption)) {
final List<String> configJsonPaths = stringsArg(
FlutterOptions.kDartDefineFromFileOption,
);
for (final String path in configJsonPaths) {
if (!globals.fs.isFileSync(path)) {
throwToolExit('Json config define file "--${FlutterOptions
.kDartDefineFromFileOption}=$path" is not a file, '
'please fix first!');
}
final String configJsonRaw = globals.fs.file(path).readAsStringSync();
try {
// Fix json convert Object value :type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Object>' in type cast
(json.decode(configJsonRaw) as Map<String, dynamic>)
.forEach((String key, dynamic value) {
dartDefineConfigJsonMap[key] = value as Object;
});
} on FormatException catch (err) {
throwToolExit('Json config define file "--${FlutterOptions
.kDartDefineFromFileOption}=$path" format err, '
'please fix first! format err:\n$err');
}
}
}
return dartDefineConfigJsonMap;
}
/// Updates dart-defines based on [webRenderer].
@visibleForTesting
static List<String> updateDartDefines(List<String> dartDefines, WebRendererMode webRenderer) {
......
......@@ -291,6 +291,24 @@ void main() {
});
});
testUsingContext('test --dart-define-from-file option with err file format', () {
globals.fs.directory('config').createSync();
final CommandRunner<void> commandRunner = createTestCommandRunner(AssembleCommand(
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
));
expect(commandRunner.run(<String>['assemble',
'-o Output',
'debug_macos_bundle_flutter_assets',
'--dart-define=k=v',
'--dart-define-from-file=config']),
throwsToolExit(message: 'Json config define file "--dart-define-from-file=config" is not a file, please fix first!'));
}, overrides: <Type, Generator>{
Cache: () => Cache.test(processManager: FakeProcessManager.any()),
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('test --dart-define-from-file option with err json format', () async {
await globals.fs.file('config.json').writeAsString(
'''
......@@ -317,6 +335,4 @@ void main() {
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment