Skip to content

Commit 229174c

Browse files
authored
Migrate tool_runner to NNBD (#2804)
* Migrate tool runner * dartfmt
1 parent 574cc7e commit 229174c

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

lib/src/dartdoc_options.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ class ToolConfiguration {
242242
setupCommand;
243243
}
244244
newToolDefinitions[name] = ToolDefinition.fromCommand(
245-
[executable] + command, setupCommand, description, resourceProvider,
245+
[executable] + command,
246+
setupCommand ?? const [],
247+
description,
248+
resourceProvider,
246249
compileArgs: compileArgs ?? const []);
247250
}
248251
return ToolConfiguration._(newToolDefinitions, resourceProvider);

lib/src/tool_definition.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class ToolDefinition {
2323
/// A list containing the command and options to setup phase for this tool.
2424
/// The first argument in the command is the tool executable, and will have
2525
/// its path evaluated relative to the `dartdoc_options.yaml` location. May
26-
/// be null or empty, in which case it will be ignored at setup time.
27-
final List<String>? setupCommand;
26+
/// be empty, in which case it will be ignored at setup time.
27+
final List<String> setupCommand;
2828

2929
/// A description of the defined tool. Must not be null.
3030
final String description;
@@ -43,7 +43,7 @@ class ToolDefinition {
4343
/// given.
4444
factory ToolDefinition.fromCommand(
4545
List<String> command,
46-
List<String>? setupCommand,
46+
List<String> setupCommand,
4747
String description,
4848
ResourceProvider resourceProvider,
4949
{List<String>? compileArgs}) {
@@ -68,10 +68,9 @@ class ToolDefinition {
6868

6969
@override
7070
String toString() {
71-
var setupCommand = this.setupCommand;
7271
final commandString =
7372
'${this is DartToolDefinition ? '(Dart) ' : ''}"${command.join(' ')}"';
74-
if (setupCommand == null) {
73+
if (setupCommand.isEmpty) {
7574
return '$runtimeType: $commandString ($description)';
7675
} else {
7776
return '$runtimeType: $commandString, with setup command '
@@ -140,7 +139,7 @@ class DartToolDefinition extends ToolDefinition {
140139
}
141140
}
142141

143-
DartToolDefinition(List<String> command, List<String>? setupCommand,
142+
DartToolDefinition(List<String> command, List<String> setupCommand,
144143
String description, this._resourceProvider,
145144
{this.compileArgs = const []})
146145
: super(command, setupCommand, description);

lib/src/tool_runner.dart

Lines changed: 12 additions & 18 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
library dartdoc.tool_runner;
86

97
import 'dart:io' show Process, ProcessException;
@@ -12,7 +10,6 @@ import 'package:analyzer/file_system/file_system.dart';
1210
import 'package:dartdoc/src/dartdoc_options.dart';
1311
import 'package:dartdoc/src/io_utils.dart';
1412
import 'package:dartdoc/src/tool_definition.dart';
15-
import 'package:meta/meta.dart';
1613
import 'package:path/path.dart' as p;
1714

1815
typedef ToolErrorCallback = void Function(String message);
@@ -97,7 +94,7 @@ class ToolRunner {
9794
/// calls [toolErrorCallback] with a detailed error message, and returns `''`.
9895
Future<String> _runProcess(String name, String content, String commandPath,
9996
List<String> args, Map<String, String> environment,
100-
{@required ToolErrorCallback toolErrorCallback}) async {
97+
{required ToolErrorCallback toolErrorCallback}) async {
10198
String commandString() => ([commandPath] + args).join(' ');
10299
try {
103100
var result =
@@ -128,10 +125,9 @@ class ToolRunner {
128125
/// be sent to to the tool is given in the optional [content]. The stdout of
129126
/// the tool is returned.
130127
Future<String> run(List<String> args,
131-
{@required String content,
132-
@required ToolErrorCallback toolErrorCallback,
133-
Map<String, String> environment}) async {
134-
assert(args != null);
128+
{required String content,
129+
required ToolErrorCallback toolErrorCallback,
130+
Map<String, String> environment = const {}}) async {
135131
assert(args.isNotEmpty);
136132
return _toolTracker.add(() {
137133
return _run(args,
@@ -142,13 +138,10 @@ class ToolRunner {
142138
}
143139

144140
Future<String> _run(List<String> args,
145-
{@required ToolErrorCallback toolErrorCallback,
146-
String content,
147-
Map<String, String> environment}) async {
148-
assert(args != null);
141+
{required ToolErrorCallback toolErrorCallback,
142+
String content = '',
143+
required Map<String, String> environment}) async {
149144
assert(args.isNotEmpty);
150-
content ??= '';
151-
environment ??= <String, String>{};
152145
var toolName = args.removeAt(0);
153146
if (!toolConfiguration.tools.containsKey(toolName)) {
154147
toolErrorCallback(
@@ -157,14 +150,14 @@ class ToolRunner {
157150
return '';
158151
}
159152
var toolDefinition = toolConfiguration.tools[toolName];
160-
var toolArgs = toolDefinition.command;
153+
var toolArgs = toolDefinition!.command;
161154

162155
// Substitute the temp filename for the "$INPUT" token, and all of the other
163156
// environment variables. Variables are allowed to either be in $(VAR) form,
164157
// or $VAR form.
165158
var envWithInput = {
166159
'INPUT': _tmpFileWithContent(content),
167-
'TOOL_COMMAND': toolDefinition.command[0],
160+
'TOOL_COMMAND': toolArgs[0],
168161
...environment,
169162
};
170163
if (toolDefinition is DartToolDefinition) {
@@ -176,7 +169,7 @@ class ToolRunner {
176169
// filesystem.
177170
envWithInput['DART_SNAPSHOT_CACHE'] = pathContext.absolute(
178171
SnapshotCache.instanceFor(resourceProvider).snapshotCache.path);
179-
if (toolDefinition.setupCommand != null) {
172+
if (toolDefinition.setupCommand.isNotEmpty) {
180173
envWithInput['DART_SETUP_COMMAND'] = toolDefinition.setupCommand[0];
181174
}
182175
}
@@ -186,7 +179,8 @@ class ToolRunner {
186179
..._substituteInArgs(args, envWithInput),
187180
];
188181

189-
if (toolDefinition.setupCommand != null && !toolDefinition.setupComplete) {
182+
if (toolDefinition.setupCommand.isNotEmpty &&
183+
!toolDefinition.setupComplete) {
190184
await _runSetup(
191185
toolName, toolDefinition, envWithInput, toolErrorCallback);
192186
}

test/tool_runner_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ echo:
8888
// yaml map (which would fail on a missing executable), or a file is deleted
8989
// during execution,it might, so we test it.
9090
toolMap.tools.addAll({
91-
'missing': ToolDefinition(['/a/missing/executable'], null, 'missing'),
91+
'missing': ToolDefinition(['/a/missing/executable'], [], 'missing'),
9292
});
9393

9494
runner = ToolRunner(toolMap);

0 commit comments

Comments
 (0)