OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 package_config.discovery; | 5 library package_config.discovery; |
6 | 6 |
7 import "dart:async"; | 7 import "dart:async"; |
8 import "dart:io" show Directory, File, FileSystemEntity; | 8 import "dart:io" show Directory, File, FileSystemEntity; |
9 import "package:path/path.dart" as path; | 9 import "package:path/path.dart" as path; |
10 import "package:http/http.dart" as http; | 10 import "package:http/http.dart" as http; |
(...skipping 26 matching lines...) Expand all Loading... |
37 /// It's called to load the `.packages` file for any unsupported scheme. | 37 /// It's called to load the `.packages` file for any unsupported scheme. |
38 /// It must return the *contents* of the file identified by the URI it's given, | 38 /// It must return the *contents* of the file identified by the URI it's given, |
39 /// which should be a UTF-8 encoded `.packages` file, and must return an | 39 /// which should be a UTF-8 encoded `.packages` file, and must return an |
40 /// error future if loading fails for any reason. | 40 /// error future if loading fails for any reason. |
41 Future<Packages> findPackages( | 41 Future<Packages> findPackages( |
42 Uri baseUri, | 42 Uri baseUri, |
43 {Future<List<int>> loader(Uri unsupportedUri)}) { | 43 {Future<List<int>> loader(Uri unsupportedUri)}) { |
44 if (baseUri.scheme == "file") { | 44 if (baseUri.scheme == "file") { |
45 return new Future<Packages>.sync(() => findPackagesFromFile(baseUri)); | 45 return new Future<Packages>.sync(() => findPackagesFromFile(baseUri)); |
46 } else if (baseUri.scheme == "http" || baseUri.scheme == "https") { | 46 } else if (baseUri.scheme == "http" || baseUri.scheme == "https") { |
47 return findPackagesFromNonFile(baseUri, _httpGet); | 47 return findPackagesFromNonFile(baseUri, loader: _httpGet); |
48 } else if (loader != null) { | 48 } else if (loader != null) { |
49 return findPackagesFromNonFile(baseUri, loader); | 49 return findPackagesFromNonFile(baseUri, loader: loader); |
50 } else { | 50 } else { |
51 return new Future<Packages>.value(Packages.noPackages); | 51 return new Future<Packages>.value(Packages.noPackages); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 /// Find the location of the package resolution file/directory for a Dart file. | 55 /// Find the location of the package resolution file/directory for a Dart file. |
56 /// | 56 /// |
57 /// Checks for a `.packages` file in the [workingDirectory]. | 57 /// Checks for a `.packages` file in the [workingDirectory]. |
58 /// If not found, checks for a `packages` directory in the same directory. | 58 /// If not found, checks for a `packages` directory in the same directory. |
59 /// If still not found, starts checking parent directories for | 59 /// If still not found, starts checking parent directories for |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 /// This function first tries to locate a `.packages` file in the [nonFileUri] | 131 /// This function first tries to locate a `.packages` file in the [nonFileUri] |
132 /// directory. If that is not found, it instead assumes a `packages/` directory | 132 /// directory. If that is not found, it instead assumes a `packages/` directory |
133 /// in the same place. | 133 /// in the same place. |
134 /// | 134 /// |
135 /// By default, this function only works for `http:` and `https:` URIs. | 135 /// By default, this function only works for `http:` and `https:` URIs. |
136 /// To support other schemes, a loader must be provided, which is used to | 136 /// To support other schemes, a loader must be provided, which is used to |
137 /// try to load the `.packages` file. The loader should return the contents | 137 /// try to load the `.packages` file. The loader should return the contents |
138 /// of the requestsed `.packages` file as bytes, which will be assumed to be | 138 /// of the requestsed `.packages` file as bytes, which will be assumed to be |
139 /// UTF-8 encoded. | 139 /// UTF-8 encoded. |
140 Future<Packages> findPackagesFromNonFile(Uri nonFileUri, | 140 Future<Packages> findPackagesFromNonFile(Uri nonFileUri, |
141 [Future<List<int>> loader(Uri name)]) { | 141 {Future<List<int>> loader(Uri name)}) { |
142 if (loader == null) loader = _httpGet; | 142 if (loader == null) loader = _httpGet; |
143 Uri packagesFileUri = nonFileUri.resolve(".packages"); | 143 Uri packagesFileUri = nonFileUri.resolve(".packages"); |
144 return loader(packagesFileUri).then((List<int> fileBytes) { | 144 return loader(packagesFileUri).then((List<int> fileBytes) { |
145 Map<String, Uri> map = pkgfile.parse(fileBytes, packagesFileUri); | 145 Map<String, Uri> map = pkgfile.parse(fileBytes, packagesFileUri); |
146 return new MapPackages(map); | 146 return new MapPackages(map); |
147 }, onError: (_) { | 147 }, onError: (_) { |
148 // Didn't manage to load ".packages". Assume a "packages/" directory. | 148 // Didn't manage to load ".packages". Assume a "packages/" directory. |
149 Uri packagesDirectoryUri = nonFileUri.resolve("packages/"); | 149 Uri packagesDirectoryUri = nonFileUri.resolve("packages/"); |
150 return new NonFilePackagesDirectoryPackages(packagesDirectoryUri); | 150 return new NonFilePackagesDirectoryPackages(packagesDirectoryUri); |
151 }); | 151 }); |
152 } | 152 } |
153 | 153 |
154 /// Fetches a file using the http library. | 154 /// Fetches a file using the http library. |
155 Future<List<int>> _httpGet(Uri uri) { | 155 Future<List<int>> _httpGet(Uri uri) { |
156 return http.get(uri).then((http.Response response) { | 156 return http.get(uri).then((http.Response response) { |
157 if (response.statusCode == 200) return response.bodyBytes; | 157 if (response.statusCode == 200) return response.bodyBytes; |
158 throw 0; // The error message isn't being used for anything. | 158 throw 0; // The error message isn't being used for anything. |
159 }); | 159 }); |
160 } | 160 } |
OLD | NEW |