config.dart 6.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7
import '../android/android_studio.dart';
8
import '../base/common.dart';
9
import '../convert.dart';
10
import '../features.dart';
11
import '../globals.dart' as globals;
12
import '../reporting/reporting.dart';
13 14 15
import '../runner/flutter_command.dart';

class ConfigCommand extends FlutterCommand {
16
  ConfigCommand({ bool verboseHelp = false }) {
17 18
    argParser.addFlag('analytics',
      negatable: true,
19
      help: 'Enable or disable reporting anonymously tool usage statistics and crash reports.');
20
    argParser.addFlag('clear-ios-signing-cert',
21
      negatable: false,
22
      help: 'Clear the saved development certificate choice used to sign apps for iOS device deployment.');
23
    argParser.addOption('android-sdk', help: 'The Android SDK directory.');
24
    argParser.addOption('android-studio-dir', help: 'The Android Studio install directory.');
25 26
    argParser.addOption('build-dir', help: 'The relative path to override a projects build directory',
        valueHelp: 'out/');
27 28 29
    argParser.addFlag('machine',
      negatable: false,
      hide: !verboseHelp,
Josh Soref's avatar
Josh Soref committed
30
      help: 'Print config values as json.');
31
    for (final Feature feature in allFeatures) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45
      if (feature.configSetting == null) {
        continue;
      }
      argParser.addFlag(
        feature.configSetting,
        help: feature.generateHelpMessage(),
        negatable: true,
      );
    }
    argParser.addFlag(
      'clear-features',
      help: 'Remove all configured features and restore them to the default values.',
      negatable: false,
    );
46 47 48 49 50 51 52 53
  }

  @override
  final String name = 'config';

  @override
  final String description =
    'Configure Flutter settings.\n\n'
54
    'To remove a setting, configure it to an empty string.\n\n'
55
    'The Flutter tool anonymously reports feature usage statistics and basic crash reports to help improve '
56
    "Flutter tools over time. See Google's privacy policy: https://www.google.com/intl/en/policies/privacy/";
57 58 59 60

  @override
  final List<String> aliases = <String>['configure'];

61 62 63
  @override
  bool get shouldUpdateCache => false;

64
  @override
65
  String get usageFooter {
66 67 68
    // List all config settings. for feature flags, include whether they
    // are available.
    final Map<String, Feature> featuresByName = <String, Feature>{};
69
    final String channel = globals.flutterVersion.channel;
70
    for (final Feature feature in allFeatures) {
71 72 73 74
      if (feature.configSetting != null) {
        featuresByName[feature.configSetting] = feature;
      }
    }
75
    String values = globals.config.keys
76 77 78 79 80 81 82 83
        .map<String>((String key) {
          String configFooter = '';
          if (featuresByName.containsKey(key)) {
            final FeatureChannelSetting setting = featuresByName[key].getSettingForChannel(channel);
            if (!setting.available) {
              configFooter = '(Unavailable)';
            }
          }
84
          return '  $key: ${globals.config.getValue(key)} $configFooter';
85
        }).join('\n');
86
    if (values.isEmpty) {
87
      values = '  No settings have been configured.';
88
    }
89
    return
90
      '\nSettings:\n$values\n\n'
91
      'Analytics reporting is currently ${globals.flutterUsage.enabled ? 'enabled' : 'disabled'}.';
92
  }
93

94
  /// Return null to disable analytics recording of the `config` command.
95
  @override
96
  Future<String> get usagePath async => null;
97 98

  @override
99
  Future<FlutterCommandResult> runCommand() async {
100
    if (boolArg('machine')) {
101
      await handleMachine();
102
      return FlutterCommandResult.success();
103
    }
104

105
    if (boolArg('clear-features')) {
106
      for (final Feature feature in allFeatures) {
107
        if (feature.configSetting != null) {
108
          globals.config.removeValue(feature.configSetting);
109 110
        }
      }
111
      return FlutterCommandResult.success();
112 113
    }

114
    if (argResults.wasParsed('analytics')) {
115
      final bool value = boolArg('analytics');
116 117
      // We send the analytics event *before* toggling the flag intentionally
      // to be sure that opt-out events are sent correctly.
118
      AnalyticsConfigEvent(enabled: value).send();
119
      globals.flutterUsage.enabled = value;
120
      globals.printStatus('Analytics reporting ${value ? 'enabled' : 'disabled'}.');
121 122
    }

123
    if (argResults.wasParsed('android-sdk')) {
124
      _updateConfig('android-sdk', stringArg('android-sdk'));
125
    }
126

127
    if (argResults.wasParsed('android-studio-dir')) {
128
      _updateConfig('android-studio-dir', stringArg('android-studio-dir'));
129
    }
130

131
    if (argResults.wasParsed('clear-ios-signing-cert')) {
132
      _updateConfig('ios-signing-cert', '');
133
    }
134

135
    if (argResults.wasParsed('build-dir')) {
136
      final String buildDir = stringArg('build-dir');
137
      if (globals.fs.path.isAbsolute(buildDir)) {
138 139 140 141 142
        throwToolExit('build-dir should be a relative path');
      }
      _updateConfig('build-dir', buildDir);
    }

143
    for (final Feature feature in allFeatures) {
144 145 146 147
      if (feature.configSetting == null) {
        continue;
      }
      if (argResults.wasParsed(feature.configSetting)) {
148
        final bool keyValue = boolArg(feature.configSetting);
149 150
        globals.config.setValue(feature.configSetting, keyValue);
        globals.printStatus('Setting "${feature.configSetting}" value to "$keyValue".');
151 152 153
      }
    }

154
    if (argResults.arguments.isEmpty) {
155
      globals.printStatus(usage);
156
    } else {
157
      globals.printStatus('\nYou may need to restart any open editors for them to read new settings.');
158
    }
159

160
    return FlutterCommandResult.success();
161
  }
162

163
  Future<void> handleMachine() async {
164 165
    // Get all the current values.
    final Map<String, dynamic> results = <String, dynamic>{};
166
    for (final String key in globals.config.keys) {
167
      results[key] = globals.config.getValue(key);
168 169 170 171 172 173
    }

    // Ensure we send any calculated ones, if overrides don't exist.
    if (results['android-studio-dir'] == null && androidStudio != null) {
      results['android-studio-dir'] = androidStudio.directory;
    }
174 175
    if (results['android-sdk'] == null && globals.androidSdk != null) {
      results['android-sdk'] = globals.androidSdk.directory;
176
    }
177

178
    globals.printStatus(const JsonEncoder.withIndent('  ').convert(results));
179 180
  }

181 182
  void _updateConfig(String keyName, String keyValue) {
    if (keyValue.isEmpty) {
183 184
      globals.config.removeValue(keyName);
      globals.printStatus('Removing "$keyName" value.');
185
    } else {
186 187
      globals.config.setValue(keyName, keyValue);
      globals.printStatus('Setting "$keyName" value to "$keyValue".');
188 189
    }
  }
190
}