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 // TODO(het): add options for how to treat unspecified packages |
| 90 if (!packages.containsKey(packageName)) { |
| 91 print('"${info.name}" from package "$packageName" was not declared ' |
| 92 'to be in an explicit part but was not in the main part'); |
| 93 } else { |
| 94 var actualPart = guessedPartMapping.inverse[outputUnitName]; |
| 95 print('"${info.name}" from package "$packageName" was specified to ' |
| 96 'be in part $expectedPart but is in part $actualPart'); |
| 97 } |
| 98 anyFailed = true; |
| 99 } |
| 100 } |
| 101 } |
| 102 } |
| 103 |
| 104 info.functions.forEach(checkInfo); |
| 105 info.fields.forEach(checkInfo); |
| 106 if (anyFailed) { |
| 107 print('The dart2js output did not meet the specification.'); |
| 108 } else { |
| 109 print('The dart2js output meets the specification'); |
| 110 } |
| 111 } |
| 112 |
| 113 LibraryInfo getLibraryOf(Info info) { |
| 114 var current = info; |
| 115 while (current is! LibraryInfo) { |
| 116 if (current == null) { |
| 117 return null; |
| 118 } |
| 119 current = current.parent; |
| 120 } |
| 121 return current; |
| 122 } |
| 123 |
| 124 bool isPackageUri(Uri uri) => uri.scheme == 'package'; |
| 125 |
| 126 String getPackageName(Uri uri) { |
| 127 assert(isPackageUri(uri)); |
| 128 return uri.pathSegments.first; |
| 129 } |
| 130 |
| 131 Future<AllInfo> infoFromFile(String fileName) async { |
| 132 var file = await new File(fileName).readAsString(); |
| 133 return new AllInfoJsonCodec().decode(JSON.decode(file)); |
| 134 } |
| 135 |
| 136 Future manifestFromFile(String fileName) async { |
| 137 var file = await new File(fileName).readAsString(); |
| 138 return loadYaml(file); |
| 139 } |
| 140 |
| 141 void usage() { |
| 142 print(''' |
| 143 usage: dart2js_info_deferred_library_check dump.info.json manifest.yaml'''); |
| 144 } |
OLD | NEW |