config.dart 3.91 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
6
import 'dart:convert';
7

8
import '../android/android_sdk.dart';
9
import '../android/android_studio.dart';
10 11
import '../globals.dart';
import '../runner/flutter_command.dart';
12
import '../usage.dart';
13 14

class ConfigCommand extends FlutterCommand {
15
  ConfigCommand({ bool verboseHelp = false }) {
16 17
    argParser.addFlag('analytics',
      negatable: true,
18
      help: 'Enable or disable reporting anonymously tool usage statistics and crash reports.');
19
    argParser.addFlag('clear-ios-signing-cert',
20
      negatable: false,
21
      help: 'Clear the saved development certificate choice used to sign apps for iOS device deployment.');
22
    argParser.addOption('gradle-dir', help: 'The gradle install directory.');
23
    argParser.addOption('android-sdk', help: 'The Android SDK directory.');
24
    argParser.addOption('android-studio-dir', help: 'The Android Studio install directory.');
25 26 27
    argParser.addFlag('machine',
      negatable: false,
      hide: !verboseHelp,
Josh Soref's avatar
Josh Soref committed
28
      help: 'Print config values as json.');
29 30 31 32 33 34 35 36
  }

  @override
  final String name = 'config';

  @override
  final String description =
    'Configure Flutter settings.\n\n'
37
    'To remove a setting, configure it to an empty string.\n\n'
38
    'The Flutter tool anonymously reports feature usage statistics and basic crash reports to help improve\n'
39
    'Flutter tools over time. See Google\'s privacy policy: https://www.google.com/intl/en/policies/privacy/';
40 41 42 43

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

44 45 46
  @override
  bool get shouldUpdateCache => false;

47
  @override
48 49 50 51 52 53 54 55 56 57 58
  String get usageFooter {
    // List all config settings.
    String values = config.keys.map((String key) {
      return '  $key: ${config.getValue(key)}';
    }).join('\n');
    if (values.isNotEmpty)
      values = '\nSettings:\n$values\n\n';
    return
      '$values'
      'Analytics reporting is currently ${flutterUsage.enabled ? 'enabled' : 'disabled'}.';
  }
59

60
  /// Return null to disable analytics recording of the `config` command.
61
  @override
62
  Future<String> get usagePath => null;
63 64

  @override
65
  Future<Null> runCommand() async {
66 67 68
    if (argResults['machine'])
      return handleMachine();

69
    if (argResults.wasParsed('analytics')) {
70
      final bool value = argResults['analytics'];
71 72 73 74
      flutterUsage.enabled = value;
      printStatus('Analytics reporting ${value ? 'enabled' : 'disabled'}.');
    }

75 76 77
    if (argResults.wasParsed('gradle-dir'))
      _updateConfig('gradle-dir', argResults['gradle-dir']);

78 79 80
    if (argResults.wasParsed('android-sdk'))
      _updateConfig('android-sdk', argResults['android-sdk']);

81 82 83
    if (argResults.wasParsed('android-studio-dir'))
      _updateConfig('android-studio-dir', argResults['android-studio-dir']);

84 85 86
    if (argResults.wasParsed('clear-ios-signing-cert'))
      _updateConfig('ios-signing-cert', '');

87 88
    if (argResults.arguments.isEmpty)
      printStatus(usage);
89
  }
90

91 92 93 94 95 96 97 98 99 100 101
  Future<Null> handleMachine() async {
    // Get all the current values.
    final Map<String, dynamic> results = <String, dynamic>{};
    for (String key in config.keys) {
      results[key] = config.getValue(key);
    }

    // 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;
    }
102 103 104
    if (results['android-sdk'] == null && androidSdk != null) {
      results['android-sdk'] = androidSdk.directory;
    }
105

106
    printStatus(const JsonEncoder.withIndent('  ').convert(results));
107 108
  }

109 110 111 112 113 114 115 116 117
  void _updateConfig(String keyName, String keyValue) {
    if (keyValue.isEmpty) {
      config.removeValue(keyName);
      printStatus('Removing "$keyName" value.');
    } else {
      config.setValue(keyName, keyValue);
      printStatus('Setting "$keyName" value to "$keyValue".');
    }
  }
118
}