Chromium Code Reviews| 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 | |
| 9 import 'dart:convert' show JSON; | 8 import 'dart:convert' show JSON; |
| 10 | 9 |
| 11 import 'package:front_end/file_system.dart' | 10 import 'package:front_end/file_system.dart' |
| 12 show FileSystem, FileSystemException; | 11 show FileSystem, FileSystemException; |
| 13 | |
| 14 import 'package:package_config/packages_file.dart' as packages_file show parse; | 12 import 'package:package_config/packages_file.dart' as packages_file show parse; |
| 15 | 13 |
| 16 import 'deprecated_problems.dart' show deprecated_inputError; | 14 import 'deprecated_problems.dart' show deprecated_inputError; |
| 17 | 15 |
| 16 /// Instances of [TranslateUri] translate absolute `dart` and `package` URIs | |
| 17 /// into corresponding `file` URIs. | |
|
Siggi Cherem (dart-lang)
2017/07/11 21:56:02
Just looking at some other CL made me think that I
scheglov
2017/07/11 23:28:31
I replaced `file` with just "file" to underscore t
| |
| 18 class TranslateUri { | 18 class TranslateUri { |
| 19 final Map<String, Uri> packages; | 19 /// Mapping from Dart library names (e.g. `math`) to `file` URIs. |
| 20 final Map<String, Uri> dartLibraries; | 20 final Map<String, Uri> dartLibraries; |
| 21 | 21 |
| 22 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just | 22 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just |
| 23 // one patch library (with parts). | 23 // one patch library (with parts). |
| 24 final Map<String, List<Uri>> patches; | 24 /// Mapping from Dart library names to the `file` URIs of patches to apply. |
| 25 final Map<String, List<Uri>> dartPatches; | |
| 25 | 26 |
| 26 TranslateUri(this.packages, this.dartLibraries, this.patches); | 27 /// Mapping from package names (e.g. `path` or `angular`) to the `file` URIs. |
| 28 final Map<String, Uri> packages; | |
| 27 | 29 |
| 30 TranslateUri(this.dartLibraries, this.dartPatches, this.packages); | |
| 31 | |
| 32 /// Returns true if [uri] is private to the platform libraries (and thus not | |
| 33 /// accessible from user code). | |
| 34 bool isPlatformImplementation(Uri uri) { | |
| 35 if (uri.scheme != "dart") return false; | |
| 36 String path = uri.path; | |
| 37 return dartLibraries[path] == null || path.startsWith("_"); | |
| 38 } | |
| 39 | |
| 40 /// If the given [uri] is a `file` or `package` URI, return the corresponding | |
| 41 /// `file` URI (possibly `null` if there is no corresponding `file` URI); | |
| 42 /// otherwise (e.g. when the [uri] is already a `file` URI) return `null`. | |
| 43 /// This is the URIs only transformation, there is no guarantee that the | |
| 44 /// corresponding file exists. | |
| 28 Uri translate(Uri uri) { | 45 Uri translate(Uri uri) { |
| 29 if (uri.scheme == "dart") return translateDartUri(uri); | 46 if (uri.scheme == "dart") return _translateDartUri(uri); |
| 30 if (uri.scheme == "package") return translatePackageUri(uri); | 47 if (uri.scheme == "package") return _translatePackageUri(uri); |
| 31 return null; | 48 return null; |
| 32 } | 49 } |
| 33 | 50 |
| 34 Uri translateDartUri(Uri uri) { | 51 /// Return the file URI that corresponds to the given `dart` URI, or `null` |
| 52 /// if there is no corresponding Dart library registered. | |
| 53 Uri _translateDartUri(Uri uri) { | |
| 35 if (!uri.isScheme('dart')) return null; | 54 if (!uri.isScheme('dart')) return null; |
| 36 String path = uri.path; | 55 String path = uri.path; |
| 37 | 56 |
| 38 int index = path.indexOf('/'); | 57 int index = path.indexOf('/'); |
| 39 if (index == -1) return dartLibraries[path]; | 58 if (index == -1) return dartLibraries[path]; |
| 40 | 59 |
| 41 String libraryName = path.substring(0, index); | 60 String libraryName = path.substring(0, index); |
| 42 String relativePath = path.substring(index + 1); | 61 String relativePath = path.substring(index + 1); |
| 43 Uri libraryFileUri = dartLibraries[libraryName]; | 62 Uri libraryFileUri = dartLibraries[libraryName]; |
| 44 return libraryFileUri?.resolve(relativePath); | 63 return libraryFileUri?.resolve(relativePath); |
| 45 } | 64 } |
| 46 | 65 |
| 47 Uri translatePackageUri(Uri uri) { | 66 /// Return the file URI that corresponds to the given `package` URI, or `null` |
| 67 /// if the `package` [uri] format is invalid, or there is no corresponding | |
| 68 /// package registered. | |
| 69 Uri _translatePackageUri(Uri uri) { | |
| 48 int index = uri.path.indexOf("/"); | 70 int index = uri.path.indexOf("/"); |
| 49 if (index == -1) return null; | 71 if (index == -1) return null; |
| 50 String name = uri.path.substring(0, index); | 72 String name = uri.path.substring(0, index); |
| 51 String path = uri.path.substring(index + 1); | 73 String path = uri.path.substring(index + 1); |
| 52 Uri root = packages[name]; | 74 Uri root = packages[name]; |
| 53 if (root == null) return null; | 75 if (root == null) return null; |
| 54 return root.resolve(path); | 76 return root.resolve(path); |
| 55 } | 77 } |
| 56 | 78 |
| 57 /// Returns true if [uri] is private to the platform libraries (and thus not | |
| 58 /// accessible from user code). | |
| 59 bool isPlatformImplementation(Uri uri) { | |
| 60 if (uri.scheme != "dart") return false; | |
| 61 String path = uri.path; | |
| 62 return dartLibraries[path] == null || path.startsWith("_"); | |
| 63 } | |
| 64 | |
| 65 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk, | 79 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk, |
| 66 {Uri packages}) async { | 80 {Uri packages}) async { |
| 67 Uri librariesJson = sdk?.resolve("lib/libraries.json"); | 81 Uri librariesJson = sdk?.resolve("lib/libraries.json"); |
| 68 | 82 |
| 69 // TODO(ahe): Provide a value for this file. | 83 // TODO(ahe): Provide a value for this file. |
| 70 Uri patches = null; | 84 Uri patches = null; |
| 71 | 85 |
| 72 packages ??= Uri.base.resolve(".packages"); | 86 packages ??= Uri.base.resolve(".packages"); |
| 73 | 87 |
| 74 List<int> bytes; | 88 List<int> bytes; |
| 75 try { | 89 try { |
| 76 bytes = await fileSystem.entityForUri(packages).readAsBytes(); | 90 bytes = await fileSystem.entityForUri(packages).readAsBytes(); |
| 77 } on FileSystemException catch (e) { | 91 } on FileSystemException catch (e) { |
| 78 deprecated_inputError(packages, -1, e.message); | 92 deprecated_inputError(packages, -1, e.message); |
| 79 } | 93 } |
| 80 | 94 |
| 81 Map<String, Uri> parsedPackages; | 95 Map<String, Uri> parsedPackages; |
| 82 try { | 96 try { |
| 83 parsedPackages = packages_file.parse(bytes, packages); | 97 parsedPackages = packages_file.parse(bytes, packages); |
| 84 } on FormatException catch (e) { | 98 } on FormatException catch (e) { |
| 85 return deprecated_inputError(packages, e.offset, e.message); | 99 return deprecated_inputError(packages, e.offset, e.message); |
| 86 } | 100 } |
| 87 return new TranslateUri( | 101 |
| 88 parsedPackages, | 102 var dartLibraries = await computeDartLibraries(fileSystem, librariesJson); |
| 89 await computeLibraries(fileSystem, librariesJson), | 103 return new TranslateUri(dartLibraries, |
| 90 await computePatches(fileSystem, patches)); | 104 await computeDartPatches(fileSystem, patches), parsedPackages); |
| 91 } | 105 } |
| 92 } | 106 } |
| 93 | 107 |
| 94 Future<Map<String, Uri>> computeLibraries( | 108 /// Read the JSON file with defined SDK libraries from the given [uri] in the |
| 109 /// [fileSystem] and return the mapping from parsed Dart library names (e.g. | |
| 110 /// `math`) to `file` URIs. | |
| 111 Future<Map<String, Uri>> computeDartLibraries( | |
| 95 FileSystem fileSystem, Uri uri) async { | 112 FileSystem fileSystem, Uri uri) async { |
| 96 if (uri == null) return const <String, Uri>{}; | 113 if (uri == null) return const <String, Uri>{}; |
| 97 Map<String, String> libraries = JSON | 114 Map<String, String> libraries = JSON |
| 98 .decode(await fileSystem.entityForUri(uri).readAsString())["libraries"]; | 115 .decode(await fileSystem.entityForUri(uri).readAsString())["libraries"]; |
| 99 Map<String, Uri> result = <String, Uri>{}; | 116 Map<String, Uri> result = <String, Uri>{}; |
| 100 libraries.forEach((String name, String path) { | 117 libraries.forEach((String name, String path) { |
| 101 result[name] = uri.resolveUri(new Uri.file(path)); | 118 result[name] = uri.resolveUri(new Uri.file(path)); |
| 102 }); | 119 }); |
| 103 return result; | 120 return result; |
| 104 } | 121 } |
| 105 | 122 |
| 106 Future<Map<String, List<Uri>>> computePatches( | 123 Future<Map<String, List<Uri>>> computeDartPatches( |
| 107 FileSystem fileSystem, Uri uri) async { | 124 FileSystem fileSystem, Uri uri) async { |
| 108 // TODO(ahe): Read patch information. | 125 // TODO(ahe): Read patch information. |
| 109 return const <String, List<Uri>>{}; | 126 return const <String, List<Uri>>{}; |
| 110 } | 127 } |
| OLD | NEW |