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

Unified Diff: pkg/analyzer/lib/src/generated/source.dart

Issue 1206323005: Package map source factory support. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer/lib/file_system/physical_file_system.dart ('k') | pkg/analyzer/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/generated/source.dart
diff --git a/pkg/analyzer/lib/src/generated/source.dart b/pkg/analyzer/lib/src/generated/source.dart
index 4900cf687fd5f68a6be3a52acc51f9d5c249b6d0..6350e89ce5d36bcea57ce9f69773e3d1c1b486e7 100644
--- a/pkg/analyzer/lib/src/generated/source.dart
+++ b/pkg/analyzer/lib/src/generated/source.dart
@@ -8,11 +8,14 @@
library engine.source;
import 'dart:collection';
+import 'dart:io';
import "dart:math" as math;
import 'package:analyzer/file_system/file_system.dart';
+import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/source/package_map_resolver.dart';
import 'package:analyzer/task/model.dart';
+import 'package:package_config/packages.dart';
import 'package:path/path.dart' as pathos;
import 'engine.dart';
@@ -575,6 +578,12 @@ class SourceFactory {
AnalysisContext context;
/**
+ * URI processor used to find mappings for `package:` URIs found in a `.packages` config
+ * file.
+ */
+ final Packages _packages;
+
+ /**
* The resolvers used to resolve absolute URI's.
*/
final List<UriResolver> _resolvers;
@@ -585,11 +594,10 @@ class SourceFactory {
LocalSourcePredicate _localSourcePredicate = LocalSourcePredicate.NOT_SDK;
/**
- * Initialize a newly created source factory.
- *
- * @param resolvers the resolvers used to resolve absolute URI's
+ * Initialize a newly created source factory with the given absolute URI [resolvers] and
+ * optional [packages] resolution helper.
*/
- SourceFactory(this._resolvers);
+ SourceFactory(this._resolvers, [this._packages = Packages.noPackages]);
/**
* Return the [DartSdk] associated with this [SourceFactory], or `null` if there
@@ -620,6 +628,20 @@ class SourceFactory {
/// A table mapping package names to paths of directories containing
/// the package (or [null] if there is no registered package URI resolver).
Map<String, List<Folder>> get packageMap {
+ // Start by looking in .packages.
+ if (_packages != Packages.noPackages) {
+ Map<String, List<Folder>> packageMap = <String, List<Folder>>{};
+ _packages.asMap().forEach((String name, Uri uri) {
+ if (uri.scheme == 'file' || uri.scheme == '' /* unspecified */) {
+ packageMap[name] = <Folder>[
+ PhysicalResourceProvider.INSTANCE.getFolder(uri.toFilePath())
+ ];
+ }
+ });
+ return packageMap;
+ }
+
+ // Default to the PackageMapUriResolver.
PackageMapUriResolver resolver = _resolvers.firstWhere(
(r) => r is PackageMapUriResolver, orElse: () => null);
return resolver != null ? resolver.packageMap : null;
@@ -727,13 +749,41 @@ class SourceFactory {
* @return the absolute URI representing the given source
*/
Uri restoreUri(Source source) {
+ // First see if a resolver can restore the URI.
for (UriResolver resolver in _resolvers) {
Uri uri = resolver.restoreAbsolute(source);
if (uri != null) {
+ // Now see if there's a package mapping.
+ Uri packageMappedUri = _getPackageMapping(uri);
+ if (packageMappedUri != null) {
+ return packageMappedUri;
+ }
+ // Fall back to the resolver's computed URI.
return uri;
}
}
- return null;
+
+ // In case no resolver can restore the URI, default to a .packages look-up.
+ return _getPackageMapping(source.uri);
+ }
+
+ Uri _getPackageMapping(Uri uri) {
+ if (uri.scheme != 'file') {
Brian Wilkerson 2015/06/30 23:29:38 I'm a little concerned that longer term we'll need
pquitslund 2015/06/30 23:46:05 Right. Added TODO.
+ return null;
+ }
+
+ String sourcePath = uri.toFilePath();
+ Uri packageUri;
+ _packages.asMap().forEach((String name, Uri uri) {
+ if (packageUri == null) {
+ String uriPath = uri.toFilePath();
+ if (sourcePath.startsWith(uriPath)) {
+ packageUri = Uri
+ .parse('package:$name/${sourcePath.substring(uriPath.length)}');
+ }
+ }
+ });
+ return packageUri;
}
/**
@@ -748,6 +798,14 @@ class SourceFactory {
* against the source object's URI
*/
Source _internalResolveUri(Source containingSource, Uri containedUri) {
+ // Check .packages first.
+ if (containedUri.scheme == 'package') {
+ Uri packageUri = _packages.resolve(containedUri);
+ //TODO(pquitslund): package_config needs to be updated to set schemes for file URIs.
+ if (packageUri != null && packageUri.scheme == '') {
+ containedUri = new Uri.file(packageUri.toFilePath());
+ }
+ }
if (!containedUri.isAbsolute) {
if (containingSource == null) {
throw new AnalysisException(
« no previous file with comments | « pkg/analyzer/lib/file_system/physical_file_system.dart ('k') | pkg/analyzer/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698