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

Commit

Permalink
add support for git references
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed Apr 5, 2015
1 parent 10f26e6 commit b80c4d9
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 45 deletions.
74 changes: 29 additions & 45 deletions lib/pub_cache.dart
Expand Up @@ -10,6 +10,8 @@ import 'package:path/path.dart' as path;
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart' as yaml;

import 'src/impl.dart';

/// A programmatic API for reflecting on Pub's cache directory.
class PubCache {

Expand Down Expand Up @@ -106,13 +108,19 @@ class PubCache {
if (dartlangDir.existsSync()) {
_packageRefs = dartlangDir.listSync()
.where((dir) => dir is Directory)
.map((dir) => new _DirectoryPackageRef('hosted', dir))
.map((dir) => new DirectoryPackageRef('hosted', dir))
.toList();
}

// TODO: Scan for git packages (ignore the git/cache directory).
// Scan for git packages (ignore the git/cache directory).
// ace-a1a140cc933e7d44d2955a6d6033308754bb9235

Directory gitDir = new Directory(path.join(location.path, 'git'));
if (gitDir.existsSync()) {
Iterable gitRefs = gitDir.listSync()
.where((dir) => dir is Directory && path.basename(dir.path) != 'cache')
.map((dir) => new GitDirectoryPackageRef(dir));
_packageRefs.addAll(gitRefs);
}
}

Directory _getSubDir(Directory dir, String name) =>
Expand Down Expand Up @@ -162,7 +170,24 @@ class Application {
Map packages = doc['packages'];
_packageRefs = packages.keys.map((key) {
Map m = packages[key];
return new _AppPackageRef(_cache, m['source'], key, m['version']);
String source = m['source'];
if (source == 'git') {
return new PackageRefImpl.git(key, m['version'], m['description'], (curRef) {
for (PackageRef ref in _cache.getPackageRefs()) {
if (ref == curRef) return ref.resolve();
}
return null;
});
} else if (source == 'hosted') {
return new PackageRefImpl.hosted(key, m['version'], (curRef) {
for (PackageRef ref in _cache.getPackageRefs()) {
if (ref == curRef) return ref.resolve();
}
return null;
});
} else {
return new PackageRefImpl(source, key, m['version']);
}
}).toList();
}
}
Expand Down Expand Up @@ -190,47 +215,6 @@ abstract class PackageRef {
String toString() => '${name} ${version}';
}

class _AppPackageRef extends PackageRef {
final PubCache cache;
final String sourceType;
final String name;
final Version version;

_AppPackageRef(this.cache, this.sourceType, this.name, String ver) :
version = new Version.parse(ver);

Package resolve() {
for (PackageRef ref in cache.getPackageRefs()) {
if (ref == this) return ref.resolve();
}

return null;
}
}

class _DirectoryPackageRef extends PackageRef {
final String sourceType;
final Directory directory;

String _name;
Version _version;

_DirectoryPackageRef(this.sourceType, this.directory) {
_name = path.basename(this.directory.path);

int index = _name.indexOf('-');
if (index != -1) {
_version = new Version.parse(_name.substring(index + 1));
_name = _name.substring(0, index);
}
}

String get name => _name;
Version get version => _version;

Package resolve() => new Package(directory, name, version);
}

/// A representation of a package, including the name, version, and location on
/// disk.
class Package {
Expand Down
105 changes: 105 additions & 0 deletions lib/src/impl.dart
@@ -0,0 +1,105 @@
// Copyright (c) 2015, 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_cache.impl;

import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart' as yaml;

import '../pub_cache.dart';

class PackageRefImpl extends PackageRef {
final String sourceType;
final String name;
final Version version;

Function _resolver;

PackageRefImpl(this.sourceType, this.name, String ver) :
version = new Version.parse(ver);

PackageRefImpl.hosted(this.name, String ver, this._resolver) :
sourceType = 'hosted', version = new Version.parse(ver);

factory PackageRefImpl.git(String name, String ver, Map description, Function resolver) {
return new GitPackageRefImpl(name, ver, description, resolver);
}

Package resolve() => _resolver == null ? null : _resolver(this);
}

class GitPackageRefImpl extends PackageRefImpl {
final Map _description;

GitPackageRefImpl(String name, String ver, this._description, Function resolver) :
super('git', name, ver) {
_resolver = resolver;
}

/// The git commit.
String get resolvedRef => _description['resolved-ref'];

/// The git url.
String get url => _description['url'];
}

class DirectoryPackageRef extends PackageRef {
final String sourceType;
final Directory directory;

String _name;
Version _version;

DirectoryPackageRef(this.sourceType, this.directory) {
_name = path.basename(this.directory.path);

int index = _name.indexOf('-');
if (index != -1) {
_version = new Version.parse(_name.substring(index + 1));
_name = _name.substring(0, index);
}
}

String get name => _name;
Version get version => _version;

Package resolve() => new Package(directory, name, version);
}

class GitDirectoryPackageRef extends PackageRef {
final String sourceType;
final Directory directory;

String _name;
Version _version;
String _resolvedRef;

GitDirectoryPackageRef(this.directory) : sourceType = 'git' {
_name = path.basename(this.directory.path);

int index = _name.indexOf('-');
if (index != -1) {
_resolvedRef = _name.substring(index + 1);
_name = _name.substring(0, index);
}

// Parse the version.
File f = new File(path.join(directory.path, 'pubspec.yaml'));
if (f.existsSync()) {
Map pubspec = yaml.loadYaml(f.readAsStringSync());
_version = new Version.parse(pubspec['version']);
}
}

String get name => _name;
Version get version => _version;

/// The git commit.
String get resolvedRef => _resolvedRef;

Package resolve() => new Package(directory, name, version);
}
16 changes: 16 additions & 0 deletions test/pub_cache_test.dart
Expand Up @@ -133,4 +133,20 @@ void defineTests() {
expect(p.toString(), isNotEmpty);
});
});

group('integration', () {
test('list', () {
PubCache cache = new PubCache();
var apps = cache.getGlobalApplications();
print('${apps.length} activated applications:');
apps.forEach((app) => print(' ${app}'));

var packages = cache.getCachedPackages();
print('\n${packages.length} packages in cache:');
packages.forEach((pkg) {
List versions = cache.getAllPackageVersions(pkg);
print(' ${pkg} [${versions.map((p) => p.version.toString()).join(', ')}]');
});
});
});
}

0 comments on commit b80c4d9

Please sign in to comment.