| 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:uri'); | |
| 9 #import('io.dart'); | |
| 10 #import('package.dart'); | |
| 11 #import('source.dart'); | |
| 12 #import('utils.dart'); | |
| 13 | |
| 14 /** | |
| 15 * A package source that installs packages from a package repository that uses | |
| 16 * the same API as pub.dartlang.org. | |
| 17 */ | |
| 18 class RepoSource extends Source { | |
| 19 final String name = "repo"; | |
| 20 | |
| 21 final bool shouldCache = true; | |
| 22 | |
| 23 // TODO(nweiz): update this comment once pub.dartlang.org is online | |
| 24 /** | |
| 25 * The URL of the default package repository. | |
| 26 * | |
| 27 * At time of writing, pub.dartlang.org is not yet online, but it should be | |
| 28 * soon. | |
| 29 */ | |
| 30 static final String defaultUrl = "http://pub.dartlang.org"; | |
| 31 | |
| 32 RepoSource(); | |
| 33 | |
| 34 /** | |
| 35 * Downloads a package from a package repository and unpacks it. | |
| 36 */ | |
| 37 Future<bool> install(PackageId id, String destPath) { | |
| 38 var parsedDescription = _parseDescription(id.description); | |
| 39 var name = parsedDescription.first; | |
| 40 var url = parsedDescription.last; | |
| 41 | |
| 42 return ensureDir(destPath).chain((destDir) { | |
| 43 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; | |
| 44 return extractTarGz(httpGet(fullUrl), destDir); | |
| 45 }); | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * The system cache directory for the repo source contains subdirectories for | |
| 50 * each separate repository URL that's used on the system. Each of these | |
| 51 * subdirectories then contains a subdirectory for each package installed from | |
| 52 * that repository. | |
| 53 */ | |
| 54 String systemCacheDirectory(PackageId id, String parent) { | |
| 55 var parsed = _parseDescription(id.description); | |
| 56 var url = parsed.last.replaceAll(new RegExp(@"^https?://"), ""); | |
| 57 var urlDir = replace(url, new RegExp(@'[<>:"\\/|?*%]'), (match) { | |
| 58 return '%${match[0].charCodeAt(0)}'; | |
| 59 }); | |
| 60 return join(parent, urlDir, "${parsed.first}-${id.version}"); | |
| 61 } | |
| 62 | |
| 63 String packageName(PackageId id) => _parseDescription(id.description).first; | |
| 64 | |
| 65 /** | |
| 66 * Ensures that [description] is a valid repo description. | |
| 67 * | |
| 68 * There are two valid formats. A plain string refers to a package with the | |
| 69 * given name from the default repository, while a map with keys "name" and | |
| 70 * "url" refers to a package with the given name from the repo at the given | |
| 71 * URL. | |
| 72 */ | |
| 73 void validateDescription(description) { | |
| 74 _parseDescription(description); | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * Parses the description blob for a package. | |
| 79 * | |
| 80 * If the package parses correctly, this returns a (name, url) pair. If not, | |
| 81 * this throws a descriptive FormatException. | |
| 82 */ | |
| 83 Pair<String, String> _parseDescription(description) { | |
| 84 if (description is String) { | |
| 85 return new Pair<String, String>(description, defaultUrl); | |
| 86 } | |
| 87 | |
| 88 if (description is! Map) { | |
| 89 throw new FormatException( | |
| 90 "The description must be a package name or map."); | |
| 91 } | |
| 92 | |
| 93 if (!description.containsKey("name")) { | |
| 94 throw new FormatException( | |
| 95 "The description map must contain a 'name' key."); | |
| 96 } | |
| 97 | |
| 98 var name = description["name"]; | |
| 99 if (name is! String) { | |
| 100 throw new FormatException("The 'name' key must have a string value."); | |
| 101 } | |
| 102 | |
| 103 var url = description.containsKey("url") ? description["url"] : defaultUrl; | |
| 104 return new Pair<String, String>(name, url); | |
| 105 } | |
| 106 } | |
| OLD | NEW |