Skip to content
This repository has been archived by the owner on May 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from dart-lang/devoncarew_query
Browse files Browse the repository at this point in the history
add some query methods to the API
  • Loading branch information
devoncarew committed Mar 20, 2015
2 parents 8eb40f8 + 4eb3c10 commit d9a9cbf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
50 changes: 31 additions & 19 deletions lib/pub_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ import 'package:path/path.dart' as path;
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart' as yaml;

// TODO: do things like get all the versions for a package?

// TODO: get the latest version for a package?

// TODO: get the latest non-dev version for a package?

// TODO: get all the packages (id's only) in the pubcache
// TODO: Scan for git packages.

/**
* TODO:
* A programattic API for reflecting on Pub's cache directory.
*/
class PubCache {

static Directory getSystemCacheLocation() {
if (Platform.environment.containsKey('PUB_CACHE')) {
return new Directory(Platform.environment['PUB_CACHE']);
Expand Down Expand Up @@ -52,10 +49,30 @@ class PubCache {
return dir.existsSync() ? dir.listSync() : [];
}

/**
* Return applications that have been installed via `pub global activate`.
*/
List<Application> getGlobalApplications() => _applications;

/**
* Get all the packages and their versions that have been installed into the
* cache.
*/
List<PackageRef> getPackageRefs() => _packageRefs;

/**
* Return the list of package names (not versions) that are available in the
* cache.
*/
List<String> getCachedPackages() =>
new Set.from(getPackageRefs().map((p) => p.name)).toList();

/**
* Return all available cached versions for a given package.
*/
List<PackageRef> getAllPackageVersions(String packageName) =>
getPackageRefs().where((p) => p.name == packageName).toList();

void _parse() {
// Read the activated applications.
_applications = [];
Expand All @@ -66,8 +83,6 @@ class PubCache {
(dir) => new Application._(this, dir)).toList();
}

// TODO: Scan for git packages.

// Scan hosted packages - just pub.dartlang.org for now.
_packageRefs = [];

Expand All @@ -90,15 +105,21 @@ class Application {
final Directory _dir;

List<PackageRef> _packageRefs;
Version _version;

Application._(this._cache, this._dir);

String get name => path.basename(_dir.path);

Version get version {
if (_packageRefs == null) _parsePubspecLock();
return _version;
PackageRef ref = getDefiningPackageRef();
return ref == null ? null : ref.version;
}

PackageRef getDefiningPackageRef() {
for (PackageRef ref in getPackageRefs()) {
if (ref.name == name) return ref;
}
return null;
}

List<PackageRef> getPackageRefs() {
Expand All @@ -116,15 +137,6 @@ class Application {
Map m = packages[key];
return new _AppPackageRef(_cache, m['source'], key, m['version']);
}).toList();

String name = this.name;

for (PackageRef ref in _packageRefs) {
if (ref.name == name) {
_version = ref.version;
break;
}
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/pub_cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ void defineTests() {
PubCache cache = new PubCache();
expect(cache.getPackageRefs(), isNotEmpty);
});

test('getCachedPackages', () {
PubCache cache = new PubCache();
expect(cache.getCachedPackages(), isNotEmpty);
});

test('getAllPackageVersions', () {
PubCache cache = new PubCache();
expect(cache.getAllPackageVersions('path'), isNotEmpty);
});
});

group('Application', () {
Expand All @@ -60,6 +70,10 @@ void defineTests() {
expect(app.version, isNotNull);
});

test('getDefiningPackageRef', () {
expect(app.getDefiningPackageRef().name, app.name);
});

test('getPackageRefs', () {
expect(app.getPackageRefs(), isNotEmpty);
});
Expand Down

0 comments on commit d9a9cbf

Please sign in to comment.