OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
ahe
2017/08/03 11:58:15
Consider renaming this file to libraries_specifica
Siggi Cherem (dart-lang)
2017/08/05 00:41:02
Done.
| |
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) JSON. The JSON representation | |
13 /// follows the following structure (which we write in YAML here for | |
14 /// readability purposes): | |
ahe
2017/08/03 11:58:15
I don't think using YAML is helpful if the format
Paul Berry
2017/08/03 17:50:41
I think I agree with this...it makes my brain hurt
Siggi Cherem (dart-lang)
2017/08/05 00:41:01
Done :)
| |
15 /// | |
16 /// vm: # top-level keys are the target names | |
17 /// libraries: # there must be a 'libraries' entry under a target | |
18 /// async: # key matches the path in `dart:*` imports | |
19 /// | |
20 /// # `path` declares the main library uri, note that the actual path | |
21 /// # is relative to this specification file | |
ahe
2017/08/03 11:58:15
You're saying that `path` is a URI (that's relativ
Paul Berry
2017/08/03 17:50:41
Yeah, since paths use different separators on Wind
Siggi Cherem (dart-lang)
2017/08/05 00:41:01
Ok - I decided to match what we do here with what
| |
22 /// path: async/async.dart | |
23 /// | |
24 /// # `patches` declares all patch files of the target # platform. | |
25 /// # note: some platforms have more than one patch file, so `patches` | |
26 /// # is expected to be a list | |
27 /// patches: | |
28 /// - _internal/js_runtime/lib/async_patch.dart | |
29 /// ... | |
30 /// | |
31 /// environment: # not supported today, but possibly in the future other | |
32 /// # platform-specific configuration can go here. | |
33 /// dart.libraries.io: true | |
34 /// | |
35 /// Note: we currently have several different files that needs to be updated | |
36 /// when changing libraries, sources, and patch files: | |
37 /// * .platform files (for dart2js) | |
38 /// * .gypi files (for vm) | |
39 /// * sdk_library_metadata/lib/libraries.dart (for analyzer, ddc) | |
40 /// | |
41 /// we are in the process of unifying them all under this format (see | |
42 /// https://github.com/dart-lang/sdk/issues/28836), but for now we need to pay | |
43 /// close attention to change them consistently. | |
44 | |
45 // TODO(sigmund): move this file to a shared package. | |
46 import 'dart:convert' show JSON; | |
47 | |
48 import '../fasta/util/relativize.dart'; | |
49 | |
50 /// Contents from a single library specification file. | |
51 /// | |
52 /// Contains information about all libraries on all target platforms defined in | |
53 /// that file. | |
54 class LibrariesSpecification { | |
55 final Map<String, TargetLibrariesSpecification> _targets; | |
56 | |
57 const LibrariesSpecification( | |
58 [this._targets = const <String, TargetLibrariesSpecification>{}]); | |
59 | |
60 /// The [TargetLibrariesSpecification] for [targetName]. | |
ahe
2017/08/03 11:58:15
Isn't this kinda obvious?
Siggi Cherem (dart-lang)
2017/08/05 00:41:01
Rephrased a bit to indicate what happens if target
| |
61 TargetLibrariesSpecification specificationFor(String targetName) => | |
62 _targets[targetName]; | |
63 | |
64 /// Parse the given [json] as a library specification, resolving any relative | |
65 /// paths from [fileUri]. | |
66 static LibrariesSpecification parse(Uri fileUri, String json) { | |
ahe
2017/08/03 11:58:15
Consider renaming fileUri to base or baseUri.
Siggi Cherem (dart-lang)
2017/08/05 00:41:01
Done.
| |
67 if (json == null) return const LibrariesSpecification(); | |
68 Map jsonData = JSON.decode(json); | |
ahe
2017/08/03 11:58:15
May throw a format exception.
ahe
2017/08/03 11:58:15
jsonData may not be a map.
Paul Berry
2017/08/03 17:50:41
Agreed, and given that we're still straddling the
Paul Berry
2017/08/03 17:50:41
I think it would be ok to address this possibility
Siggi Cherem (dart-lang)
2017/08/05 00:41:02
Done. I added more general error handling here and
| |
69 var targets = <String, TargetLibrariesSpecification>{}; | |
70 jsonData.forEach((String targetName, targetData) { | |
ahe
2017/08/03 11:58:15
targetData here.
Siggi Cherem (dart-lang)
2017/08/05 00:41:02
Done
| |
71 Map<String, LibraryInfo> libraries = <String, LibraryInfo>{}; | |
72 Map targetData = jsonData[targetName]; | |
ahe
2017/08/03 11:58:15
targetData may not be a map.
ahe
2017/08/03 11:58:15
And here.
Siggi Cherem (dart-lang)
2017/08/05 00:41:01
Done.
| |
73 if (targetData != null) { | |
ahe
2017/08/03 11:58:15
Consider:
if (targetData is Map) {
...
} else {
Siggi Cherem (dart-lang)
2017/08/05 00:41:01
Done.
| |
74 Map librariesData = targetData["libraries"]; | |
ahe
2017/08/03 11:58:15
librariesData may not be a map.
Siggi Cherem (dart-lang)
2017/08/05 00:41:02
Done.
| |
75 Uri resolve(String path) => fileUri.resolveUri(new Uri.file(path)); | |
ahe
2017/08/03 11:58:15
Why is it a filename, and not a URI? I'm pretty su
Paul Berry
2017/08/03 17:50:41
Agreed. And considering the security concern I br
Siggi Cherem (dart-lang)
2017/08/05 00:41:02
Done. this is now consistently handled as a Uri re
| |
76 librariesData.forEach((String name, Map data) { | |
77 var uri = resolve(data['path']); | |
78 var patches = data['patches']?.map(resolve)?.toList() ?? const []; | |
79 libraries[name] = new LibraryInfo(name, uri, patches); | |
80 }); | |
81 } | |
82 targets[targetName] = | |
83 new TargetLibrariesSpecification(targetName, libraries); | |
84 }); | |
85 return new LibrariesSpecification(targets); | |
86 } | |
87 | |
88 /// Serialize this specification to json. | |
89 /// | |
90 /// If possible serializes paths relative to [outputUri]. | |
91 String toJsonString(Uri outputUri) => JSON.encode(toJsonMap(outputUri)); | |
92 | |
93 Map toJsonMap(Uri outputUri) { | |
94 var result = {}; | |
95 var dir = outputUri.resolve('.'); | |
96 String pathFor(Uri uri) => relativizeUri(uri, base: dir); | |
97 _targets.forEach((targetName, target) { | |
98 var libraries = {}; | |
99 target._libraries.forEach((name, lib) { | |
100 libraries[name] = { | |
101 'path': pathFor(lib.uri), | |
ahe
2017/08/03 11:58:15
This is written as a URI, not a file name.
Siggi Cherem (dart-lang)
2017/08/05 00:41:02
Done.
| |
102 'patches': lib.patches.map(pathFor).toList(), | |
103 }; | |
104 }); | |
105 result[targetName] = {'libraries': libraries}; | |
106 }); | |
107 return result; | |
108 } | |
109 } | |
110 | |
111 /// Specifies information about all libraries supported by a given target. | |
112 class TargetLibrariesSpecification { | |
113 /// Name of the target platform. | |
114 final String targetName; | |
115 | |
116 final Map<String, LibraryInfo> _libraries; | |
117 | |
118 const TargetLibrariesSpecification(this.targetName, | |
119 [this._libraries = const <String, LibraryInfo>{}]); | |
120 | |
121 /// Details about a library whose import is `dart:$name`. | |
122 LibraryInfo libraryInfoFor(String name) => _libraries[name]; | |
123 } | |
124 | |
125 /// Information about a `dart:` library in a specific target platform. | |
126 class LibraryInfo { | |
127 /// The name of the library, which is the path developers use to import this | |
128 /// library (as `dart:$name`). | |
129 final String name; | |
130 | |
131 /// The file defining the main implementation of the library. | |
132 final Uri uri; | |
133 | |
134 /// Patch files used for this library in the target platform, if any. | |
135 final List<Uri> patches; | |
136 | |
137 const LibraryInfo(this.name, this.uri, this.patches); | |
138 } | |
OLD | NEW |