forked from firka/flutter
[tool] Proposal to multiple defines for --dart-define-from-file (#120878)
[tool] Proposal to multiple defines for --dart-define-from-file
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user