Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: pkg/front_end/lib/src/fasta/translate_uri.dart

Issue 2865843002: Use FileSystem to read files in SourceLoader and TranslateUri. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 'package:package_config/packages_file.dart' as packages_file show parse; 10 import 'package:package_config/packages_file.dart' as packages_file show parse;
10 11
11 import 'errors.dart' show inputError; 12 import 'errors.dart' show inputError;
12 13
13 import 'io.dart' show readBytesFromFile;
14
15 class TranslateUri { 14 class TranslateUri {
16 final Map<String, Uri> packages; 15 final Map<String, Uri> packages;
17 final Map<String, Uri> dartLibraries; 16 final Map<String, Uri> dartLibraries;
18 17
19 TranslateUri(this.packages, this.dartLibraries); 18 TranslateUri(this.packages, this.dartLibraries);
20 19
21 Uri translate(Uri uri) { 20 Uri translate(Uri uri) {
22 if (uri.scheme == "dart") return translateDartUri(uri); 21 if (uri.scheme == "dart") return translateDartUri(uri);
23 if (uri.scheme == "package") return translatePackageUri(uri); 22 if (uri.scheme == "package") return translatePackageUri(uri);
24 return null; 23 return null;
25 } 24 }
26 25
27 Uri translateDartUri(Uri uri) => dartLibraries[uri.path]; 26 Uri translateDartUri(Uri uri) => dartLibraries[uri.path];
28 27
29 Uri translatePackageUri(Uri uri) { 28 Uri translatePackageUri(Uri uri) {
30 int index = uri.path.indexOf("/"); 29 int index = uri.path.indexOf("/");
31 if (index == -1) return null; 30 if (index == -1) return null;
32 String name = uri.path.substring(0, index); 31 String name = uri.path.substring(0, index);
33 String path = uri.path.substring(index + 1); 32 String path = uri.path.substring(index + 1);
34 Uri root = packages[name]; 33 Uri root = packages[name];
35 if (root == null) return null; 34 if (root == null) return null;
36 return root.resolve(path); 35 return root.resolve(path);
37 } 36 }
38 37
39 static Future<TranslateUri> parse(Uri sdk, [Uri uri]) async { 38 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk,
39 [Uri uri]) async {
40 // This list below is generated with [bin/generate_dart_libraries.dart] and 40 // This list below is generated with [bin/generate_dart_libraries.dart] and
41 // additional entries for _builtin, _vmservice, profiler, and vmservice_io. 41 // additional entries for _builtin, _vmservice, profiler, and vmservice_io.
42 // 42 //
43 // TODO(ahe): This is only used with the option --compile-sdk, and 43 // TODO(ahe): This is only used with the option --compile-sdk, and
44 // currently doesn't work outside the SDK source tree. 44 // currently doesn't work outside the SDK source tree.
45 Map<String, Uri> dartLibraries = <String, Uri>{}; 45 Map<String, Uri> dartLibraries = <String, Uri>{};
46 if (sdk != null) { 46 if (sdk != null) {
47 dartLibraries = <String, Uri>{ 47 dartLibraries = <String, Uri>{
48 "_async_await_error_codes": sdk.resolve( 48 "_async_await_error_codes": sdk.resolve(
49 "lib/_internal/js_runtime/lib/shared/async_await_error_codes.dart"), 49 "lib/_internal/js_runtime/lib/shared/async_await_error_codes.dart"),
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 "svg": sdk.resolve("lib/svg/dartium/svg_dartium.dart"), 90 "svg": sdk.resolve("lib/svg/dartium/svg_dartium.dart"),
91 "typed_data": sdk.resolve("lib/typed_data/typed_data.dart"), 91 "typed_data": sdk.resolve("lib/typed_data/typed_data.dart"),
92 "vmservice_io": sdk.resolve("lib/vmservice_io/vmservice_io.dart"), 92 "vmservice_io": sdk.resolve("lib/vmservice_io/vmservice_io.dart"),
93 "web_audio": 93 "web_audio":
94 sdk.resolve("lib/web_audio/dartium/web_audio_dartium.dart"), 94 sdk.resolve("lib/web_audio/dartium/web_audio_dartium.dart"),
95 "web_gl": sdk.resolve("lib/web_gl/dartium/web_gl_dartium.dart"), 95 "web_gl": sdk.resolve("lib/web_gl/dartium/web_gl_dartium.dart"),
96 "web_sql": sdk.resolve("lib/web_sql/dartium/web_sql_dartium.dart"), 96 "web_sql": sdk.resolve("lib/web_sql/dartium/web_sql_dartium.dart"),
97 }; 97 };
98 } 98 }
99 uri ??= Uri.base.resolve(".packages"); 99 uri ??= Uri.base.resolve(".packages");
100 List<int> bytes = 100 List<int> bytes = await fileSystem.entityForUri(uri).readAsBytes();
101 await readBytesFromFile(uri, ensureZeroTermination: false);
102 Map<String, Uri> packages = const <String, Uri>{}; 101 Map<String, Uri> packages = const <String, Uri>{};
103 try { 102 try {
104 packages = packages_file.parse(bytes, uri); 103 packages = packages_file.parse(bytes, uri);
105 } on FormatException catch (e) { 104 } on FormatException catch (e) {
106 return inputError(uri, e.offset, e.message); 105 return inputError(uri, e.offset, e.message);
107 } 106 }
108 return new TranslateUri(packages, dartLibraries); 107 return new TranslateUri(packages, dartLibraries);
109 } 108 }
110 } 109 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698