OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, 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 /// A command-line tool that verifies that deferred libraries split the code as | |
6 /// expected. | |
7 /// This tool checks that the output from dart2js meets a given specification, | |
8 /// given in a YAML file. The format of the YAML file is: | |
9 /// | |
10 /// main: | |
11 /// packages: | |
12 /// - some_package | |
13 /// - other_package | |
14 /// | |
15 /// foo: | |
16 /// packages: | |
17 /// - foo | |
18 /// - bar | |
19 /// | |
20 /// baz: | |
21 /// packages: | |
22 /// - baz | |
23 /// - quux | |
24 /// | |
25 /// The YAML file consists of a list of declarations, one for each deferred | |
26 /// part expected in the output. At least one of these parts must be named | |
27 /// "main"; this is the main part that contains the program entrypoint. Each | |
28 /// top-level part contains a list of package names that are expected to be | |
29 /// contained in that part. Any package that is not explicitly listed is | |
30 /// expected to be in the main part. For instance, in the example YAML above | |
31 /// the part named "baz" is expected to contain the packages "baz" and "quux". | |
32 /// | |
33 /// The names for parts given in the specification YAML file (besides "main") | |
34 /// are arbitrary and just used for reporting when the output does not meet the | |
35 /// specification. | |
36 library dart2js_info.bin.deferred_library_check; | |
37 | |
38 import 'dart:async'; | |
39 import 'dart:convert'; | |
40 import 'dart:io'; | |
41 | |
42 import 'package:dart2js_info/info.dart'; | |
43 import 'package:quiver/collection.dart'; | |
44 import 'package:yaml/yaml.dart'; | |
45 | |
46 Future main(List<String> args) async { | |
47 if (args.length < 2) { | |
48 usage(); | |
49 exit(1); | |
50 } | |
51 var info = await infoFromFile(args[0]); | |
52 var manifest = await manifestFromFile(args[1]); | |
53 | |
54 // For each part in the manifest, record the expected "packages" for that | |
55 // part. | |
56 var packages = <String, String>{}; | |
57 for (var part in manifest.keys) { | |
58 for (var package in manifest[part]['packages']) { | |
59 if (packages.containsKey(package)) { | |
60 print('You cannot specify that package "$package" maps to both parts ' | |
61 '"$part" and "${packages[package]}".'); | |
62 exit(1); | |
63 } | |
64 packages[package] = part; | |
65 } | |
66 } | |
67 | |
68 var guessedPartMapping = new BiMap<String, String>(); | |
69 guessedPartMapping['main'] = 'main'; | |
70 | |
71 bool anyFailed = false; | |
72 | |
73 checkInfo(BasicInfo info) { | |
74 var lib = getLibraryOf(info); | |
75 if (lib != null && isPackageUri(lib.uri)) { | |
76 var packageName = getPackageName(lib.uri); | |
77 var outputUnitName = info.outputUnit.name; | |
78 var expectedPart; | |
79 if (packages.containsKey(packageName)) { | |
80 expectedPart = packages[packageName]; | |
81 } else { | |
82 expectedPart = 'main'; | |
83 } | |
84 var expectedOutputUnit = guessedPartMapping[expectedPart]; | |
85 if (expectedOutputUnit == null) { | |
86 guessedPartMapping[expectedPart] = outputUnitName; | |
87 } else { | |
88 if (expectedOutputUnit != outputUnitName) { | |
89 if (!packages.containsKey(packageName)) { | |
Siggi Cherem (dart-lang)
2015/10/28 19:57:47
might be worth adding a TODO to mention that we do
Harry Terkelsen
2015/10/28 20:49:29
Done.
| |
90 print('"${info.name}" from package "$packageName" was not declared ' | |
91 'to be in an explicit part but was not in the main part'); | |
92 } else { | |
93 var actualPart = guessedPartMapping.inverse[outputUnitName]; | |
94 print('"${info.name}" from package "$packageName" was specified to ' | |
95 'be in part $expectedPart but is in part $actualPart'); | |
96 } | |
97 anyFailed = true; | |
98 } | |
99 } | |
100 } | |
101 } | |
102 | |
103 info.functions.forEach(checkInfo); | |
104 info.fields.forEach(checkInfo); | |
105 if (anyFailed) { | |
106 print('The dart2js output did not meet the specification.'); | |
107 } else { | |
108 print('The dart2js output meets the specification'); | |
109 } | |
110 } | |
111 | |
112 LibraryInfo getLibraryOf(Info info) { | |
113 var current = info; | |
114 while (current is! LibraryInfo) { | |
115 if (current == null) { | |
116 return null; | |
117 } | |
118 current = current.parent; | |
119 } | |
120 return current; | |
121 } | |
122 | |
123 bool isPackageUri(Uri uri) => uri.scheme == 'package'; | |
124 | |
125 String getPackageName(Uri uri) { | |
126 assert(isPackageUri(uri)); | |
127 return uri.pathSegments.first; | |
128 } | |
129 | |
130 Future<AllInfo> infoFromFile(String fileName) async { | |
131 var file = await new File(fileName).readAsString(); | |
132 return new AllInfoJsonCodec().decode(JSON.decode(file)); | |
133 } | |
134 | |
135 Future manifestFromFile(String fileName) async { | |
136 var file = await new File(fileName).readAsString(); | |
137 return loadYaml(file); | |
138 } | |
139 | |
140 void usage() { | |
141 print(''' | |
142 usage: dart2js_info_deferred_library_check dump.info.json manifest.yaml'''); | |
143 } | |
OLD | NEW |