Index: pkg/kernel/lib/repository.dart |
diff --git a/pkg/kernel/lib/repository.dart b/pkg/kernel/lib/repository.dart |
index d24748c0ea641ba0ee7b5bfb2f75b53a923d9b4c..7689d90f162bf639101e33c76f5e594955c8ccf7 100644 |
--- a/pkg/kernel/lib/repository.dart |
+++ b/pkg/kernel/lib/repository.dart |
@@ -3,6 +3,10 @@ |
// BSD-style license that can be found in the LICENSE file. |
library kernel.repository; |
+import 'dart:io'; |
+ |
+import 'package:path/path.dart' as pathlib; |
+ |
import 'ast.dart'; |
/// Keeps track of which [Library] objects have been created for a given URI. |
@@ -10,18 +14,61 @@ import 'ast.dart'; |
/// To load different files into the same IR, pass in the same repository |
/// object to the loaders. |
class Repository { |
+ final String workingDirectory; |
final Map<Uri, Library> _uriToLibrary = <Uri, Library>{}; |
final List<Library> libraries = <Library>[]; |
+ Repository({String workingDirectory}) |
+ : this.workingDirectory = workingDirectory ?? Directory.current.path; |
+ |
+ /// Get the [Library] object for the library addresesd by [path]; possibly |
+ /// as an external library. |
+ /// |
+ /// The [path] may be a relative or absolute file path, or a URI string with a |
+ /// `dart:`, `package:` or `file:` scheme. |
+ /// |
+ /// Note that this method does not check if the library can be loaded at all. |
+ Library getLibrary(String path) { |
+ return getLibraryReference(normalizePath(path)); |
+ } |
+ |
+ String normalizeFileExtension(String path) { |
+ if (path.endsWith('.dill')) { |
+ return path.substring(0, path.length - '.dill'.length) + '.dart'; |
+ } else { |
+ return path; |
+ } |
+ } |
+ |
+ /// Get the canonical URI for the library addressed by the given [path]. |
+ /// |
+ /// The [path] may be a relative or absolute file path, or a URI string with a |
+ /// `dart:`, `package:` or `file:` scheme. |
+ Uri normalizePath(String path) { |
+ var uri = Uri.parse(path); |
+ if (!uri.hasScheme) { |
+ if (!pathlib.isAbsolute(path)) { |
+ path = pathlib.join(workingDirectory, path); |
+ } |
+ uri = new Uri(scheme: 'file', path: normalizeFileExtension(path)); |
+ } else if (uri.scheme == 'file') { |
+ var path = normalizeFileExtension(uri.path); |
+ if (!uri.hasAbsolutePath) { |
+ uri = uri.replace(path: pathlib.join(workingDirectory, path)); |
+ } else { |
+ uri = uri.replace(path: path); |
+ } |
+ } |
+ return uri; |
+ } |
+ |
Library getLibraryReference(Uri uri) { |
assert(uri.hasScheme); |
+ assert(uri.scheme != 'file' || uri.hasAbsolutePath); |
return _uriToLibrary.putIfAbsent(uri, () => _buildLibraryReference(uri)); |
} |
Library _buildLibraryReference(Uri uri) { |
- assert(uri.hasScheme); |
- assert(uri.scheme != 'file'); |
- assert(!uri.hasAbsolutePath); |
var library = new Library(uri, isExternal: true); |
libraries.add(library); |
return library; |