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

Migrate channel, clean and a few other commands to null safety (#92950)

parent a8f67ee3
......@@ -6,7 +6,7 @@
/// where the tool should exit with a clear message to the user
/// and no stack trace unless the --verbose option is specified.
/// For example: network errors.
Never throwToolExit(String message, { int? exitCode }) {
Never throwToolExit(String? message, { int? exitCode }) {
throw ToolExit(message, exitCode: exitCode);
}
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import '../base/common.dart';
import '../cache.dart';
import '../globals.dart' as globals;
......@@ -16,7 +14,6 @@ class ChannelCommand extends FlutterCommand {
'all',
abbr: 'a',
help: 'Include all the available branches (including local branches) when listing channels.',
defaultsTo: false,
hide: !verboseHelp,
);
}
......@@ -31,29 +28,30 @@ class ChannelCommand extends FlutterCommand {
final String category = FlutterCommandCategory.sdk;
@override
String get invocation => '${runner.executableName} $name [<channel-name>]';
String get invocation => '${runner?.executableName} $name [<channel-name>]';
@override
Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{};
@override
Future<FlutterCommandResult> runCommand() async {
switch (argResults.rest.length) {
final List<String> rest = argResults?.rest ?? <String>[];
switch (rest.length) {
case 0:
await _listChannels(
showAll: boolArg('all'),
verbose: globalResults['verbose'] as bool,
verbose: globalResults?['verbose'] == true,
);
return FlutterCommandResult.success();
case 1:
await _switchChannel(argResults.rest[0]);
await _switchChannel(rest[0]);
return FlutterCommandResult.success();
default:
throw ToolExit('Too many arguments.\n$usage');
}
}
Future<void> _listChannels({ bool showAll, bool verbose }) async {
Future<void> _listChannels({ required bool showAll, required bool verbose }) async {
// Beware: currentBranch could contain PII. See getBranchName().
final String currentChannel = globals.flutterVersion.channel;
final String currentBranch = globals.flutterVersion.getBranchName();
......
......@@ -2,10 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:meta/meta.dart';
import '../../src/macos/xcode.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../build_info.dart';
......@@ -40,7 +39,8 @@ class CleanCommand extends FlutterCommand {
// Clean Xcode to remove intermediate DerivedData artifacts.
// Do this before removing ephemeral directory, which would delete the xcworkspace.
final FlutterProject flutterProject = FlutterProject.current();
if (globals.xcode.isInstalledAndMeetsVersionCheck) {
final Xcode? xcode = globals.xcode;
if (xcode != null && xcode.isInstalledAndMeetsVersionCheck) {
await _cleanXcode(flutterProject.ios);
await _cleanXcode(flutterProject.macos);
}
......@@ -78,15 +78,16 @@ class CleanCommand extends FlutterCommand {
'Cleaning Xcode workspace...',
);
try {
final XcodeProjectInterpreter xcodeProjectInterpreter = globals.xcodeProjectInterpreter!;
final Directory xcodeWorkspace = xcodeProject.xcodeWorkspace;
final XcodeProjectInfo projectInfo = await globals.xcodeProjectInterpreter.getInfo(xcodeWorkspace.parent.path);
final XcodeProjectInfo projectInfo = await xcodeProjectInterpreter.getInfo(xcodeWorkspace.parent.path);
for (final String scheme in projectInfo.schemes) {
await globals.xcodeProjectInterpreter.cleanWorkspace(xcodeWorkspace.path, scheme, verbose: _verbose);
await xcodeProjectInterpreter.cleanWorkspace(xcodeWorkspace.path, scheme, verbose: _verbose);
}
} on Exception catch (error) {
globals.printTrace('Could not clean Xcode workspace: $error');
} finally {
xcodeStatus?.stop();
xcodeStatus.stop();
}
}
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import '../../src/android/android_sdk.dart';
import '../../src/android/android_studio.dart';
import '../base/common.dart';
import '../convert.dart';
import '../features.dart';
......@@ -14,7 +14,6 @@ import '../runner/flutter_command.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,
......@@ -28,13 +27,13 @@ class ConfigCommand extends FlutterCommand {
hide: !verboseHelp,
help: 'Print config values as json.');
for (final Feature feature in allFeatures) {
if (feature.configSetting == null) {
final String? configSetting = feature.configSetting;
if (configSetting == null) {
continue;
}
argParser.addFlag(
feature.configSetting,
configSetting,
help: feature.generateHelpMessage(),
negatable: true,
);
}
argParser.addFlag(
......@@ -70,15 +69,16 @@ class ConfigCommand extends FlutterCommand {
final Map<String, Feature> featuresByName = <String, Feature>{};
final String channel = globals.flutterVersion.channel;
for (final Feature feature in allFeatures) {
if (feature.configSetting != null) {
featuresByName[feature.configSetting] = feature;
final String? configSetting = feature.configSetting;
if (configSetting != null) {
featuresByName[configSetting] = feature;
}
}
String values = globals.config.keys
.map<String>((String key) {
String configFooter = '';
if (featuresByName.containsKey(key)) {
final FeatureChannelSetting setting = featuresByName[key].getSettingForChannel(channel);
final FeatureChannelSetting setting = featuresByName[key]!.getSettingForChannel(channel);
if (!setting.available) {
configFooter = '(Unavailable)';
}
......@@ -97,7 +97,7 @@ class ConfigCommand extends FlutterCommand {
/// Return null to disable analytics recording of the `config` command.
@override
Future<String> get usagePath async => null;
Future<String?> get usagePath async => null;
@override
Future<FlutterCommandResult> runCommand() async {
......@@ -108,14 +108,15 @@ class ConfigCommand extends FlutterCommand {
if (boolArg('clear-features')) {
for (final Feature feature in allFeatures) {
if (feature.configSetting != null) {
globals.config.removeValue(feature.configSetting);
final String? configSetting = feature.configSetting;
if (configSetting != null) {
globals.config.removeValue(configSetting);
}
}
return FlutterCommandResult.success();
}
if (argResults.wasParsed('analytics')) {
if (argResults?.wasParsed('analytics') == true) {
final bool value = boolArg('analytics');
// The tool sends the analytics event *before* toggling the flag
// intentionally to be sure that opt-out events are sent correctly.
......@@ -130,20 +131,20 @@ class ConfigCommand extends FlutterCommand {
globals.printStatus('Analytics reporting ${value ? 'enabled' : 'disabled'}.');
}
if (argResults.wasParsed('android-sdk')) {
_updateConfig('android-sdk', stringArg('android-sdk'));
if (argResults?.wasParsed('android-sdk') == true) {
_updateConfig('android-sdk', stringArg('android-sdk')!);
}
if (argResults.wasParsed('android-studio-dir')) {
_updateConfig('android-studio-dir', stringArg('android-studio-dir'));
if (argResults?.wasParsed('android-studio-dir') == true) {
_updateConfig('android-studio-dir', stringArg('android-studio-dir')!);
}
if (argResults.wasParsed('clear-ios-signing-cert')) {
if (argResults?.wasParsed('clear-ios-signing-cert') == true) {
_updateConfig('ios-signing-cert', '');
}
if (argResults.wasParsed('build-dir')) {
final String buildDir = stringArg('build-dir');
if (argResults?.wasParsed('build-dir') == true) {
final String buildDir = stringArg('build-dir')!;
if (globals.fs.path.isAbsolute(buildDir)) {
throwToolExit('build-dir should be a relative path');
}
......@@ -151,17 +152,18 @@ class ConfigCommand extends FlutterCommand {
}
for (final Feature feature in allFeatures) {
if (feature.configSetting == null) {
final String? configSetting = feature.configSetting;
if (configSetting == null) {
continue;
}
if (argResults.wasParsed(feature.configSetting)) {
final bool keyValue = boolArg(feature.configSetting);
globals.config.setValue(feature.configSetting, keyValue);
globals.printStatus('Setting "${feature.configSetting}" value to "$keyValue".');
if (argResults?.wasParsed(configSetting) == true) {
final bool keyValue = boolArg(configSetting);
globals.config.setValue(configSetting, keyValue);
globals.printStatus('Setting "$configSetting" value to "$keyValue".');
}
}
if (argResults.arguments.isEmpty) {
if (argResults == null || argResults!.arguments.isEmpty) {
globals.printStatus(usage);
} else {
globals.printStatus('\nYou may need to restart any open editors for them to read new settings.');
......@@ -172,17 +174,19 @@ class ConfigCommand extends FlutterCommand {
Future<void> handleMachine() async {
// Get all the current values.
final Map<String, dynamic> results = <String, dynamic>{};
final Map<String, Object?> results = <String, Object?>{};
for (final String key in globals.config.keys) {
results[key] = globals.config.getValue(key);
}
// Ensure we send any calculated ones, if overrides don't exist.
if (results['android-studio-dir'] == null && globals.androidStudio != null) {
results['android-studio-dir'] = globals.androidStudio.directory;
final AndroidStudio? androidStudio = globals.androidStudio;
if (results['android-studio-dir'] == null && androidStudio != null) {
results['android-studio-dir'] = androidStudio.directory;
}
if (results['android-sdk'] == null && globals.androidSdk != null) {
results['android-sdk'] = globals.androidSdk.directory.path;
final AndroidSdk? androidSdk = globals.androidSdk;
if (results['android-sdk'] == null && androidSdk != null) {
results['android-sdk'] = androidSdk.directory.path;
}
globals.printStatus(const JsonEncoder.withIndent(' ').convert(results));
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'dart:async';
import '../debug_adapters/server.dart';
......@@ -31,7 +29,6 @@ class DebugAdapterCommand extends FlutterCommand {
argParser
.addFlag(
'test',
defaultsTo: false,
help: 'Whether to use the "flutter test" debug adapter to run tests'
' and emit custom events for test progress/results.',
);
......@@ -59,9 +56,9 @@ class DebugAdapterCommand extends FlutterCommand {
globals.stdio.stdout.nonBlocking,
fileSystem: globals.fs,
platform: globals.platform,
ipv6: ipv6,
ipv6: ipv6 == true,
enableDds: enableDds,
test: boolArg('test') ?? false,
test: boolArg('test'),
);
await server.channel.closed;
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import '../base/common.dart';
import '../base/utils.dart';
import '../convert.dart';
......@@ -20,7 +18,6 @@ class DevicesCommand extends FlutterCommand {
argParser.addOption(
'timeout',
abbr: 't',
defaultsTo: null,
help: '(deprecated) This option has been replaced by "--${FlutterOptions.kDeviceTimeout}".',
hide: !verboseHelp,
);
......@@ -37,9 +34,9 @@ class DevicesCommand extends FlutterCommand {
final String category = FlutterCommandCategory.tools;
@override
Duration get deviceDiscoveryTimeout {
if (argResults['timeout'] != null) {
final int timeoutSeconds = int.tryParse(stringArg('timeout'));
Duration? get deviceDiscoveryTimeout {
if (argResults?['timeout'] != null) {
final int? timeoutSeconds = int.tryParse(stringArg('timeout')!);
if (timeoutSeconds == null) {
throwToolExit('Could not parse -t/--timeout argument. It must be an integer.');
}
......@@ -50,7 +47,7 @@ class DevicesCommand extends FlutterCommand {
@override
Future<void> validateCommand() {
if (argResults['timeout'] != null) {
if (argResults?['timeout'] != null) {
globals.printError('${globals.logger.terminal.warningMark} The "--timeout" argument is deprecated; use "--${FlutterOptions.kDeviceTimeout}" instead.');
}
return super.validateCommand();
......@@ -58,14 +55,14 @@ class DevicesCommand extends FlutterCommand {
@override
Future<FlutterCommandResult> runCommand() async {
if (!globals.doctor.canListAnything) {
if (globals.doctor?.canListAnything != true) {
throwToolExit(
"Unable to locate a development device; please run 'flutter doctor' for "
'information about installing additional components.',
exitCode: 1);
}
final List<Device> devices = await globals.deviceManager.refreshAllConnectedDevices(timeout: deviceDiscoveryTimeout);
final List<Device> devices = await globals.deviceManager?.refreshAllConnectedDevices(timeout: deviceDiscoveryTimeout) ?? <Device>[];
if (boolArg('machine')) {
await printDevicesAsJson(devices);
......@@ -93,7 +90,7 @@ class DevicesCommand extends FlutterCommand {
}
Future<void> _printDiagnostics() async {
final List<String> diagnostics = await globals.deviceManager.getDeviceDiagnostics();
final List<String> diagnostics = await globals.deviceManager?.getDeviceDiagnostics() ?? <String>[];
if (diagnostics.isNotEmpty) {
globals.printStatus('');
for (final String diagnostic in diagnostics) {
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'dart:async';
import '../base/common.dart';
......@@ -34,10 +32,10 @@ class LogsCommand extends FlutterCommand {
@override
Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{};
Device device;
Device? device;
@override
Future<FlutterCommandResult> verifyThenRunCommand(String commandPath) async {
Future<FlutterCommandResult> verifyThenRunCommand(String? commandPath) async {
device = await findTargetDevice(includeUnsupportedDevices: true);
if (device == null) {
throwToolExit(null);
......@@ -47,11 +45,12 @@ class LogsCommand extends FlutterCommand {
@override
Future<FlutterCommandResult> runCommand() async {
final Device cachedDevice = device!;
if (boolArg('clear')) {
device.clearLogs();
cachedDevice.clearLogs();
}
final DeviceLogReader logReader = await device.getLogReader();
final DeviceLogReader logReader = await cachedDevice.getLogReader();
globals.printStatus('Showing $logReader logs:');
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import '../runner/flutter_command.dart';
class MakeHostAppEditableCommand extends FlutterCommand {
......
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