Unverified Commit a4e7b599 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate flutter_command to null safety (#92871)

parent 19804929
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:args/args.dart'; import 'package:args/args.dart';
import 'package:args/command_runner.dart'; import 'package:args/command_runner.dart';
import 'package:completion/completion.dart'; import 'package:completion/completion.dart';
...@@ -45,14 +43,12 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -45,14 +43,12 @@ class FlutterCommandRunner extends CommandRunner<void> {
argParser.addFlag('prefixed-errors', argParser.addFlag('prefixed-errors',
negatable: false, negatable: false,
help: 'Causes lines sent to stderr to be prefixed with "ERROR:".', help: 'Causes lines sent to stderr to be prefixed with "ERROR:".',
hide: !verboseHelp, hide: !verboseHelp);
defaultsTo: false);
argParser.addFlag('quiet', argParser.addFlag('quiet',
negatable: false, negatable: false,
hide: !verboseHelp, hide: !verboseHelp,
help: 'Reduce the amount of output from some commands.'); help: 'Reduce the amount of output from some commands.');
argParser.addFlag('wrap', argParser.addFlag('wrap',
negatable: true,
hide: !verboseHelp, hide: !verboseHelp,
help: 'Toggles output word wrapping, regardless of whether or not the output is a terminal.', help: 'Toggles output word wrapping, regardless of whether or not the output is a terminal.',
defaultsTo: true); defaultsTo: true);
...@@ -60,8 +56,7 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -60,8 +56,7 @@ class FlutterCommandRunner extends CommandRunner<void> {
hide: !verboseHelp, hide: !verboseHelp,
help: 'Sets the output wrap column. If not set, uses the width of the terminal. No ' help: 'Sets the output wrap column. If not set, uses the width of the terminal. No '
'wrapping occurs if not writing to a terminal. Use "--no-wrap" to turn off wrapping ' 'wrapping occurs if not writing to a terminal. Use "--no-wrap" to turn off wrapping '
'when connected to a terminal.', 'when connected to a terminal.');
defaultsTo: null);
argParser.addOption('device-id', argParser.addOption('device-id',
abbr: 'd', abbr: 'd',
help: 'Target device id or name (prefixes allowed).'); help: 'Target device id or name (prefixes allowed).');
...@@ -73,12 +68,10 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -73,12 +68,10 @@ class FlutterCommandRunner extends CommandRunner<void> {
hide: !verboseHelp, hide: !verboseHelp,
help: 'When used with the "--version" flag, outputs the information using JSON.'); help: 'When used with the "--version" flag, outputs the information using JSON.');
argParser.addFlag('color', argParser.addFlag('color',
negatable: true,
hide: !verboseHelp, hide: !verboseHelp,
help: 'Whether to use terminal colors (requires support for ANSI escape sequences).', help: 'Whether to use terminal colors (requires support for ANSI escape sequences).',
defaultsTo: true); defaultsTo: true);
argParser.addFlag('version-check', argParser.addFlag('version-check',
negatable: true,
defaultsTo: true, defaultsTo: true,
hide: !verboseHelp, hide: !verboseHelp,
help: 'Allow Flutter to check for updates when this command runs.'); help: 'Allow Flutter to check for updates when this command runs.');
...@@ -158,12 +151,12 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -158,12 +151,12 @@ class FlutterCommandRunner extends CommandRunner<void> {
usageException(error.message); usageException(error.message);
} }
Command<void> command = commands[error.commands.first]; Command<void>? command = commands[error.commands.first];
for (final String commandName in error.commands.skip(1)) { for (final String commandName in error.commands.skip(1)) {
command = command.subcommands[commandName]; command = command?.subcommands[commandName];
} }
command.usageException(error.message); command!.usageException(error.message);
} }
} }
...@@ -184,12 +177,12 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -184,12 +177,12 @@ class FlutterCommandRunner extends CommandRunner<void> {
@override @override
Future<void> runCommand(ArgResults topLevelResults) async { Future<void> runCommand(ArgResults topLevelResults) async {
final Map<Type, dynamic> contextOverrides = <Type, dynamic>{}; final Map<Type, Object?> contextOverrides = <Type, Object?>{};
// Don't set wrapColumns unless the user said to: if it's set, then all // Don't set wrapColumns unless the user said to: if it's set, then all
// wrapping will occur at this width explicitly, and won't adapt if the // wrapping will occur at this width explicitly, and won't adapt if the
// terminal size changes during a run. // terminal size changes during a run.
int wrapColumn; int? wrapColumn;
if (topLevelResults.wasParsed('wrap-column')) { if (topLevelResults.wasParsed('wrap-column')) {
try { try {
wrapColumn = int.parse(topLevelResults['wrap-column'] as String); wrapColumn = int.parse(topLevelResults['wrap-column'] as String);
...@@ -208,53 +201,53 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -208,53 +201,53 @@ class FlutterCommandRunner extends CommandRunner<void> {
: globals.stdio.terminalColumns != null && topLevelResults['wrap'] as bool; : globals.stdio.terminalColumns != null && topLevelResults['wrap'] as bool;
contextOverrides[OutputPreferences] = OutputPreferences( contextOverrides[OutputPreferences] = OutputPreferences(
wrapText: useWrapping, wrapText: useWrapping,
showColor: topLevelResults['color'] as bool, showColor: topLevelResults['color'] as bool?,
wrapColumn: wrapColumn, wrapColumn: wrapColumn,
); );
if (topLevelResults['show-test-device'] as bool || if ((topLevelResults['show-test-device'] as bool?) == true ||
topLevelResults['device-id'] == FlutterTesterDevices.kTesterDeviceId) { topLevelResults['device-id'] == FlutterTesterDevices.kTesterDeviceId) {
FlutterTesterDevices.showFlutterTesterDevice = true; FlutterTesterDevices.showFlutterTesterDevice = true;
} }
if (topLevelResults['show-web-server-device'] as bool || if ((topLevelResults['show-web-server-device'] as bool?) == true ||
topLevelResults['device-id'] == WebServerDevice.kWebServerDeviceId) { topLevelResults['device-id'] == WebServerDevice.kWebServerDeviceId) {
WebServerDevice.showWebServerDevice = true; WebServerDevice.showWebServerDevice = true;
} }
// Set up the tooling configuration. // Set up the tooling configuration.
final EngineBuildPaths engineBuildPaths = await globals.localEngineLocator.findEnginePath( final EngineBuildPaths? engineBuildPaths = await globals.localEngineLocator?.findEnginePath(
topLevelResults['local-engine-src-path'] as String, topLevelResults['local-engine-src-path'] as String?,
topLevelResults['local-engine'] as String, topLevelResults['local-engine'] as String?,
topLevelResults['packages'] as String, topLevelResults['packages'] as String?,
); );
if (engineBuildPaths != null) { if (engineBuildPaths != null) {
contextOverrides.addAll(<Type, dynamic>{ contextOverrides.addAll(<Type, Object?>{
Artifacts: Artifacts.getLocalEngine(engineBuildPaths), Artifacts: Artifacts.getLocalEngine(engineBuildPaths),
}); });
} }
await context.run<void>( await context.run<void>(
overrides: contextOverrides.map<Type, Generator>((Type type, dynamic value) { overrides: contextOverrides.map<Type, Generator>((Type type, Object? value) {
return MapEntry<Type, Generator>(type, () => value); return MapEntry<Type, Generator>(type, () => value);
}), }),
body: () async { body: () async {
globals.logger.quiet = topLevelResults['quiet'] as bool; globals.logger.quiet = (topLevelResults['quiet'] as bool?) == true;
if (globals.platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') { if (globals.platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') {
await globals.cache.lock(); await globals.cache.lock();
} }
if (topLevelResults['suppress-analytics'] as bool) { if ((topLevelResults['suppress-analytics'] as bool?) == true) {
globals.flutterUsage.suppressAnalytics = true; globals.flutterUsage.suppressAnalytics = true;
} }
globals.flutterVersion.ensureVersionFile(); globals.flutterVersion.ensureVersionFile();
final bool machineFlag = topLevelResults['machine'] as bool; final bool machineFlag = topLevelResults['machine'] as bool? ?? false;
final bool ci = await globals.botDetector.isRunningOnBot; final bool ci = await globals.botDetector.isRunningOnBot;
final bool redirectedCompletion = !globals.stdio.hasTerminal && final bool redirectedCompletion = !globals.stdio.hasTerminal &&
(topLevelResults.command?.name ?? '').endsWith('-completion'); (topLevelResults.command?.name ?? '').endsWith('-completion');
final bool isMachine = machineFlag || ci || redirectedCompletion; final bool isMachine = machineFlag || ci || redirectedCompletion;
final bool versionCheckFlag = topLevelResults['version-check'] as bool; final bool versionCheckFlag = topLevelResults['version-check'] as bool? ?? false;
final bool explicitVersionCheckPassed = topLevelResults.wasParsed('version-check') && versionCheckFlag; final bool explicitVersionCheckPassed = topLevelResults.wasParsed('version-check') && versionCheckFlag;
if (topLevelResults.command?.name != 'upgrade' && if (topLevelResults.command?.name != 'upgrade' &&
...@@ -263,16 +256,16 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -263,16 +256,16 @@ class FlutterCommandRunner extends CommandRunner<void> {
} }
// See if the user specified a specific device. // See if the user specified a specific device.
globals.deviceManager.specifiedDeviceId = topLevelResults['device-id'] as String; globals.deviceManager?.specifiedDeviceId = topLevelResults['device-id'] as String?;
if (topLevelResults['version'] as bool) { if ((topLevelResults['version'] as bool?) == true) {
globals.flutterUsage.sendCommand('version'); globals.flutterUsage.sendCommand('version');
globals.flutterVersion.fetchTagsAndUpdate(); globals.flutterVersion.fetchTagsAndUpdate();
String status; String status;
if (machineFlag) { if (machineFlag) {
final Map<String, Object> jsonOut = globals.flutterVersion.toJson(); final Map<String, Object> jsonOut = globals.flutterVersion.toJson();
if (jsonOut != null) { if (jsonOut != null) {
jsonOut['flutterRoot'] = Cache.flutterRoot; jsonOut['flutterRoot'] = Cache.flutterRoot!;
} }
status = const JsonEncoder.withIndent(' ').convert(jsonOut); status = const JsonEncoder.withIndent(' ').convert(jsonOut);
} else { } else {
...@@ -292,7 +285,7 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -292,7 +285,7 @@ class FlutterCommandRunner extends CommandRunner<void> {
/// Get the root directories of the repo - the directories containing Dart packages. /// Get the root directories of the repo - the directories containing Dart packages.
List<String> getRepoRoots() { List<String> getRepoRoots() {
final String root = globals.fs.path.absolute(Cache.flutterRoot); final String root = globals.fs.path.absolute(Cache.flutterRoot!);
// not bin, and not the root // not bin, and not the root
return <String>['dev', 'examples', 'packages'].map<String>((String item) { return <String>['dev', 'examples', 'packages'].map<String>((String item) {
return globals.fs.path.join(root, item); return globals.fs.path.join(root, item);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment