Skip to content
Snippets Groups Projects
Commit a58d0175 authored by rnystrom@google.com's avatar rnystrom@google.com Committed by Natalie Weizenbaum
Browse files

Add "pub global list" command to show all globally activated packages.

Clean up global_packages.dart.

BUG=http://dartbug.com/19962
R=rnystrom@google.com

Review URL: https://codereview.chromium.org//489823002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge@39466 260f80e4-7a28-3924-810f-c04153c831b5
parent 14d48feb
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ library pub.command.global;
import '../command.dart';
import 'global_activate.dart';
import 'global_deactivate.dart';
import 'global_list.dart';
import 'global_run.dart';
/// Handles the `global` pub command.
......@@ -17,6 +18,7 @@ class GlobalCommand extends PubCommand {
final subcommands = {
"activate": new GlobalActivateCommand(),
"deactivate": new GlobalDeactivateCommand(),
"list": new GlobalListCommand(),
"run": new GlobalRunCommand()
};
}
// 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.global_list;
import 'dart:async';
import '../command.dart';
/// Handles the `global list` pub command.
class GlobalListCommand extends PubCommand {
bool get allowTrailingOptions => false;
String get description => 'List globally activated packages.';
String get usage => 'pub global list';
Future onRun() {
globals.listActivePackages();
return null;
}
}
......@@ -147,40 +147,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.
......@@ -190,7 +179,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.
......@@ -204,16 +193,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;
......@@ -258,4 +238,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(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}';
}
}
}
......@@ -20,6 +20,6 @@ main() {
schedulePub(args: ["global", "activate", "-sgit", "../foo.git"]);
schedulePub(args: ["global", "deactivate", "foo"],
output: 'Deactivated package foo from Git repository "../foo.git".');
output: 'Deactivated package foo 1.0.0 from Git repository "../foo.git".');
});
}
......@@ -22,6 +22,6 @@ main() {
var path = canonicalize(p.join(sandboxDir, "foo"));
schedulePub(args: ["global", "deactivate", "foo"],
output: 'Deactivated package foo at path "$path".');
output: 'Deactivated package foo 1.0.0 at path "$path".');
});
}
// 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.
import 'package:path/path.dart' as p;
import '../../lib/src/io.dart';
import '../descriptor.dart' as d;
import '../test_pub.dart';
main() {
initConfig();
integration('lists an activated hosted package', () {
servePackages([
packageMap('foo', '1.0.0')
]);
schedulePub(args: ['global', 'activate', 'foo']);
schedulePub(args: ['global', 'list'], output: 'foo 1.0.0');
});
integration('lists an activated Git package', () {
ensureGit();
d.git('foo.git', [
d.libPubspec('foo', '1.0.0'),
d.dir('bin', [
d.file('foo.dart', 'main() => print("ok");')
])
]).create();
schedulePub(args: ['global', 'activate', '-sgit', '../foo.git']);
schedulePub(args: ['global', 'list'],
output: 'foo 1.0.0 from Git repository "../foo.git"');
});
integration('lists an activated Path package', () {
d.dir('foo', [
d.libPubspec('foo', '1.0.0'),
d.dir('bin', [
d.file('foo.dart', 'main() => print("ok");')
])
]).create();
schedulePub(args: ['global', 'activate', '-spath', '../foo']);
var path = canonicalize(p.join(sandboxDir, 'foo'));
schedulePub(args: ['global', 'list'],
output: 'foo 1.0.0 at path "$path"');
});
integration('lists activated packages in alphabetical order', () {
servePackages([
packageMap('aaa', '1.0.0'),
packageMap('bbb', '1.0.0'),
packageMap('ccc', '1.0.0')
]);
schedulePub(args: ['global', 'activate', 'ccc']);
schedulePub(args: ['global', 'activate', 'aaa']);
schedulePub(args: ['global', 'activate', 'bbb']);
schedulePub(args: ['global', 'list'], output: '''
aaa 1.0.0
bbb 1.0.0
ccc 1.0.0
''');
});
integration('lists nothing when no packages activated', () {
schedulePub(args: ['global', 'list'], output: '\n');
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment