| 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:async'; |
| 7 import 'dart:io' as io; | 8 import 'dart:io' as io; |
| 8 import 'dart:json'; | 9 import 'dart:json' as json; |
| 9 import 'dart:uri'; | 10 import 'dart:uri'; |
| 10 | 11 |
| 11 // TODO(nweiz): Make this import better. | 12 // TODO(nweiz): Make this import better. |
| 12 import '../../pkg/http/lib/http.dart' as http; | 13 import '../../pkg/http/lib/http.dart' as http; |
| 13 import 'http.dart'; | 14 import 'http.dart'; |
| 14 import 'io.dart'; | 15 import 'io.dart'; |
| 15 import 'log.dart' as log; | 16 import 'log.dart' as log; |
| 16 import 'package.dart'; | 17 import 'package.dart'; |
| 17 import 'pubspec.dart'; | 18 import 'pubspec.dart'; |
| 18 import 'source.dart'; | 19 import 'source.dart'; |
| 19 import 'source_registry.dart'; | 20 import 'source_registry.dart'; |
| 20 import 'utils.dart'; | 21 import 'utils.dart'; |
| 21 import 'version.dart'; | 22 import 'version.dart'; |
| 22 | 23 |
| 23 /// A package source that installs packages from a package hosting site that | 24 /// A package source that installs packages from a package hosting site that |
| 24 /// uses the same API as pub.dartlang.org. | 25 /// uses the same API as pub.dartlang.org. |
| 25 class HostedSource extends Source { | 26 class HostedSource extends Source { |
| 26 final name = "hosted"; | 27 final name = "hosted"; |
| 27 final shouldCache = true; | 28 final shouldCache = true; |
| 28 | 29 |
| 29 /// The URL of the default package repository. | 30 /// The URL of the default package repository. |
| 30 static final defaultUrl = "http://pub.dartlang.org"; | 31 static final defaultUrl = "http://pub.dartlang.org"; |
| 31 | 32 |
| 32 /// Downloads a list of all versions of a package that are available from the | 33 /// Downloads a list of all versions of a package that are available from the |
| 33 /// site. | 34 /// site. |
| 34 Future<List<Version>> getVersions(String name, description) { | 35 Future<List<Version>> getVersions(String name, description) { |
| 35 var parsed = _parseDescription(description); | 36 var parsed = _parseDescription(description); |
| 36 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; | 37 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; |
| 37 | 38 |
| 38 return httpClient.read(fullUrl).transform((body) { | 39 return httpClient.read(fullUrl).then((body) { |
| 39 var doc = JSON.parse(body); | 40 var doc = json.parse(body); |
| 40 return doc['versions'].map((version) => new Version.parse(version)); | 41 return doc['versions'] |
| 42 .mappedBy((version) => new Version.parse(version)) |
| 43 .toList(); |
| 41 }).transformException((ex) { | 44 }).transformException((ex) { |
| 42 _throwFriendlyError(ex, parsed.first, parsed.last); | 45 _throwFriendlyError(ex, parsed.first, parsed.last); |
| 43 }); | 46 }); |
| 44 } | 47 } |
| 45 | 48 |
| 46 /// Downloads and parses the pubspec for a specific version of a package that | 49 /// Downloads and parses the pubspec for a specific version of a package that |
| 47 /// is available from the site. | 50 /// is available from the site. |
| 48 Future<Pubspec> describe(PackageId id) { | 51 Future<Pubspec> describe(PackageId id) { |
| 49 var parsed = _parseDescription(id.description); | 52 var parsed = _parseDescription(id.description); |
| 50 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" | 53 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" |
| 51 "${id.version}.yaml"; | 54 "${id.version}.yaml"; |
| 52 | 55 |
| 53 return httpClient.read(fullUrl).transform((yaml) { | 56 return httpClient.read(fullUrl).then((yaml) { |
| 54 return new Pubspec.parse(yaml, systemCache.sources); | 57 return new Pubspec.parse(yaml, systemCache.sources); |
| 55 }).transformException((ex) { | 58 }).transformException((ex) { |
| 56 _throwFriendlyError(ex, id, parsed.last); | 59 _throwFriendlyError(ex, id, parsed.last); |
| 57 }); | 60 }); |
| 58 } | 61 } |
| 59 | 62 |
| 60 /// Downloads a package from the site and unpacks it. | 63 /// Downloads a package from the site and unpacks it. |
| 61 Future<bool> install(PackageId id, String destPath) { | 64 Future<bool> install(PackageId id, String destPath) { |
| 62 var parsedDescription = _parseDescription(id.description); | 65 var parsedDescription = _parseDescription(id.description); |
| 63 var name = parsedDescription.first; | 66 var name = parsedDescription.first; |
| 64 var url = parsedDescription.last; | 67 var url = parsedDescription.last; |
| 65 | 68 |
| 66 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; | 69 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; |
| 67 | 70 |
| 68 log.message('Downloading $id...'); | 71 log.message('Downloading $id...'); |
| 69 | 72 |
| 70 // Download and extract the archive to a temp directory. | 73 // Download and extract the archive to a temp directory. |
| 71 var tempDir; | 74 var tempDir; |
| 72 return Futures.wait([ | 75 return Futures.wait([ |
| 73 httpClient.send(new http.Request("GET", new Uri.fromString(fullUrl))) | 76 httpClient.send(new http.Request("GET", new Uri.fromString(fullUrl))) |
| 74 .transform((response) => response.stream), | 77 .then((response) => response.stream), |
| 75 systemCache.createTempDir() | 78 systemCache.createTempDir() |
| 76 ]).chain((args) { | 79 ]).then((args) { |
| 77 tempDir = args[1]; | 80 tempDir = args[1]; |
| 78 return timeout(extractTarGz(args[0], tempDir), HTTP_TIMEOUT, | 81 return timeout(extractTarGz(args[0], tempDir), HTTP_TIMEOUT, |
| 79 'fetching URL "$fullUrl"'); | 82 'fetching URL "$fullUrl"'); |
| 80 }).chain((_) { | 83 }).then((_) { |
| 81 // Now that the install has succeeded, move it to the real location in | 84 // Now that the install has succeeded, move it to the real location in |
| 82 // the cache. This ensures that we don't leave half-busted ghost | 85 // the cache. This ensures that we don't leave half-busted ghost |
| 83 // directories in the user's pub cache if an install fails. | 86 // directories in the user's pub cache if an install fails. |
| 84 return renameDir(tempDir, destPath); | 87 return renameDir(tempDir, destPath); |
| 85 }).transform((_) => true); | 88 }).then((_) => true); |
| 86 } | 89 } |
| 87 | 90 |
| 88 /// The system cache directory for the hosted source contains subdirectories | 91 /// The system cache directory for the hosted source contains subdirectories |
| 89 /// for each separate repository URL that's used on the system. Each of these | 92 /// for each separate repository URL that's used on the system. Each of these |
| 90 /// subdirectories then contains a subdirectory for each package installed | 93 /// subdirectories then contains a subdirectory for each package installed |
| 91 /// from that site. | 94 /// from that site. |
| 92 String systemCacheDirectory(PackageId id) { | 95 String systemCacheDirectory(PackageId id) { |
| 93 var parsed = _parseDescription(id.description); | 96 var parsed = _parseDescription(id.description); |
| 94 var url = parsed.last.replaceAll(new RegExp(r"^https?://"), ""); | 97 var url = parsed.last.replaceAll(new RegExp(r"^https?://"), ""); |
| 95 var urlDir = replace(url, new RegExp(r'[<>:"\\/|?*%]'), (match) { | 98 var urlDir = replace(url, new RegExp(r'[<>:"\\/|?*%]'), (match) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 157 |
| 155 var name = description["name"]; | 158 var name = description["name"]; |
| 156 if (name is! String) { | 159 if (name is! String) { |
| 157 throw new FormatException("The 'name' key must have a string value."); | 160 throw new FormatException("The 'name' key must have a string value."); |
| 158 } | 161 } |
| 159 | 162 |
| 160 var url = description.containsKey("url") ? description["url"] : defaultUrl; | 163 var url = description.containsKey("url") ? description["url"] : defaultUrl; |
| 161 return new Pair<String, String>(name, url); | 164 return new Pair<String, String>(name, url); |
| 162 } | 165 } |
| 163 } | 166 } |
| OLD | NEW |