Line data Source code
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 0 : PackageResolver get asAsync => new AsyncPackageResolver(this);
22 :
23 0 : String get processArgument => "--package-root=$packageRoot";
24 :
25 : PackageRootResolver(packageRoot)
26 0 : : packageRoot = ensureTrailingSlash(asUri(packageRoot, "packageRoot"));
27 :
28 : Uri resolveUri(packageUri) {
29 0 : packageUri = asPackageUri(packageUri, "packageUri");
30 :
31 : // Following [Isolate.resolvePackageUri], "package:foo" resolves to null.
32 0 : if (packageUri.pathSegments.length == 1) return null;
33 0 : return packageRoot.resolve(packageUri.path);
34 : }
35 :
36 : Uri urlFor(String package, [String path]) {
37 0 : var result = packageRoot.resolve("$package/");
38 0 : return path == null ? result : result.resolve(path);
39 : }
40 :
41 : Uri packageUriFor(url) {
42 0 : var packageRootString = packageRoot.toString();
43 0 : url = asUri(url, "url").toString();
44 0 : if (!p.url.isWithin(packageRootString, url)) return null;
45 :
46 0 : var relative = p.url.relative(url, from: packageRootString);
47 0 : if (!relative.contains("/")) relative += "/";
48 0 : return Uri.parse("package:$relative");
49 : }
50 :
51 : String packagePath(String package) {
52 0 : if (packageRoot.scheme != 'file') return null;
53 :
54 0 : var libLink = p.join(p.fromUri(packageRoot), package);
55 0 : if (!new Link(libLink).existsSync()) return null;
56 :
57 0 : return p.dirname(new Link(libLink).resolveSymbolicLinksSync());
58 : }
59 : }
|