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/uri_translator.dart'; | 12 import 'package:front_end/src/fasta/uri_translator.dart'; |
13 import 'package:package_config/packages_file.dart' as packages_file show parse; | 13 import 'package:package_config/packages_file.dart' as packages_file show parse; |
14 import 'package:package_config/packages.dart' show Packages; | |
15 import 'package:package_config/src/packages_impl.dart' show MapPackages; | |
14 | 16 |
15 import 'deprecated_problems.dart' show deprecated_inputError; | 17 import 'deprecated_problems.dart' show deprecated_inputError; |
16 | 18 |
17 /// Read the JSON file with defined SDK libraries from the given [uri] in the | 19 /// 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. | 20 /// [fileSystem] and return the mapping from parsed Dart library names (e.g. |
19 /// `math`) to file URIs. | 21 /// `math`) to file URIs. |
20 Future<Map<String, Uri>> computeDartLibraries( | 22 Future<Map<String, Uri>> computeDartLibraries( |
21 FileSystem fileSystem, Uri uri) async { | 23 FileSystem fileSystem, Uri uri) async { |
22 if (uri == null) return const <String, Uri>{}; | 24 if (uri == null) return const <String, Uri>{}; |
23 Map<String, String> libraries = JSON | 25 Map<String, String> libraries = JSON |
(...skipping 15 matching lines...) Expand all Loading... | |
39 class UriTranslatorImpl implements UriTranslator { | 41 class UriTranslatorImpl implements UriTranslator { |
40 /// Mapping from Dart library names (e.g. `math`) to file URIs. | 42 /// Mapping from Dart library names (e.g. `math`) to file URIs. |
41 final Map<String, Uri> dartLibraries; | 43 final Map<String, Uri> dartLibraries; |
42 | 44 |
43 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just | 45 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just |
44 // one patch library (with parts). | 46 // one patch library (with parts). |
45 /// Mapping from Dart library names to the file URIs of patches to apply. | 47 /// Mapping from Dart library names to the file URIs of patches to apply. |
46 final Map<String, List<Uri>> dartPatches; | 48 final Map<String, List<Uri>> dartPatches; |
47 | 49 |
48 /// Mapping from package names (e.g. `angular`) to the file URIs. | 50 /// Mapping from package names (e.g. `angular`) to the file URIs. |
49 final Map<String, Uri> packages; | 51 final Packages packages; |
50 | 52 |
51 UriTranslatorImpl(this.dartLibraries, this.dartPatches, this.packages); | 53 UriTranslatorImpl(this.dartLibraries, this.dartPatches, this.packages); |
52 | 54 |
53 @override | 55 @override |
54 List<Uri> getDartPatches(String libraryName) => dartPatches[libraryName]; | 56 List<Uri> getDartPatches(String libraryName) => dartPatches[libraryName]; |
55 | 57 |
56 @override | 58 @override |
57 bool isPlatformImplementation(Uri uri) { | 59 bool isPlatformImplementation(Uri uri) { |
58 if (uri.scheme != "dart") return false; | 60 if (uri.scheme != "dart") return false; |
59 String path = uri.path; | 61 String path = uri.path; |
(...skipping 19 matching lines...) Expand all Loading... | |
79 String libraryName = path.substring(0, index); | 81 String libraryName = path.substring(0, index); |
80 String relativePath = path.substring(index + 1); | 82 String relativePath = path.substring(index + 1); |
81 Uri libraryFileUri = dartLibraries[libraryName]; | 83 Uri libraryFileUri = dartLibraries[libraryName]; |
82 return libraryFileUri?.resolve(relativePath); | 84 return libraryFileUri?.resolve(relativePath); |
83 } | 85 } |
84 | 86 |
85 /// Return the file URI that corresponds to the given `package` URI, or | 87 /// 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 | 88 /// `null` if the `package` [uri] format is invalid, or there is no |
87 /// corresponding package registered. | 89 /// corresponding package registered. |
88 Uri _translatePackageUri(Uri uri) { | 90 Uri _translatePackageUri(Uri uri) { |
89 int index = uri.path.indexOf("/"); | 91 try { |
90 if (index == -1) return null; | 92 // TODO(sigmund): once we remove the `parse` API, we can ensure that |
91 String name = uri.path.substring(0, index); | 93 // packages will never be null and get rid of `?` below. |
92 String path = uri.path.substring(index + 1); | 94 return packages?.resolve(uri, notFound: (_) => null); |
ahe
2017/07/14 13:44:57
Handle not found by reporting an error?
Siggi Cherem (dart-lang)
2017/07/14 19:54:06
Done.
Note I was preserving the old semantics her
| |
93 Uri root = packages[name]; | 95 } on ArgumentError catch (_) { |
ahe
2017/07/14 13:44:58
Argument error can occur for many reasons. Why is
Siggi Cherem (dart-lang)
2017/07/14 19:54:06
I hear you. As discussed offline, I filed a bug on
| |
94 if (root == null) return null; | 96 // TODO(sigmund): report an error. |
Siggi Cherem (dart-lang)
2017/07/14 04:58:52
(I plan to do so once Peter lands his CL adding co
| |
95 return root.resolve(path); | 97 return null; |
98 } | |
96 } | 99 } |
97 | 100 |
98 static Future<UriTranslator> parse(FileSystem fileSystem, Uri sdk, | 101 static Future<UriTranslator> parse(FileSystem fileSystem, Uri sdk, |
99 {Uri packages}) async { | 102 {Uri packages}) async { |
100 Uri librariesJson = sdk?.resolve("lib/libraries.json"); | 103 Uri librariesJson = sdk?.resolve("lib/libraries.json"); |
101 | 104 |
102 // TODO(ahe): Provide a value for this file. | 105 // TODO(ahe): Provide a value for this file. |
103 Uri patches = null; | 106 Uri patches = null; |
104 | 107 |
105 packages ??= Uri.base.resolve(".packages"); | 108 packages ??= Uri.base.resolve(".packages"); |
106 | 109 |
107 List<int> bytes; | 110 List<int> bytes; |
108 try { | 111 try { |
109 bytes = await fileSystem.entityForUri(packages).readAsBytes(); | 112 bytes = await fileSystem.entityForUri(packages).readAsBytes(); |
110 } on FileSystemException catch (e) { | 113 } on FileSystemException catch (e) { |
111 deprecated_inputError(packages, -1, e.message); | 114 deprecated_inputError(packages, -1, e.message); |
112 } | 115 } |
113 | 116 |
114 Map<String, Uri> parsedPackages; | 117 Packages parsedPackages; |
115 try { | 118 try { |
116 parsedPackages = packages_file.parse(bytes, packages); | 119 parsedPackages = new MapPackages(packages_file.parse(bytes, packages)); |
117 } on FormatException catch (e) { | 120 } on FormatException catch (e) { |
118 return deprecated_inputError(packages, e.offset, e.message); | 121 return deprecated_inputError(packages, e.offset, e.message); |
119 } | 122 } |
120 | 123 |
121 var dartLibraries = await computeDartLibraries(fileSystem, librariesJson); | 124 var dartLibraries = await computeDartLibraries(fileSystem, librariesJson); |
122 return new UriTranslatorImpl(dartLibraries, | 125 return new UriTranslatorImpl(dartLibraries, |
123 await computeDartPatches(fileSystem, patches), parsedPackages); | 126 await computeDartPatches(fileSystem, patches), parsedPackages); |
124 } | 127 } |
125 } | 128 } |
OLD | NEW |