Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 'package:path/path.dart' as p; | |
| 6 | |
| 7 /// The class `UriResolver` implements the rules for resolving URIs to file | |
| 8 /// paths. | |
| 9 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.
| |
| 10 /// A map from package name to the file URI of the "lib" directory of the | |
| 11 /// corresponding package. This is equivalent to the format returned by | |
| 12 /// the "package_config" package's parse() function. | |
| 13 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
| |
| 14 | |
| 15 /// A map from SDK library name (e.g. `core` for `dart:core`) to the file URI | |
| 16 /// of the defining compilation unit of the SDK library. | |
| 17 final Map<String, Uri> sdkLibraries; | |
| 18 | |
| 19 /// The path context which should be used to convert from file URIs to file | |
| 20 /// paths. | |
| 21 final p.Context pathContext; | |
| 22 | |
| 23 /// The URI scheme used for "package" URIs. | |
| 24 static const PACKAGE_SCHEME = 'package'; | |
| 25 | |
| 26 /// The URI scheme used for "dart" URIs. | |
| 27 static const DART_SCHEME = 'dart'; | |
| 28 | |
| 29 /// The URI scheme used for "file" URIs. | |
| 30 static const FILE_SCHEME = 'file'; | |
| 31 | |
| 32 UriResolver(this.packages, this.sdkLibraries, this.pathContext); | |
| 33 | |
| 34 /// Converts a URI to a file path. | |
| 35 /// | |
| 36 /// If the given URI is valid, and of a recognized form, returns the file path | |
| 37 /// it corresponds to. Otherwise returns `null`. It is not necessary for the | |
| 38 /// URI to be absolute (relative URIs will be converted to relative file | |
| 39 /// paths). | |
| 40 /// | |
| 41 /// Note that no I/O is performed; the file path that is returned will be | |
| 42 /// independent of whether or not any particular file exists on the file | |
| 43 /// system. | |
| 44 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,
| |
| 45 Uri fileUri; | |
| 46 if (uri.scheme == FILE_SCHEME) { | |
| 47 fileUri = uri; | |
| 48 } else { | |
| 49 var path = uri.path; | |
| 50 var slashIndex = path.indexOf('/'); | |
| 51 String prefix; | |
| 52 String rest; | |
| 53 if (slashIndex >= 0) { | |
| 54 prefix = path.substring(0, slashIndex); | |
| 55 rest = path.substring(slashIndex + 1); | |
| 56 } else { | |
| 57 prefix = path; | |
| 58 rest = ''; | |
| 59 } | |
| 60 Uri libUri; | |
| 61 if (uri.scheme == PACKAGE_SCHEME) { | |
| 62 if (slashIndex < 0) return null; | |
| 63 libUri = packages[prefix]; | |
| 64 } else if (uri.scheme == DART_SCHEME) { | |
| 65 libUri = sdkLibraries[prefix]; | |
| 66 } | |
|
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
| |
| 67 if (libUri == null) return null; | |
| 68 fileUri = libUri.resolve(rest); | |
| 69 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.
| |
| 70 } | |
| 71 return pathContext.fromUri(fileUri); | |
| 72 } | |
| 73 } | |
| OLD | NEW |