| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:collection'; | 6 import 'dart:collection'; |
| 7 import 'dart:core' hide Resource; | 7 import 'dart:core' hide Resource; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 * | 73 * |
| 74 * The client should call [getLinkedBundles] after creating a new | 74 * The client should call [getLinkedBundles] after creating a new |
| 75 * [AnalysisContext] and configuring its source factory, but before computing | 75 * [AnalysisContext] and configuring its source factory, but before computing |
| 76 * any analysis results. The returned linked bundles can be used to create and | 76 * any analysis results. The returned linked bundles can be used to create and |
| 77 * configure [ResynthesizerResultProvider] for the context. | 77 * configure [ResynthesizerResultProvider] for the context. |
| 78 */ | 78 */ |
| 79 class PubSummaryManager { | 79 class PubSummaryManager { |
| 80 static const UNLINKED_NAME = 'unlinked.ds'; | 80 static const UNLINKED_NAME = 'unlinked.ds'; |
| 81 static const UNLINKED_SPEC_NAME = 'unlinked_spec.ds'; | 81 static const UNLINKED_SPEC_NAME = 'unlinked_spec.ds'; |
| 82 | 82 |
| 83 /** |
| 84 * See [PackageBundleAssembler.currentMajorVersion]. |
| 85 */ |
| 86 final int majorVersion; |
| 87 |
| 83 final ResourceProvider resourceProvider; | 88 final ResourceProvider resourceProvider; |
| 84 | 89 |
| 85 /** | 90 /** |
| 86 * The name of the temporary file that is used for atomic writes. | 91 * The name of the temporary file that is used for atomic writes. |
| 87 */ | 92 */ |
| 88 final String tempFileName; | 93 final String tempFileName; |
| 89 | 94 |
| 90 /** | 95 /** |
| 91 * The map from [PubPackage]s to their unlinked [PackageBundle]s in the pub | 96 * The map from [PubPackage]s to their unlinked [PackageBundle]s in the pub |
| 92 * cache. | 97 * cache. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 110 * for their unlinked bundle existence, or scheduled its computing. | 115 * for their unlinked bundle existence, or scheduled its computing. |
| 111 */ | 116 */ |
| 112 final Set<PubPackage> seenPackages = new Set<PubPackage>(); | 117 final Set<PubPackage> seenPackages = new Set<PubPackage>(); |
| 113 | 118 |
| 114 /** | 119 /** |
| 115 * The [Completer] that completes when computing of all scheduled unlinked | 120 * The [Completer] that completes when computing of all scheduled unlinked |
| 116 * bundles is complete. | 121 * bundles is complete. |
| 117 */ | 122 */ |
| 118 Completer _onUnlinkedCompleteCompleter; | 123 Completer _onUnlinkedCompleteCompleter; |
| 119 | 124 |
| 120 PubSummaryManager(this.resourceProvider, this.tempFileName); | 125 PubSummaryManager(this.resourceProvider, this.tempFileName, |
| 126 {@visibleForTesting this.majorVersion: |
| 127 PackageBundleAssembler.currentMajorVersion}); |
| 121 | 128 |
| 122 /** | 129 /** |
| 123 * The [Future] that completes when computing of all scheduled unlinked | 130 * The [Future] that completes when computing of all scheduled unlinked |
| 124 * bundles is complete. | 131 * bundles is complete. |
| 125 */ | 132 */ |
| 126 Future get onUnlinkedComplete { | 133 Future get onUnlinkedComplete { |
| 127 if (packagesToComputeUnlinked.isEmpty) { | 134 if (packagesToComputeUnlinked.isEmpty) { |
| 128 return new Future.value(); | 135 return new Future.value(); |
| 129 } | 136 } |
| 130 _onUnlinkedCompleteCompleter ??= new Completer(); | 137 _onUnlinkedCompleteCompleter ??= new Completer(); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 } | 342 } |
| 336 for (Resource child in children) { | 343 for (Resource child in children) { |
| 337 if (child is Folder) { | 344 if (child is Folder) { |
| 338 addDartFiles(child); | 345 addDartFiles(child); |
| 339 } | 346 } |
| 340 } | 347 } |
| 341 } | 348 } |
| 342 | 349 |
| 343 try { | 350 try { |
| 344 addDartFiles(libFolder); | 351 addDartFiles(libFolder); |
| 345 List<int> bytes = assembler.assemble().toBuffer(); | 352 PackageBundleBuilder bundleWriter = assembler.assemble(); |
| 353 bundleWriter.majorVersion = majorVersion; |
| 354 List<int> bytes = bundleWriter.toBuffer(); |
| 346 String fileName = _getUnlinkedName(strong); | 355 String fileName = _getUnlinkedName(strong); |
| 347 _writeAtomic(package.folder, fileName, bytes); | 356 _writeAtomic(package.folder, fileName, bytes); |
| 348 } on FileSystemException { | 357 } on FileSystemException { |
| 349 // Ignore file system exceptions. | 358 // Ignore file system exceptions. |
| 350 } | 359 } |
| 351 } | 360 } |
| 352 | 361 |
| 353 /** | 362 /** |
| 354 * Return the name of the file for a linked bundle, in strong or spec mode. | 363 * Return the name of the file for a linked bundle, in strong or spec mode. |
| 355 */ | 364 */ |
| (...skipping 19 matching lines...) Expand all Loading... |
| 375 /** | 384 /** |
| 376 * Return the unlinked [PackageBundle] for the given [package]. If the bundle | 385 * Return the unlinked [PackageBundle] for the given [package]. If the bundle |
| 377 * has not been compute yet, return `null` and schedule its computation. | 386 * has not been compute yet, return `null` and schedule its computation. |
| 378 */ | 387 */ |
| 379 PackageBundle _getUnlinkedOrSchedule(PubPackage package, bool strong) { | 388 PackageBundle _getUnlinkedOrSchedule(PubPackage package, bool strong) { |
| 380 // Try to find in the cache. | 389 // Try to find in the cache. |
| 381 PackageBundle bundle = unlinkedBundleMap[package]; | 390 PackageBundle bundle = unlinkedBundleMap[package]; |
| 382 if (bundle != null) { | 391 if (bundle != null) { |
| 383 return bundle; | 392 return bundle; |
| 384 } | 393 } |
| 394 |
| 385 // Try to read from the file system. | 395 // Try to read from the file system. |
| 386 String fileName = _getUnlinkedName(strong); | 396 String fileName = _getUnlinkedName(strong); |
| 387 File unlinkedFile = package.folder.getChildAssumingFile(fileName); | 397 File file = package.folder.getChildAssumingFile(fileName); |
| 388 if (unlinkedFile.exists) { | 398 if (file.exists) { |
| 389 try { | 399 try { |
| 390 List<int> bytes = unlinkedFile.readAsBytesSync(); | 400 List<int> bytes = file.readAsBytesSync(); |
| 391 bundle = new PackageBundle.fromBuffer(bytes); | 401 bundle = new PackageBundle.fromBuffer(bytes); |
| 392 unlinkedBundleMap[package] = bundle; | |
| 393 // TODO(scheglov) if not in the pub cache, check for consistency | |
| 394 return bundle; | |
| 395 } on FileSystemException { | 402 } on FileSystemException { |
| 396 // Ignore file system exceptions. | 403 // Ignore file system exceptions. |
| 397 } | 404 } |
| 398 } | 405 } |
| 406 |
| 407 bool isInPubCache = isPathInPubCache(pathContext, package.folder.path); |
| 408 |
| 409 // Verify compatibility. |
| 410 // TODO(scheglov) if not in the pub cache, check for consistency |
| 411 if (bundle != null && bundle.majorVersion == majorVersion) { |
| 412 unlinkedBundleMap[package] = bundle; |
| 413 return bundle; |
| 414 } |
| 415 |
| 399 // Schedule computation in the background, if in the pub cache. | 416 // Schedule computation in the background, if in the pub cache. |
| 400 if (isPathInPubCache(pathContext, package.folder.path)) { | 417 if (isInPubCache) { |
| 401 if (seenPackages.add(package)) { | 418 if (seenPackages.add(package)) { |
| 402 _scheduleUnlinked(package); | 419 _scheduleUnlinked(package); |
| 403 } | 420 } |
| 404 } | 421 } |
| 405 // The bundle is for available. | 422 |
| 423 // The bundle is not available. |
| 406 return null; | 424 return null; |
| 407 } | 425 } |
| 408 | 426 |
| 409 /** | 427 /** |
| 410 * Parse the given [source] into AST. | 428 * Parse the given [source] into AST. |
| 411 */ | 429 */ |
| 412 CompilationUnit _parse(Source source, bool strong) { | 430 CompilationUnit _parse(Source source, bool strong) { |
| 413 String code = source.contents.data; | 431 String code = source.contents.data; |
| 414 AnalysisErrorListener errorListener = AnalysisErrorListener.NULL_LISTENER; | 432 AnalysisErrorListener errorListener = AnalysisErrorListener.NULL_LISTENER; |
| 415 CharSequenceReader reader = new CharSequenceReader(code); | 433 CharSequenceReader reader = new CharSequenceReader(code); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 } | 725 } |
| 708 | 726 |
| 709 /** | 727 /** |
| 710 * Check whether the given `package:` [uri] is listed in the package map. | 728 * Check whether the given `package:` [uri] is listed in the package map. |
| 711 */ | 729 */ |
| 712 bool isListed(String uri) { | 730 bool isListed(String uri) { |
| 713 String package = PubSummaryManager.getPackageName(uri); | 731 String package = PubSummaryManager.getPackageName(uri); |
| 714 return names.contains(package); | 732 return names.contains(package); |
| 715 } | 733 } |
| 716 } | 734 } |
| OLD | NEW |