Skip to content

Commit 7ce4e6a

Browse files
authored
Add initial support for build_runner serve (#4)
Refactor shared arguments into abstract class.
1 parent 9dd2045 commit 7ce4e6a

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

webdev/lib/src/command/build_command.dart

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import 'dart:async';
2+
23
import 'package:io/io.dart';
34

4-
import 'package:args/command_runner.dart';
5+
import 'build_runner_command_base.dart';
56

67
/// Command to execute pub run build_runner build.
7-
class BuildCommand extends Command {
8+
class BuildCommand extends BuildRunnerCommandBase {
89
@override
910
final name = 'build';
1011

1112
@override
1213
final description = 'Run builders to build a package.';
1314

14-
BuildCommand() {
15-
// TODO(nshahan) Expose more args passed to build_runner build.
16-
// build_runner might expose args for use in wrapping scripts like this one.
17-
argParser
18-
..addOption('output',
19-
abbr: 'o', help: 'A directory to write the result of a build to.')
20-
..addFlag('verbose',
21-
abbr: 'v',
22-
defaultsTo: false,
23-
negatable: false,
24-
help: 'Enables verbose logging.');
25-
}
26-
2715
@override
2816
Future run() async {
2917
final manager = new ProcessManager();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:args/command_runner.dart';
2+
3+
/// Extend to get a command with the arguments common to all build_runner
4+
/// commands.
5+
abstract class BuildRunnerCommandBase extends Command {
6+
BuildRunnerCommandBase() {
7+
// TODO(nshahan) Expose more common args passed to build_runner commands.
8+
// build_runner might expose args for use in wrapping scripts like this one.
9+
argParser
10+
..addOption('output',
11+
abbr: 'o', help: 'A directory to write the result of a build to.')
12+
..addFlag('verbose',
13+
abbr: 'v',
14+
defaultsTo: false,
15+
negatable: false,
16+
help: 'Enables verbose logging.');
17+
}
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'dart:async';
2+
3+
import 'package:io/io.dart';
4+
5+
import 'build_runner_command_base.dart';
6+
7+
/// Command to execute pub run build_runner serve.
8+
class ServeCommand extends BuildRunnerCommandBase {
9+
@override
10+
final name = 'serve';
11+
12+
@override
13+
final description = 'Run a local web development server and a file system'
14+
' watcher that re-builds on changes.';
15+
16+
ServeCommand() {
17+
// TODO(nshahan) Expose more args passed to build_runner serve.
18+
// build_runner might expose args for use in wrapping scripts like this one.
19+
argParser
20+
..addOption('hostname',
21+
defaultsTo: 'localhost', help: 'Specify the hostname to serve on.');
22+
}
23+
24+
@override
25+
Future run() async {
26+
final manager = new ProcessManager();
27+
final executable = 'pub';
28+
final arguments = ['run', 'build_runner', 'serve', '--assume-tty'];
29+
arguments.addAll(argResults.arguments);
30+
var spawn = await manager.spawn(executable, arguments);
31+
32+
await spawn.exitCode;
33+
await sharedStdIn.terminate();
34+
}
35+
}
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import 'package:args/command_runner.dart';
22

33
import 'command/build_command.dart';
4+
import 'command/serve_command.dart';
45

56
/// All available top level commands.
6-
CommandRunner webdevCommandRunner() {
7-
final commandRunner =
8-
new CommandRunner('webdev', 'A tool to develop Dart web projects.');
9-
10-
commandRunner.addCommand(new BuildCommand());
11-
12-
return commandRunner;
13-
}
7+
CommandRunner webdevCommandRunner() =>
8+
new CommandRunner('webdev', 'A tool to develop Dart web projects.')
9+
..addCommand(new BuildCommand())
10+
..addCommand(new ServeCommand());

0 commit comments

Comments
 (0)