Skip to content

Add generate-build-script command #838

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
Jan 12, 2018
Merged
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
27 changes: 23 additions & 4 deletions build_runner/bin/build_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:io';
import 'dart:isolate';

import 'package:args/command_runner.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;

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

Future<Null> main(List<String> args) async {
var logListener = Logger.root.onRecord.listen(stdIOLogListener);

// Use the actual command runner to parse the args and immediately print the
// usage information if there is no command provided or the help command was
// explicitly invoked.
var commandRunner = new BuildCommandRunner([]);
var commandRunner = new BuildCommandRunner([])
..addCommand(new _GenerateBuildScript());
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason we shouldn't add this to the command runner generally?

Copy link
Member Author

Choose a reason for hiding this comment

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

The other place we use this CommandRunner doesn't have the capability of performing this command.

var parsedArgs = commandRunner.parse(args);
var commandName = parsedArgs.command?.name;
if (commandName == null || commandName == 'help') {
commandRunner.printUsage();
return;
}

StreamSubscription logListener;
if (commandName != _generateCommand) {
logListener = Logger.root.onRecord.listen(stdIOLogListener);
}
var buildScript = await generateBuildScript();
var scriptFile = new File(scriptLocation)..createSync(recursive: true);
scriptFile.writeAsStringSync(buildScript);
if (commandName == _generateCommand) {
print(p.absolute(scriptLocation));
return;
}

var exitPort = new ReceivePort();
await Isolate.spawnUri(new Uri.file(p.absolute(scriptLocation)), args, null,
onExit: exitPort.sendPort);
await exitPort.first;
await logListener.cancel();
await logListener?.cancel();
}

const _generateCommand = 'generate-build-script';

class _GenerateBuildScript extends Command {
@override
final description = 'Generate a script to run builds and print the file path '
'with no other logging. Useful for wrapping builds with other tools.';

@override
final name = _generateCommand;
}