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

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

Issue 212923006: Rationalize arg handling for pub build and serve. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 6 years, 9 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/serve.dart
diff --git a/sdk/lib/_internal/pub/lib/src/command/serve.dart b/sdk/lib/_internal/pub/lib/src/command/serve.dart
index f7c140a778f7d45b40f6ae85490627ec882459c9..a2cc64e5400ae0c72e417f373f0818f08a8720a6 100644
--- a/sdk/lib/_internal/pub/lib/src/command/serve.dart
+++ b/sdk/lib/_internal/pub/lib/src/command/serve.dart
@@ -8,25 +8,22 @@ import 'dart:async';
import 'dart:math' as math;
import 'package:barback/barback.dart';
-import 'package:path/path.dart' as p;
import '../barback/build_environment.dart';
import '../barback/pub_package_provider.dart';
-import '../command.dart';
-import '../io.dart';
import '../log.dart' as log;
import '../utils.dart';
+import 'barback.dart';
final _arrow = getSpecial('\u2192', '=>');
/// Handles the `serve` pub command.
-class ServeCommand extends PubCommand {
+class ServeCommand extends BarbackCommand {
String get description =>
'Run a local web development server.\n\n'
'By default, this serves "web/" and "test/", but an explicit list of \n'
'directories to serve can be provided as well.';
String get usage => "pub serve [directories...]";
- final takesArguments = true;
PubPackageProvider _provider;
@@ -51,8 +48,9 @@ class ServeCommand extends PubCommand {
/// `true` if the admin server URL should be displayed on startup.
bool get logAdminUrl => commandOptions['log-admin-url'];
- /// The build mode.
- BarbackMode get mode => new BarbackMode(commandOptions['mode']);
+ BarbackMode get defaultMode => BarbackMode.DEBUG;
+
+ List<String> get defaultSourceDirectories => ["web", "test"];
/// This completer is used to keep pub running (by not completing) and to
/// pipe fatal errors to pub's top-level error-handling machinery.
@@ -78,24 +76,20 @@ class ServeCommand extends PubCommand {
help: 'Compile Dart to JavaScript.');
commandParser.addFlag('force-poll', defaultsTo: false,
help: 'Force the use of a polling filesystem watcher.');
- commandParser.addOption('mode', defaultsTo: BarbackMode.DEBUG.toString(),
- help: 'Mode to run transformers in.');
}
- Future onRun() {
+ Future onRunTransformerCommand() {
var port = parseInt(commandOptions['port'], 'port');
var adminPort = commandOptions['admin-port'] == null ? null :
parseInt(commandOptions['admin-port'], 'admin port');
- var directories = _parseDirectoriesToServe();
-
var watcherType = commandOptions['force-poll'] ?
WatcherType.POLLING : WatcherType.AUTO;
return BuildEnvironment.create(entrypoint, hostname, port, mode,
watcherType, useDart2JS: useDart2JS).then((environment) {
- var directoryLength = directories.map((dir) => dir.length)
+ var directoryLength = sourceDirectories.map((dir) => dir.length)
.reduce(math.max);
return environment.startAdminServer(adminPort).then((server) {
@@ -113,7 +107,7 @@ class ServeCommand extends PubCommand {
// that we don't log spurious build results in the middle of listing
// out the bound servers.
environment.pauseUpdates();
- return Future.forEach(directories, (directory) {
+ return Future.forEach(sourceDirectories, (directory) {
return _startServer(environment, directory, directoryLength);
});
}).then((_) {
@@ -178,50 +172,6 @@ class ServeCommand extends PubCommand {
});
}
- /// Returns the set of directories that will be served from servers exposed
- /// to the user.
- ///
- /// Throws a [UsageException] if the command-line arguments are invalid.
- List<String> _parseDirectoriesToServe() {
- if (commandOptions.rest.isEmpty) {
- var directories = ['web', 'test'].where(dirExists).toList();
- if (directories.isNotEmpty) return directories;
- usageError(
- 'Your package must have "web" and/or "test" directories to serve,\n'
- 'or you must pass in directories to serve explicitly.');
- }
-
- var directories = commandOptions.rest.map(p.normalize).toList();
- var invalid = directories.where((dir) => !p.isWithin('.', dir));
- if (invalid.isNotEmpty) {
- usageError("${_directorySentence(invalid, "isn't", "aren't")} in this "
- "package.");
- }
-
- var nonExistent = directories.where((dir) => !dirExists(dir));
- if (nonExistent.isNotEmpty) {
- usageError("${_directorySentence(nonExistent, "doesn't", "don't")} "
- "exist.");
- }
-
- return directories;
- }
-
- /// Converts a list of [directoryNames] to a sentence.
- ///
- /// After the list of directories, [singularVerb] will be used if there is
- /// only one directory and [pluralVerb] will be used if there are more than
- /// one.
- String _directorySentence(Iterable<String> directoryNames,
- String singularVerb, String pluralVerb) {
- var directories = pluralize('Directory', directoryNames.length,
- plural: 'Directories');
- var names = toSentence(ordered(directoryNames).map((dir) => '"$dir"'));
- var verb = pluralize(singularVerb, directoryNames.length,
- plural: pluralVerb);
- return "$directories $names $verb";
- }
-
/// Reports [error] and exits the server.
void _fatalError(error, [stackTrace]) {
if (_completer.isCompleted) return;

Powered by Google App Engine
This is Rietveld 408576698