Files
sdk/utils/pub/repo_source.dart
T
nweiz@google.com 7b72bd1b8d Use a lockfile to persist Pub's installed version constellation.
This fixes a race condition in r9767 caused by issue 4155.

Review URL: https://chromiumcodereview.appspot.com//10790079

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@9824 260f80e4-7a28-3924-810f-c04153c831b5
2012-07-23 19:36:22 +00:00

141 lines
4.6 KiB
Dart

// Copyright (c) 2012, 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('dartlang_source');
#import('dart:io');
#import('dart:json');
#import('dart:uri');
#import('io.dart');
#import('package.dart');
#import('pubspec.dart');
#import('source.dart');
#import('source_registry.dart');
#import('utils.dart');
#import('version.dart');
/**
* A package source that installs packages from a package repository that uses
* the same API as pub.dartlang.org.
*/
class RepoSource extends Source {
final String name = "repo";
final bool shouldCache = true;
// TODO(nweiz): update this comment once pub.dartlang.org is online
/**
* The URL of the default package repository.
*
* At time of writing, pub.dartlang.org is not yet online, but it should be
* soon.
*/
static final String defaultUrl = "http://pub.dartlang.org";
RepoSource();
/**
* Downloads a list of all versions of a package that have been uploaded to
* pub.dartlang.org.
*/
Future<List<Version>> getVersions(description) {
var parsed = _parseDescription(description);
var fullUrl = "${parsed.last}/packages/${parsed.first}.json";
return consumeInputStream(httpGet(fullUrl)).transform((data) {
var doc = JSON.parse(new String.fromCharCodes(data));
return doc['versions'].map((version) => new Version.parse(version));
});
}
/**
* Downloads and parses the pubspec for a specific version of a package that
* has been uploaded to pub.dartlang.org.
*/
Future<Pubspec> describe(PackageId id) {
var parsed = _parseDescription(id.description);
var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/"
"${id.version}.yaml";
return consumeInputStream(httpGet(fullUrl)).transform((data) {
return new Pubspec.parse(
new String.fromCharCodes(data), systemCache.sources);
});
}
/**
* Downloads a package from a package repository and unpacks it.
*/
Future<bool> install(PackageId id, String destPath) {
var parsedDescription = _parseDescription(id.description);
var name = parsedDescription.first;
var url = parsedDescription.last;
return ensureDir(destPath).chain((destDir) {
var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz";
return extractTarGz(httpGet(fullUrl), destDir);
});
}
/**
* The system cache directory for the repo source contains subdirectories for
* each separate repository URL that's used on the system. Each of these
* subdirectories then contains a subdirectory for each package installed from
* that repository.
*/
String systemCacheDirectory(PackageId id) {
var parsed = _parseDescription(id.description);
var url = parsed.last.replaceAll(new RegExp(@"^https?://"), "");
var urlDir = replace(url, new RegExp(@'[<>:"\\/|?*%]'), (match) {
return '%${match[0].charCodeAt(0)}';
});
return join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}");
}
String packageName(description) => _parseDescription(description).first;
bool descriptionsEqual(description1, description2) =>
_parseDescription(description1) == _parseDescription(description2);
/**
* Ensures that [description] is a valid repo description.
*
* There are two valid formats. A plain string refers to a package with the
* given name from the default repository, while a map with keys "name" and
* "url" refers to a package with the given name from the repo at the given
* URL.
*/
void validateDescription(description, [bool fromLockFile=false]) {
_parseDescription(description);
}
/**
* Parses the description blob for a package.
*
* If the package parses correctly, this returns a (name, url) pair. If not,
* this throws a descriptive FormatException.
*/
Pair<String, String> _parseDescription(description) {
if (description is String) {
return new Pair<String, String>(description, defaultUrl);
}
if (description is! Map) {
throw new FormatException(
"The description must be a package name or map.");
}
if (!description.containsKey("name")) {
throw new FormatException(
"The description map must contain a 'name' key.");
}
var name = description["name"];
if (name is! String) {
throw new FormatException("The 'name' key must have a string value.");
}
var url = description.containsKey("url") ? description["url"] : defaultUrl;
return new Pair<String, String>(name, url);
}
}