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 | 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 } |
OLD | NEW |