| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 /// 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, | 5 /// 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: | 6 /// given in a YAML file. The format of the YAML file is: |
| 9 /// | 7 /// |
| 10 /// main: | 8 /// main: |
| 11 /// packages: | 9 /// packages: |
| 12 /// - some_package | 10 /// - some_package |
| 13 /// - other_package | 11 /// - other_package |
| 14 /// | 12 /// |
| 15 /// foo: | 13 /// foo: |
| 16 /// packages: | 14 /// packages: |
| 17 /// - foo | 15 /// - foo |
| 18 /// - bar | 16 /// - bar |
| 19 /// | 17 /// |
| 20 /// baz: | 18 /// baz: |
| 21 /// packages: | 19 /// packages: |
| 22 /// - baz | 20 /// - baz |
| 23 /// - quux | 21 /// - quux |
| 24 /// | 22 /// |
| 25 /// The YAML file consists of a list of declarations, one for each deferred | 23 /// 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 | 24 /// 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 | 25 /// "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 | 26 /// 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 | 27 /// 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 | 28 /// 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". | 29 /// the part named "baz" is expected to contain the packages "baz" and "quux". |
| 32 /// | 30 /// |
| 33 /// The names for parts given in the specification YAML file (besides "main") | 31 /// 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 | 32 /// are arbitrary and just used for reporting when the output does not meet the |
| 35 /// specification. | 33 /// specification. |
| 36 library dart2js_info.bin.deferred_library_check; | 34 library dart2js_info.deferred_library_check; |
| 37 | 35 |
| 38 import 'dart:async'; | 36 import 'info.dart'; |
| 39 import 'dart:convert'; | 37 import 'package:quiver/collection.dart'; |
| 40 import 'dart:io'; | |
| 41 | 38 |
| 42 import 'package:dart2js_info/info.dart'; | 39 List<ManifestComplianceFailure> checkDeferredLibraryManifest( |
| 43 import 'package:quiver/collection.dart'; | 40 AllInfo info, Map manifest) { |
| 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 | 41 // For each part in the manifest, record the expected "packages" for that |
| 55 // part. | 42 // part. |
| 56 var packages = <String, String>{}; | 43 var packages = <String, String>{}; |
| 57 for (var part in manifest.keys) { | 44 for (var part in manifest.keys) { |
| 58 for (var package in manifest[part]['packages']) { | 45 for (var package in manifest[part]['packages']) { |
| 59 if (packages.containsKey(package)) { | 46 if (packages.containsKey(package)) { |
| 60 print('You cannot specify that package "$package" maps to both parts ' | 47 throw new ArgumentError.value( |
| 48 manifest, |
| 49 'manifest', |
| 50 'You cannot specify that package "$package" maps to both parts ' |
| 61 '"$part" and "${packages[package]}".'); | 51 '"$part" and "${packages[package]}".'); |
| 62 exit(1); | |
| 63 } | 52 } |
| 64 packages[package] = part; | 53 packages[package] = part; |
| 65 } | 54 } |
| 66 } | 55 } |
| 67 | 56 |
| 68 var guessedPartMapping = new BiMap<String, String>(); | 57 var guessedPartMapping = new BiMap<String, String>(); |
| 69 guessedPartMapping['main'] = 'main'; | 58 guessedPartMapping['main'] = 'main'; |
| 70 | 59 |
| 71 bool anyFailed = false; | 60 var failures = <ManifestComplianceFailure>[]; |
| 72 | 61 |
| 73 checkInfo(BasicInfo info) { | 62 checkInfo(BasicInfo info) { |
| 74 var lib = getLibraryOf(info); | 63 var lib = _getLibraryOf(info); |
| 75 if (lib != null && isPackageUri(lib.uri)) { | 64 if (lib != null && _isPackageUri(lib.uri)) { |
| 76 var packageName = getPackageName(lib.uri); | 65 var packageName = _getPackageName(lib.uri); |
| 77 var outputUnitName = info.outputUnit.name; | 66 var outputUnitName = info.outputUnit.name; |
| 78 var expectedPart; | 67 var expectedPart; |
| 79 if (packages.containsKey(packageName)) { | 68 if (packages.containsKey(packageName)) { |
| 80 expectedPart = packages[packageName]; | 69 expectedPart = packages[packageName]; |
| 81 } else { | 70 } else { |
| 82 expectedPart = 'main'; | 71 expectedPart = 'main'; |
| 83 } | 72 } |
| 84 var expectedOutputUnit = guessedPartMapping[expectedPart]; | 73 var expectedOutputUnit = guessedPartMapping[expectedPart]; |
| 85 if (expectedOutputUnit == null) { | 74 if (expectedOutputUnit == null) { |
| 86 guessedPartMapping[expectedPart] = outputUnitName; | 75 guessedPartMapping[expectedPart] = outputUnitName; |
| 87 } else { | 76 } else { |
| 88 if (expectedOutputUnit != outputUnitName) { | 77 if (expectedOutputUnit != outputUnitName) { |
| 89 // TODO(het): add options for how to treat unspecified packages | 78 // TODO(het): add options for how to treat unspecified packages |
| 90 if (!packages.containsKey(packageName)) { | 79 if (!packages.containsKey(packageName)) { |
| 91 print('"${info.name}" from package "$packageName" was not declared ' | 80 failures.add(new ManifestComplianceFailure(info.name, packageName)); |
| 92 'to be in an explicit part but was not in the main part'); | |
| 93 } else { | 81 } else { |
| 94 var actualPart = guessedPartMapping.inverse[outputUnitName]; | 82 var actualPart = guessedPartMapping.inverse[outputUnitName]; |
| 95 print('"${info.name}" from package "$packageName" was specified to ' | 83 failures.add(new ManifestComplianceFailure( |
| 96 'be in part $expectedPart but is in part $actualPart'); | 84 info.name, packageName, expectedPart, actualPart)); |
| 97 } | 85 } |
| 98 anyFailed = true; | |
| 99 } | 86 } |
| 100 } | 87 } |
| 101 } | 88 } |
| 102 } | 89 } |
| 103 | 90 |
| 104 info.functions.forEach(checkInfo); | 91 info.functions.forEach(checkInfo); |
| 105 info.fields.forEach(checkInfo); | 92 info.fields.forEach(checkInfo); |
| 106 if (anyFailed) { | 93 |
| 107 print('The dart2js output did not meet the specification.'); | 94 return failures; |
| 108 } else { | |
| 109 print('The dart2js output meets the specification'); | |
| 110 } | |
| 111 } | 95 } |
| 112 | 96 |
| 113 LibraryInfo getLibraryOf(Info info) { | 97 LibraryInfo _getLibraryOf(Info info) { |
| 114 var current = info; | 98 var current = info; |
| 115 while (current is! LibraryInfo) { | 99 while (current is! LibraryInfo) { |
| 116 if (current == null) { | 100 if (current == null) { |
| 117 return null; | 101 return null; |
| 118 } | 102 } |
| 119 current = current.parent; | 103 current = current.parent; |
| 120 } | 104 } |
| 121 return current; | 105 return current; |
| 122 } | 106 } |
| 123 | 107 |
| 124 bool isPackageUri(Uri uri) => uri.scheme == 'package'; | 108 bool _isPackageUri(Uri uri) => uri.scheme == 'package'; |
| 125 | 109 |
| 126 String getPackageName(Uri uri) { | 110 String _getPackageName(Uri uri) { |
| 127 assert(isPackageUri(uri)); | 111 assert(_isPackageUri(uri)); |
| 128 return uri.pathSegments.first; | 112 return uri.pathSegments.first; |
| 129 } | 113 } |
| 130 | 114 |
| 131 Future<AllInfo> infoFromFile(String fileName) async { | 115 class ManifestComplianceFailure { |
| 132 var file = await new File(fileName).readAsString(); | 116 final String infoName; |
| 133 return new AllInfoJsonCodec().decode(JSON.decode(file)); | 117 final String packageName; |
| 118 final String expectedPart; |
| 119 final String actualPart; |
| 120 |
| 121 const ManifestComplianceFailure(this.infoName, this.packageName, |
| 122 [this.expectedPart, this.actualPart]); |
| 123 |
| 124 String toString() { |
| 125 if (expectedPart == null && actualPart == null) { |
| 126 return '"$infoName" from package "$packageName" was not declared ' |
| 127 'to be in an explicit part but was not in the main part'; |
| 128 } else { |
| 129 return '"$infoName" from package "$packageName" was specified to ' |
| 130 'be in part $expectedPart but is in part $actualPart'; |
| 131 } |
| 132 } |
| 134 } | 133 } |
| 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 |