Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: pkg/analyzer_cli/lib/src/package_analyzer.dart

Issue 1722413002: Extract PackageBundleAssembler and use for SDK and package summary generation. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/tool/summary/build_sdk_summaries.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 library analyzer_cli.src.package_analyzer; 5 library analyzer_cli.src.package_analyzer;
6 6
7 import 'dart:convert';
8 import 'dart:core' hide Resource; 7 import 'dart:core' hide Resource;
9 import 'dart:io' as io; 8 import 'dart:io' as io;
10 9
11 import 'package:analyzer/dart/element/element.dart'; 10 import 'package:analyzer/dart/element/element.dart';
12 import 'package:analyzer/file_system/file_system.dart'; 11 import 'package:analyzer/file_system/file_system.dart';
13 import 'package:analyzer/file_system/physical_file_system.dart'; 12 import 'package:analyzer/file_system/physical_file_system.dart';
14 import 'package:analyzer/source/package_map_resolver.dart'; 13 import 'package:analyzer/source/package_map_resolver.dart';
15 import 'package:analyzer/src/context/cache.dart'; 14 import 'package:analyzer/src/context/cache.dart';
16 import 'package:analyzer/src/context/context.dart'; 15 import 'package:analyzer/src/context/context.dart';
17 import 'package:analyzer/src/generated/engine.dart'; 16 import 'package:analyzer/src/generated/engine.dart';
18 import 'package:analyzer/src/generated/error.dart'; 17 import 'package:analyzer/src/generated/error.dart';
19 import 'package:analyzer/src/generated/resolver.dart'; 18 import 'package:analyzer/src/generated/resolver.dart';
20 import 'package:analyzer/src/generated/sdk_io.dart'; 19 import 'package:analyzer/src/generated/sdk_io.dart';
21 import 'package:analyzer/src/generated/source.dart'; 20 import 'package:analyzer/src/generated/source.dart';
22 import 'package:analyzer/src/generated/source_io.dart'; 21 import 'package:analyzer/src/generated/source_io.dart';
23 import 'package:analyzer/src/summary/format.dart'; 22 import 'package:analyzer/src/summary/format.dart';
24 import 'package:analyzer/src/summary/idl.dart'; 23 import 'package:analyzer/src/summary/idl.dart';
25 import 'package:analyzer/src/summary/resynthesize.dart'; 24 import 'package:analyzer/src/summary/resynthesize.dart';
26 import 'package:analyzer/src/summary/summarize_elements.dart'; 25 import 'package:analyzer/src/summary/summarize_elements.dart';
27 import 'package:analyzer/src/summary/summary_sdk.dart'; 26 import 'package:analyzer/src/summary/summary_sdk.dart';
28 import 'package:analyzer/src/task/dart.dart'; 27 import 'package:analyzer/src/task/dart.dart';
29 import 'package:analyzer/task/dart.dart'; 28 import 'package:analyzer/task/dart.dart';
30 import 'package:analyzer/task/model.dart'; 29 import 'package:analyzer/task/model.dart';
31 import 'package:analyzer_cli/src/analyzer_impl.dart'; 30 import 'package:analyzer_cli/src/analyzer_impl.dart';
32 import 'package:analyzer_cli/src/driver.dart'; 31 import 'package:analyzer_cli/src/driver.dart';
33 import 'package:analyzer_cli/src/error_formatter.dart'; 32 import 'package:analyzer_cli/src/error_formatter.dart';
34 import 'package:analyzer_cli/src/options.dart'; 33 import 'package:analyzer_cli/src/options.dart';
35 import 'package:crypto/crypto.dart';
36 import 'package:path/path.dart' as pathos; 34 import 'package:path/path.dart' as pathos;
37 35
38 /** 36 /**
39 * If [uri] has the `package` scheme in form of `package:pkg/file.dart`, 37 * If [uri] has the `package` scheme in form of `package:pkg/file.dart`,
40 * return the `pkg` name. Otherwise return `null`. 38 * return the `pkg` name. Otherwise return `null`.
41 */ 39 */
42 String getPackageName(Uri uri) { 40 String getPackageName(Uri uri) {
43 if (uri.scheme != 'package') { 41 if (uri.scheme != 'package') {
44 return null; 42 return null;
45 } 43 }
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 class PackageAnalyzer { 240 class PackageAnalyzer {
243 final CommandLineOptions options; 241 final CommandLineOptions options;
244 242
245 String packagePath; 243 String packagePath;
246 String packageLibPath; 244 String packageLibPath;
247 245
248 final ResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE; 246 final ResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE;
249 InternalAnalysisContext context; 247 InternalAnalysisContext context;
250 final List<Source> explicitSources = <Source>[]; 248 final List<Source> explicitSources = <Source>[];
251 249
252 final List<String> linkedLibraryUris = <String>[];
253 final List<LinkedLibraryBuilder> linkedLibraries = <LinkedLibraryBuilder>[];
254 final List<String> unlinkedUnitUris = <String>[];
255 final List<UnlinkedUnitBuilder> unlinkedUnits = <UnlinkedUnitBuilder>[];
256 final List<String> unlinkedUnitHashes = <String>[];
257
258 PackageAnalyzer(this.options); 250 PackageAnalyzer(this.options);
259 251
260 /** 252 /**
261 * Perform package analysis according to the given [options]. 253 * Perform package analysis according to the given [options].
262 */ 254 */
263 ErrorSeverity analyze() { 255 ErrorSeverity analyze() {
264 packagePath = options.packageModePath; 256 packagePath = options.packageModePath;
265 packageLibPath = resourceProvider.pathContext.join(packagePath, 'lib'); 257 packageLibPath = resourceProvider.pathContext.join(packagePath, 'lib');
266 if (packageLibPath == null) { 258 if (packageLibPath == null) {
267 errorSink.writeln('--package-mode-path must be set to the root ' 259 errorSink.writeln('--package-mode-path must be set to the root '
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 // Perform full analysis. 291 // Perform full analysis.
300 while (true) { 292 while (true) {
301 AnalysisResult analysisResult = context.performAnalysisTask(); 293 AnalysisResult analysisResult = context.performAnalysisTask();
302 if (!analysisResult.hasMoreWork) { 294 if (!analysisResult.hasMoreWork) {
303 break; 295 break;
304 } 296 }
305 } 297 }
306 298
307 // Write summary for Dart libraries. 299 // Write summary for Dart libraries.
308 if (options.packageSummaryOutput != null) { 300 if (options.packageSummaryOutput != null) {
301 PackageBundleAssembler assembler = new PackageBundleAssembler();
309 for (Source source in context.librarySources) { 302 for (Source source in context.librarySources) {
310 if (pathos.isWithin(packageLibPath, source.fullName)) { 303 if (pathos.isWithin(packageLibPath, source.fullName)) {
311 LibraryElement libraryElement = context.getLibraryElement(source); 304 LibraryElement libraryElement = context.getLibraryElement(source);
312 if (libraryElement != null) { 305 if (libraryElement != null) {
313 _serializeSingleLibrary(libraryElement); 306 assembler.serializeLibraryElement(libraryElement);
314 } 307 }
315 } 308 }
316 } 309 }
317 // Write the whole package bundle. 310 // Write the whole package bundle.
318 PackageBundleBuilder sdkBundle = new PackageBundleBuilder( 311 PackageBundleBuilder sdkBundle = assembler.assemble();
319 linkedLibraryUris: linkedLibraryUris,
320 linkedLibraries: linkedLibraries,
321 unlinkedUnitUris: unlinkedUnitUris,
322 unlinkedUnits: unlinkedUnits,
323 unlinkedUnitHashes: unlinkedUnitHashes);
324 io.File file = new io.File(options.packageSummaryOutput); 312 io.File file = new io.File(options.packageSummaryOutput);
325 file.writeAsBytesSync(sdkBundle.toBuffer(), mode: io.FileMode.WRITE_ONLY); 313 file.writeAsBytesSync(sdkBundle.toBuffer(), mode: io.FileMode.WRITE_ONLY);
326 } 314 }
327 315
328 // Process errors. 316 // Process errors.
329 _printErrors(); 317 _printErrors();
330 return _computeMaxSeverity(); 318 return _computeMaxSeverity();
331 } 319 }
332 320
333 ErrorSeverity _computeMaxSeverity() { 321 ErrorSeverity _computeMaxSeverity() {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 Source _createSourceInContext(File file) { 366 Source _createSourceInContext(File file) {
379 Source source = file.createSource(); 367 Source source = file.createSource();
380 if (context == null) { 368 if (context == null) {
381 return source; 369 return source;
382 } 370 }
383 Uri uri = context.sourceFactory.restoreUri(source); 371 Uri uri = context.sourceFactory.restoreUri(source);
384 return file.createSource(uri); 372 return file.createSource(uri);
385 } 373 }
386 374
387 /** 375 /**
388 * Compute a hash of the given file contents.
389 */
390 String _hash(String contents) {
391 MD5 md5 = new MD5();
392 md5.add(UTF8.encode(contents));
393 return CryptoUtils.bytesToHex(md5.close());
394 }
395
396 /**
397 * Print errors for all explicit sources. 376 * Print errors for all explicit sources.
398 */ 377 */
399 void _printErrors() { 378 void _printErrors() {
400 StringSink sink = options.machineFormat ? errorSink : outSink; 379 StringSink sink = options.machineFormat ? errorSink : outSink;
401 ErrorFormatter formatter = new ErrorFormatter( 380 ErrorFormatter formatter = new ErrorFormatter(
402 sink, 381 sink,
403 options, 382 options,
404 (AnalysisError error) => 383 (AnalysisError error) =>
405 AnalyzerImpl.processError(error, options, context)); 384 AnalyzerImpl.processError(error, options, context));
406 for (Source source in explicitSources) { 385 for (Source source in explicitSources) {
407 AnalysisErrorInfo errorInfo = context.getErrors(source); 386 AnalysisErrorInfo errorInfo = context.getErrors(source);
408 formatter.formatErrors([errorInfo]); 387 formatter.formatErrors([errorInfo]);
409 } 388 }
410 } 389 }
411
412 /**
413 * Serialize the library with the given [element].
414 */
415 void _serializeSingleLibrary(LibraryElement element) {
416 String uri = element.source.uri.toString();
417 LibrarySerializationResult libraryResult =
418 serializeLibrary(element, context.typeProvider, options.strongMode);
419 linkedLibraryUris.add(uri);
420 linkedLibraries.add(libraryResult.linked);
421 unlinkedUnitUris.addAll(libraryResult.unitUris);
422 unlinkedUnits.addAll(libraryResult.unlinkedUnits);
423 for (Source source in libraryResult.unitSources) {
424 unlinkedUnitHashes.add(_hash(source.contents.data));
425 }
426 }
427 } 390 }
OLDNEW
« no previous file with comments | « pkg/analyzer/tool/summary/build_sdk_summaries.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698