Chromium Code Reviews| Index: pkg/analyzer/tool/summary/build_sdk_summaries.dart |
| diff --git a/pkg/analyzer/tool/summary/build_sdk_summaries.dart b/pkg/analyzer/tool/summary/build_sdk_summaries.dart |
| index 29f49db17b6962a46ea56d57d5190c9fe6b3c069..c33dcda35cb7fa2cd7fbe59ecbe650e43cea6e80 100644 |
| --- a/pkg/analyzer/tool/summary/build_sdk_summaries.dart |
| +++ b/pkg/analyzer/tool/summary/build_sdk_summaries.dart |
| @@ -7,65 +7,160 @@ import 'package:analyzer/src/generated/java_io.dart'; |
| import 'package:analyzer/src/generated/sdk.dart'; |
| import 'package:analyzer/src/generated/sdk_io.dart'; |
| import 'package:analyzer/src/generated/source.dart'; |
| -import 'package:analyzer/src/summary/format.dart'; |
| +import 'package:analyzer/src/summary/flat_buffers.dart' as fb; |
| import 'package:analyzer/src/summary/index_unit.dart'; |
| import 'package:analyzer/src/summary/summarize_elements.dart'; |
| import 'package:path/path.dart'; |
| main(List<String> args) { |
| - if (args.length < 1 || args.length > 2) { |
| + if (args.length < 1) { |
| _printUsage(); |
| exitCode = 1; |
| return; |
| } |
| - // |
| - // Prepare output file path. |
| - // |
| - String outputDirectoryPath = args[0]; |
| - if (!FileSystemEntity.isDirectorySync(outputDirectoryPath)) { |
| - print("'$outputDirectoryPath' is not a directory."); |
| + String command = args[0]; |
| + if (command == 'multiple-outputs') { |
|
Paul Berry
2016/03/09 18:23:21
Add `&& args.length >= 2 && args.length <= 3`. Ot
|
| + // |
| + // Prepare the output path. |
| + // |
| + String outputDirectoryPath = args[1]; |
| + if (!FileSystemEntity.isDirectorySync(outputDirectoryPath)) { |
| + print("'$outputDirectoryPath' is not a directory."); |
| + _printUsage(); |
| + exitCode = 1; |
| + return; |
| + } |
| + // |
| + // Prepare results. |
| + // |
| + String sdkPath = args.length > 2 ? args[2] : null; |
| + _Output output = _buildMultipleOutputs(sdkPath); |
| + if (output == null) { |
| + exitCode = 1; |
| + return; |
| + } |
| + // |
| + // Write results. |
| + // |
| + output.spec.writeMultiple(outputDirectoryPath, 'spec'); |
| + output.strong.writeMultiple(outputDirectoryPath, 'strong'); |
| + } else if (command == 'single-output') { |
|
Paul Berry
2016/03/09 18:23:21
Same comment here.
|
| + String outputPath = args[1]; |
| + String sdkPath = args.length > 2 ? args[2] : null; |
| + // |
| + // Prepare results. |
| + // |
| + _Output output = _buildMultipleOutputs(sdkPath); |
| + if (output == null) { |
| + exitCode = 1; |
| + return; |
| + } |
| + // |
| + // Write results. |
| + // |
| + fb.Builder builder = new fb.Builder(); |
| + fb.Offset specSumOffset = builder.writeListUint8(output.spec.sum); |
| + fb.Offset specIndexOffset = builder.writeListUint8(output.spec.index); |
| + fb.Offset strongSumOffset = builder.writeListUint8(output.strong.sum); |
| + fb.Offset strongIndexOffset = builder.writeListUint8(output.strong.index); |
| + builder.startTable(); |
| + builder.addOffset(_FIELD_SPEC_SUM, specSumOffset); |
| + builder.addOffset(_FIELD_SPEC_INDEX, specIndexOffset); |
| + builder.addOffset(_FIELD_STRONG_SUM, strongSumOffset); |
| + builder.addOffset(_FIELD_STRONG_INDEX, strongIndexOffset); |
| + fb.Offset offset = builder.endTable(); |
| + new File(outputPath) |
| + .writeAsBytesSync(builder.finish(offset), mode: FileMode.WRITE_ONLY); |
| + } else if (command == 'extract-spec-sum' && args.length == 3) { |
| + String inputPath = args[1]; |
| + String outputPath = args[2]; |
| + _extractSingleOutput(inputPath, _FIELD_SPEC_SUM, outputPath); |
| + } else if (command == 'extract-spec-index' && args.length == 3) { |
| + String inputPath = args[1]; |
| + String outputPath = args[2]; |
| + _extractSingleOutput(inputPath, _FIELD_SPEC_INDEX, outputPath); |
| + } else if (command == 'extract-strong-sum' && args.length == 3) { |
| + String inputPath = args[1]; |
| + String outputPath = args[2]; |
| + _extractSingleOutput(inputPath, _FIELD_STRONG_SUM, outputPath); |
| + } else if (command == 'extract-strong-index' && args.length == 3) { |
| + String inputPath = args[1]; |
| + String outputPath = args[2]; |
| + _extractSingleOutput(inputPath, _FIELD_STRONG_INDEX, outputPath); |
| + } else { |
| _printUsage(); |
| exitCode = 1; |
| return; |
| } |
| +} |
| + |
| +/** |
| + * The name of the SDK summaries builder application. |
| + */ |
| +const BINARY_NAME = "build_sdk_summaries"; |
| + |
| +const int _FIELD_SPEC_INDEX = 1; |
| +const int _FIELD_SPEC_SUM = 0; |
| +const int _FIELD_STRONG_INDEX = 3; |
| +const int _FIELD_STRONG_SUM = 2; |
| + |
| +_Output _buildMultipleOutputs(String sdkPath) { |
| // |
| - // Prepare SDK path. |
| + // Validate the SDK path. |
| // |
| - String sdkPath; |
| - if (args.length == 2) { |
| - sdkPath = args[1]; |
| + if (sdkPath != null) { |
| if (!FileSystemEntity.isDirectorySync('$sdkPath/lib')) { |
| print("'$sdkPath/lib' does not exist."); |
| _printUsage(); |
| - exitCode = 1; |
| - return; |
| + return null; |
| } |
| } else { |
| sdkPath = DirectoryBasedDartSdk.defaultSdkDirectory.getAbsolutePath(); |
| } |
| // |
| - // Build spec and strong summaries. |
| + // Build spec and strong outputs. |
| // |
| - new _Builder(sdkPath, outputDirectoryPath, false).build(); |
| - new _Builder(sdkPath, outputDirectoryPath, true).build(); |
| + _BuilderOutput spec = new _Builder(sdkPath, false).build(); |
| + _BuilderOutput strong = new _Builder(sdkPath, true).build(); |
| + return new _Output(spec, strong); |
| } |
| /** |
| - * The name of the SDK summaries builder application. |
| + * Open the flat buffer in [inputPath] and extract the byte array in the [field] |
| + * into the [outputPath] file. |
| */ |
| -const BINARY_NAME = "build_sdk_summaries"; |
| +void _extractSingleOutput(String inputPath, int field, String outputPath) { |
| + List<int> bytes = new File(inputPath).readAsBytesSync(); |
| + fb.BufferPointer bp = new fb.BufferPointer.fromBytes(bytes); |
| + fb.BufferPointer table = bp.derefObject(); |
| + List<int> fieldBytes = |
| + const fb.ListReader(const fb.Uint8Reader()).vTableGet(table, field); |
| + new File(outputPath).writeAsBytesSync(fieldBytes, mode: FileMode.WRITE_ONLY); |
| +} |
| /** |
| * Print information about how to use the SDK summaries builder. |
| */ |
| void _printUsage() { |
| - print('Usage: $BINARY_NAME output_directory_path [sdk_path]'); |
| - print('Build files spec.sum and strong.sum in the output directory.'); |
| +// print('Usage: $BINARY_NAME command output_directory_path [sdk_path]'); |
| + print('Usage: $BINARY_NAME command arguments'); |
| + print('Where command can be one of the following:'); |
| + print(' multiple-outputs output_directory_path [sdk_path]'); |
| + print(' Generate separate summary and index files.'); |
| + print(' single-output output_file_path [sdk_path]'); |
| + print(' Generate a single file with summary and index.'); |
| + print(' extract-spec-sum input_file output_file'); |
| + print(' Extract the spec-mode summary file.'); |
| + print(' extract-strong-sum input_file output_file'); |
| + print(' Extract the strong-mode summary file.'); |
| + print(' extract-spec-index input_file output_file'); |
| + print(' Extract the spec-mode index file.'); |
| + print(' extract-strong-index input_file output_file'); |
| + print(' Extract the strong-mode index file.'); |
| } |
| class _Builder { |
| final String sdkPath; |
| - final String outputDirectoryPath; |
| final bool strongMode; |
| AnalysisContext context; |
| @@ -74,12 +169,12 @@ class _Builder { |
| final PackageBundleAssembler bundleAssembler = new PackageBundleAssembler(); |
| final PackageIndexAssembler indexAssembler = new PackageIndexAssembler(); |
| - _Builder(this.sdkPath, this.outputDirectoryPath, this.strongMode); |
| + _Builder(this.sdkPath, this.strongMode); |
| /** |
| * Build a strong or spec mode summary for the Dart SDK at [sdkPath]. |
| */ |
| - void build() { |
| + _BuilderOutput build() { |
| String modeName = strongMode ? 'strong' : 'spec'; |
| print('Generating $modeName mode summary and index.'); |
| Stopwatch sw = new Stopwatch()..start(); |
| @@ -107,27 +202,12 @@ class _Builder { |
| _serializeLibrary(libSource); |
| } |
| // |
| - // Write the whole SDK bundle. |
| - // |
| - { |
| - PackageBundleBuilder bundle = bundleAssembler.assemble(); |
| - String outputPath = join(outputDirectoryPath, '$modeName.sum'); |
| - File file = new File(outputPath); |
| - file.writeAsBytesSync(bundle.toBuffer(), mode: FileMode.WRITE_ONLY); |
| - } |
| - // |
| - // Write the whole SDK index. |
| - // |
| - { |
| - PackageIndexBuilder index = indexAssembler.assemble(); |
| - String outputPath = join(outputDirectoryPath, '$modeName.index'); |
| - File file = new File(outputPath); |
| - file.writeAsBytesSync(index.toBuffer(), mode: FileMode.WRITE_ONLY); |
| - } |
| - // |
| - // Done. |
| + // Assemble the output. |
| // |
| + List<int> sumBytes = bundleAssembler.assemble().toBuffer(); |
| + List<int> indexBytes = indexAssembler.assemble().toBuffer(); |
| print('\tDone in ${sw.elapsedMilliseconds} ms.'); |
| + return new _BuilderOutput(sumBytes, indexBytes); |
| } |
| /** |
| @@ -151,3 +231,32 @@ class _Builder { |
| } |
| } |
| } |
| + |
| +class _BuilderOutput { |
| + final List<int> sum; |
| + final List<int> index; |
| + |
| + _BuilderOutput(this.sum, this.index); |
| + |
| + void writeMultiple(String outputDirectoryPath, String modeName) { |
| + // Write summary. |
| + { |
| + String outputPath = join(outputDirectoryPath, '$modeName.sum'); |
| + File file = new File(outputPath); |
| + file.writeAsBytesSync(sum, mode: FileMode.WRITE_ONLY); |
| + } |
| + // Write index. |
| + { |
| + String outputPath = join(outputDirectoryPath, '$modeName.index'); |
| + File file = new File(outputPath); |
| + file.writeAsBytesSync(index, mode: FileMode.WRITE_ONLY); |
| + } |
| + } |
| +} |
| + |
| +class _Output { |
| + final _BuilderOutput spec; |
| + final _BuilderOutput strong; |
| + |
| + _Output(this.spec, this.strong); |
| +} |