-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathdart_dev_runner.dart
83 lines (74 loc) · 2.65 KB
/
dart_dev_runner.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import 'dart:async';
import 'dart:io';
import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'dart_dev_tool.dart';
import 'events.dart' as events;
import 'utils/version.dart';
// import 'package:completion/completion.dart' as completion;
class DartDevRunner extends CommandRunner<int> {
DartDevRunner(Map<String, DevTool> commands)
: super('dart_dev', 'Dart tool runner.') {
commands.forEach((name, builder) {
final command = builder.toCommand(name);
if (command.name != name) {
throw CommandNameMismatch(command.name, name);
}
addCommand(command);
});
argParser
..addFlag('verbose',
abbr: 'v', negatable: false, help: 'Enables verbose logging.')
..addFlag('version',
negatable: false, help: 'Prints the dart_dev version.');
}
@override
ArgResults parse(Iterable<String> args) {
// TODO: get this completion working with bash/zsh
// try {
// return completion.tryArgsCompletion(args, argParser);
// } catch (_) {
// return super.parse(args);
// }
return super.parse(args);
}
@override
Future<int> run(Iterable<String> args) async {
final argResults = parse(args);
if (argResults['version'] ?? false) {
print(dartDevVersion);
return 0;
}
// About the only mechanism I can find for communicating between the different levels of this
// is that this runner holds an actual list of command instances, so we can associate the log
// file path with that.
print("commands = $commands");
print("we're trying to execute ${argResults.name}");
print("or is that ${argResults.command.name}");
final command = commands[argResults.command.name];
print(
"We are trying to write a log at ${(command as DevToolCommand).logFilePath}");
final stopwatch = Stopwatch()..start();
print("running with ${args}");
final exitCode = (await super.run(args)) ?? 0;
stopwatch.stop();
String log;
if (command != null && command is DevToolCommand) {
print("We expect to read a log at ${command.logFilePath}");
log = await File(command.logFilePath).readAsString();
print("Got ${log.length} bytes of log");
}
events.CommandResult result =
events.CommandResult(args, exitCode, stopwatch.elapsed, log: log);
await events.commandComplete(result);
return result.exitCode;
}
}
class CommandNameMismatch implements Exception {
final String actual;
final String expected;
CommandNameMismatch(this.actual, this.expected);
@override
String toString() => 'CommandNameMismatch: '
'Expected a "$expected" command but got one named "$actual".';
}