Skip to content

Commit ca783ab

Browse files
authored
Refactor build_runner command line (#4165)
* Refactor build_runner CLI to separate options from other state. * Address review comments.
1 parent c63229d commit ca783ab

File tree

68 files changed

+2277
-2343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2277
-2343
lines changed

_test/test/help_test.dart

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,52 @@ import 'common/utils.dart';
1313

1414
void main() {
1515
test('dart run build_runner help', () async {
16-
await _testHelpCommand([
17-
'help',
18-
], checkContent: 'Unified interface for running Dart builds.');
19-
await _testHelpCommand([
20-
'--help',
21-
], checkContent: 'Unified interface for running Dart builds.');
16+
await _testHelpCommand(['help'], checkContent: 'Dart build tool.');
17+
await _testHelpCommand(['--help'], checkContent: 'Dart build tool.');
2218
});
2319

2420
test('dart run build_runner build --help', () async {
2521
await _testHelpCommand([
2622
'build',
2723
'--help',
28-
], checkContent: 'Performs a single build on the specified targets');
24+
], checkContent: 'Builds the current package.');
2925
await _testHelpCommand([
3026
'help',
3127
'build',
32-
], checkContent: 'Performs a single build on the specified targets');
28+
], checkContent: 'Builds the current package.');
3329
});
3430

3531
test('dart run build_runner serve --help', () async {
3632
await _testHelpCommand([
3733
'serve',
3834
'--help',
39-
], checkContent: 'Runs a development server');
35+
], checkContent: 'Continuously builds and serves the current package.');
4036
await _testHelpCommand([
4137
'help',
4238
'serve',
43-
], checkContent: 'Runs a development server');
39+
], checkContent: 'Continuously builds and serves the current package.');
4440
});
4541

4642
test('dart run build_runner test --help', () async {
4743
await _testHelpCommand([
4844
'test',
4945
'--help',
50-
], checkContent: 'and then runs tests using');
46+
], checkContent: 'Builds the current package then runs tests.');
5147
await _testHelpCommand([
5248
'help',
5349
'test',
54-
], checkContent: 'and then runs tests using');
50+
], checkContent: 'Builds the current package then runs tests.');
5551
});
5652

5753
test('dart run build_runner watch --help', () async {
5854
await _testHelpCommand([
5955
'watch',
6056
'--help',
61-
], checkContent: 'watching the file system for updates');
57+
], checkContent: 'Continuously builds the current package.');
6258
await _testHelpCommand([
6359
'help',
6460
'watch',
65-
], checkContent: 'watching the file system for updates');
61+
], checkContent: 'Continuously builds the current package.');
6662
});
6763
}
6864

_test_common/lib/test_phases.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:build_runner_core/build_runner_core.dart';
99
import 'package:build_test/build_test.dart';
1010
// ignore: implementation_imports
1111
import 'package:build_test/src/in_memory_reader_writer.dart';
12+
import 'package:built_collection/built_collection.dart';
1213
import 'package:logging/logging.dart';
1314
import 'package:test/test.dart';
1415

@@ -142,7 +143,7 @@ Future<TestBuildersResult> testPhases(
142143
b.verbose = verbose;
143144
});
144145

145-
var options = await BuildOptions.create(
146+
var options = await BuildConfiguration.create(
146147
packageGraph: packageGraph,
147148
reader: environment.reader,
148149
skipBuildScriptCheck: true,
@@ -154,14 +155,14 @@ Future<TestBuildersResult> testPhases(
154155
var build = await BuildRunner.create(
155156
options,
156157
environment,
157-
builders,
158-
const {},
158+
builders.build(),
159+
BuiltMap(),
159160
isReleaseBuild: false,
160161
);
161162
result = await build.run(
162163
{},
163-
buildDirs: buildDirs,
164-
buildFilters: buildFilters,
164+
buildDirs: buildDirs.build(),
165+
buildFilters: buildFilters.build(),
165166
);
166167
await build.beforeExit();
167168

build_runner/bin/build_runner.dart

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,99 +2,7 @@
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-
import 'dart:async';
6-
import 'dart:io';
5+
import 'package:build_runner/src/build_runner.dart';
76

8-
import 'package:args/args.dart';
9-
import 'package:args/command_runner.dart';
10-
import 'package:build_runner/src/build_script_generate/bootstrap.dart';
11-
import 'package:build_runner/src/build_script_generate/build_process_state.dart';
12-
import 'package:build_runner/src/entrypoint/options.dart';
13-
import 'package:build_runner/src/entrypoint/runner.dart';
14-
import 'package:build_runner_core/build_runner_core.dart';
15-
import 'package:io/ansi.dart';
16-
import 'package:io/io.dart';
17-
18-
import 'src/commands/clean.dart';
19-
20-
Future<void> main(List<String> args) async {
21-
// Use the actual command runner to parse the args and immediately print the
22-
// usage information if there is no command provided or the help command was
23-
// explicitly invoked.
24-
var commandRunner = BuildCommandRunner(
25-
[],
26-
await PackageGraph.forThisPackage(),
27-
);
28-
var localCommands = [CleanCommand()];
29-
var localCommandNames = localCommands.map((c) => c.name).toSet();
30-
for (var command in localCommands) {
31-
commandRunner.addCommand(command);
32-
// This flag is added to each command individually and not the top level.
33-
command.argParser.addFlag(
34-
verboseOption,
35-
abbr: 'v',
36-
defaultsTo: false,
37-
negatable: false,
38-
help: 'Verbose logging: displays info logged by builders.',
39-
);
40-
}
41-
42-
ArgResults parsedArgs;
43-
try {
44-
parsedArgs = commandRunner.parse(args);
45-
} on UsageException catch (e) {
46-
print(red.wrap(e.message));
47-
print('');
48-
print(e.usage);
49-
exitCode = ExitCode.usage.code;
50-
return;
51-
}
52-
53-
var commandName = parsedArgs.command?.name;
54-
55-
if (parsedArgs.rest.isNotEmpty) {
56-
print(
57-
yellow.wrap('Could not find a command named "${parsedArgs.rest[0]}".'),
58-
);
59-
print('');
60-
print(commandRunner.usageWithoutDescription);
61-
exitCode = ExitCode.usage.code;
62-
return;
63-
}
64-
65-
if (commandName == 'help' ||
66-
parsedArgs.wasParsed('help') ||
67-
(parsedArgs.command?.wasParsed('help') ?? false)) {
68-
await commandRunner.runCommand(parsedArgs);
69-
return;
70-
}
71-
72-
if (commandName == null) {
73-
commandRunner.printUsage();
74-
exitCode = ExitCode.usage.code;
75-
return;
76-
}
77-
78-
if (localCommandNames.contains(commandName)) {
79-
exitCode = await commandRunner.runCommand(parsedArgs) ?? 1;
80-
} else {
81-
var experiments = parsedArgs.command!['enable-experiment'] as List<String>?;
82-
// Set the buildLog mode before the command starts so the first few log
83-
// messages are displayed correctly.
84-
switch (commandName) {
85-
case 'build':
86-
case 'serve':
87-
case 'watch':
88-
case 'test':
89-
buildLog.configuration = buildLog.configuration.rebuild((b) {
90-
b.mode = BuildLogMode.build;
91-
});
92-
case 'daemon':
93-
buildLog.configuration = buildLog.configuration.rebuild((b) {
94-
b.mode = BuildLogMode.daemon;
95-
});
96-
}
97-
while ((exitCode = await generateAndRun(args, experiments: experiments)) ==
98-
ExitCode.tempFail.code) {}
99-
}
100-
}
7+
Future<void> main(List<String> arguments) =>
8+
BuildRunner(arguments: arguments, builders: null).run();

build_runner/bin/src/commands/clean.dart

Lines changed: 0 additions & 30 deletions
This file was deleted.

build_runner/lib/build_runner.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,17 @@
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+
import 'package:build_runner_core/build_runner_core.dart'
6+
show BuilderApplication;
7+
8+
import 'src/build_runner.dart' show BuildRunner;
9+
510
export 'src/daemon/constants.dart' show assetServerPort;
6-
export 'src/entrypoint/run.dart' show run;
11+
12+
/// Runs `build_runner` with [arguments] and [builders].
13+
///
14+
/// The `build_runner` tool generates a script `.dart_tool/build/entrypoint/build.dart` that
15+
/// depends on the configured builders so it can instantiate them to pass the
16+
/// [builders] parameter.
17+
Future<int> run(List<String> arguments, List<BuilderApplication> builders) =>
18+
BuildRunner(arguments: arguments, builders: builders).run();

0 commit comments

Comments
 (0)