Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 14 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 2 additions & 18 deletions packages/flutter_tools/lib/src/commands/assemble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(',');
}
Expand Down
84 changes: 57 additions & 27 deletions packages/flutter_tools/lib/src/runner/flutter_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down Expand Up @@ -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)) {
Expand All @@ -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')
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'''
Expand All @@ -317,6 +335,4 @@ void main() {
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
});


}