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

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

Issue 489823002: Add "pub global list" command to show all globally activated packages. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address code review comments Created 6 years, 4 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/global_packages.dart
diff --git a/sdk/lib/_internal/pub/lib/src/global_packages.dart b/sdk/lib/_internal/pub/lib/src/global_packages.dart
index 57f806a14af74b18b587438eab3041b827c7fc01..055e2d624e17248ae73700c5da03607d0c4d149f 100644
--- a/sdk/lib/_internal/pub/lib/src/global_packages.dart
+++ b/sdk/lib/_internal/pub/lib/src/global_packages.dart
@@ -141,40 +141,29 @@ class GlobalPackages {
lockFile.serialize(cache.rootDir, cache.sources));
var id = lockFile.packages[package];
- if (id.source == "git") {
- var url = GitSource.urlFromDescription(id.description);
- log.message('Activated ${log.bold(id.name)} ${id.version} from Git '
- 'repository "$url".');
- } else if (id.source == "path") {
- var path = PathSource.pathFromDescription(id.description);
- log.message('Activated ${log.bold(id.name)} ${id.version} at path '
- '"$path".');
- } else {
- log.message("Activated ${log.bold(id.name)} ${id.version}.");
- }
+ log.message('Activated ${_formatPackage(id)}.');
// TODO(rnystrom): Look in "bin" and display list of binaries that
// user can run.
}
/// Shows the user the currently active package with [name], if any.
- void _describeActive(String package) {
+ void _describeActive(String name) {
try {
- var lockFile = new LockFile.load(_getLockFilePath(package),
- cache.sources);
- var id = lockFile.packages[package];
+ var lockFile = new LockFile.load(_getLockFilePath(name), cache.sources);
+ var id = lockFile.packages[name];
- if (id.source == "git") {
+ if (id.source == 'git') {
var url = GitSource.urlFromDescription(id.description);
- log.message('Package ${log.bold(id.name)} is currently active from '
- 'Git repository "${url}".');
- } else if (id.source == "path") {
+ log.message('Package ${log.bold(name)} is currently active from Git '
+ 'repository "${url}".');
+ } else if (id.source == 'path') {
var path = PathSource.pathFromDescription(id.description);
- log.message('Package ${log.bold(package)} is currently active at '
- 'path "$path".');
+ log.message('Package ${log.bold(name)} is currently active at path '
+ '"$path".');
} else {
- log.message("Package ${log.bold(package)} is currently active at "
- "version ${log.bold(id.version)}.");
+ log.message('Package ${log.bold(name)} is currently active at version '
+ '${log.bold(id.version)}.');
}
} on IOException catch (error) {
// If we couldn't read the lock file, it's not activated.
@@ -184,7 +173,7 @@ class GlobalPackages {
/// Deactivates a previously-activated package named [name].
///
- /// If [logDeletion] is true, displays to the user when a package is
+ /// If [logDeactivate] is true, displays to the user when a package is
/// deactivated. Otherwise, deactivates silently.
///
/// Returns `false` if no package with [name] was currently active.
@@ -198,16 +187,7 @@ class GlobalPackages {
deleteEntry(lockFilePath);
if (logDeactivate) {
- if (id.source == "git") {
- var url = GitSource.urlFromDescription(id.description);
- log.message('Deactivated package ${log.bold(name)} from Git repository '
- '"$url".');
- } else if (id.source == "path") {
- var path = PathSource.pathFromDescription(id.description);
- log.message('Deactivated package ${log.bold(name)} at path "$path".');
- } else {
- log.message("Deactivated package ${log.bold(name)} ${id.version}.");
- }
+ log.message('Deactivated package ${_formatPackage(id)}.');
}
return true;
@@ -252,4 +232,38 @@ class GlobalPackages {
/// Gets the path to the lock file for an activated cached package with
/// [name].
String _getLockFilePath(name) => p.join(_directory, name + ".lock");
+
+ /// Shows to the user formatted list of globally activated packages.
+ void listActivePackages() {
+ if (!dirExists(_directory)) return;
+
+ // Loads lock [file] and returns [PackageId] of the activated package.
+ loadPackageId(file) {
+ var name = p.basenameWithoutExtension(file);
+ var lockFile = new LockFile.load(p.join(_directory, file), cache.sources);
+ return lockFile.packages[name];
+ }
+
+ var packages = listDir(_directory, includeDirs: false)
+ .where((file) => p.extension(file) == '.lock')
+ .map((file) => loadPackageId)
+ .toList();
+
+ packages
+ ..sort((id1, id2) => id1.name.compareTo(id2.name))
+ ..forEach((id) => log.message(_formatPackage(id)));
+ }
+
+ /// Returns formatted string representing the package [id].
+ String _formatPackage(PackageId id) {
+ if (id.source == 'git') {
+ var url = GitSource.urlFromDescription(id.description);
+ return '${log.bold(id.name)} ${id.version} from Git repository "$url"';
+ } else if (id.source == 'path') {
+ var path = PathSource.pathFromDescription(id.description);
+ return '${log.bold(id.name)} ${id.version} at path "$path"';
+ } else {
+ return '${log.bold(id.name)} ${id.version}';
+ }
+ }
}

Powered by Google App Engine
This is Rietveld 408576698