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:io'; |
| 6 |
| 7 import 'package:path/path.dart' as p; |
| 8 |
| 9 import 'async_package_resolver.dart'; |
| 10 import 'package_resolver.dart'; |
| 11 import 'sync_package_resolver.dart'; |
| 12 import 'utils.dart'; |
| 13 |
| 14 /// A package resolution strategy based on a package root URI. |
| 15 class PackageRootResolver implements SyncPackageResolver { |
| 16 final packageConfigMap = null; |
| 17 final packageConfigUri = null; |
| 18 |
| 19 final Uri packageRoot; |
| 20 |
| 21 PackageResolver get asAsync => new AsyncPackageResolver(this); |
| 22 |
| 23 String get processArgument => "--package-root=$packageRoot"; |
| 24 |
| 25 PackageRootResolver(packageRoot) |
| 26 : packageRoot = ensureTrailingSlash(asUri(packageRoot, "packageRoot")); |
| 27 |
| 28 Uri resolveUri(packageUri) { |
| 29 packageUri = asPackageUri(packageUri, "packageUri"); |
| 30 |
| 31 // Following [Isolate.resolvePackageUri], "package:foo" resolves to null. |
| 32 if (packageUri.pathSegments.length == 1) return null; |
| 33 return packageRoot.resolve(packageUri.path); |
| 34 } |
| 35 |
| 36 Uri urlFor(String package, [String path]) { |
| 37 var result = packageRoot.resolve("$package/"); |
| 38 return path == null ? result : result.resolve(path); |
| 39 } |
| 40 |
| 41 Uri packageUriFor(url) { |
| 42 var packageRootString = packageRoot.toString(); |
| 43 url = asUri(url, "url").toString(); |
| 44 if (!p.url.isWithin(packageRootString, url)) return null; |
| 45 |
| 46 var relative = p.url.relative(url, from: packageRootString); |
| 47 if (!relative.contains("/")) relative += "/"; |
| 48 return Uri.parse("package:$relative"); |
| 49 } |
| 50 |
| 51 String packagePath(String package) { |
| 52 if (packageRoot.scheme != 'file') return null; |
| 53 |
| 54 var libLink = p.join(p.fromUri(packageRoot), package); |
| 55 if (!new Link(libLink).existsSync()) return null; |
| 56 |
| 57 return p.dirname(new Link(libLink).resolveSymbolicLinksSync()); |
| 58 } |
| 59 } |
OLD | NEW |