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

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

Issue 2393963004: Add BazelPackageUriResolver with resolveAbsolute(). (Closed)
Patch Set: Drop extract prefixes for field names in _BazelPackageUri. Created 4 years, 2 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 | « no previous file | pkg/analyzer/test/generated/bazel_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/generated/bazel.dart
diff --git a/pkg/analyzer/lib/src/generated/bazel.dart b/pkg/analyzer/lib/src/generated/bazel.dart
index 4f4f019966bcb53dc1c4b17536af6c08771bfc6e..b6d810aa5b8744e0d878da9417516bbe71b9f6e2 100644
--- a/pkg/analyzer/lib/src/generated/bazel.dart
+++ b/pkg/analyzer/lib/src/generated/bazel.dart
@@ -4,6 +4,7 @@
library analyzer.src.generated.bazel;
+import 'dart:collection';
import 'dart:core';
import 'package:analyzer/file_system/file_system.dart';
@@ -38,6 +39,42 @@ class BazelFileUriResolver extends ResourceUriResolver {
}
/**
+ * The [UriResolver] that can resolve `package` URIs in [BazelWorkspace].
+ */
+class BazelPackageUriResolver extends UriResolver {
+ final BazelWorkspace workspace;
+
+ /**
+ * The cache of absolute [Uri]s to [Source]s mappings.
+ */
+ final Map<Uri, Source> _sourceCache = new HashMap<Uri, Source>();
+
+ BazelPackageUriResolver(this.workspace);
+
+ @override
+ Source resolveAbsolute(Uri uri, [Uri actualUri]) {
+ return _sourceCache.putIfAbsent(uri, () {
+ _BazelPackageUri bazelUri = _BazelPackageUri.parse(uri);
+ if (bazelUri == null) {
+ return null;
+ }
+ if (bazelUri.package.indexOf('.') == -1) {
+ String path = '${workspace.root}/third_party/dart/'
Paul Berry 2016/10/07 15:30:57 This won't work on Windows because it uses hardcod
scheglov 2016/10/07 16:02:43 Thanks. I tested it now manually with Windows styl
+ '${bazelUri.package}/lib/${bazelUri.path}';
+ File file = workspace.getFile(path);
+ return file?.createSource(uri);
+ } else {
+ String packagePath = bazelUri.package
+ .replaceAll('.', workspace.provider.pathContext.separator);
+ String path = '${workspace.root}/$packagePath/lib/${bazelUri.path}';
Paul Berry 2016/10/07 15:30:57 Similar problem here.
scheglov 2016/10/07 16:02:43 Acknowledged.
+ File file = workspace.findFile(path);
+ return file?.createSource(uri);
+ }
+ });
+ }
+}
+
+/**
* Information about a Bazel workspace.
*/
class BazelWorkspace {
@@ -165,4 +202,74 @@ class BazelWorkspace {
return null;
}
}
+
+ /**
+ * Return the file with the given [absolutePath] in the workspace [root].
+ * The file is returned even if it does not exist. Return `null` if the given
+ * [absolutePath] is not in the workspace [root].
Paul Berry 2016/10/07 15:30:57 Since the caller always provides a path within the
scheglov 2016/10/07 16:02:43 Done.
+ */
+ File getFile(String absolutePath) {
+ if (provider.pathContext.isWithin(root, absolutePath)) {
+ return provider.getFile(absolutePath);
+ }
+ return null;
+ }
+}
+
+/**
+ * A `package` URI that has been parsed according to Bazel rules.
+ *
+ * For example, "package:dart.tools.analyzer/src/foo.dart" will be split
+ * into "dart.tools.analyzer" (the package) and "src/foo.dart" (the suffix).
+ */
+class _BazelPackageUri {
+ /**
+ * The name of the package being imported or exported.
+ *
+ * For example, if the URI is "package:foo.bar/baz.dart" then the package
+ * name is "foo.bar".
+ */
+ final String package;
+
+ /**
+ * The path from the `lib` folder for the package to the file.
+ *
+ * For example, if the URI is "package:foo/bar/baz.dart" then the path is
+ * "bar/baz.dart".
+ */
+ final String path;
+
+ _BazelPackageUri(this.package, this.path);
+
+ @override
+ String toString() => toUri().toString();
+
+ /**
+ * Return the reconstituted `package` URI.
+ */
+ Uri toUri() {
+ return new Uri(scheme: 'package', path: '$package/$path');
+ }
+
+ /**
+ * Parses a Dart `package` URI according to Bazel rules.
+ *
+ * Returns `null` if it's not a valid Bazel `package` URI.
+ */
+ static _BazelPackageUri parse(Uri uri) {
+ if (uri.scheme != 'package') {
+ return null;
+ }
+ String path = uri.path;
+ int slash = path.indexOf('/');
+
+ // If the path either starts with a slash or has no slash, it is invalid.
+ if (slash < 1) {
+ return null;
+ }
+
+ String packageName = path.substring(0, slash);
+ String suffix = path.substring(slash + 1);
+ return new _BazelPackageUri(packageName, suffix);
+ }
}
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/bazel_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698