| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library fasta.translate_uri; |
| 6 |
| 7 import 'dart:async' show |
| 8 Future; |
| 9 |
| 10 import 'dart:io' show |
| 11 File; |
| 12 |
| 13 import 'package:package_config/packages_file.dart' as packages_file show |
| 14 parse; |
| 15 |
| 16 import 'errors.dart' show |
| 17 internalError; |
| 18 |
| 19 class TranslateUri { |
| 20 final Map<String, Uri> packages; |
| 21 |
| 22 TranslateUri(this.packages); |
| 23 |
| 24 Uri translate(Uri uri) { |
| 25 if (uri.scheme == "dart") return translateDartUri(uri); |
| 26 if (uri.scheme == "package") return translatePackageUri(uri); |
| 27 return null; |
| 28 } |
| 29 |
| 30 Uri translateDartUri(Uri uri) { |
| 31 throw internalError("dart: URIs not implemented yet."); |
| 32 } |
| 33 |
| 34 Uri translatePackageUri(Uri uri) { |
| 35 int index = uri.path.indexOf("/"); |
| 36 if (index == -1) return null; |
| 37 String name = uri.path.substring(0, index); |
| 38 String path = uri.path.substring(index + 1); |
| 39 Uri root = packages[name]; |
| 40 if (root == null) return null; |
| 41 return root.resolve(path); |
| 42 } |
| 43 |
| 44 static Future<TranslateUri> parse([Uri uri]) async { |
| 45 uri ??= Uri.base.resolve(".packages"); |
| 46 File file = new File.fromUri(uri); |
| 47 List<int> bytes = await file.readAsBytes(); |
| 48 Map<String, Uri> packages = packages_file.parse(bytes, uri); |
| 49 return new TranslateUri(packages); |
| 50 } |
| 51 } |
| OLD | NEW |