Skip to content

Commit 71b79a6

Browse files
authored
Add generate-build-script command (#838)
Closes #778 Wrapping entrypoints can use this to generate the build script and then run it using `Isolate.spawnUri` and avoid workaarounds like `--assume-tty`.
1 parent 752ad73 commit 71b79a6

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

build_runner/bin/build_runner.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66
import 'dart:io';
77
import 'dart:isolate';
88

9+
import 'package:args/command_runner.dart';
910
import 'package:logging/logging.dart';
1011
import 'package:path/path.dart' as p;
1112

@@ -14,26 +15,44 @@ import 'package:build_runner/src/entrypoint/options.dart';
1415
import 'package:build_runner/src/logging/std_io_logging.dart';
1516

1617
Future<Null> main(List<String> args) async {
17-
var logListener = Logger.root.onRecord.listen(stdIOLogListener);
18-
1918
// Use the actual command runner to parse the args and immediately print the
2019
// usage information if there is no command provided or the help command was
2120
// explicitly invoked.
22-
var commandRunner = new BuildCommandRunner([]);
21+
var commandRunner = new BuildCommandRunner([])
22+
..addCommand(new _GenerateBuildScript());
2323
var parsedArgs = commandRunner.parse(args);
2424
var commandName = parsedArgs.command?.name;
2525
if (commandName == null || commandName == 'help') {
2626
commandRunner.printUsage();
2727
return;
2828
}
2929

30+
StreamSubscription logListener;
31+
if (commandName != _generateCommand) {
32+
logListener = Logger.root.onRecord.listen(stdIOLogListener);
33+
}
3034
var buildScript = await generateBuildScript();
3135
var scriptFile = new File(scriptLocation)..createSync(recursive: true);
3236
scriptFile.writeAsStringSync(buildScript);
37+
if (commandName == _generateCommand) {
38+
print(p.absolute(scriptLocation));
39+
return;
40+
}
3341

3442
var exitPort = new ReceivePort();
3543
await Isolate.spawnUri(new Uri.file(p.absolute(scriptLocation)), args, null,
3644
onExit: exitPort.sendPort);
3745
await exitPort.first;
38-
await logListener.cancel();
46+
await logListener?.cancel();
47+
}
48+
49+
const _generateCommand = 'generate-build-script';
50+
51+
class _GenerateBuildScript extends Command {
52+
@override
53+
final description = 'Generate a script to run builds and print the file path '
54+
'with no other logging. Useful for wrapping builds with other tools.';
55+
56+
@override
57+
final name = _generateCommand;
3958
}

0 commit comments

Comments
 (0)