OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import 'dart:async'; |
| 6 import 'dart:isolate'; |
| 7 |
| 8 import 'package:path/path.dart' as p; |
| 9 |
| 10 import 'package_config_resolver.dart'; |
| 11 import 'package_resolver.dart'; |
| 12 import 'package_root_resolver.dart'; |
| 13 import 'sync_package_resolver.dart'; |
| 14 import 'utils.dart'; |
| 15 |
| 16 /// The package resolution strategy used by the current isolate. |
| 17 class CurrentIsolateResolver implements PackageResolver { |
| 18 Future<Map<String, Uri>> get packageConfigMap async { |
| 19 if (_packageConfigMap != null) return _packageConfigMap; |
| 20 |
| 21 var url = await Isolate.packageConfig; |
| 22 if (url == null) return null; |
| 23 |
| 24 return await loadConfigMap(url); |
| 25 } |
| 26 Map<String, Uri> _packageConfigMap; |
| 27 |
| 28 Future<Uri> get packageConfigUri => Isolate.packageConfig; |
| 29 |
| 30 Future<Uri> get packageRoot => Isolate.packageRoot; |
| 31 |
| 32 Future<SyncPackageResolver> get asSync async { |
| 33 var root = await packageRoot; |
| 34 if (root != null) return new PackageRootResolver(root); |
| 35 |
| 36 var map = await packageConfigMap; |
| 37 |
| 38 // It's hard to imagine how there would be no package resolution strategy |
| 39 // for an Isolate that can load the package_resolver package, but it's easy |
| 40 // to handle that case so we do. |
| 41 if (map == null) return SyncPackageResolver.none; |
| 42 |
| 43 return new PackageConfigResolver(map, uri: await packageConfigUri); |
| 44 } |
| 45 |
| 46 Future<String> get processArgument async { |
| 47 var configUri = await packageConfigUri; |
| 48 if (configUri != null) return "--packages=$configUri"; |
| 49 |
| 50 var root = await packageRoot; |
| 51 if (root != null) return "--package-root=$root"; |
| 52 |
| 53 return null; |
| 54 } |
| 55 |
| 56 Future<Uri> resolveUri(packageUri) => |
| 57 Isolate.resolvePackageUri(asPackageUri(packageUri, "packageUri")); |
| 58 |
| 59 Future<Uri> urlFor(String package, [String path]) => |
| 60 Isolate.resolvePackageUri(Uri.parse("package:$package/${path ?? ''}")); |
| 61 |
| 62 Future<Uri> packageUriFor(url) async => (await asSync).packageUriFor(url); |
| 63 |
| 64 Future<String> packagePath(String package) async { |
| 65 var root = await packageRoot; |
| 66 if (root != null) return new PackageRootResolver(root).packagePath(package); |
| 67 |
| 68 return p.dirname(p.fromUri(await urlFor(package))); |
| 69 } |
| 70 } |
OLD | NEW |