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

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

Issue 2132443003: Add package implementation. (Closed) Base URL: git@github.com:dart-lang/package_resolver@master
Patch Set: Code review changes 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
« no previous file with comments | « lib/src/no_package_resolver.dart ('k') | lib/src/package_resolver.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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:collection';
6
7 import 'package:collection/collection.dart';
8 import 'package:package_config/packages_file.dart' as packages_file;
9 import 'package:path/path.dart' as p;
10
11 import 'async_package_resolver.dart';
12 import 'package_resolver.dart';
13 import 'sync_package_resolver.dart';
14 import 'utils.dart';
15
16 /// A package resolution strategy based on a package config map.
17 class PackageConfigResolver implements SyncPackageResolver {
18 final packageRoot = null;
19
20 final Map<String, Uri> packageConfigMap;
21
22 Uri get packageConfigUri {
23 if (_uri != null) return _uri;
24
25 var buffer = new StringBuffer();
26 packages_file.write(buffer, packageConfigMap, comment: "");
27 _uri = new UriData.fromString(buffer.toString(),
28 parameters: {"charset": "utf-8"})
29 .uri;
30 return _uri;
31 }
32 Uri _uri;
33
34 PackageResolver get asAsync => new AsyncPackageResolver(this);
35
36 String get processArgument => "--packages=$packageConfigUri";
37
38 PackageConfigResolver(Map<String, Uri> packageConfigMap, {uri})
39 : packageConfigMap = _normalizeMap(packageConfigMap),
40 _uri = uri == null ? null : asUri(uri, "uri");
41
42 /// Normalizes the URIs in [map] to ensure that they all end in a trailing
43 /// slash.
44 static Map<String, Uri> _normalizeMap(Map<String, Uri> map) =>
45 new UnmodifiableMapView(
46 mapMap(map, value: (_, uri) => ensureTrailingSlash(uri)));
47
48 Uri resolveUri(packageUri) {
49 var uri = asPackageUri(packageUri, "packageUri");
50
51 var baseUri = packageConfigMap[uri.pathSegments.first];
52 if (baseUri == null) return null;
53
54 var segments = baseUri.pathSegments.toList()
55 ..removeLast(); // Remove the trailing slash.
56
57 // Following [Isolate.resolvePackageUri], "package:foo" resolves to null.
58 if (uri.pathSegments.length == 1) return null;
59
60 segments.addAll(uri.pathSegments.skip(1));
61 return baseUri.replace(pathSegments: segments);
62 }
63
64 Uri urlFor(String package, [String path]) {
65 var baseUri = packageConfigMap[package];
66 if (baseUri == null) return null;
67 if (path == null) return baseUri;
68 return baseUri.resolve(path);
69 }
70
71 Uri packageUriFor(url) {
72 url = asUri(url, "url").toString();
73
74 // Make sure isWithin works if [url] is exactly the base.
75 var nested = p.url.join(url, "_");
76 for (var package in packageConfigMap.keys) {
77 var base = packageConfigMap[package].toString();
78 if (!p.url.isWithin(base, nested)) continue;
79
80 var relative = p.url.relative(url, from: base);
81 if (relative == '.') relative = '';
82 return Uri.parse("package:$package/$relative");
83 }
84
85 return null;
86 }
87
88 String packagePath(String package) {
89 var lib = packageConfigMap[package];
90 if (lib == null) return null;
91 if (lib.scheme != 'file') return null;
92 return p.dirname(p.fromUri(lib));
93 }
94 }
OLDNEW
« no previous file with comments | « lib/src/no_package_resolver.dart ('k') | lib/src/package_resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698