config.dart 2.39 KB
Newer Older
1 2 3 4 5 6 7 8
// 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';

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

class ConfigCommand extends FlutterCommand {
  ConfigCommand() {
    argParser.addFlag('analytics',
      negatable: true,
15
      help: 'Enable or disable reporting anonymously tool usage statistics and crash reports.');
16 17
    argParser.addOption('gradle-dir', help: 'The gradle install directory.');
    argParser.addOption('android-studio-dir', help: 'The Android Studio install directory.');
18 19 20 21 22 23 24 25 26
  }

  @override
  final String name = 'config';

  @override
  final String description =
    'Configure Flutter settings.\n\n'
    'The Flutter tool anonymously reports feature usage statistics and basic crash reports to help improve\n'
27
    'Flutter tools over time. See Google\'s privacy policy: https://www.google.com/intl/en/policies/privacy/';
28 29 30 31

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

32
  @override
33 34 35 36 37 38 39 40 41 42 43
  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'}.';
  }
44

45 46 47 48 49
  /// Return `null` to disable tracking of the `config` command.
  @override
  String get usagePath => null;

  @override
50
  Future<Null> runCommand() async {
51
    if (argResults.wasParsed('analytics')) {
52
      final bool value = argResults['analytics'];
53 54 55 56
      flutterUsage.enabled = value;
      printStatus('Analytics reporting ${value ? 'enabled' : 'disabled'}.');
    }

57 58 59 60 61 62 63 64
    if (argResults.wasParsed('gradle-dir'))
      _updateConfig('gradle-dir', argResults['gradle-dir']);

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

    if (argResults.arguments.isEmpty)
      printStatus(usage);
65
  }
66 67 68 69 70 71 72 73 74 75

  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".');
    }
  }
76
}