| 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"; | 8 import "dart:io"; |
| 9 import "dart:typed_data" show Uint8List; | 9 import "dart:typed_data" show Uint8List; |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 /// | 26 /// |
| 27 /// This function can be used to load an explicitly configured package | 27 /// This function can be used to load an explicitly configured package |
| 28 /// resolution file, for example one specified using a `--packages` | 28 /// resolution file, for example one specified using a `--packages` |
| 29 /// command-line parameter. | 29 /// command-line parameter. |
| 30 Future<Packages> loadPackagesFile(Uri packagesFile, | 30 Future<Packages> loadPackagesFile(Uri packagesFile, |
| 31 {Future<List<int>> loader(Uri uri)}) async { | 31 {Future<List<int>> loader(Uri uri)}) async { |
| 32 Packages parseBytes(List<int> bytes) { | 32 Packages parseBytes(List<int> bytes) { |
| 33 Map<String, Uri> packageMap = pkgfile.parse(bytes, packagesFile); | 33 Map<String, Uri> packageMap = pkgfile.parse(bytes, packagesFile); |
| 34 return new MapPackages(packageMap); | 34 return new MapPackages(packageMap); |
| 35 } | 35 } |
| 36 |
| 36 if (packagesFile.scheme == "file") { | 37 if (packagesFile.scheme == "file") { |
| 37 File file = new File.fromUri(packagesFile); | 38 File file = new File.fromUri(packagesFile); |
| 38 return parseBytes(await file.readAsBytes()); | 39 return parseBytes(await file.readAsBytes()); |
| 39 } | 40 } |
| 40 if (loader == null) { | 41 if (loader == null) { |
| 41 return parseBytes(await _httpGet(packagesFile)); | 42 return parseBytes(await _httpGet(packagesFile)); |
| 42 } | 43 } |
| 43 return parseBytes(await loader(packagesFile)); | 44 return parseBytes(await loader(packagesFile)); |
| 44 } | 45 } |
| 45 | 46 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 if (!dir.existsSync()) { | 119 if (!dir.existsSync()) { |
| 119 throw new ArgumentError.value( | 120 throw new ArgumentError.value( |
| 120 workingDirectory, "workingDirectory", "Directory does not exist."); | 121 workingDirectory, "workingDirectory", "Directory does not exist."); |
| 121 } | 122 } |
| 122 File checkForConfigFile(Directory directory) { | 123 File checkForConfigFile(Directory directory) { |
| 123 assert(directory.isAbsolute); | 124 assert(directory.isAbsolute); |
| 124 var file = new File(path.join(directory.path, ".packages")); | 125 var file = new File(path.join(directory.path, ".packages")); |
| 125 if (file.existsSync()) return file; | 126 if (file.existsSync()) return file; |
| 126 return null; | 127 return null; |
| 127 } | 128 } |
| 129 |
| 128 // Check for $cwd/.packages | 130 // Check for $cwd/.packages |
| 129 var packagesCfgFile = checkForConfigFile(dir); | 131 var packagesCfgFile = checkForConfigFile(dir); |
| 130 if (packagesCfgFile != null) return packagesCfgFile; | 132 if (packagesCfgFile != null) return packagesCfgFile; |
| 131 // Check for $cwd/packages/ | 133 // Check for $cwd/packages/ |
| 132 var packagesDir = new Directory(path.join(dir.path, "packages")); | 134 var packagesDir = new Directory(path.join(dir.path, "packages")); |
| 133 if (packagesDir.existsSync()) return packagesDir; | 135 if (packagesDir.existsSync()) return packagesDir; |
| 134 // Check for cwd(/..)+/.packages | 136 // Check for cwd(/..)+/.packages |
| 135 var parentDir = dir.parent; | 137 var parentDir = dir.parent; |
| 136 while (parentDir.path != dir.path) { | 138 while (parentDir.path != dir.path) { |
| 137 packagesCfgFile = checkForConfigFile(parentDir); | 139 packagesCfgFile = checkForConfigFile(parentDir); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 return new NonFilePackagesDirectoryPackages(packagesDirectoryUri); | 205 return new NonFilePackagesDirectoryPackages(packagesDirectoryUri); |
| 204 } | 206 } |
| 205 } | 207 } |
| 206 | 208 |
| 207 /// Fetches a file over http. | 209 /// Fetches a file over http. |
| 208 Future<List<int>> _httpGet(Uri uri) async { | 210 Future<List<int>> _httpGet(Uri uri) async { |
| 209 HttpClient client = new HttpClient(); | 211 HttpClient client = new HttpClient(); |
| 210 HttpClientRequest request = await client.getUrl(uri); | 212 HttpClientRequest request = await client.getUrl(uri); |
| 211 HttpClientResponse response = await request.close(); | 213 HttpClientResponse response = await request.close(); |
| 212 if (response.statusCode != HttpStatus.OK) { | 214 if (response.statusCode != HttpStatus.OK) { |
| 213 throw 'Failure getting $uri: ' | 215 throw new HttpException('${response.statusCode} ${response.reasonPhrase}', |
| 214 '${response.statusCode} ${response.reasonPhrase}'; | 216 uri: uri); |
| 215 } | 217 } |
| 216 List<List<int>> splitContent = await response.toList(); | 218 List<List<int>> splitContent = await response.toList(); |
| 217 int totalLength = 0; | 219 int totalLength = 0; |
| 218 for (var list in splitContent) { | 220 for (var list in splitContent) { |
| 219 totalLength += list.length; | 221 totalLength += list.length; |
| 220 } | 222 } |
| 221 Uint8List result = new Uint8List(totalLength); | 223 Uint8List result = new Uint8List(totalLength); |
| 222 int offset = 0; | 224 int offset = 0; |
| 223 for (List<int> contentPart in splitContent) { | 225 for (List<int> contentPart in splitContent) { |
| 224 result.setRange(offset, offset + contentPart.length, contentPart); | 226 result.setRange(offset, offset + contentPart.length, contentPart); |
| 225 offset += contentPart.length; | 227 offset += contentPart.length; |
| 226 } | 228 } |
| 227 return result; | 229 return result; |
| 228 } | 230 } |
| OLD | NEW |