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

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

Issue 2641833004: Don't crash on missing dart: URI. (Closed)
Patch Set: Created 3 years, 11 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 7 import 'dart:async' show
8 Future; 8 Future;
9 9
10 import 'dart:io' show 10 import 'dart:io' show
11 File; 11 File;
12 12
13 import 'package:package_config/packages_file.dart' as packages_file show 13 import 'package:package_config/packages_file.dart' as packages_file show
14 parse; 14 parse;
15 15
16 import 'errors.dart' show
17 internalError;
18
19 class TranslateUri { 16 class TranslateUri {
20 final Map<String, Uri> packages; 17 final Map<String, Uri> packages;
21 18
22 TranslateUri(this.packages); 19 TranslateUri(this.packages);
23 20
24 Uri translate(Uri uri) { 21 Uri translate(Uri uri) {
25 if (uri.scheme == "dart") return translateDartUri(uri); 22 if (uri.scheme == "dart") return translateDartUri(uri);
26 if (uri.scheme == "package") return translatePackageUri(uri); 23 if (uri.scheme == "package") return translatePackageUri(uri);
27 return null; 24 return null;
28 } 25 }
29 26
30 Uri translateDartUri(Uri uri) { 27 // TODO(ahe): Implement loading platform file on demand. This method must
31 throw internalError("dart: URIs not implemented yet."); 28 // become async for that to happen.
32 } 29 Uri translateDartUri(Uri uri) => null;
33 30
34 Uri translatePackageUri(Uri uri) { 31 Uri translatePackageUri(Uri uri) {
35 int index = uri.path.indexOf("/"); 32 int index = uri.path.indexOf("/");
36 if (index == -1) return null; 33 if (index == -1) return null;
37 String name = uri.path.substring(0, index); 34 String name = uri.path.substring(0, index);
38 String path = uri.path.substring(index + 1); 35 String path = uri.path.substring(index + 1);
39 Uri root = packages[name]; 36 Uri root = packages[name];
40 if (root == null) return null; 37 if (root == null) return null;
41 return root.resolve(path); 38 return root.resolve(path);
42 } 39 }
43 40
44 static Future<TranslateUri> parse([Uri uri]) async { 41 static Future<TranslateUri> parse([Uri uri]) async {
45 uri ??= Uri.base.resolve(".packages"); 42 uri ??= Uri.base.resolve(".packages");
46 File file = new File.fromUri(uri); 43 File file = new File.fromUri(uri);
47 List<int> bytes = await file.readAsBytes(); 44 List<int> bytes = await file.readAsBytes();
48 Map<String, Uri> packages = packages_file.parse(bytes, uri); 45 Map<String, Uri> packages = packages_file.parse(bytes, uri);
49 return new TranslateUri(packages); 46 return new TranslateUri(packages);
50 } 47 }
51 } 48 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698