| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library kernel.repository; | 4 library kernel.repository; |
| 5 | 5 |
| 6 import 'dart:io'; | |
| 7 | |
| 8 import 'package:path/path.dart' as pathlib; | |
| 9 | |
| 10 import 'ast.dart'; | 6 import 'ast.dart'; |
| 11 | 7 |
| 12 /// Keeps track of which [Library] objects have been created for a given URI. | 8 /// Keeps track of which [Library] objects have been created for a given URI. |
| 13 /// | 9 /// |
| 14 /// To load different files into the same IR, pass in the same repository | 10 /// To load different files into the same IR, pass in the same repository |
| 15 /// object to the loaders. | 11 /// object to the loaders. |
| 16 class Repository { | 12 class Repository { |
| 17 final String workingDirectory; | |
| 18 final Map<Uri, Library> _uriToLibrary = <Uri, Library>{}; | 13 final Map<Uri, Library> _uriToLibrary = <Uri, Library>{}; |
| 19 final List<Library> libraries = <Library>[]; | 14 final List<Library> libraries = <Library>[]; |
| 20 | 15 |
| 21 Repository({String workingDirectory}) | |
| 22 : this.workingDirectory = workingDirectory ?? Directory.current.path; | |
| 23 | |
| 24 /// Get the [Library] object for the library addresesd by [path]; possibly | |
| 25 /// as an external library. | |
| 26 /// | |
| 27 /// The [path] may be a relative or absolute file path, or a URI string with a | |
| 28 /// `dart:`, `package:` or `file:` scheme. | |
| 29 /// | |
| 30 /// Note that this method does not check if the library can be loaded at all. | |
| 31 Library getLibrary(String path) { | |
| 32 return getLibraryReference(normalizePath(path)); | |
| 33 } | |
| 34 | |
| 35 String normalizeFileExtension(String path) { | |
| 36 if (path.endsWith('.dill')) { | |
| 37 return path.substring(0, path.length - '.dill'.length) + '.dart'; | |
| 38 } else { | |
| 39 return path; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 /// Get the canonical URI for the library addressed by the given [path]. | |
| 44 /// | |
| 45 /// The [path] may be a relative or absolute file path, or a URI string with a | |
| 46 /// `dart:`, `package:` or `file:` scheme. | |
| 47 Uri normalizePath(String path) { | |
| 48 var uri = Uri.parse(path); | |
| 49 if (!uri.hasScheme) { | |
| 50 if (!pathlib.isAbsolute(path)) { | |
| 51 path = pathlib.join(workingDirectory, path); | |
| 52 } | |
| 53 uri = new Uri(scheme: 'file', path: normalizeFileExtension(path)); | |
| 54 } else if (uri.scheme == 'file') { | |
| 55 var path = normalizeFileExtension(uri.path); | |
| 56 if (!uri.hasAbsolutePath) { | |
| 57 uri = uri.replace(path: pathlib.join(workingDirectory, path)); | |
| 58 } else { | |
| 59 uri = uri.replace(path: path); | |
| 60 } | |
| 61 } | |
| 62 return uri; | |
| 63 } | |
| 64 | |
| 65 Library getLibraryReference(Uri uri) { | 16 Library getLibraryReference(Uri uri) { |
| 66 assert(uri.hasScheme); | 17 assert(uri.hasScheme); |
| 67 assert(uri.scheme != 'file' || uri.hasAbsolutePath); | |
| 68 return _uriToLibrary.putIfAbsent(uri, () => _buildLibraryReference(uri)); | 18 return _uriToLibrary.putIfAbsent(uri, () => _buildLibraryReference(uri)); |
| 69 } | 19 } |
| 70 | 20 |
| 71 Library _buildLibraryReference(Uri uri) { | 21 Library _buildLibraryReference(Uri uri) { |
| 22 assert(uri.hasScheme); |
| 72 var library = new Library(uri, isExternal: true); | 23 var library = new Library(uri, isExternal: true); |
| 73 libraries.add(library); | 24 libraries.add(library); |
| 74 return library; | 25 return library; |
| 75 } | 26 } |
| 76 } | 27 } |
| OLD | NEW |