From 229b39eda58608de0cdd6b3f1d35b6470fc83183 Mon Sep 17 00:00:00 2001 From: blendthink <32213113+blendthink@users.noreply.github.com> Date: Sat, 12 Nov 2022 03:48:01 +0900 Subject: [PATCH] [flutter_tools] Fix so that the value set by `--dart-define-from-file` can be passed to Gradle (#114297) * Add dartDefineConfigJsonMap to the checklist * Fix typo * Align formatting with other code * Fix toGradleConfig method --- packages/flutter_tools/lib/src/build_info.dart | 14 ++++++-------- .../test/general.shard/build_info_test.dart | 16 +++++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_info.dart b/packages/flutter_tools/lib/src/build_info.dart index 8af32e1db5..ce9b4993ed 100644 --- a/packages/flutter_tools/lib/src/build_info.dart +++ b/packages/flutter_tools/lib/src/build_info.dart @@ -333,18 +333,16 @@ class BuildInfo { for (String projectArg in androidProjectArgs) '-P$projectArg', ]; - if(dartDefineConfigJsonMap != null) { - final List items = []; - for (final String gradleConf in result) { - final String key = gradleConf.split('=')[0].substring(2); - if (dartDefineConfigJsonMap!.containsKey(key)) { + if (dartDefineConfigJsonMap != null) { + final Iterable gradleConfKeys = result.map((final String gradleConf) => gradleConf.split('=')[0].substring(2)); + dartDefineConfigJsonMap!.forEach((String key, Object value) { + if (gradleConfKeys.contains(key)) { globals.printWarning( 'The key: [$key] already exists, you cannot use gradle variables that have been used by the system!'); } else { - items.add('-P$key=${dartDefineConfigJsonMap?[key]}'); + result.add('-P$key=$value'); } - } - result.addAll(items); + }); } return result; } diff --git a/packages/flutter_tools/test/general.shard/build_info_test.dart b/packages/flutter_tools/test/general.shard/build_info_test.dart index 0b59be018f..d3bdbd550c 100644 --- a/packages/flutter_tools/test/general.shard/build_info_test.dart +++ b/packages/flutter_tools/test/general.shard/build_info_test.dart @@ -230,6 +230,7 @@ void main() { treeShakeIcons: true, trackWidgetCreation: true, dartDefines: ['foo=2', 'bar=2'], + dartDefineConfigJsonMap: {'baz': '2'}, dartObfuscation: true, splitDebugInfoPath: 'foo/', extraFrontEndOptions: ['--enable-experiment=non-nullable', 'bar'], @@ -252,6 +253,7 @@ void main() { '-Pcode-size-directory=foo/code-size', '-Pfoo=bar', '-Pfizz=bazz', + '-Pbaz=2', ]); }); @@ -287,7 +289,7 @@ void main() { treeShakeIcons: true, trackWidgetCreation: true, dartDefines: ['foo=2', 'bar=2'], - dartDefineConfigJsonMap: { 'DART_DEFINES' : 'Define a variable, but it occupies the variable name of the system'}, + dartDefineConfigJsonMap: {'DART_DEFINES': 'Define a variable, but it occupies the variable name of the system'}, dartObfuscation: true, ); buildInfo.toEnvironmentConfig(); @@ -313,11 +315,11 @@ void main() { treeShakeIcons: true, trackWidgetCreation: true, dartDefines: ['foo=2', 'bar=2'], - dartDefineConfigJsonMap: { 'dart-defines' : 'Define a variable, but it occupies the variable name of the system'}, + dartDefineConfigJsonMap: {'dart-defines': 'Define a variable, but it occupies the variable name of the system'}, dartObfuscation: true, ); buildInfo.toGradleConfig(); - expect(testLogger.warningText, contains('he key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system')); + expect(testLogger.warningText, contains('The key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system')); }); testUsingContext('toGradleConfig repeated variable with not set', () async { @@ -326,11 +328,11 @@ void main() { treeShakeIcons: true, trackWidgetCreation: true, dartDefines: ['dart-defines=Define a variable, but it occupies the variable name of the system'], - dartDefineConfigJsonMap: { 'dart-defines' : 'Define a variable, but it occupies the variable name of the system'}, + dartDefineConfigJsonMap: {'dart-defines': 'Define a variable, but it occupies the variable name of the system'}, dartObfuscation: true, ); buildInfo.toGradleConfig(); - expect(testLogger.warningText, contains('he key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system')); + expect(testLogger.warningText, contains('The key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system')); }); testUsingContext('toGradleConfig with androidProjectArgs override gradle project variant', () async { @@ -338,11 +340,11 @@ void main() { treeShakeIcons: true, trackWidgetCreation: true, androidProjectArgs: ['applicationId=com.google'], - dartDefineConfigJsonMap: { 'applicationId' : 'override applicationId'}, + dartDefineConfigJsonMap: {'applicationId': 'override applicationId'}, dartObfuscation: true, ); buildInfo.toGradleConfig(); - expect(testLogger.warningText, contains('The key: [applicationId] already exists, you cannot use gradle variables that have been used')); + expect(testLogger.warningText, contains('The key: [applicationId] already exists, you cannot use gradle variables that have been used by the system')); }); });