Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Unified Diff: sdk/lib/_internal/pub/lib/src/command/run.dart

Issue 301063002: First stab at "pub run" command. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/pub/lib/src/command/run.dart
diff --git a/sdk/lib/_internal/pub/lib/src/command/run.dart b/sdk/lib/_internal/pub/lib/src/command/run.dart
new file mode 100644
index 0000000000000000000000000000000000000000..87f0d3bf339815812cafee6c72796ec9be43559b
--- /dev/null
+++ b/sdk/lib/_internal/pub/lib/src/command/run.dart
@@ -0,0 +1,83 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library pub.command.run;
+
+import 'dart:async';
+import 'dart:io';
+
+import 'package:barback/barback.dart';
+
+import '../barback/asset_environment.dart';
+import '../command.dart';
+import '../exit_codes.dart' as exit_codes;
+import '../io.dart';
+import '../log.dart' as log;
+import '../utils.dart';
+
+final _arrow = getSpecial('\u2192', '=>');
+
+/// Handles the `run` pub command.
+class RunCommand extends PubCommand {
+ bool get takesArguments => true;
+ bool get allowTrailingOptions => false;
+ String get description => "Run an executable from a package.";
+ String get usage => "pub run <executable> [args...]";
+
+ Future onRun() {
+ // We only want output from the command itself, not pub.
+ log.showError();
nweiz 2014/05/29 19:30:09 I think we also want to see warnings. How does th
Bob Nystrom 2014/05/30 18:17:47 Done.
+
+ AssetEnvironment environment;
nweiz 2014/05/29 19:30:09 Nit: I don't like typing this, and I don't think i
Bob Nystrom 2014/05/30 18:17:47 Oops, that was a mistake. I sometimes temporarily
+ return AssetEnvironment.create(entrypoint, BarbackMode.RELEASE,
+ WatcherType.NONE, useDart2JS: false)
+ .then((_environment) {
+ environment = _environment;
+
+ // Show in-progress errors, but not results. Those get handled
+ // implicitly by getAllAssets().
nweiz 2014/05/29 19:30:09 What about barback logs? They should be integrated
Bob Nystrom 2014/05/30 18:17:47 That should be handled by AssetEnvironment, which
+ environment.barback.errors.listen((error) {
+ log.error(log.red("Build error:\n$error"));
+ });
+
+ return environment.serveDirectory("bin");
+ }).then((server) {
+ var command;
+ var args;
+ if (commandOptions.rest.isEmpty) {
nweiz 2014/05/29 19:30:09 This feels weird to me. I get that it's nice to ha
Bob Nystrom 2014/05/30 18:17:47 Oops, I meant to delete this. You're exactly right
+ command = entrypoint.root.name;
+ args = [];
+ } else {
+ command = commandOptions.rest[0];
+ args = commandOptions.rest.skip(1).toList();
+ }
+
+ var scriptPath = "bin/$command.dart";
nweiz 2014/05/29 19:30:09 Add a TODO to support commands in other directorie
Bob Nystrom 2014/05/30 18:17:47 Done.
+
+ // Try to make sure the entrypoint script exists (or is generated) before
+ // we spawn the process to run it.
+ return environment.barback.getAssetById(
+ new AssetId(entrypoint.root.name, scriptPath)).then((_) {
+ return environment.getUrlsForAssetPath(scriptPath).then((urls) {
+ // Should only be bound to one port.
+ assert(urls.length == 1);
+
+ // The URL to the Dart entrypoint is the first argument.
+ args.insert(0, urls[0].toString());
+ return Process.start(Platform.executable, args);
nweiz 2014/05/29 19:30:09 I think this should run in checked mode by default
Bob Nystrom 2014/05/30 18:17:47 Done.
+ }).then((process) {
+ // Wire up pub's pipes to the spawned process.
nweiz 2014/05/29 19:30:09 File a bug against dart:io to support "exec()" on
Bob Nystrom 2014/05/30 18:17:47 I could be wrong, but I don't think we'd be able t
nweiz 2014/06/10 20:20:18 There's a trick to making it work, although we'll
Bob Nystrom 2014/06/11 18:07:33 I figured we're eventually want to support running
nweiz 2014/06/12 01:17:37 Okay, but for Dart processes we'll definitely need
Bob Nystrom 2014/06/12 21:17:50 Do you mean non-package absolute path imports? Th
nweiz 2014/06/12 21:22:53 No, I mean any imports that go through barback.
+ process.stderr.pipe(stderr);
+ process.stdout.pipe(stdout);
+ stdin.pipe(process.stdin);
+
+ return process.exitCode;
+ }).then(flushThenExit);
+ }).catchError((error) {
+ log.error("Could not find $scriptPath.");
nweiz 2014/05/29 19:30:09 [log.fine] the (Chain-ified) stack trace as well.
Bob Nystrom 2014/05/30 18:17:47 Done.
+ return flushThenExit(exit_codes.NO_INPUT);
+ }, test: (error) => error is AssetNotFoundException);
nweiz 2014/05/29 19:30:09 Nit: I prefer using "if (error is! AssetNotFoundEx
Bob Nystrom 2014/05/30 18:17:47 Done. I used "test:" because I think it maintains
nweiz 2014/06/10 20:20:18 [catchError] will retain the original stack trace
Bob Nystrom 2014/06/11 18:07:33 TIL.
+ });
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698