flutter/packages/flutter_tools/lib/src/commands/config.dart
Greg Spencer 081d2a7a86
Re-land text wrapping/color PR (#22831)
This attempts to re-land #22656.

There are two changes from the original:

I turned off wrapping completely when not sending output to a terminal. Previously I had defaulted to wrapping at and arbitrary 100 chars in that case, just to keep long messages from being too long, but that turns out the be a bad idea because there are tests that are relying on the specific form of the output. It's also pretty arbitrary, and mostly people sending output to a non-terminal will want unwrapped text.

I found a better way to terminate ANSI color/bold sequences, so that they can be embedded within each other without needed quite as complex a dance with removing redundant sequences.

As part of these changes, I removed the Logger.supportsColor setter so that the one source of truth for color support is in AnsiTerminal.supportsColor.

*     Turn on line wrapping again in usage and status messages, adds ANSI color to doctor and analysis messages. (#22656)

    This turns on text wrapping for usage messages and status messages. When on a terminal, wraps to the width of the terminal. When writing to a non-terminal, wrap lines at a default column width (currently defined to be 100 chars). If --no-wrap is specified, then no wrapping occurs. If --wrap-column is specified, wraps to that column (if --wrap is on).

    Adds ANSI color to the doctor and analysis output on terminals. This is in this PR with the wrapping, since wrapping needs to know how to count visible characters in the presence of ANSI sequences. (This is just one more step towards re-implementing all of Curses for Flutter. :-)) Will not print ANSI sequences when sent to a non-terminal, or of --no-color is specified.

    Fixes ANSI color and bold sequences so that they can be combined (bold, colored text), and a small bug in indentation calculation for wrapping.

    Since wrapping is now turned on, also removed many redundant '\n's in the code.
2018-10-10 18:17:56 -07:00

123 lines
4.0 KiB
Dart

// 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 'dart:convert';
import '../android/android_sdk.dart';
import '../android/android_studio.dart';
import '../globals.dart';
import '../runner/flutter_command.dart';
import '../usage.dart';
class ConfigCommand extends FlutterCommand {
ConfigCommand({ bool verboseHelp = false }) {
argParser.addFlag('analytics',
negatable: true,
help: 'Enable or disable reporting anonymously tool usage statistics and crash reports.');
argParser.addFlag('clear-ios-signing-cert',
negatable: false,
help: 'Clear the saved development certificate choice used to sign apps for iOS device deployment.');
argParser.addOption('gradle-dir', help: 'The gradle install directory.');
argParser.addOption('android-sdk', help: 'The Android SDK directory.');
argParser.addOption('android-studio-dir', help: 'The Android Studio install directory.');
argParser.addFlag('machine',
negatable: false,
hide: !verboseHelp,
help: 'Print config values as json.');
}
@override
final String name = 'config';
@override
final String description =
'Configure Flutter settings.\n\n'
'To remove a setting, configure it to an empty string.\n\n'
'The Flutter tool anonymously reports feature usage statistics and basic crash reports to help improve '
'Flutter tools over time. See Google\'s privacy policy: https://www.google.com/intl/en/policies/privacy/';
@override
final List<String> aliases = <String>['configure'];
@override
bool get shouldUpdateCache => false;
@override
String get usageFooter {
// List all config settings.
String values = config.keys.map<String>((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'}.';
}
/// Return null to disable analytics recording of the `config` command.
@override
Future<String> get usagePath => null;
@override
Future<FlutterCommandResult> runCommand() async {
if (argResults['machine']) {
await handleMachine();
return null;
}
if (argResults.wasParsed('analytics')) {
final bool value = argResults['analytics'];
flutterUsage.enabled = value;
printStatus('Analytics reporting ${value ? 'enabled' : 'disabled'}.');
}
if (argResults.wasParsed('gradle-dir'))
_updateConfig('gradle-dir', argResults['gradle-dir']);
if (argResults.wasParsed('android-sdk'))
_updateConfig('android-sdk', argResults['android-sdk']);
if (argResults.wasParsed('android-studio-dir'))
_updateConfig('android-studio-dir', argResults['android-studio-dir']);
if (argResults.wasParsed('clear-ios-signing-cert'))
_updateConfig('ios-signing-cert', '');
if (argResults.arguments.isEmpty)
printStatus(usage);
return null;
}
Future<void> 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;
}
if (results['android-sdk'] == null && androidSdk != null) {
results['android-sdk'] = androidSdk.directory;
}
printStatus(const JsonEncoder.withIndent(' ').convert(results));
}
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".');
}
}
}