Skip to content

Migrate tool_runner to NNBD #2804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/src/dartdoc_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ class ToolConfiguration {
setupCommand;
}
newToolDefinitions[name] = ToolDefinition.fromCommand(
[executable] + command, setupCommand, description, resourceProvider,
[executable] + command,
setupCommand ?? const [],
description,
resourceProvider,
compileArgs: compileArgs ?? const []);
}
return ToolConfiguration._(newToolDefinitions, resourceProvider);
Expand Down
11 changes: 5 additions & 6 deletions lib/src/tool_definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class ToolDefinition {
/// A list containing the command and options to setup phase for this tool.
/// The first argument in the command is the tool executable, and will have
/// its path evaluated relative to the `dartdoc_options.yaml` location. May
/// be null or empty, in which case it will be ignored at setup time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha great catch; I don't think I've been careful about catching these.

final List<String>? setupCommand;
/// be empty, in which case it will be ignored at setup time.
final List<String> setupCommand;

/// A description of the defined tool. Must not be null.
final String description;
Expand All @@ -43,7 +43,7 @@ class ToolDefinition {
/// given.
factory ToolDefinition.fromCommand(
List<String> command,
List<String>? setupCommand,
List<String> setupCommand,
String description,
ResourceProvider resourceProvider,
{List<String>? compileArgs}) {
Expand All @@ -68,10 +68,9 @@ class ToolDefinition {

@override
String toString() {
var setupCommand = this.setupCommand;
final commandString =
'${this is DartToolDefinition ? '(Dart) ' : ''}"${command.join(' ')}"';
if (setupCommand == null) {
if (setupCommand.isEmpty) {
return '$runtimeType: $commandString ($description)';
} else {
return '$runtimeType: $commandString, with setup command '
Expand Down Expand Up @@ -140,7 +139,7 @@ class DartToolDefinition extends ToolDefinition {
}
}

DartToolDefinition(List<String> command, List<String>? setupCommand,
DartToolDefinition(List<String> command, List<String> setupCommand,
String description, this._resourceProvider,
{this.compileArgs = const []})
: super(command, setupCommand, description);
Expand Down
30 changes: 12 additions & 18 deletions lib/src/tool_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

library dartdoc.tool_runner;

import 'dart:io' show Process, ProcessException;
Expand All @@ -12,7 +10,6 @@ import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/io_utils.dart';
import 'package:dartdoc/src/tool_definition.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;

typedef ToolErrorCallback = void Function(String message);
Expand Down Expand Up @@ -97,7 +94,7 @@ class ToolRunner {
/// calls [toolErrorCallback] with a detailed error message, and returns `''`.
Future<String> _runProcess(String name, String content, String commandPath,
List<String> args, Map<String, String> environment,
{@required ToolErrorCallback toolErrorCallback}) async {
{required ToolErrorCallback toolErrorCallback}) async {
String commandString() => ([commandPath] + args).join(' ');
try {
var result =
Expand Down Expand Up @@ -128,10 +125,9 @@ class ToolRunner {
/// be sent to to the tool is given in the optional [content]. The stdout of
/// the tool is returned.
Future<String> run(List<String> args,
{@required String content,
@required ToolErrorCallback toolErrorCallback,
Map<String, String> environment}) async {
assert(args != null);
{required String content,
required ToolErrorCallback toolErrorCallback,
Map<String, String> environment = const {}}) async {
assert(args.isNotEmpty);
return _toolTracker.add(() {
return _run(args,
Expand All @@ -142,13 +138,10 @@ class ToolRunner {
}

Future<String> _run(List<String> args,
{@required ToolErrorCallback toolErrorCallback,
String content,
Map<String, String> environment}) async {
assert(args != null);
{required ToolErrorCallback toolErrorCallback,
String content = '',
required Map<String, String> environment}) async {
assert(args.isNotEmpty);
content ??= '';
environment ??= <String, String>{};
var toolName = args.removeAt(0);
if (!toolConfiguration.tools.containsKey(toolName)) {
toolErrorCallback(
Expand All @@ -157,14 +150,14 @@ class ToolRunner {
return '';
}
var toolDefinition = toolConfiguration.tools[toolName];
var toolArgs = toolDefinition.command;
var toolArgs = toolDefinition!.command;

// Substitute the temp filename for the "$INPUT" token, and all of the other
// environment variables. Variables are allowed to either be in $(VAR) form,
// or $VAR form.
var envWithInput = {
'INPUT': _tmpFileWithContent(content),
'TOOL_COMMAND': toolDefinition.command[0],
'TOOL_COMMAND': toolArgs[0],
...environment,
};
if (toolDefinition is DartToolDefinition) {
Expand All @@ -176,7 +169,7 @@ class ToolRunner {
// filesystem.
envWithInput['DART_SNAPSHOT_CACHE'] = pathContext.absolute(
SnapshotCache.instanceFor(resourceProvider).snapshotCache.path);
if (toolDefinition.setupCommand != null) {
if (toolDefinition.setupCommand.isNotEmpty) {
envWithInput['DART_SETUP_COMMAND'] = toolDefinition.setupCommand[0];
}
}
Expand All @@ -186,7 +179,8 @@ class ToolRunner {
..._substituteInArgs(args, envWithInput),
];

if (toolDefinition.setupCommand != null && !toolDefinition.setupComplete) {
if (toolDefinition.setupCommand.isNotEmpty &&
!toolDefinition.setupComplete) {
await _runSetup(
toolName, toolDefinition, envWithInput, toolErrorCallback);
}
Expand Down
2 changes: 1 addition & 1 deletion test/tool_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ echo:
// yaml map (which would fail on a missing executable), or a file is deleted
// during execution,it might, so we test it.
toolMap.tools.addAll({
'missing': ToolDefinition(['/a/missing/executable'], null, 'missing'),
'missing': ToolDefinition(['/a/missing/executable'], [], 'missing'),
});

runner = ToolRunner(toolMap);
Expand Down