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

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

Issue 14092006: Move pub's arg parsing to bin/pub.dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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
« no previous file with comments | « sdk/lib/_internal/pub/bin/pub.dart ('k') | sdk/lib/_internal/pub/lib/src/command_cache.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/pub/lib/src/command.dart
diff --git a/sdk/lib/_internal/pub/lib/src/pub.dart b/sdk/lib/_internal/pub/lib/src/command.dart
similarity index 51%
rename from sdk/lib/_internal/pub/lib/src/pub.dart
rename to sdk/lib/_internal/pub/lib/src/command.dart
index dc663af23734f459219509e664e3542f00da7f24..21ad9b6cb23e5c2605e52b369e4e78dca2a72eb3 100644
--- a/sdk/lib/_internal/pub/lib/src/pub.dart
+++ b/sdk/lib/_internal/pub/lib/src/command.dart
@@ -1,205 +1,49 @@
-// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2013, 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.
-/// The main entrypoint for the pub command line application.
-library pub;
-
-import 'dart:async';
import 'dart:io';
-import 'dart:math';
+import 'dart:async';
import 'package:args/args.dart';
import 'package:pathos/path.dart' as path;
+import 'command_cache.dart';
import 'command_help.dart';
import 'command_install.dart';
import 'command_lish.dart';
import 'command_update.dart';
import 'command_uploader.dart';
import 'command_version.dart';
-import 'command_cache.dart';
import 'entrypoint.dart';
import 'exit_codes.dart' as exit_codes;
import 'http.dart';
-import 'io.dart';
import 'log.dart' as log;
import 'package.dart';
-import 'pubspec.dart';
-import 'sdk.dart' as sdk;
-import 'source.dart';
-import 'source_registry.dart';
import 'system_cache.dart';
import 'utils.dart';
-import 'version.dart';
-
-/// The commands that Pub understands.
-Map<String, PubCommand> get pubCommands {
- var commands = {
- 'cache': new CacheCommand(),
- 'help': new HelpCommand(),
- 'install': new InstallCommand(),
- 'publish': new LishCommand(),
- 'update': new UpdateCommand(),
- 'uploader': new UploaderCommand(),
- 'version': new VersionCommand()
- };
- for (var command in commands.values.toList()) {
- for (var alias in command.aliases) {
- commands[alias] = command;
- }
- }
- return commands;
-}
-/// The parser for arguments that are global to Pub rather than specific to a
-/// single command.
-ArgParser get pubArgParser {
- var parser = new ArgParser();
- parser.addFlag('help', abbr: 'h', negatable: false,
- help: 'Print this usage information.');
- parser.addFlag('version', negatable: false,
- help: 'Print pub version.');
- parser.addFlag('trace',
- help: 'Print debugging information when an error occurs.');
- parser.addOption('verbosity',
- help: 'Control output verbosity.',
- allowed: ['normal', 'io', 'solver', 'all'],
- allowedHelp: {
- 'normal': 'Show errors, warnings, and user messages.',
- 'io': 'Also show IO operations.',
- 'solver': 'Show steps during version resolution.',
- 'all': 'Show all output including internal tracing messages.'
- });
- parser.addFlag('verbose', abbr: 'v', negatable: false,
- help: 'Shortcut for "--verbosity=all"');
- return parser;
-}
-
-void run() {
- var globalOptions;
- try {
- globalOptions = pubArgParser.parse(new Options().arguments);
- } on FormatException catch (e) {
- log.error(e.message);
- log.error('Run "pub help" to see available options.');
- exit(exit_codes.USAGE);
- }
-
- if (globalOptions['version']) {
- printVersion();
- return;
- }
-
- if (globalOptions['help'] || globalOptions.rest.isEmpty) {
- printUsage();
- return;
- }
-
- if (globalOptions['trace']) {
- log.recordTranscript();
- }
-
- switch (globalOptions['verbosity']) {
- case 'normal': log.showNormal(); break;
- case 'io': log.showIO(); break;
- case 'solver': log.showSolver(); break;
- case 'all': log.showAll(); break;
- default:
- // No specific verbosity given, so check for the shortcut.
- if (globalOptions['verbose']) {
- log.showAll();
- } else {
- log.showNormal();
+/// The base class for commands for the pub executable.
+abstract class PubCommand {
+ /// The commands that Pub understands.
+ static Map<String, PubCommand> get commands {
+ var commands = {
+ 'cache': new CacheCommand(),
+ 'help': new HelpCommand(),
+ 'install': new InstallCommand(),
+ 'publish': new LishCommand(),
+ 'update': new UpdateCommand(),
+ 'uploader': new UploaderCommand(),
+ 'version': new VersionCommand()
+ };
+ for (var command in commands.values.toList()) {
+ for (var alias in command.aliases) {
+ commands[alias] = command;
}
- break;
- }
-
- SecureSocket.initialize(database: resourcePath('certs'));
-
- var cacheDir;
- if (Platform.environment.containsKey('PUB_CACHE')) {
- cacheDir = Platform.environment['PUB_CACHE'];
- } else if (Platform.operatingSystem == 'windows') {
- var appData = Platform.environment['APPDATA'];
- cacheDir = path.join(appData, 'Pub', 'Cache');
- } else {
- cacheDir = '${Platform.environment['HOME']}/.pub-cache';
- }
-
- validatePlatform().then((_) {
- var cache = new SystemCache.withSources(cacheDir);
-
- // Select the command.
- var command = pubCommands[globalOptions.rest[0]];
- if (command == null) {
- log.error('Could not find a command named "${globalOptions.rest[0]}".');
- log.error('Run "pub help" to see available commands.');
- exit(exit_codes.USAGE);
- return;
}
-
- var commandArgs = globalOptions.rest.sublist(1);
- command.run(cache, globalOptions, commandArgs);
- });
-}
-
-/// Checks that pub is running on a supported platform. If it isn't, it prints
-/// an error message and exits. Completes when the validation is done.
-Future validatePlatform() {
- return new Future.sync(() {
- if (Platform.operatingSystem != 'windows') return;
-
- return runProcess('ver', []).then((result) {
- if (result.stdout.join('\n').contains('XP')) {
- log.error('Sorry, but pub is not supported on Windows XP.');
- exit(exit_codes.USAGE);
- }
- });
- });
-}
-
-/// Displays usage information for the app.
-void printUsage([String description = 'Pub is a package manager for Dart.']) {
- // Build up a buffer so it shows up as a single log entry.
- var buffer = new StringBuffer();
- buffer.write(description);
- buffer.write('\n\n');
- buffer.write('Usage: pub command [arguments]\n\n');
- buffer.write('Global options:\n');
- buffer.write('${pubArgParser.getUsage()}\n\n');
-
- // Show the commands sorted.
- buffer.write('Available commands:\n');
-
- // TODO(rnystrom): A sorted map would be nice.
- int length = 0;
- var names = <String>[];
- for (var command in pubCommands.keys) {
- // Hide aliases.
- if (pubCommands[command].aliases.indexOf(command) >= 0) continue;
- length = max(length, command.length);
- names.add(command);
- }
-
- names.sort((a, b) => a.compareTo(b));
-
- for (var name in names) {
- buffer.write(' ${padRight(name, length)} '
- '${pubCommands[name].description}\n');
+ return commands;
}
- buffer.write('\n');
- buffer.write(
- 'Use "pub help [command]" for more information about a command.');
- log.message(buffer.toString());
-}
-
-void printVersion() {
- log.message('Pub ${sdk.version}');
-}
-
-abstract class PubCommand {
SystemCache cache;
ArgResults globalOptions;
ArgResults commandOptions;
« no previous file with comments | « sdk/lib/_internal/pub/bin/pub.dart ('k') | sdk/lib/_internal/pub/lib/src/command_cache.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698