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.translate_uri; | 5 library fasta.translate_uri; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'package:front_end/file_system.dart'; | 9 import 'dart:convert' show JSON; |
10 | |
11 import 'package:front_end/file_system.dart' | |
12 show FileSystem, FileSystemException; | |
13 | |
10 import 'package:package_config/packages_file.dart' as packages_file show parse; | 14 import 'package:package_config/packages_file.dart' as packages_file show parse; |
11 | 15 |
12 import 'errors.dart' show inputError; | 16 import 'errors.dart' show inputError; |
13 | 17 |
14 class TranslateUri { | 18 class TranslateUri { |
15 final Map<String, Uri> packages; | 19 final Map<String, Uri> packages; |
16 final Map<String, Uri> dartLibraries; | 20 final Map<String, Uri> dartLibraries; |
21 final Map<String, List<Uri>> patches; | |
Siggi Cherem (dart-lang)
2017/05/22 16:21:15
BTW - I pinged Siva on Friday to see if we can cha
ahe
2017/05/22 16:26:47
Agreed.
| |
17 | 22 |
18 TranslateUri(this.packages, this.dartLibraries); | 23 TranslateUri(this.packages, this.dartLibraries, this.patches); |
19 | 24 |
20 Uri translate(Uri uri) { | 25 Uri translate(Uri uri) { |
21 if (uri.scheme == "dart") return translateDartUri(uri); | 26 if (uri.scheme == "dart") return translateDartUri(uri); |
22 if (uri.scheme == "package") return translatePackageUri(uri); | 27 if (uri.scheme == "package") return translatePackageUri(uri); |
23 return null; | 28 return null; |
24 } | 29 } |
25 | 30 |
26 Uri translateDartUri(Uri uri) { | 31 Uri translateDartUri(Uri uri) => dartLibraries[uri.path]; |
Siggi Cherem (dart-lang)
2017/05/22 16:21:15
Let's document the invariant that you told me abou
ahe
2017/05/22 16:26:47
I'll take a further look at this.
ahe
2017/05/24 16:09:46
Let's go with the analyzer's approach. Personally,
| |
27 if (!uri.isScheme('dart')) return null; | |
28 String path = uri.path; | |
29 | |
30 int index = path.indexOf('/'); | |
31 if (index == -1) return dartLibraries[path]; | |
32 | |
33 String libraryName = path.substring(0, index); | |
34 String relativePath = path.substring(index + 1); | |
35 Uri libraryFileUri = dartLibraries[libraryName]; | |
36 return libraryFileUri?.resolve(relativePath); | |
37 } | |
38 | 32 |
39 Uri translatePackageUri(Uri uri) { | 33 Uri translatePackageUri(Uri uri) { |
40 int index = uri.path.indexOf("/"); | 34 int index = uri.path.indexOf("/"); |
41 if (index == -1) return null; | 35 if (index == -1) return null; |
42 String name = uri.path.substring(0, index); | 36 String name = uri.path.substring(0, index); |
43 String path = uri.path.substring(index + 1); | 37 String path = uri.path.substring(index + 1); |
44 Uri root = packages[name]; | 38 Uri root = packages[name]; |
45 if (root == null) return null; | 39 if (root == null) return null; |
46 return root.resolve(path); | 40 return root.resolve(path); |
47 } | 41 } |
48 | 42 |
49 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk, | 43 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk, |
50 [Uri uri]) async { | 44 {Uri packages}) async { |
51 // This list below is generated with [bin/generate_dart_libraries.dart] and | 45 Uri librariesJson = sdk?.resolve("lib/libraries.json"); |
52 // additional entries for _builtin, _vmservice, profiler, and vmservice_io. | 46 |
53 // | 47 // TODO(ahe): Should be something like |
54 // TODO(ahe): This is only used with the option --compile-sdk, and | 48 // `Uri.base.resolve("runtime/vm/libraries.json")`. |
Siggi Cherem (dart-lang)
2017/05/22 16:21:15
On the design doc, I was pushing for having a sing
ahe
2017/05/22 16:26:47
OK. That's great, in that case, I should delete ru
| |
55 // currently doesn't work outside the SDK source tree. | 49 Uri patches = null; |
56 Map<String, Uri> dartLibraries = <String, Uri>{}; | 50 |
57 if (sdk != null) { | 51 packages ??= Uri.base.resolve(".packages"); |
58 dartLibraries = <String, Uri>{ | |
59 "_async_await_error_codes": sdk.resolve( | |
60 "lib/_internal/js_runtime/lib/shared/async_await_error_codes.dart"), | |
61 "_blink": sdk.resolve("lib/_blink/dartium/_blink_dartium.dart"), | |
62 "_builtin": sdk.resolve("lib/_builtin/_builtin.dart"), | |
63 "_chrome": sdk.resolve("lib/_chrome/dart2js/chrome_dart2js.dart"), | |
64 "_foreign_helper": | |
65 sdk.resolve("lib/_internal/js_runtime/lib/foreign_helper.dart"), | |
66 "_interceptors": | |
67 sdk.resolve("lib/_internal/js_runtime/lib/interceptors.dart"), | |
68 "_internal": sdk.resolve("lib/internal/internal.dart"), | |
69 "_isolate_helper": | |
70 sdk.resolve("lib/_internal/js_runtime/lib/isolate_helper.dart"), | |
71 "_js_embedded_names": sdk | |
72 .resolve("lib/_internal/js_runtime/lib/shared/embedded_names.dart"), | |
73 "_js_helper": | |
74 sdk.resolve("lib/_internal/js_runtime/lib/js_helper.dart"), | |
75 "_js_mirrors": | |
76 sdk.resolve("lib/_internal/js_runtime/lib/js_mirrors.dart"), | |
77 "_js_names": sdk.resolve("lib/_internal/js_runtime/lib/js_names.dart"), | |
78 "_js_primitives": | |
79 sdk.resolve("lib/_internal/js_runtime/lib/js_primitives.dart"), | |
80 "_metadata": sdk.resolve("lib/html/html_common/metadata.dart"), | |
81 "_native_typed_data": | |
82 sdk.resolve("lib/_internal/js_runtime/lib/native_typed_data.dart"), | |
83 "_vmservice": sdk.resolve("lib/vmservice/vmservice.dart"), | |
84 "async": sdk.resolve("lib/async/async.dart"), | |
85 "collection": sdk.resolve("lib/collection/collection.dart"), | |
86 "convert": sdk.resolve("lib/convert/convert.dart"), | |
87 "core": sdk.resolve("lib/core/core.dart"), | |
88 "developer": sdk.resolve("lib/developer/developer.dart"), | |
89 "html": sdk.resolve("lib/html/dart2js/html_dart2js.dart"), | |
90 "html_common": | |
91 sdk.resolve("lib/html/html_common/html_common_dart2js.dart"), | |
92 "indexed_db": | |
93 sdk.resolve("lib/indexed_db/dart2js/indexed_db_dart2js.dart"), | |
94 "io": sdk.resolve("lib/io/io.dart"), | |
95 "isolate": sdk.resolve("lib/isolate/isolate.dart"), | |
96 "js": sdk.resolve("lib/js/dart2js/js_dart2js.dart"), | |
97 "js_util": sdk.resolve("lib/js_util/dart2js/js_util_dart2js.dart"), | |
98 "math": sdk.resolve("lib/math/math.dart"), | |
99 "mirrors": sdk.resolve("lib/mirrors/mirrors.dart"), | |
100 "nativewrappers": sdk.resolve("lib/html/dartium/nativewrappers.dart"), | |
101 "profiler": sdk.resolve("lib/profiler/profiler.dart"), | |
102 "svg": sdk.resolve("lib/svg/dart2js/svg_dart2js.dart"), | |
103 "typed_data": sdk.resolve("lib/typed_data/typed_data.dart"), | |
104 "vmservice_io": sdk.resolve("lib/vmservice_io/vmservice_io.dart"), | |
105 "web_audio": | |
106 sdk.resolve("lib/web_audio/dart2js/web_audio_dart2js.dart"), | |
107 "web_gl": sdk.resolve("lib/web_gl/dart2js/web_gl_dart2js.dart"), | |
108 "web_sql": sdk.resolve("lib/web_sql/dart2js/web_sql_dart2js.dart"), | |
109 }; | |
110 } | |
111 uri ??= Uri.base.resolve(".packages"); | |
112 | 52 |
113 List<int> bytes; | 53 List<int> bytes; |
114 try { | 54 try { |
115 bytes = await fileSystem.entityForUri(uri).readAsBytes(); | 55 bytes = await fileSystem.entityForUri(packages).readAsBytes(); |
116 } on FileSystemException catch (e) { | 56 } on FileSystemException catch (e) { |
117 inputError(uri, -1, e.message); | 57 inputError(packages, -1, e.message); |
118 } | 58 } |
119 | 59 |
120 Map<String, Uri> packages = const <String, Uri>{}; | 60 Map<String, Uri> parsedPackages; |
121 try { | 61 try { |
122 packages = packages_file.parse(bytes, uri); | 62 parsedPackages = packages_file.parse(bytes, packages); |
123 } on FormatException catch (e) { | 63 } on FormatException catch (e) { |
124 return inputError(uri, e.offset, e.message); | 64 return inputError(packages, e.offset, e.message); |
125 } | 65 } |
126 return new TranslateUri(packages, dartLibraries); | 66 return new TranslateUri( |
67 parsedPackages, | |
68 await computeLibraries(fileSystem, librariesJson), | |
69 await computePatches(fileSystem, patches)); | |
127 } | 70 } |
128 } | 71 } |
72 | |
73 Future<Map<String, Uri>> computeLibraries( | |
74 FileSystem fileSystem, Uri uri) async { | |
75 if (uri == null) return const <String, Uri>{}; | |
76 Map<String, String> libraries = JSON | |
77 .decode(await fileSystem.entityForUri(uri).readAsString())["libraries"]; | |
78 Map<String, Uri> result = <String, Uri>{}; | |
79 libraries.forEach((String name, String path) { | |
80 result[name] = uri.resolve(path); | |
81 }); | |
82 return result; | |
83 } | |
84 | |
85 Future<Map<String, List<Uri>>> computePatches( | |
86 FileSystem fileSystem, Uri uri) async { | |
87 if (uri == null) return const <String, List<Uri>>{}; | |
88 Map<String, List<Uri>> result = <String, List<Uri>>{}; | |
Siggi Cherem (dart-lang)
2017/05/22 16:21:15
until we add the support for patching and decide o
ahe
2017/05/22 16:26:47
Sure.
| |
89 JSON | |
90 .decode(await fileSystem.entityForUri(uri).readAsString()) | |
91 .forEach((String name, data) { | |
92 List<String> sources = data["sources"]; | |
93 Uri directory = uri.resolve("${data['directory']}/"); | |
94 List<Uri> resolvedSources = <Uri>[]; | |
95 for (String source in sources) { | |
96 if (source.endsWith(".dart")) { | |
97 resolvedSources.add(directory.resolve(source)); | |
98 } | |
99 } | |
100 if (resolvedSources.isNotEmpty) { | |
101 result[name] = resolvedSources; | |
102 } | |
103 }); | |
104 return result; | |
105 } | |
OLD | NEW |