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 | 4 |
5 library fasta.uri_translator_impl; | 5 library fasta.uri_translator_impl; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 import 'dart:convert' show JSON; | 8 import 'dart:convert' show JSON; |
9 | 9 |
10 import 'package:front_end/file_system.dart' | 10 import 'package:front_end/file_system.dart' |
11 show FileSystem, FileSystemException; | 11 show FileSystem, FileSystemException; |
| 12 import 'package:front_end/src/fasta/compiler_context.dart' show CompilerContext; |
| 13 import 'package:front_end/src/fasta/fasta_codes.dart'; |
| 14 import 'package:front_end/src/fasta/severity.dart' show Severity; |
12 import 'package:front_end/src/fasta/uri_translator.dart'; | 15 import 'package:front_end/src/fasta/uri_translator.dart'; |
13 import 'package:package_config/packages_file.dart' as packages_file show parse; | 16 import 'package:package_config/packages_file.dart' as packages_file show parse; |
| 17 import 'package:package_config/packages.dart' show Packages; |
| 18 import 'package:package_config/src/packages_impl.dart' show MapPackages; |
14 | 19 |
15 import 'deprecated_problems.dart' show deprecated_inputError; | 20 import 'deprecated_problems.dart' show deprecated_inputError; |
16 | 21 |
17 /// Read the JSON file with defined SDK libraries from the given [uri] in the | 22 /// Read the JSON file with defined SDK libraries from the given [uri] in the |
18 /// [fileSystem] and return the mapping from parsed Dart library names (e.g. | 23 /// [fileSystem] and return the mapping from parsed Dart library names (e.g. |
19 /// `math`) to file URIs. | 24 /// `math`) to file URIs. |
20 Future<Map<String, Uri>> computeDartLibraries( | 25 Future<Map<String, Uri>> computeDartLibraries( |
21 FileSystem fileSystem, Uri uri) async { | 26 FileSystem fileSystem, Uri uri) async { |
22 if (uri == null) return const <String, Uri>{}; | 27 if (uri == null) return const <String, Uri>{}; |
23 Map<String, String> libraries = JSON | 28 Map<String, String> libraries = JSON |
(...skipping 15 matching lines...) Expand all Loading... |
39 class UriTranslatorImpl implements UriTranslator { | 44 class UriTranslatorImpl implements UriTranslator { |
40 /// Mapping from Dart library names (e.g. `math`) to file URIs. | 45 /// Mapping from Dart library names (e.g. `math`) to file URIs. |
41 final Map<String, Uri> dartLibraries; | 46 final Map<String, Uri> dartLibraries; |
42 | 47 |
43 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just | 48 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just |
44 // one patch library (with parts). | 49 // one patch library (with parts). |
45 /// Mapping from Dart library names to the file URIs of patches to apply. | 50 /// Mapping from Dart library names to the file URIs of patches to apply. |
46 final Map<String, List<Uri>> dartPatches; | 51 final Map<String, List<Uri>> dartPatches; |
47 | 52 |
48 /// Mapping from package names (e.g. `angular`) to the file URIs. | 53 /// Mapping from package names (e.g. `angular`) to the file URIs. |
49 final Map<String, Uri> packages; | 54 final Packages packages; |
50 | 55 |
51 UriTranslatorImpl(this.dartLibraries, this.dartPatches, this.packages); | 56 UriTranslatorImpl(this.dartLibraries, this.dartPatches, this.packages); |
52 | 57 |
53 @override | 58 @override |
54 List<Uri> getDartPatches(String libraryName) => dartPatches[libraryName]; | 59 List<Uri> getDartPatches(String libraryName) => dartPatches[libraryName]; |
55 | 60 |
56 @override | 61 @override |
57 bool isPlatformImplementation(Uri uri) { | 62 bool isPlatformImplementation(Uri uri) { |
58 if (uri.scheme != "dart") return false; | 63 if (uri.scheme != "dart") return false; |
59 String path = uri.path; | 64 String path = uri.path; |
60 return dartLibraries[path] == null || path.startsWith("_"); | 65 return dartLibraries[path] == null || path.startsWith("_"); |
61 } | 66 } |
62 | 67 |
63 @override | 68 @override |
| 69 // TODO(sigmund, ahe): consider expanding this API to include an error |
| 70 // callback, so we can provide an error location when one is available. For |
| 71 // example, if the error occurs in an `import`. |
64 Uri translate(Uri uri) { | 72 Uri translate(Uri uri) { |
65 if (uri.scheme == "dart") return _translateDartUri(uri); | 73 if (uri.scheme == "dart") return _translateDartUri(uri); |
66 if (uri.scheme == "package") return _translatePackageUri(uri); | 74 if (uri.scheme == "package") return _translatePackageUri(uri); |
67 return null; | 75 return null; |
68 } | 76 } |
69 | 77 |
70 /// Return the file URI that corresponds to the given `dart` URI, or `null` | 78 /// Return the file URI that corresponds to the given `dart` URI, or `null` |
71 /// if there is no corresponding Dart library registered. | 79 /// if there is no corresponding Dart library registered. |
72 Uri _translateDartUri(Uri uri) { | 80 Uri _translateDartUri(Uri uri) { |
73 if (!uri.isScheme('dart')) return null; | 81 if (!uri.isScheme('dart')) return null; |
74 String path = uri.path; | 82 String path = uri.path; |
75 | 83 |
76 int index = path.indexOf('/'); | 84 int index = path.indexOf('/'); |
77 if (index == -1) return dartLibraries[path]; | 85 if (index == -1) return dartLibraries[path]; |
78 | 86 |
79 String libraryName = path.substring(0, index); | 87 String libraryName = path.substring(0, index); |
80 String relativePath = path.substring(index + 1); | 88 String relativePath = path.substring(index + 1); |
81 Uri libraryFileUri = dartLibraries[libraryName]; | 89 Uri libraryFileUri = dartLibraries[libraryName]; |
82 return libraryFileUri?.resolve(relativePath); | 90 return libraryFileUri?.resolve(relativePath); |
83 } | 91 } |
84 | 92 |
85 /// Return the file URI that corresponds to the given `package` URI, or | 93 /// Return the file URI that corresponds to the given `package` URI, or |
86 /// `null` if the `package` [uri] format is invalid, or there is no | 94 /// `null` if the `package` [uri] format is invalid, or there is no |
87 /// corresponding package registered. | 95 /// corresponding package registered. |
88 Uri _translatePackageUri(Uri uri) { | 96 Uri _translatePackageUri(Uri uri) { |
89 int index = uri.path.indexOf("/"); | 97 try { |
90 if (index == -1) return null; | 98 // TODO(sigmund): once we remove the `parse` API, we can ensure that |
91 String name = uri.path.substring(0, index); | 99 // packages will never be null and get rid of `?` below. |
92 String path = uri.path.substring(index + 1); | 100 return packages?.resolve(uri, notFound: _packageUriNotFound); |
93 Uri root = packages[name]; | 101 } on ArgumentError catch (e) { |
94 if (root == null) return null; | 102 // TODO(sigmund): catch a more precise error when |
95 return root.resolve(path); | 103 // https://github.com/dart-lang/package_config/issues/40 is fixed. |
| 104 CompilerContext.current.reportWithoutLocation( |
| 105 templateInvalidPackageUri.withArguments(uri, '$e'), Severity.error); |
| 106 return null; |
| 107 } |
| 108 } |
| 109 |
| 110 static Uri _packageUriNotFound(Uri uri) { |
| 111 String name = uri.pathSegments.first; |
| 112 CompilerContext.current.reportWithoutLocation( |
| 113 templatePackageNotFound.withArguments(name, uri), Severity.error); |
| 114 // TODO(sigmund, ahe): ensure we only report an error once, |
| 115 // this null result will likely cause another error further down in the |
| 116 // compiler. |
| 117 return null; |
96 } | 118 } |
97 | 119 |
98 static Future<UriTranslator> parse(FileSystem fileSystem, Uri sdk, | 120 static Future<UriTranslator> parse(FileSystem fileSystem, Uri sdk, |
99 {Uri packages}) async { | 121 {Uri packages}) async { |
100 Uri librariesJson = sdk?.resolve("lib/libraries.json"); | 122 Uri librariesJson = sdk?.resolve("lib/libraries.json"); |
101 | 123 |
102 // TODO(ahe): Provide a value for this file. | 124 // TODO(ahe): Provide a value for this file. |
103 Uri patches = null; | 125 Uri patches = null; |
104 | 126 |
105 packages ??= Uri.base.resolve(".packages"); | 127 packages ??= Uri.base.resolve(".packages"); |
106 | 128 |
107 List<int> bytes; | 129 List<int> bytes; |
108 try { | 130 try { |
109 bytes = await fileSystem.entityForUri(packages).readAsBytes(); | 131 bytes = await fileSystem.entityForUri(packages).readAsBytes(); |
110 } on FileSystemException catch (e) { | 132 } on FileSystemException catch (e) { |
111 deprecated_inputError(packages, -1, e.message); | 133 deprecated_inputError(packages, -1, e.message); |
112 } | 134 } |
113 | 135 |
114 Map<String, Uri> parsedPackages; | 136 Packages parsedPackages; |
115 try { | 137 try { |
116 parsedPackages = packages_file.parse(bytes, packages); | 138 parsedPackages = new MapPackages(packages_file.parse(bytes, packages)); |
117 } on FormatException catch (e) { | 139 } on FormatException catch (e) { |
118 return deprecated_inputError(packages, e.offset, e.message); | 140 return deprecated_inputError(packages, e.offset, e.message); |
119 } | 141 } |
120 | 142 |
121 var dartLibraries = await computeDartLibraries(fileSystem, librariesJson); | 143 var dartLibraries = await computeDartLibraries(fileSystem, librariesJson); |
122 return new UriTranslatorImpl(dartLibraries, | 144 return new UriTranslatorImpl(dartLibraries, |
123 await computeDartPatches(fileSystem, patches), parsedPackages); | 145 await computeDartPatches(fileSystem, patches), parsedPackages); |
124 } | 146 } |
125 } | 147 } |
OLD | NEW |