Skip to content

Commit 5343edb

Browse files
authored
Migrate more files to null-safety (#1758)
1 parent ce498c2 commit 5343edb

File tree

12 files changed

+43
-63
lines changed

12 files changed

+43
-63
lines changed

webdev/lib/src/command/build_command.dart

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:io' show Directory;
97

@@ -13,6 +11,7 @@ import 'package:build_daemon/client.dart';
1311
import 'package:build_daemon/data/build_status.dart';
1412
import 'package:build_daemon/data/build_target.dart';
1513
import 'package:build_daemon/data/server_log.dart';
14+
import 'package:collection/collection.dart' show IterableExtension;
1615
import 'package:logging/logging.dart' as logging;
1716

1817
import '../daemon_client.dart';
@@ -37,22 +36,21 @@ class BuildCommand extends Command<int> {
3736

3837
@override
3938
Future<int> run() async {
40-
var unsupported =
41-
argResults.rest.where((arg) => !arg.startsWith('-')).toList();
39+
var extraArgs = argResults?.rest ?? [];
40+
var unsupported = extraArgs.where((arg) => !arg.startsWith('-')).toList();
4241
if (unsupported.isNotEmpty) {
4342
throw UsageException(
4443
'Arguments were provided that are not supported: '
4544
'"${unsupported.join(' ')}".',
4645
argParser.usage);
4746
}
48-
var extraArgs =
49-
argResults.rest.where((arg) => arg.startsWith('-')).toList();
47+
var validExtraArgs = extraArgs.where((arg) => arg.startsWith('-')).toList();
5048

5149
var configuration = Configuration.fromArgs(argResults);
5250
configureLogWriter(configuration.verbose);
5351
var pubspecLock = await readPubspecLock(configuration);
5452
final arguments = buildRunnerArgs(pubspecLock, configuration)
55-
..addAll(extraArgs);
53+
..addAll(validExtraArgs);
5654

5755
try {
5856
logWriter(logging.Level.INFO, 'Connecting to the build daemon...');
@@ -66,12 +64,13 @@ class BuildCommand extends Command<int> {
6664
stackTrace: serverLog.stackTrace);
6765
},
6866
);
69-
OutputLocation outputLocation;
67+
OutputLocation? outputLocation;
68+
var outputInput = configuration.outputInput;
7069
if (configuration.outputPath != null) {
7170
outputLocation = OutputLocation((b) => b
7271
..output = configuration.outputPath
7372
..useSymlinks = false
74-
..hoist = configuration.outputInput.isNotEmpty);
73+
..hoist = outputInput != null && outputInput.isNotEmpty);
7574
}
7675
client.registerBuildTarget(DefaultBuildTarget((b) => b
7776
..target = configuration.outputInput
@@ -80,9 +79,8 @@ class BuildCommand extends Command<int> {
8079
var exitCode = 0;
8180
var gotBuildStart = false;
8281
await for (final result in client.buildResults) {
83-
var targetResult = result.results.firstWhere(
84-
(buildResult) => buildResult.target == configuration.outputInput,
85-
orElse: () => null);
82+
var targetResult = result.results.firstWhereOrNull(
83+
(buildResult) => buildResult.target == configuration.outputInput);
8684
if (targetResult == null) continue;
8785
// We ignore any builds that happen before we get a `started` event,
8886
// because those could be stale (from some other client).
@@ -97,8 +95,9 @@ class BuildCommand extends Command<int> {
9795
exitCode = 1;
9896
}
9997

100-
if (targetResult.error?.isNotEmpty == true) {
101-
logWriter(logging.Level.SEVERE, targetResult.error);
98+
var error = targetResult.error ?? '';
99+
if (error.isNotEmpty) {
100+
logWriter(logging.Level.SEVERE, error);
102101
}
103102
break;
104103
}

webdev/lib/src/daemon/daemon.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:io';
97

@@ -27,7 +25,7 @@ class Daemon {
2725
);
2826
}
2927

30-
StreamSubscription<Map<String, dynamic>> _commandSubscription;
28+
late StreamSubscription<Map<String, dynamic>> _commandSubscription;
3129

3230
final void Function(Map<String, dynamic>) _sendCommand;
3331

@@ -55,19 +53,20 @@ class Daemon {
5553
}
5654

5755
try {
58-
var method = request['method'] as String ?? '';
56+
var method = request['method'] as String? ?? '';
5957
if (!method.contains('.')) {
6058
throw ArgumentError('method not understood: $method');
6159
}
6260

6361
var domain = method.substring(0, method.indexOf('.'));
6462
var name = method.substring(method.indexOf('.') + 1);
65-
if (_domainMap[domain] == null) {
63+
var domainValue = _domainMap[domain];
64+
if (domainValue == null) {
6665
throw ArgumentError('no domain for method: $method');
6766
}
6867

69-
_domainMap[domain].handleCommand(
70-
name, id, request['params'] as Map<String, dynamic> ?? {});
68+
domainValue.handleCommand(
69+
name, id, request['params'] as Map<String, dynamic>? ?? {});
7170
} catch (error, trace) {
7271
send(<String, dynamic>{
7372
'id': id,
@@ -79,7 +78,7 @@ class Daemon {
7978

8079
void send(Map<String, dynamic> map) => _sendCommand(map);
8180

82-
void shutdown({dynamic error}) {
81+
void shutdown({Object? error}) {
8382
_commandSubscription.cancel();
8483
for (var domain in _domainMap.values) {
8584
domain.dispose();

webdev/lib/src/daemon/daemon_domain.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:io';
97

webdev/lib/src/daemon/domain.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86

97
import 'daemon.dart';
@@ -26,7 +24,7 @@ abstract class Domain {
2624

2725
void handleCommand(String command, dynamic id, Map<String, dynamic> args) {
2826
Future<dynamic>.sync(() {
29-
if (_handlers.containsKey(command)) return _handlers[command](args);
27+
if (_handlers.containsKey(command)) return _handlers[command]!(args);
3028
throw ArgumentError('command not understood: $name.$command');
3129
}).then<dynamic>((dynamic result) {
3230
if (result == null) {

webdev/lib/src/daemon/utilites.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
dynamic toJsonable(dynamic obj) {
86
if (obj is String ||
97
obj is int ||
@@ -14,7 +12,7 @@ dynamic toJsonable(dynamic obj) {
1412
return '$obj';
1513
}
1614

17-
String getStringArg(Map<String, dynamic> args, String name,
15+
String? getStringArg(Map<String, dynamic> args, String name,
1816
{bool required = false}) {
1917
if (required && !args.containsKey(name)) {
2018
throw ArgumentError('$name is required');
@@ -23,24 +21,25 @@ String getStringArg(Map<String, dynamic> args, String name,
2321
if (val != null && val is! String) {
2422
throw ArgumentError('$name is not a String');
2523
}
26-
return val as String;
24+
return val as String?;
2725
}
2826

29-
bool getBoolArg(Map<String, dynamic> args, String name,
27+
bool? getBoolArg(Map<String, dynamic> args, String name,
3028
{bool required = false}) {
3129
if (required && !args.containsKey(name)) {
3230
throw ArgumentError('$name is required');
3331
}
3432
var val = args[name];
3533
if (val != null && val is! bool) throw ArgumentError('$name is not a bool');
36-
return val as bool;
34+
return val as bool?;
3735
}
3836

39-
int getIntArg(Map<String, dynamic> args, String name, {bool required = false}) {
37+
int? getIntArg(Map<String, dynamic> args, String name,
38+
{bool required = false}) {
4039
if (required && !args.containsKey(name)) {
4140
throw ArgumentError('$name is required');
4241
}
4342
var val = args[name];
4443
if (val != null && val is! int) throw ArgumentError('$name is not an int');
45-
return val as int;
44+
return val as int?;
4645
}

webdev/lib/src/daemon_client.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:io';
97

webdev/lib/src/serve/chrome.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:io';
97

@@ -48,9 +46,9 @@ class Chrome {
4846
/// user profile, or starts a new session without sign in,
4947
/// if not specified.
5048
static Future<Chrome> start(List<String> urls,
51-
{int port, String userDataDir}) async {
49+
{required int port, String? userDataDir}) async {
5250
var signIn = false;
53-
String dir;
51+
String? dir;
5452
// Re-using the directory causes flakiness on Windows, so on that platform
5553
// pass null to have it create a directory.
5654
// Issue: https://github.com/dart-lang/webdev/issues/1545
@@ -129,14 +127,13 @@ class ChromeError extends Error {
129127
}
130128
}
131129

132-
String autoDetectChromeUserDataDirectory() {
130+
String? autoDetectChromeUserDataDirectory() {
133131
Directory directory;
132+
var home = Platform.environment['HOME'] ?? '';
134133
if (Platform.isMacOS) {
135-
var home = Platform.environment['HOME'];
136134
directory = Directory(
137135
path.join(home, 'Library', 'Application Support', 'Google', 'Chrome'));
138136
} else if (Platform.isLinux) {
139-
var home = Platform.environment['HOME'];
140137
directory = Directory(path.join(home, '.config', 'google-chrome'));
141138
} else {
142139
_logger.warning('Auto detecting chrome user data directory option is not '

webdev/lib/src/serve/handlers/favicon_handler.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97
import 'dart:io';

webdev/lib/src/serve/server_manager.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86

97
import 'package:build_daemon/data/build_status.dart';

webdev/lib/src/serve/utils.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:io';
97

0 commit comments

Comments
 (0)