| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2017, 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 specification in-memory representation. | |
| 6 /// | |
| 7 /// Many dart tools are configurable to support different target platforms. For | |
| 8 /// a given target, they need to know what libraries are available and where are | |
| 9 /// the sources and target-specific patches. | |
| 10 /// | |
| 11 /// Here we define APIs to represent this specification and implement | |
| 12 /// serialization to (and deserialization from) a JSON file. | |
| 13 /// | |
| 14 /// Here is an example specification JSON file: | |
| 15 /// | |
| 16 /// { | |
| 17 /// "vm": { | |
| 18 /// "libraries": { | |
| 19 /// "core": { | |
| 20 /// "uri": "async/core.dart", | |
| 21 /// "patches": [ | |
| 22 /// "path/to/core_patch.dart", | |
| 23 /// "path/to/list_patch.dart" | |
| 24 /// ] | |
| 25 /// } | |
| 26 /// "async": { | |
| 27 /// "uri": "async/async.dart", | |
| 28 /// "patches": "path/to/async_patch.dart" | |
| 29 /// } | |
| 30 /// "convert": { | |
| 31 /// "uri": "convert/convert.dart", | |
| 32 /// } | |
| 33 /// } | |
| 34 /// } | |
| 35 /// } | |
| 36 /// | |
| 37 /// The format contains: | |
| 38 /// - a top level entry for each target. Keys are target names (e.g. "vm" | |
| 39 /// above), and values contain the entire specification of a target. | |
| 40 /// | |
| 41 /// - each target specification is a map. Today only one key ("libraries") is | |
| 42 /// supported, but this may be extended in the future to add more | |
| 43 /// information on each target. | |
| 44 /// | |
| 45 /// - The "libraries" entry contains details for how each platform library is | |
| 46 /// implemented. The entry is a map, where keys are the name of the platform | |
| 47 /// library and values contain details for where to find the implementation | |
| 48 /// fo that library. | |
| 49 /// | |
| 50 /// - The name of the library is a single token (e.g. "core") that matches the | |
| 51 /// Uri path used after `dart:` (e.g. "dart:core"). | |
| 52 /// | |
| 53 /// - The "uri" entry on the library information is mandatory. The value is a | |
| 54 /// string URI reference. The "patches" entry is optional and may have as a | |
| 55 /// value a string URI reference or a list of URI references. | |
| 56 /// | |
| 57 /// All URI references can either be a file URI or a relative URI path, | |
| 58 /// which will be resolved relative to the location of the library | |
| 59 /// specification file. | |
| 60 /// | |
| 61 /// | |
| 62 /// Note: we currently have several different files that need to be updated | |
| 63 /// when changing libraries, sources, and patch files: | |
| 64 /// * .platform files (for dart2js) | |
| 65 /// * .gypi files (for vm) | |
| 66 /// * sdk_library_metadata/lib/libraries.dart (for analyzer, ddc) | |
| 67 /// | |
| 68 /// we are in the process of unifying them all under this format (see | |
| 69 /// https://github.com/dart-lang/sdk/issues/28836), but for now we need to pay | |
| 70 /// close attention to change them consistently. | |
| 71 | |
| 72 // TODO(sigmund): move this file to a shared package. | |
| 73 import 'dart:convert' show JSON; | |
| 74 | |
| 75 import '../fasta/util/relativize.dart'; | |
| 76 | |
| 77 /// Contents from a single library specification file. | |
| 78 /// | |
| 79 /// Contains information about all libraries on all target platforms defined in | |
| 80 /// that file. | |
| 81 class LibrariesSpecification { | |
| 82 final Map<String, TargetLibrariesSpecification> _targets; | |
| 83 | |
| 84 const LibrariesSpecification( | |
| 85 [this._targets = const <String, TargetLibrariesSpecification>{}]); | |
| 86 | |
| 87 /// The library specification for a given [target], or null if none is | |
| 88 /// available. | |
| 89 TargetLibrariesSpecification specificationFor(String target) => | |
| 90 _targets[target]; | |
| 91 | |
| 92 /// Parse the given [json] as a library specification, resolving any relative | |
| 93 /// paths from [baseUri]. | |
| 94 /// | |
| 95 /// May throw an exception if [json] is not properly formatted or contains | |
| 96 /// invalid values. | |
| 97 static LibrariesSpecification parse(Uri baseUri, String json) { | |
| 98 if (json == null) return const LibrariesSpecification(); | |
| 99 var jsonData; | |
| 100 try { | |
| 101 var data = JSON.decode(json); | |
| 102 if (data is! Map) { | |
| 103 return _reportError('top-level specification is not a map'); | |
| 104 } | |
| 105 jsonData = data as Map; | |
| 106 } on FormatException catch (e) { | |
| 107 throw new LibrariesSpecificationException(e); | |
| 108 } | |
| 109 var targets = <String, TargetLibrariesSpecification>{}; | |
| 110 jsonData.forEach((String targetName, targetData) { | |
| 111 Map<String, LibraryInfo> libraries = <String, LibraryInfo>{}; | |
| 112 if (targetData is! Map) { | |
| 113 return _reportError( | |
| 114 "target specification for '$targetName' is not a map"); | |
| 115 } | |
| 116 if (!targetData.containsKey("libraries")) { | |
| 117 return _reportError("target specification " | |
| 118 "for '$targetName' doesn't have a libraries entry"); | |
| 119 } | |
| 120 var librariesData = targetData["libraries"]; | |
| 121 if (librariesData is! Map) { | |
| 122 return _reportError("libraries entry for '$targetName' is not a map"); | |
| 123 } | |
| 124 librariesData.forEach((String name, data) { | |
| 125 if (data is! Map) { | |
| 126 return _reportError( | |
| 127 "library data for '$name' in target '$targetName' is not a map"); | |
| 128 } | |
| 129 Uri checkAndResolve(uriString) { | |
| 130 if (uriString is! String) { | |
| 131 return _reportError("uri value '$uriString' is not a string" | |
| 132 "(from library '$name' in target '$targetName')"); | |
| 133 } | |
| 134 var uri = Uri.parse(uriString); | |
| 135 if (uri.scheme != '' && uri.scheme != 'file') { | |
| 136 return _reportError("uri scheme in '$uriString' is not supported."); | |
| 137 } | |
| 138 return baseUri.resolveUri(uri); | |
| 139 } | |
| 140 | |
| 141 var uri = checkAndResolve(data['uri']); | |
| 142 var patches; | |
| 143 if (data['patches'] is List) { | |
| 144 patches = data['patches'].map(baseUri.resolve).toList(); | |
| 145 } else if (data['patches'] is String) { | |
| 146 patches = [checkAndResolve(data['patches'])]; | |
| 147 } else if (data['patches'] == null) { | |
| 148 patches = const []; | |
| 149 } else { | |
| 150 return _reportError( | |
| 151 "patches entry for '$name' is not a list or a string"); | |
| 152 } | |
| 153 libraries[name] = new LibraryInfo(name, uri, patches); | |
| 154 }); | |
| 155 targets[targetName] = | |
| 156 new TargetLibrariesSpecification(targetName, libraries); | |
| 157 }); | |
| 158 return new LibrariesSpecification(targets); | |
| 159 } | |
| 160 | |
| 161 static _reportError(String error) => | |
| 162 throw new LibrariesSpecificationException(error); | |
| 163 | |
| 164 /// Serialize this specification to json. | |
| 165 /// | |
| 166 /// If possible serializes paths relative to [outputUri]. | |
| 167 String toJsonString(Uri outputUri) => JSON.encode(toJsonMap(outputUri)); | |
| 168 | |
| 169 Map toJsonMap(Uri outputUri) { | |
| 170 var result = {}; | |
| 171 var dir = outputUri.resolve('.'); | |
| 172 String pathFor(Uri uri) => relativizeUri(uri, base: dir); | |
| 173 _targets.forEach((targetName, target) { | |
| 174 var libraries = {}; | |
| 175 target._libraries.forEach((name, lib) { | |
| 176 libraries[name] = { | |
| 177 'uri': pathFor(lib.uri), | |
| 178 'patches': lib.patches.map(pathFor).toList(), | |
| 179 }; | |
| 180 }); | |
| 181 result[targetName] = {'libraries': libraries}; | |
| 182 }); | |
| 183 return result; | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 /// Specifies information about all libraries supported by a given target. | |
| 188 class TargetLibrariesSpecification { | |
| 189 /// Name of the target platform. | |
| 190 final String targetName; | |
| 191 | |
| 192 final Map<String, LibraryInfo> _libraries; | |
| 193 | |
| 194 const TargetLibrariesSpecification(this.targetName, | |
| 195 [this._libraries = const <String, LibraryInfo>{}]); | |
| 196 | |
| 197 /// Details about a library whose import is `dart:$name`. | |
| 198 LibraryInfo libraryInfoFor(String name) => _libraries[name]; | |
| 199 } | |
| 200 | |
| 201 /// Information about a `dart:` library in a specific target platform. | |
| 202 class LibraryInfo { | |
| 203 /// The name of the library, which is the path developers use to import this | |
| 204 /// library (as `dart:$name`). | |
| 205 final String name; | |
| 206 | |
| 207 /// The file defining the main implementation of the library. | |
| 208 final Uri uri; | |
| 209 | |
| 210 /// Patch files used for this library in the target platform, if any. | |
| 211 final List<Uri> patches; | |
| 212 | |
| 213 const LibraryInfo(this.name, this.uri, this.patches); | |
| 214 } | |
| 215 | |
| 216 class LibrariesSpecificationException { | |
| 217 Object error; | |
| 218 LibrariesSpecificationException(this.error); | |
| 219 | |
| 220 String toString() => '$error'; | |
| 221 } | |
| OLD | NEW |