22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5+ import 'package:args/args.dart' ;
6+ import 'package:flutter_tools_core/flutter_tools_core.dart' ;
7+ import 'package:flutter_tools_extension/flutter_tools_extension.dart' ;
8+
59import '../android/android_sdk.dart' ;
610import '../android/android_studio.dart' ;
711import '../android/java.dart' ;
812import '../base/common.dart' ;
913import '../convert.dart' ;
14+ import '../experimental/config.dart' ;
15+ import '../experimental/extension_arg_parser.dart' ;
16+ import '../experimental/extension_manager.dart' ;
1017import '../features.dart' ;
1118import '../globals.dart' as globals;
1219import '../ios/code_signing.dart' ;
1320import '../runner/flutter_command.dart' ;
1421import '../runner/flutter_command_runner.dart' ;
1522
16- class ConfigCommand extends FlutterCommand {
17- ConfigCommand ({bool verboseHelp = false }) {
18- argParser.addFlag (
19- 'list' ,
20- help: 'List all settings and their current values.' ,
21- negatable: false ,
22- );
23- argParser.addFlag (
23+ class ConfigCommand extends FlutterCommand with ExtensionArgParserMixin {
24+ ConfigCommand ({bool verboseHelp = false , ExtensionManager ? extensionManager})
25+ : _verboseHelp = verboseHelp,
26+ _extensionManager = extensionManager;
27+
28+ final bool _verboseHelp;
29+
30+ var _extensionSettingsGroups = const < ExtensionSettingsGroup > [];
31+
32+ @override
33+ ArgParser createBaseArgParser () {
34+ final ArgParser parser = super .createBaseArgParser ();
35+ parser.addFlag ('list' , help: 'List all settings and their current values.' , negatable: false );
36+ parser.addFlag (
2437 'analytics' ,
25- hide: ! verboseHelp ,
38+ hide: ! _verboseHelp ,
2639 help:
2740 'Enable or disable reporting anonymously tool usage statistics and crash reports.\n '
2841 '(An alias for "--${FlutterGlobalOptions .kEnableAnalyticsFlag }" '
2942 'and "--${FlutterGlobalOptions .kDisableAnalyticsFlag }" top level flags.)' ,
3043 );
31- argParser .addFlag (
44+ parser .addFlag (
3245 'clear-ios-signing-settings' ,
3346 negatable: false ,
3447 aliases: < String > ['clear-ios-signing-cert' ],
3548 help:
3649 'Clear the saved development certificate or provisioning profile choice used to sign apps for iOS device deployment.' ,
3750 );
38- argParser .addFlag (
51+ parser .addFlag (
3952 'select-ios-signing-settings' ,
4053 negatable: false ,
4154 help:
4255 'Complete prompt to select and save code signing settings used to sign apps for iOS device deployment.' ,
4356 );
44- argParser .addOption ('android-sdk' , help: 'The Android SDK directory.' );
45- argParser .addOption (
57+ parser .addOption ('android-sdk' , help: 'The Android SDK directory.' );
58+ parser .addOption (
4659 'android-studio-dir' ,
4760 help:
4861 'The Android Studio installation directory. If unset, flutter will search for valid installations at well-known locations.' ,
4962 );
50- argParser .addOption (
63+ parser .addOption (
5164 'jdk-dir' ,
5265 help:
5366 'The Java Development Kit (JDK) installation directory. '
@@ -56,29 +69,83 @@ class ConfigCommand extends FlutterCommand {
5669 ' 2) the JDK found at the directory found in the JAVA_HOME environment variable, and\n '
5770 " 3) the directory containing the java binary found in the user's path." ,
5871 );
59- argParser .addOption (
72+ parser .addOption (
6073 'build-dir' ,
6174 help: 'The relative path to override a projects build directory.' ,
6275 valueHelp: 'out/' ,
6376 );
64- addMachineOutputFlag (verboseHelp: verboseHelp);
77+ parser.addFlag (
78+ FlutterGlobalOptions .kMachineFlag,
79+ negatable: false ,
80+ help: 'Outputs in a machine readable structured JSON format.' ,
81+ hide: ! _verboseHelp,
82+ );
6583 for (final Feature feature in featureFlags.allFeatures) {
6684 final String ? configSetting = feature.configSetting;
6785 if (configSetting == null ) {
6886 continue ;
6987 }
7088 final String channel = globals.flutterVersion.channel;
71- argParser .addFlag (
89+ parser .addFlag (
7290 configSetting,
7391 help: feature.generateHelpMessage (),
7492 defaultsTo: feature.getSettingForChannel (channel).enabledByDefault,
7593 );
7694 }
77- argParser .addFlag (
95+ parser .addFlag (
7896 'clear-features' ,
7997 help: 'Remove all configured features and restore them to the default values.' ,
8098 negatable: false ,
8199 );
100+ return parser;
101+ }
102+
103+ final ExtensionManager ? _extensionManager;
104+
105+ Future <ExtensionConfiguration ?> get _activeExtensionConfig async {
106+ if (_extensionManager case final extensionManager? ) {
107+ await extensionManager.ensureInitialized ();
108+ final List <ConfigurationExtension > extensions = extensionManager.configurationExtensions;
109+ if (extensions.isNotEmpty) {
110+ return ExtensionConfiguration (extensions: extensions, logger: globals.logger);
111+ }
112+ }
113+ return null ;
114+ }
115+
116+ @override
117+ Future <void > initializeDynamicOptions () async {
118+ if (await _activeExtensionConfig case final activeConfig? ) {
119+ _extensionSettingsGroups = await activeConfig.fetchExtensionSettings ();
120+ if (_extensionSettingsGroups.isNotEmpty) {
121+ rebuildDynamicArgParser ();
122+ }
123+ }
124+ }
125+
126+ @override
127+ ArgParser buildDynamicArgParser (ArgParser dynamicParser) {
128+ for (final ExtensionSettingsGroup (: featureFlags, : configOptions) in _extensionSettingsGroups) {
129+ for (final FeatureFlag (: name, : help, : enabledByDefault) in featureFlags) {
130+ if (! dynamicParser.options.containsKey (name)) {
131+ dynamicParser.addFlag (name, help: help, defaultsTo: enabledByDefault);
132+ } else {
133+ globals.printTrace (
134+ 'Extension feature flag "$name " conflicts with an existing option and was skipped.' ,
135+ );
136+ }
137+ }
138+ for (final ConfigOption (: name, : help, : value) in configOptions) {
139+ if (! dynamicParser.options.containsKey (name)) {
140+ dynamicParser.addOption (name, help: help, defaultsTo: value);
141+ } else {
142+ globals.printTrace (
143+ 'Extension config option "$name " conflicts with an existing option and was skipped.' ,
144+ );
145+ }
146+ }
147+ }
148+ return dynamicParser;
82149 }
83150
84151 @override
@@ -122,7 +189,7 @@ class ConfigCommand extends FlutterCommand {
122189 }
123190
124191 if (boolArg ('list' )) {
125- globals.printStatus (settingsText);
192+ globals.printStatus (await settingsText);
126193 return FlutterCommandResult .success ();
127194 }
128195
@@ -138,6 +205,15 @@ class ConfigCommand extends FlutterCommand {
138205 globals.config.removeValue (configSetting);
139206 }
140207 }
208+ final ExtensionConfiguration ? activeConfig = await _activeExtensionConfig;
209+ final List <ExtensionSettingsGroup > groups = _extensionSettingsGroups.isNotEmpty
210+ ? _extensionSettingsGroups
211+ : await activeConfig? .fetchExtensionSettings () ?? const < ExtensionSettingsGroup > [];
212+ for (final ExtensionSettingsGroup (: featureFlags) in groups) {
213+ for (final FeatureFlag (: name) in featureFlags) {
214+ globals.config.removeValue (name);
215+ }
216+ }
141217 globals.printStatus (requireReloadTipText);
142218 return FlutterCommandResult .success ();
143219 }
@@ -150,15 +226,15 @@ class ConfigCommand extends FlutterCommand {
150226 }
151227
152228 if (argResults! .wasParsed ('android-sdk' )) {
153- _updateConfig ('android-sdk' , stringArg ('android-sdk' )! );
229+ _updateConfig ('android-sdk' , stringArg ('android-sdk' ));
154230 }
155231
156232 if (argResults! .wasParsed ('android-studio-dir' )) {
157- _updateConfig ('android-studio-dir' , stringArg ('android-studio-dir' )! );
233+ _updateConfig ('android-studio-dir' , stringArg ('android-studio-dir' ));
158234 }
159235
160236 if (argResults! .wasParsed ('jdk-dir' )) {
161- _updateConfig ('jdk-dir' , stringArg ('jdk-dir' )! );
237+ _updateConfig ('jdk-dir' , stringArg ('jdk-dir' ));
162238 }
163239
164240 if (argResults! .wasParsed ('clear-ios-signing-settings' )) {
@@ -203,6 +279,21 @@ class ConfigCommand extends FlutterCommand {
203279 }
204280 }
205281
282+ for (final ExtensionSettingsGroup (: featureFlags, : configOptions) in _extensionSettingsGroups) {
283+ for (final FeatureFlag (: name) in featureFlags) {
284+ if (argResults! .wasParsed (name)) {
285+ final bool keyValue = boolArg (name);
286+ globals.config.setValue (name, keyValue);
287+ globals.printStatus ('Setting "$name " value to "$keyValue ".' );
288+ }
289+ }
290+ for (final ConfigOption (: name) in configOptions) {
291+ if (argResults! .wasParsed (name)) {
292+ _updateConfig (name, stringArg (name));
293+ }
294+ }
295+ }
296+
206297 if (argResults == null || argResults! .arguments.isEmpty) {
207298 globals.printStatus (usage);
208299 } else {
@@ -214,10 +305,9 @@ class ConfigCommand extends FlutterCommand {
214305
215306 Future <void > handleMachine () async {
216307 // Get all the current values.
217- final results = < String , Object ? > {};
218- for (final String key in globals.config.keys) {
219- results[key] = globals.config.getValue (key);
220- }
308+ final results = < String , Object ? > {
309+ for (final String key in globals.config.keys) key: globals.config.getValue (key),
310+ };
221311
222312 // Ensure we send any calculated ones, if overrides don't exist.
223313 final AndroidStudio ? androidStudio = globals.androidStudio;
@@ -236,8 +326,8 @@ class ConfigCommand extends FlutterCommand {
236326 globals.printStatus (const JsonEncoder .withIndent (' ' ).convert (results));
237327 }
238328
239- void _updateConfig (String keyName, String keyValue) {
240- if (keyValue.isEmpty) {
329+ void _updateConfig (String keyName, String ? keyValue) {
330+ if (keyValue == null || keyValue .isEmpty) {
241331 globals.config.removeValue (keyName);
242332 globals.printStatus ('Removing "$keyName " value.' );
243333 } else {
@@ -247,15 +337,12 @@ class ConfigCommand extends FlutterCommand {
247337 }
248338
249339 /// List all config settings. for feature flags, include whether they are available.
250- String get settingsText {
251- final featuresByName = < String , Feature > {};
340+ Future <String > get settingsText async {
341+ final featuresByName = < String , Feature > {
342+ for (final feature in featureFlags.allFeatures)
343+ if (feature.configSetting case final configSetting? ) configSetting: feature,
344+ };
252345 final String channel = globals.flutterVersion.channel;
253- for (final Feature feature in featureFlags.allFeatures) {
254- final String ? configSetting = feature.configSetting;
255- if (configSetting != null ) {
256- featuresByName[configSetting] = feature;
257- }
258- }
259346 final keys = < String > {
260347 ...featureFlags.allFeatures.map ((Feature e) => e.configSetting).whereType <String >(),
261348 ...globals.config.keys,
@@ -264,8 +351,8 @@ class ConfigCommand extends FlutterCommand {
264351 Object ? value = globals.config.getValue (key);
265352 value ?? = '(Not set)' ;
266353 final buffer = StringBuffer (' $key : $value ' );
267- if (featuresByName. containsKey ( key) ) {
268- final FeatureChannelSetting setting = featuresByName[key] ! .getSettingForChannel (channel);
354+ if (featuresByName[ key] case final feature ? ) {
355+ final FeatureChannelSetting setting = feature .getSettingForChannel (channel);
269356 if (! setting.available) {
270357 buffer.write (' (Unavailable)' );
271358 }
@@ -279,6 +366,30 @@ class ConfigCommand extends FlutterCommand {
279366 } else {
280367 buffer.writeln (settings.join ('\n ' ));
281368 }
369+
370+ final ExtensionConfiguration ? activeConfig = await _activeExtensionConfig;
371+ final List <ExtensionSettingsGroup > groups = _extensionSettingsGroups.isNotEmpty
372+ ? _extensionSettingsGroups
373+ : (await activeConfig? .fetchExtensionSettings ()) ?? const < ExtensionSettingsGroup > [];
374+ if (groups.any ((ExtensionSettingsGroup g) => g.isNotEmpty)) {
375+ buffer.writeln ('\n Extension Settings:' );
376+ for (final ExtensionSettingsGroup (: title, : featureFlags, : configOptions, : isEmpty)
377+ in groups) {
378+ if (isEmpty) {
379+ continue ;
380+ }
381+ buffer.writeln (' $title :' );
382+ for (final FeatureFlag (: name, : enabledByDefault) in featureFlags) {
383+ final Object val = globals.config.getValue (name) ?? enabledByDefault;
384+ buffer.writeln (' $name : $val ' );
385+ }
386+ for (final ConfigOption (: name, : value) in configOptions) {
387+ final Object val = globals.config.getValue (name) ?? value ?? '(Not set)' ;
388+ buffer.writeln (' $name : $val ' );
389+ }
390+ }
391+ }
392+
282393 return buffer.toString ();
283394 }
284395
0 commit comments