| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library hosted_source; | 5 library hosted_source; |
| 6 | 6 |
| 7 import 'dart:io' as io; | 7 import 'dart:io' as io; |
| 8 import 'dart:json'; | 8 import 'dart:json'; |
| 9 import 'dart:uri'; | 9 import 'dart:uri'; |
| 10 | 10 |
| 11 // TODO(nweiz): Make this import better. |
| 12 import '../../pkg/http/lib/http.dart' as http; |
| 11 import 'io.dart'; | 13 import 'io.dart'; |
| 12 import 'package.dart'; | 14 import 'package.dart'; |
| 13 import 'pubspec.dart'; | 15 import 'pubspec.dart'; |
| 14 import 'source.dart'; | 16 import 'source.dart'; |
| 15 import 'source_registry.dart'; | 17 import 'source_registry.dart'; |
| 16 import 'utils.dart'; | 18 import 'utils.dart'; |
| 17 import 'version.dart'; | 19 import 'version.dart'; |
| 18 | 20 |
| 19 /** | 21 /** |
| 20 * A package source that installs packages from a package hosting site that | 22 * A package source that installs packages from a package hosting site that |
| 21 * uses the same API as pub.dartlang.org. | 23 * uses the same API as pub.dartlang.org. |
| 22 */ | 24 */ |
| 23 class HostedSource extends Source { | 25 class HostedSource extends Source { |
| 24 final name = "hosted"; | 26 final name = "hosted"; |
| 25 final shouldCache = true; | 27 final shouldCache = true; |
| 26 | 28 |
| 27 /** | 29 /** |
| 28 * The URL of the default package repository. | 30 * The URL of the default package repository. |
| 29 */ | 31 */ |
| 30 static final defaultUrl = "http://pub.dartlang.org"; | 32 static final defaultUrl = "http://pub.dartlang.org"; |
| 31 | 33 |
| 32 /** | 34 /** |
| 33 * Downloads a list of all versions of a package that are available from the | 35 * Downloads a list of all versions of a package that are available from the |
| 34 * site. | 36 * site. |
| 35 */ | 37 */ |
| 36 Future<List<Version>> getVersions(String name, description) { | 38 Future<List<Version>> getVersions(String name, description) { |
| 37 var parsed = _parseDescription(description); | 39 var parsed = _parseDescription(description); |
| 38 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; | 40 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; |
| 39 | 41 |
| 40 return httpGetString(fullUrl).transform((body) { | 42 return httpClient.read(fullUrl).transform((body) { |
| 41 var doc = JSON.parse(body); | 43 var doc = JSON.parse(body); |
| 42 return doc['versions'].map((version) => new Version.parse(version)); | 44 return doc['versions'].map((version) => new Version.parse(version)); |
| 43 }).transformException((ex) { | 45 }).transformException((ex) { |
| 44 _throwFriendlyError(ex, parsed.first, parsed.last); | 46 _throwFriendlyError(ex, parsed.first, parsed.last); |
| 45 }); | 47 }); |
| 46 } | 48 } |
| 47 | 49 |
| 48 /** | 50 /** |
| 49 * Downloads and parses the pubspec for a specific version of a package that | 51 * Downloads and parses the pubspec for a specific version of a package that |
| 50 * is available from the site. | 52 * is available from the site. |
| 51 */ | 53 */ |
| 52 Future<Pubspec> describe(PackageId id) { | 54 Future<Pubspec> describe(PackageId id) { |
| 53 var parsed = _parseDescription(id.description); | 55 var parsed = _parseDescription(id.description); |
| 54 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" | 56 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" |
| 55 "${id.version}.yaml"; | 57 "${id.version}.yaml"; |
| 56 | 58 |
| 57 return httpGetString(fullUrl).transform((yaml) { | 59 return httpClient.read(fullUrl).transform((yaml) { |
| 58 return new Pubspec.parse(yaml, systemCache.sources); | 60 return new Pubspec.parse(yaml, systemCache.sources); |
| 59 }).transformException((ex) { | 61 }).transformException((ex) { |
| 60 _throwFriendlyError(ex, id, parsed.last); | 62 _throwFriendlyError(ex, id, parsed.last); |
| 61 }); | 63 }); |
| 62 } | 64 } |
| 63 | 65 |
| 64 /** | 66 /** |
| 65 * Downloads a package from the site and unpacks it. | 67 * Downloads a package from the site and unpacks it. |
| 66 */ | 68 */ |
| 67 Future<bool> install(PackageId id, String destPath) { | 69 Future<bool> install(PackageId id, String destPath) { |
| 68 var parsedDescription = _parseDescription(id.description); | 70 var parsedDescription = _parseDescription(id.description); |
| 69 var name = parsedDescription.first; | 71 var name = parsedDescription.first; |
| 70 var url = parsedDescription.last; | 72 var url = parsedDescription.last; |
| 71 | 73 |
| 72 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; | 74 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; |
| 73 | 75 |
| 74 print('Downloading $id...'); | 76 print('Downloading $id...'); |
| 75 | 77 |
| 76 // Download and extract the archive to a temp directory. | 78 // Download and extract the archive to a temp directory. |
| 77 var tempDir; | 79 var tempDir; |
| 78 return Futures.wait([httpGet(fullUrl), | 80 return Futures.wait([ |
| 79 systemCache.createTempDir()]).chain((args) { | 81 httpClient.send(new http.Request("GET", new Uri.fromString(fullUrl))) |
| 82 .transform((response) => response.stream), |
| 83 systemCache.createTempDir() |
| 84 ]).chain((args) { |
| 80 tempDir = args[1]; | 85 tempDir = args[1]; |
| 81 return timeout(extractTarGz(args[0], tempDir), HTTP_TIMEOUT, | 86 return timeout(extractTarGz(args[0], tempDir), HTTP_TIMEOUT, |
| 82 'fetching URL "$fullUrl"'); | 87 'fetching URL "$fullUrl"'); |
| 83 }).chain((_) { | 88 }).chain((_) { |
| 84 // Now that the install has succeeded, move it to the real location in | 89 // Now that the install has succeeded, move it to the real location in |
| 85 // the cache. This ensures that we don't leave half-busted ghost | 90 // the cache. This ensures that we don't leave half-busted ghost |
| 86 // directories in the user's pub cache if an install fails. | 91 // directories in the user's pub cache if an install fails. |
| 87 return renameDir(tempDir, destPath); | 92 return renameDir(tempDir, destPath); |
| 88 }).transform((_) => true); | 93 }).transform((_) => true); |
| 89 } | 94 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 116 * refers to a package with the given name from the host at the given URL. | 121 * refers to a package with the given name from the host at the given URL. |
| 117 */ | 122 */ |
| 118 void validateDescription(description, {bool fromLockFile: false}) { | 123 void validateDescription(description, {bool fromLockFile: false}) { |
| 119 _parseDescription(description); | 124 _parseDescription(description); |
| 120 } | 125 } |
| 121 | 126 |
| 122 /// When an error occurs trying to read something about [package] from [url], | 127 /// When an error occurs trying to read something about [package] from [url], |
| 123 /// this tries to translate into a more user friendly error message. Always | 128 /// this tries to translate into a more user friendly error message. Always |
| 124 /// throws an error, either the original one or a better one. | 129 /// throws an error, either the original one or a better one. |
| 125 void _throwFriendlyError(ex, package, url) { | 130 void _throwFriendlyError(ex, package, url) { |
| 126 if (ex is PubHttpException && ex.statusCode == 404) { | 131 if (ex is PubHttpException && ex.response.statusCode == 404) { |
| 127 throw 'Could not find package "$package" at $url.'; | 132 throw 'Could not find package "$package" at $url.'; |
| 128 } | 133 } |
| 129 | 134 |
| 130 if (ex is TimeoutException) { | 135 if (ex is TimeoutException) { |
| 131 throw 'Timed out trying to find package "$package" at $url.'; | 136 throw 'Timed out trying to find package "$package" at $url.'; |
| 132 } | 137 } |
| 133 | 138 |
| 134 if (ex is io.SocketIOException) { | 139 if (ex is io.SocketIOException) { |
| 135 throw 'Got socket error trying to find package "$package" at $url.\n' | 140 throw 'Got socket error trying to find package "$package" at $url.\n' |
| 136 '${ex.osError}'; | 141 '${ex.osError}'; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 163 | 168 |
| 164 var name = description["name"]; | 169 var name = description["name"]; |
| 165 if (name is! String) { | 170 if (name is! String) { |
| 166 throw new FormatException("The 'name' key must have a string value."); | 171 throw new FormatException("The 'name' key must have a string value."); |
| 167 } | 172 } |
| 168 | 173 |
| 169 var url = description.containsKey("url") ? description["url"] : defaultUrl; | 174 var url = description.containsKey("url") ? description["url"] : defaultUrl; |
| 170 return new Pair<String, String>(name, url); | 175 return new Pair<String, String>(name, url); |
| 171 } | 176 } |
| 172 } | 177 } |
| OLD | NEW |