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

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: 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 f2828ecf215330977f05c61119b1e33ae5c96c47..43871ef5e0f43dd01be52c385842e08118423a44 100644
--- a/sdk/lib/_internal/pub/lib/src/global_packages.dart
+++ b/sdk/lib/_internal/pub/lib/src/global_packages.dart
@@ -168,40 +168,29 @@ class GlobalPackages {
writeTextFile(_getLockFilePath(id.name),
lockFile.serialize(cache.rootDir, cache.sources));
- 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)}.');
Anton Moiseev 2014/08/20 13:49:34 Didn't want to duplicate this the fourth time, mov
Bob Nystrom 2014/08/20 17:21:21 Excellent, I was hoping you'd do that. :)
Anton Moiseev 2014/08/20 21:22:47 Acknowledged.
// 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) {
Anton Moiseev 2014/08/20 13:49:35 No meaningful changes here. Noticed that actual pa
Bob Nystrom 2014/08/20 17:21:21 Thanks!
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.
@@ -211,7 +200,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.
@@ -225,16 +214,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;
@@ -279,4 +259,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");
+
+ /// Returns [PackageId]s of the packages that have been globally activated. If
+ /// there is no packages activated, returns empty list.
+ ///
+ /// If [logPackages] is `true`, displays to the user formatted list of
+ /// activated packages. Otherwise, returns them silently.
+ List<PackageId> getActivePackages({bool logPackages: false}) {
Bob Nystrom 2014/08/20 17:21:21 Right now, since this is only used by pub global l
Anton Moiseev 2014/08/20 21:22:47 Makes sense. Done.
+ if (!dirExists(_directory)) return [];
+
+ return listDir(_directory).map((file) {
Bob Nystrom 2014/08/20 17:21:22 Skip files that don't end with .lock. Otherwise, y
Anton Moiseev 2014/08/20 21:22:46 I was assuming hidden files are skipped by default
+ var name = p.basenameWithoutExtension(file);
+ var lockFile = new LockFile.load(p.join(_directory, file), cache.sources);
+ var packageId = lockFile.packages[name];
Bob Nystrom 2014/08/20 17:21:22 Nit, but "packageId" -> "id". I think we tend to u
Anton Moiseev 2014/08/20 21:22:47 Right, thanks.
+
+ if (logPackages) {
+ log.message(_formatPackage(packageId));
+ }
+
+ return packageId;
+ }).toList();
Bob Nystrom 2014/08/20 17:21:21 We should sort the results alphabetically by packa
Anton Moiseev 2014/08/20 21:22:46 Good point, fixed, thank you.
+ }
+
+ /// 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