config.dart 6.52 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_sdk.dart';
8
import '../android/android_studio.dart';
9
import '../base/common.dart';
10
import '../convert.dart';
11
import '../features.dart';
12
import '../globals.dart' as globals;
13
import '../reporting/reporting.dart';
14 15 16
import '../runner/flutter_command.dart';

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

  @override
  final String name = 'config';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

161
    return FlutterCommandResult.success();
162
  }
163

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

    // 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;
    }
175 176 177
    if (results['android-sdk'] == null && androidSdk != null) {
      results['android-sdk'] = androidSdk.directory;
    }
178

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

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