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 { ...@@ -258,24 +258,8 @@ class AssembleCommand extends FlutterCommand {
results[kExtraGenSnapshotOptions] = (argumentResults[FlutterOptions.kExtraGenSnapshotOptions] as List<String>).join(','); results[kExtraGenSnapshotOptions] = (argumentResults[FlutterOptions.kExtraGenSnapshotOptions] as List<String>).join(',');
} }
List<String> dartDefines = <String>[]; final Map<String, Object>? defineConfigJsonMap = extractDartDefineConfigJsonMap();
if (argumentResults.wasParsed(FlutterOptions.kDartDefinesOption)) { final List<String> dartDefines = extractDartDefines(defineConfigJsonMap: defineConfigJsonMap);
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');
}
}
}
if(dartDefines.isNotEmpty){ if(dartDefines.isNotEmpty){
results[kDartDefines] = dartDefines.join(','); results[kDartDefines] = dartDefines.join(',');
} }
......
...@@ -642,12 +642,14 @@ abstract class FlutterCommand extends Command<void> { ...@@ -642,12 +642,14 @@ abstract class FlutterCommand extends Command<void> {
} }
void useDartDefineConfigJsonFileOption() { void useDartDefineConfigJsonFileOption() {
argParser.addOption( argParser.addMultiOption(
FlutterOptions.kDartDefineFromFileOption, FlutterOptions.kDartDefineFromFileOption,
help: 'The path of a json format file where flutter define a global constant pool. ' 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, ' '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.', 'int.fromEnvironment, and double.fromEnvironment constructors; the key and field are json values.\n'
valueHelp: 'use-define-config.json' '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> { ...@@ -1185,9 +1187,8 @@ abstract class FlutterCommand extends Command<void> {
? stringArgDeprecated(FlutterOptions.kPerformanceMeasurementFile) ? stringArgDeprecated(FlutterOptions.kPerformanceMeasurementFile)
: null; : null;
List<String> dartDefines = argParser.options.containsKey(FlutterOptions.kDartDefinesOption) final Map<String, Object>? defineConfigJsonMap = extractDartDefineConfigJsonMap();
? stringsArg(FlutterOptions.kDartDefinesOption) List<String> dartDefines = extractDartDefines(defineConfigJsonMap: defineConfigJsonMap);
: <String>[];
WebRendererMode webRenderer = WebRendererMode.autoDetect; WebRendererMode webRenderer = WebRendererMode.autoDetect;
if (argParser.options.containsKey(FlutterOptions.kWebRendererFlag)) { if (argParser.options.containsKey(FlutterOptions.kWebRendererFlag)) {
...@@ -1198,27 +1199,6 @@ abstract class FlutterCommand extends Command<void> { ...@@ -1198,27 +1199,6 @@ abstract class FlutterCommand extends Command<void> {
dartDefines = updateDartDefines(dartDefines, webRenderer); 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, return BuildInfo(buildMode,
argParser.options.containsKey('flavor') argParser.options.containsKey('flavor')
? stringArgDeprecated('flavor') ? stringArgDeprecated('flavor')
...@@ -1334,6 +1314,56 @@ abstract class FlutterCommand extends Command<void> { ...@@ -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]. /// Updates dart-defines based on [webRenderer].
@visibleForTesting @visibleForTesting
static List<String> updateDartDefines(List<String> dartDefines, WebRendererMode webRenderer) { static List<String> updateDartDefines(List<String> dartDefines, WebRendererMode webRenderer) {
......
...@@ -291,6 +291,24 @@ void main() { ...@@ -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 { testUsingContext('test --dart-define-from-file option with err json format', () async {
await globals.fs.file('config.json').writeAsString( await globals.fs.file('config.json').writeAsString(
''' '''
...@@ -317,6 +335,4 @@ void main() { ...@@ -317,6 +335,4 @@ void main() {
FileSystem: () => MemoryFileSystem.test(), FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(), 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