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

Unified Diff: pkg/front_end/lib/src/base/uri_resolver.dart

Issue 2577503002: Implement URI resolution in the front end. (Closed)
Patch Set: Created 4 years 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/front_end/test/src/base/uri_resolver_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/lib/src/base/uri_resolver.dart
diff --git a/pkg/front_end/lib/src/base/uri_resolver.dart b/pkg/front_end/lib/src/base/uri_resolver.dart
new file mode 100644
index 0000000000000000000000000000000000000000..34b0e1754a863edfa0175ce512251da3b2917d6f
--- /dev/null
+++ b/pkg/front_end/lib/src/base/uri_resolver.dart
@@ -0,0 +1,73 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:path/path.dart' as p;
+
+/// The class `UriResolver` implements the rules for resolving URIs to file
+/// paths.
+class UriResolver {
Brian Wilkerson 2016/12/13 19:13:54 I don't know whether this class needs to support t
Paul Berry 2016/12/13 19:22:34 I don't know either. I've added a TODO for now.
+ /// A map from package name to the file URI of the "lib" directory of the
+ /// corresponding package. This is equivalent to the format returned by
+ /// the "package_config" package's parse() function.
+ final Map<String, Uri> packages;
Brian Wilkerson 2016/12/13 19:13:54 I'm probably missing context here, but I don't bel
Paul Berry 2016/12/13 19:22:34 The plan for how to support the Bazel case is that
+
+ /// A map from SDK library name (e.g. `core` for `dart:core`) to the file URI
+ /// of the defining compilation unit of the SDK library.
+ final Map<String, Uri> sdkLibraries;
+
+ /// The path context which should be used to convert from file URIs to file
+ /// paths.
+ final p.Context pathContext;
+
+ /// The URI scheme used for "package" URIs.
+ static const PACKAGE_SCHEME = 'package';
+
+ /// The URI scheme used for "dart" URIs.
+ static const DART_SCHEME = 'dart';
+
+ /// The URI scheme used for "file" URIs.
+ static const FILE_SCHEME = 'file';
+
+ UriResolver(this.packages, this.sdkLibraries, this.pathContext);
+
+ /// Converts a URI to a file path.
+ ///
+ /// If the given URI is valid, and of a recognized form, returns the file path
+ /// it corresponds to. Otherwise returns `null`. It is not necessary for the
+ /// URI to be absolute (relative URIs will be converted to relative file
+ /// paths).
+ ///
+ /// Note that no I/O is performed; the file path that is returned will be
+ /// independent of whether or not any particular file exists on the file
+ /// system.
+ String resolve(Uri uri) {
Siggi Cherem (dart-lang) 2016/12/13 20:25:57 General question about the design here: how much d
Paul Berry 2016/12/13 21:15:17 Hmm, I like your idea. Taking it a step further,
+ Uri fileUri;
+ if (uri.scheme == FILE_SCHEME) {
+ fileUri = uri;
+ } else {
+ var path = uri.path;
+ var slashIndex = path.indexOf('/');
+ String prefix;
+ String rest;
+ if (slashIndex >= 0) {
+ prefix = path.substring(0, slashIndex);
+ rest = path.substring(slashIndex + 1);
+ } else {
+ prefix = path;
+ rest = '';
+ }
+ Uri libUri;
+ if (uri.scheme == PACKAGE_SCHEME) {
+ if (slashIndex < 0) return null;
+ libUri = packages[prefix];
+ } else if (uri.scheme == DART_SCHEME) {
+ libUri = sdkLibraries[prefix];
+ }
Siggi Cherem (dart-lang) 2016/12/13 20:25:57 maybe add an else to throw for unsupported schemes
Paul Berry 2016/12/13 21:15:17 Actually the behavior I'm aiming for is that unsup
+ if (libUri == null) return null;
+ fileUri = libUri.resolve(rest);
+ if (fileUri.scheme != FILE_SCHEME) return null;
Siggi Cherem (dart-lang) 2016/12/13 20:25:57 I'd also be OK validating that the map is initiali
Paul Berry 2016/12/13 21:15:17 Acknowledged.
+ }
+ return pathContext.fromUri(fileUri);
+ }
+}
« no previous file with comments | « no previous file | pkg/front_end/test/src/base/uri_resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698