OLD | NEW |
1 import 'dart:convert'; | |
2 import 'dart:io'; | 1 import 'dart:io'; |
3 | 2 |
4 import 'package:analyzer/dart/element/element.dart'; | 3 import 'package:analyzer/dart/element/element.dart'; |
5 import 'package:analyzer/src/generated/engine.dart'; | 4 import 'package:analyzer/src/generated/engine.dart'; |
6 import 'package:analyzer/src/generated/java_io.dart'; | 5 import 'package:analyzer/src/generated/java_io.dart'; |
7 import 'package:analyzer/src/generated/sdk.dart'; | 6 import 'package:analyzer/src/generated/sdk.dart'; |
8 import 'package:analyzer/src/generated/sdk_io.dart'; | 7 import 'package:analyzer/src/generated/sdk_io.dart'; |
9 import 'package:analyzer/src/generated/source.dart'; | 8 import 'package:analyzer/src/generated/source.dart'; |
10 import 'package:analyzer/src/summary/format.dart'; | 9 import 'package:analyzer/src/summary/format.dart'; |
11 import 'package:analyzer/src/summary/summarize_elements.dart'; | 10 import 'package:analyzer/src/summary/summarize_elements.dart'; |
12 import 'package:crypto/crypto.dart'; | |
13 import 'package:path/path.dart'; | 11 import 'package:path/path.dart'; |
14 | 12 |
15 main(List<String> args) { | 13 main(List<String> args) { |
16 if (args.length < 1 || args.length > 2) { | 14 if (args.length < 1 || args.length > 2) { |
17 _printUsage(); | 15 _printUsage(); |
18 exitCode = 1; | 16 exitCode = 1; |
19 return; | 17 return; |
20 } | 18 } |
21 // | 19 // |
22 // Prepare output file path. | 20 // Prepare output file path. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 62 } |
65 | 63 |
66 class _Builder { | 64 class _Builder { |
67 final String sdkPath; | 65 final String sdkPath; |
68 final String outputDirectoryPath; | 66 final String outputDirectoryPath; |
69 final bool strongMode; | 67 final bool strongMode; |
70 | 68 |
71 AnalysisContext context; | 69 AnalysisContext context; |
72 final Set<Source> processedSources = new Set<Source>(); | 70 final Set<Source> processedSources = new Set<Source>(); |
73 | 71 |
74 final List<String> linkedLibraryUris = <String>[]; | 72 final PackageBundleAssembler assembler = new PackageBundleAssembler(); |
75 final List<LinkedLibraryBuilder> linkedLibraries = <LinkedLibraryBuilder>[]; | |
76 final List<String> unlinkedUnitUris = <String>[]; | |
77 final List<UnlinkedUnitBuilder> unlinkedUnits = <UnlinkedUnitBuilder>[]; | |
78 final List<String> unlinkedUnitHashes = <String>[]; | |
79 | 73 |
80 _Builder(this.sdkPath, this.outputDirectoryPath, this.strongMode); | 74 _Builder(this.sdkPath, this.outputDirectoryPath, this.strongMode); |
81 | 75 |
82 /** | 76 /** |
83 * Build a strong or spec mode summary for the Dart SDK at [sdkPath]. | 77 * Build a strong or spec mode summary for the Dart SDK at [sdkPath]. |
84 */ | 78 */ |
85 void build() { | 79 void build() { |
86 print('Generating ${strongMode ? 'strong' : 'spec'} mode summary.'); | 80 print('Generating ${strongMode ? 'strong' : 'spec'} mode summary.'); |
87 Stopwatch sw = new Stopwatch()..start(); | 81 Stopwatch sw = new Stopwatch()..start(); |
88 // | 82 // |
(...skipping 15 matching lines...) Expand all Loading... |
104 // | 98 // |
105 // Serialize each SDK library. | 99 // Serialize each SDK library. |
106 // | 100 // |
107 for (String uri in uriSet) { | 101 for (String uri in uriSet) { |
108 Source libSource = sdk.mapDartUri(uri); | 102 Source libSource = sdk.mapDartUri(uri); |
109 _serializeLibrary(libSource); | 103 _serializeLibrary(libSource); |
110 } | 104 } |
111 // | 105 // |
112 // Write the whole SDK bundle. | 106 // Write the whole SDK bundle. |
113 // | 107 // |
114 PackageBundleBuilder sdkBundle = new PackageBundleBuilder( | 108 PackageBundleBuilder sdkBundle = assembler.assemble(); |
115 linkedLibraryUris: linkedLibraryUris, | |
116 linkedLibraries: linkedLibraries, | |
117 unlinkedUnitUris: unlinkedUnitUris, | |
118 unlinkedUnits: unlinkedUnits, | |
119 unlinkedUnitHashes: unlinkedUnitHashes); | |
120 String outputFilePath = | 109 String outputFilePath = |
121 join(outputDirectoryPath, strongMode ? 'strong.sum' : 'spec.sum'); | 110 join(outputDirectoryPath, strongMode ? 'strong.sum' : 'spec.sum'); |
122 File file = new File(outputFilePath); | 111 File file = new File(outputFilePath); |
123 file.writeAsBytesSync(sdkBundle.toBuffer(), mode: FileMode.WRITE_ONLY); | 112 file.writeAsBytesSync(sdkBundle.toBuffer(), mode: FileMode.WRITE_ONLY); |
124 // | 113 // |
125 // Done. | 114 // Done. |
126 // | 115 // |
127 print('\tDone in ${sw.elapsedMilliseconds} ms.'); | 116 print('\tDone in ${sw.elapsedMilliseconds} ms.'); |
128 } | 117 } |
129 | 118 |
130 /** | 119 /** |
131 * Compute a hash of the given file contents. | |
132 */ | |
133 String _hash(String contents) { | |
134 MD5 md5 = new MD5(); | |
135 md5.add(UTF8.encode(contents)); | |
136 return CryptoUtils.bytesToHex(md5.close()); | |
137 } | |
138 | |
139 /** | |
140 * Serialize the library with the given [source] and all its direct or | 120 * Serialize the library with the given [source] and all its direct or |
141 * indirect imports and exports. | 121 * indirect imports and exports. |
142 */ | 122 */ |
143 void _serializeLibrary(Source source) { | 123 void _serializeLibrary(Source source) { |
144 if (!processedSources.add(source)) { | 124 if (!processedSources.add(source)) { |
145 return; | 125 return; |
146 } | 126 } |
147 LibraryElement element = context.computeLibraryElement(source); | 127 LibraryElement element = context.computeLibraryElement(source); |
148 _serializeSingleLibrary(element); | 128 assembler.serializeLibraryElement(element); |
149 element.importedLibraries.forEach((e) => _serializeLibrary(e.source)); | 129 element.importedLibraries.forEach((e) => _serializeLibrary(e.source)); |
150 element.exportedLibraries.forEach((e) => _serializeLibrary(e.source)); | 130 element.exportedLibraries.forEach((e) => _serializeLibrary(e.source)); |
151 } | 131 } |
152 | |
153 /** | |
154 * Serialize the library with the given [element]. | |
155 */ | |
156 void _serializeSingleLibrary(LibraryElement element) { | |
157 String uri = element.source.uri.toString(); | |
158 LibrarySerializationResult libraryResult = | |
159 serializeLibrary(element, context.typeProvider, strongMode); | |
160 linkedLibraryUris.add(uri); | |
161 linkedLibraries.add(libraryResult.linked); | |
162 unlinkedUnitUris.addAll(libraryResult.unitUris); | |
163 unlinkedUnits.addAll(libraryResult.unlinkedUnits); | |
164 for (Source source in libraryResult.unitSources) { | |
165 unlinkedUnitHashes.add(_hash(source.contents.data)); | |
166 } | |
167 } | |
168 } | 132 } |
OLD | NEW |