| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #library('dartlang_source'); | |
| 6 | |
| 7 #import('dart:io'); | |
| 8 #import('dart:json'); | |
| 9 #import('dart:uri'); | |
| 10 #import('io.dart'); | |
| 11 #import('package.dart'); | |
| 12 #import('pubspec.dart'); | |
| 13 #import('source.dart'); | |
| 14 #import('source_registry.dart'); | |
| 15 #import('utils.dart'); | |
| 16 #import('version.dart'); | |
| 17 | |
| 18 /** | |
| 19 * A package source that installs packages from a package repository that uses | |
| 20 * the same API as pub.dartlang.org. | |
| 21 */ | |
| 22 class RepoSource extends Source { | |
| 23 final String name = "repo"; | |
| 24 | |
| 25 final bool shouldCache = true; | |
| 26 | |
| 27 // TODO(nweiz): update this comment once pub.dartlang.org is online | |
| 28 /** | |
| 29 * The URL of the default package repository. | |
| 30 * | |
| 31 * At time of writing, pub.dartlang.org is not yet online, but it should be | |
| 32 * soon. | |
| 33 */ | |
| 34 static final String defaultUrl = "http://pub.dartlang.org"; | |
| 35 | |
| 36 RepoSource(); | |
| 37 | |
| 38 /** | |
| 39 * Downloads a list of all versions of a package that have been uploaded to | |
| 40 * pub.dartlang.org. | |
| 41 */ | |
| 42 Future<List<Version>> getVersions(description) { | |
| 43 var parsed = _parseDescription(description); | |
| 44 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; | |
| 45 return consumeInputStream(httpGet(fullUrl)).transform((data) { | |
| 46 var doc = JSON.parse(new String.fromCharCodes(data)); | |
| 47 return doc['versions'].map((version) => new Version.parse(version)); | |
| 48 }); | |
| 49 } | |
| 50 | |
| 51 /** | |
| 52 * Downloads and parses the pubspec for a specific version of a package that | |
| 53 * has been uploaded to pub.dartlang.org. | |
| 54 */ | |
| 55 Future<Pubspec> describe(PackageId id) { | |
| 56 var parsed = _parseDescription(id.description); | |
| 57 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" | |
| 58 "${id.version}.yaml"; | |
| 59 return consumeInputStream(httpGet(fullUrl)).transform((data) { | |
| 60 return new Pubspec.parse( | |
| 61 new String.fromCharCodes(data), systemCache.sources); | |
| 62 }); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Downloads a package from a package repository and unpacks it. | |
| 67 */ | |
| 68 Future<bool> install(PackageId id, String destPath) { | |
| 69 var parsedDescription = _parseDescription(id.description); | |
| 70 var name = parsedDescription.first; | |
| 71 var url = parsedDescription.last; | |
| 72 | |
| 73 return ensureDir(destPath).chain((destDir) { | |
| 74 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; | |
| 75 return extractTarGz(httpGet(fullUrl), destDir); | |
| 76 }); | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * The system cache directory for the repo source contains subdirectories for | |
| 81 * each separate repository URL that's used on the system. Each of these | |
| 82 * subdirectories then contains a subdirectory for each package installed from | |
| 83 * that repository. | |
| 84 */ | |
| 85 String systemCacheDirectory(PackageId id) { | |
| 86 var parsed = _parseDescription(id.description); | |
| 87 var url = parsed.last.replaceAll(new RegExp(@"^https?://"), ""); | |
| 88 var urlDir = replace(url, new RegExp(@'[<>:"\\/|?*%]'), (match) { | |
| 89 return '%${match[0].charCodeAt(0)}'; | |
| 90 }); | |
| 91 return join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}"); | |
| 92 } | |
| 93 | |
| 94 String packageName(description) => _parseDescription(description).first; | |
| 95 | |
| 96 bool descriptionsEqual(description1, description2) => | |
| 97 _parseDescription(description1) == _parseDescription(description2); | |
| 98 | |
| 99 /** | |
| 100 * Ensures that [description] is a valid repo description. | |
| 101 * | |
| 102 * There are two valid formats. A plain string refers to a package with the | |
| 103 * given name from the default repository, while a map with keys "name" and | |
| 104 * "url" refers to a package with the given name from the repo at the given | |
| 105 * URL. | |
| 106 */ | |
| 107 void validateDescription(description, [bool fromLockFile=false]) { | |
| 108 _parseDescription(description); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Parses the description blob for a package. | |
| 113 * | |
| 114 * If the package parses correctly, this returns a (name, url) pair. If not, | |
| 115 * this throws a descriptive FormatException. | |
| 116 */ | |
| 117 Pair<String, String> _parseDescription(description) { | |
| 118 if (description is String) { | |
| 119 return new Pair<String, String>(description, defaultUrl); | |
| 120 } | |
| 121 | |
| 122 if (description is! Map) { | |
| 123 throw new FormatException( | |
| 124 "The description must be a package name or map."); | |
| 125 } | |
| 126 | |
| 127 if (!description.containsKey("name")) { | |
| 128 throw new FormatException( | |
| 129 "The description map must contain a 'name' key."); | |
| 130 } | |
| 131 | |
| 132 var name = description["name"]; | |
| 133 if (name is! String) { | |
| 134 throw new FormatException("The 'name' key must have a string value."); | |
| 135 } | |
| 136 | |
| 137 var url = description.containsKey("url") ? description["url"] : defaultUrl; | |
| 138 return new Pair<String, String>(name, url); | |
| 139 } | |
| 140 } | |
| OLD | NEW |