Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: lib/src/package_root_resolver.dart

Issue 2132443003: Add package implementation. (Closed) Base URL: git@github.com:dart-lang/package_resolver@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 = (() {
27 packageRoot = asUri(packageRoot, "packageRoot");
28 return packageRoot.pathSegments.last.isEmpty
29 ? packageRoot
30 : packageRoot.replace(
31 pathSegments: packageRoot.pathSegments.toList()..add(""));
Bob Nystrom 2016/07/21 18:00:32 This logic pretty much exists elsewhere too. Make
nweiz 2016/07/21 19:42:43 Done.
32 })();
33
34 Uri resolveUri(packageUri) {
35 packageUri = asPackageUri(packageUri, "packageUri");
36
37 // Following [Isolate.resolvePackageUri], "package:foo" resolves to null.
38 if (packageUri.pathSegments.length == 1) return null;
39 return packageRoot.resolve(packageUri.path);
40 }
41
42 Uri urlFor(String package, [String path]) {
43 var result = packageRoot.resolve("$package/");
44 return path == null ? result : result.resolve(path);
45 }
46
47 Uri packageUriFor(url) {
48 var packageRootString = packageRoot.toString();
49 url = asUri(url, "url").toString();
50 if (!p.url.isWithin(packageRootString, url)) return null;
51
52 var relative = p.url.relative(url, from: packageRootString);
53 if (!relative.contains("/")) relative += "/";
54 return Uri.parse("package:$relative");
55 }
56
57 String packagePath(String package) {
58 if (packageRoot.scheme != 'file') return null;
59
60 var libLink = p.join(p.fromUri(packageRoot), package);
61 if (!new Link(libLink).existsSync()) return null;
62
63 return p.dirname(new Link(libLink).resolveSymbolicLinksSync());
64 }
65 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698