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'; | 7 import 'dart:core'; |
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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // print('LOADED ${unlinkedBundles.length} unlinked bundles' | 199 // print('LOADED ${unlinkedBundles.length} unlinked bundles' |
200 // ' in ${timer.elapsedMilliseconds} ms'); | 200 // ' in ${timer.elapsedMilliseconds} ms'); |
201 // timer..reset(); | 201 // timer..reset(); |
202 | 202 |
203 // If no unlinked bundles, there is nothing we can try to link. | 203 // If no unlinked bundles, there is nothing we can try to link. |
204 if (unlinkedBundles.isEmpty) { | 204 if (unlinkedBundles.isEmpty) { |
205 return const <LinkedPubPackage>[]; | 205 return const <LinkedPubPackage>[]; |
206 } | 206 } |
207 | 207 |
208 // Create graph nodes for packages. | 208 // Create graph nodes for packages. |
209 List<_LinkedNode> nodes = <_LinkedNode>[]; | 209 List<_LinkNode> nodes = <_LinkNode>[]; |
210 Map<String, _LinkedNode> packageToNode = <String, _LinkedNode>{}; | 210 Map<String, _LinkNode> packageToNode = <String, _LinkNode>{}; |
211 unlinkedBundles.forEach((package, unlinked) { | 211 unlinkedBundles.forEach((package, unlinked) { |
212 _LinkedNode node = new _LinkedNode(sdkBundle, getDeclaredVariable, | 212 _LinkNode node = new _LinkNode(sdkBundle, getDeclaredVariable, |
213 listedPackages, package, unlinked, packageToNode); | 213 listedPackages, package, unlinked, packageToNode); |
214 nodes.add(node); | 214 nodes.add(node); |
215 packageToNode[package.name] = node; | 215 packageToNode[package.name] = node; |
216 }); | 216 }); |
217 | 217 |
218 // Compute transitive dependencies, mark some nodes as failed. | 218 // Compute transitive dependencies, mark some nodes as failed. |
219 for (_LinkedNode node in nodes) { | 219 for (_LinkNode node in nodes) { |
220 node.computeTransitiveDependencies(); | 220 node.computeTransitiveDependencies(); |
221 } | 221 } |
222 | 222 |
223 // Attempt to read existing linked bundles. | 223 // Attempt to read existing linked bundles. |
224 for (_LinkedNode node in nodes) { | 224 for (_LinkNode node in nodes) { |
225 _readLinked(node, strong); | 225 _readLinked(node, strong); |
226 } | 226 } |
227 | 227 |
228 // Link new bundles, if allowed. | 228 // Link new bundles, if allowed. |
229 if (allowLinking) { | 229 if (allowLinking) { |
230 // Fill the store with bundles. | 230 // Fill the store with bundles. |
231 // Append the linked SDK bundle. | 231 // Append the linked SDK bundle. |
232 // Append unlinked and (if read from a cache) linked package bundles. | 232 // Append unlinked and (if read from a cache) linked package bundles. |
233 SummaryDataStore store = new SummaryDataStore(const <String>[]); | 233 SummaryDataStore store = new SummaryDataStore(const <String>[]); |
234 store.addBundle(null, sdkBundle); | 234 store.addBundle(null, sdkBundle); |
235 for (_LinkedNode node in nodes) { | 235 for (_LinkNode node in nodes) { |
236 store.addBundle(null, node.unlinked); | 236 store.addBundle(null, node.unlinked); |
237 if (node.linked != null) { | 237 if (node.linked != null) { |
238 store.addBundle(null, node.linked); | 238 store.addBundle(null, node.linked); |
239 } | 239 } |
240 } | 240 } |
241 | 241 |
242 // Link each package node. | 242 // Link each package node. |
243 for (_LinkedNode node in nodes) { | 243 for (_LinkNode node in nodes) { |
244 if (!node.isEvaluated) { | 244 if (!node.isEvaluated) { |
245 new _LinkedWalker(getDeclaredVariable, listedPackages, store, strong) | 245 new _LinkWalker(getDeclaredVariable, listedPackages, store, strong) |
246 .walk(node); | 246 .walk(node); |
247 } | 247 } |
248 } | 248 } |
249 | 249 |
250 // Write newly linked bundles. | 250 // Write newly linked bundles. |
251 for (_LinkedNode node in nodes) { | 251 for (_LinkNode node in nodes) { |
252 _writeLinked(node, strong); | 252 _writeLinked(node, strong); |
253 } | 253 } |
254 } | 254 } |
255 | 255 |
256 // Create successfully linked packages. | 256 // Create successfully linked packages. |
257 List<LinkedPubPackage> linkedPackages = <LinkedPubPackage>[]; | 257 List<LinkedPubPackage> linkedPackages = <LinkedPubPackage>[]; |
258 for (_LinkedNode node in nodes) { | 258 for (_LinkNode node in nodes) { |
259 if (node.linked != null) { | 259 if (node.linked != null) { |
260 linkedPackages.add(new LinkedPubPackage( | 260 linkedPackages.add(new LinkedPubPackage( |
261 node.package, node.unlinked, node.linked, node.linkedHash)); | 261 node.package, node.unlinked, node.linked, node.linkedHash)); |
262 } | 262 } |
263 } | 263 } |
264 | 264 |
265 // TODO(scheglov) remove debug output after optimizing | 265 // TODO(scheglov) remove debug output after optimizing |
266 // print('LINKED ${linkedPackages.length} bundles' | 266 // print('LINKED ${linkedPackages.length} bundles' |
267 // ' in ${timer.elapsedMilliseconds} ms'); | 267 // ' in ${timer.elapsedMilliseconds} ms'); |
268 | 268 |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 LineInfo lineInfo = new LineInfo(scanner.lineStarts); | 505 LineInfo lineInfo = new LineInfo(scanner.lineStarts); |
506 Parser parser = new Parser(source, errorListener); | 506 Parser parser = new Parser(source, errorListener); |
507 parser.parseGenericMethodComments = strong; | 507 parser.parseGenericMethodComments = strong; |
508 CompilationUnit unit = parser.parseCompilationUnit(token); | 508 CompilationUnit unit = parser.parseCompilationUnit(token); |
509 unit.lineInfo = lineInfo; | 509 unit.lineInfo = lineInfo; |
510 return unit; | 510 return unit; |
511 } | 511 } |
512 | 512 |
513 /** | 513 /** |
514 * Attempt to find the linked bundle that corresponds to the given [node] | 514 * Attempt to find the linked bundle that corresponds to the given [node] |
515 * with all its transitive dependencies and put it into [_LinkedNode.linked]. | 515 * with all its transitive dependencies and put it into [_LinkNode.linked]. |
516 */ | 516 */ |
517 void _readLinked(_LinkedNode node, bool strong) { | 517 void _readLinked(_LinkNode node, bool strong) { |
518 String hash = node.linkedHash; | 518 String hash = node.linkedHash; |
519 if (hash != null) { | 519 if (hash != null) { |
520 String fileName = _getLinkedName(hash, strong); | 520 String fileName = _getLinkedName(hash, strong); |
521 File file = node.package.folder.getChildAssumingFile(fileName); | 521 File file = node.package.folder.getChildAssumingFile(fileName); |
522 // Try to find in the cache. | 522 // Try to find in the cache. |
523 PackageBundle linked = linkedBundleMap[file.path]; | 523 PackageBundle linked = linkedBundleMap[file.path]; |
524 if (linked != null) { | 524 if (linked != null) { |
525 node.linked = linked; | 525 node.linked = linked; |
526 return; | 526 return; |
527 } | 527 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 String filePath = folder.getChildAssumingFile(fileName).path; | 566 String filePath = folder.getChildAssumingFile(fileName).path; |
567 File tempFile = folder.getChildAssumingFile(tempFileName); | 567 File tempFile = folder.getChildAssumingFile(tempFileName); |
568 tempFile.writeAsBytesSync(bytes); | 568 tempFile.writeAsBytesSync(bytes); |
569 tempFile.renameSync(filePath); | 569 tempFile.renameSync(filePath); |
570 } | 570 } |
571 | 571 |
572 /** | 572 /** |
573 * If a new linked bundle was linked for the given [node], write the bundle | 573 * If a new linked bundle was linked for the given [node], write the bundle |
574 * into the memory cache and the file system. | 574 * into the memory cache and the file system. |
575 */ | 575 */ |
576 void _writeLinked(_LinkedNode node, bool strong) { | 576 void _writeLinked(_LinkNode node, bool strong) { |
577 String hash = node.linkedHash; | 577 String hash = node.linkedHash; |
578 if (hash != null && node.linkedNewBytes != null) { | 578 if (hash != null && node.linkedNewBytes != null) { |
579 String fileName = _getLinkedName(hash, strong); | 579 String fileName = _getLinkedName(hash, strong); |
580 File file = node.package.folder.getChildAssumingFile(fileName); | 580 File file = node.package.folder.getChildAssumingFile(fileName); |
581 linkedBundleMap[file.path] = node.linked; | 581 linkedBundleMap[file.path] = node.linked; |
582 _writeAtomic(node.package.folder, fileName, node.linkedNewBytes); | 582 _writeAtomic(node.package.folder, fileName, node.linkedNewBytes); |
583 } | 583 } |
584 } | 584 } |
585 | 585 |
586 /** | 586 /** |
(...skipping 26 matching lines...) Expand all Loading... |
613 return true; | 613 return true; |
614 } | 614 } |
615 } | 615 } |
616 return false; | 616 return false; |
617 } | 617 } |
618 } | 618 } |
619 | 619 |
620 /** | 620 /** |
621 * Specialization of [Node] for linking packages in proper dependency order. | 621 * Specialization of [Node] for linking packages in proper dependency order. |
622 */ | 622 */ |
623 class _LinkedNode extends Node<_LinkedNode> { | 623 class _LinkNode extends Node<_LinkNode> { |
624 final PackageBundle sdkBundle; | 624 final PackageBundle sdkBundle; |
625 final _GetDeclaredVariable getDeclaredVariable; | 625 final _GetDeclaredVariable getDeclaredVariable; |
626 final _ListedPackages listedPackages; | 626 final _ListedPackages listedPackages; |
627 final PubPackage package; | 627 final PubPackage package; |
628 final PackageBundle unlinked; | 628 final PackageBundle unlinked; |
629 final Map<String, _LinkedNode> packageToNode; | 629 final Map<String, _LinkNode> packageToNode; |
630 | 630 |
631 bool failed = false; | 631 bool failed = false; |
632 Set<_LinkedNode> transitiveDependencies; | 632 Set<_LinkNode> transitiveDependencies; |
633 String _linkedHash; | 633 String _linkedHash; |
634 | 634 |
635 List<int> linkedNewBytes; | 635 List<int> linkedNewBytes; |
636 PackageBundle linked; | 636 PackageBundle linked; |
637 | 637 |
638 _LinkedNode(this.sdkBundle, this.getDeclaredVariable, this.listedPackages, | 638 _LinkNode(this.sdkBundle, this.getDeclaredVariable, this.listedPackages, |
639 this.package, this.unlinked, this.packageToNode); | 639 this.package, this.unlinked, this.packageToNode); |
640 | 640 |
641 @override | 641 @override |
642 bool get isEvaluated => linked != null || failed; | 642 bool get isEvaluated => linked != null || failed; |
643 | 643 |
644 /** | 644 /** |
645 * Return the hash string that corresponds to this linked bundle in the | 645 * Return the hash string that corresponds to this linked bundle in the |
646 * context of its [sdkBundle] and transitive dependencies. Return `null` if | 646 * context of its [sdkBundle] and transitive dependencies. Return `null` if |
647 * the hash computation fails, because for example the full transitive | 647 * the hash computation fails, because for example the full transitive |
648 * dependencies cannot computed. | 648 * dependencies cannot computed. |
(...skipping 10 matching lines...) Expand all Loading... |
659 signatures.sort(); | 659 signatures.sort(); |
660 signatures.forEach(signature.addString); | 660 signatures.forEach(signature.addString); |
661 // Combine into a single hash. | 661 // Combine into a single hash. |
662 _appendDeclaredVariables(signature); | 662 _appendDeclaredVariables(signature); |
663 _linkedHash = signature.toHex(); | 663 _linkedHash = signature.toHex(); |
664 } | 664 } |
665 return _linkedHash; | 665 return _linkedHash; |
666 } | 666 } |
667 | 667 |
668 @override | 668 @override |
669 List<_LinkedNode> computeDependencies() { | 669 List<_LinkNode> computeDependencies() { |
670 Set<_LinkedNode> dependencies = new Set<_LinkedNode>(); | 670 Set<_LinkNode> dependencies = new Set<_LinkNode>(); |
671 | 671 |
672 void appendDependency(String uriStr) { | 672 void appendDependency(String uriStr) { |
673 Uri uri = FastUri.parse(uriStr); | 673 Uri uri = FastUri.parse(uriStr); |
674 if (!uri.hasScheme) { | 674 if (!uri.hasScheme) { |
675 // A relative path in this package, skip it. | 675 // A relative path in this package, skip it. |
676 } else if (uri.scheme == 'dart') { | 676 } else if (uri.scheme == 'dart') { |
677 // Dependency on the SDK is implicit and always added. | 677 // Dependency on the SDK is implicit and always added. |
678 // The SDK linked bundle is precomputed before linking packages. | 678 // The SDK linked bundle is precomputed before linking packages. |
679 } else if (uriStr.startsWith('package:')) { | 679 } else if (uriStr.startsWith('package:')) { |
680 String package = PubSummaryManager.getPackageName(uriStr); | 680 String package = PubSummaryManager.getPackageName(uriStr); |
681 _LinkedNode packageNode = packageToNode[package]; | 681 _LinkNode packageNode = packageToNode[package]; |
682 if (packageNode == null && listedPackages.isListed(uriStr)) { | 682 if (packageNode == null && listedPackages.isListed(uriStr)) { |
683 failed = true; | 683 failed = true; |
684 } | 684 } |
685 if (packageNode != null) { | 685 if (packageNode != null) { |
686 dependencies.add(packageNode); | 686 dependencies.add(packageNode); |
687 } | 687 } |
688 } else { | 688 } else { |
689 failed = true; | 689 failed = true; |
690 } | 690 } |
691 } | 691 } |
(...skipping 13 matching lines...) Expand all Loading... |
705 } | 705 } |
706 | 706 |
707 /** | 707 /** |
708 * Compute the set of existing transitive dependencies for this node. | 708 * Compute the set of existing transitive dependencies for this node. |
709 * If any `package` dependency cannot be resolved, but it is one of the | 709 * If any `package` dependency cannot be resolved, but it is one of the |
710 * [listedPackages] then set [failed] to `true`. | 710 * [listedPackages] then set [failed] to `true`. |
711 * Only [unlinked] is used, so this method can be called before linking. | 711 * Only [unlinked] is used, so this method can be called before linking. |
712 */ | 712 */ |
713 void computeTransitiveDependencies() { | 713 void computeTransitiveDependencies() { |
714 if (transitiveDependencies == null) { | 714 if (transitiveDependencies == null) { |
715 transitiveDependencies = new Set<_LinkedNode>(); | 715 transitiveDependencies = new Set<_LinkNode>(); |
716 | 716 |
717 void appendDependencies(_LinkedNode node) { | 717 void appendDependencies(_LinkNode node) { |
718 if (transitiveDependencies.add(node)) { | 718 if (transitiveDependencies.add(node)) { |
719 node.dependencies.forEach(appendDependencies); | 719 node.dependencies.forEach(appendDependencies); |
720 } | 720 } |
721 } | 721 } |
722 | 722 |
723 appendDependencies(this); | 723 appendDependencies(this); |
724 if (transitiveDependencies.any((node) => node.failed)) { | 724 if (transitiveDependencies.any((node) => node.failed)) { |
725 failed = true; | 725 failed = true; |
726 } | 726 } |
727 } | 727 } |
728 } | 728 } |
729 | 729 |
730 @override | 730 @override |
731 String toString() => package.toString(); | 731 String toString() => package.toString(); |
732 | 732 |
733 /** | 733 /** |
734 * Append names and values of all referenced declared variables (even the | 734 * Append names and values of all referenced declared variables (even the |
735 * ones without actually declared values) to the given [signature]. | 735 * ones without actually declared values) to the given [signature]. |
736 */ | 736 */ |
737 void _appendDeclaredVariables(ApiSignature signature) { | 737 void _appendDeclaredVariables(ApiSignature signature) { |
738 Set<String> nameSet = new Set<String>(); | 738 Set<String> nameSet = new Set<String>(); |
739 for (_LinkedNode node in transitiveDependencies) { | 739 for (_LinkNode node in transitiveDependencies) { |
740 for (UnlinkedUnit unit in node.unlinked.unlinkedUnits) { | 740 for (UnlinkedUnit unit in node.unlinked.unlinkedUnits) { |
741 for (UnlinkedImport import in unit.imports) { | 741 for (UnlinkedImport import in unit.imports) { |
742 for (UnlinkedConfiguration configuration in import.configurations) { | 742 for (UnlinkedConfiguration configuration in import.configurations) { |
743 nameSet.add(configuration.name); | 743 nameSet.add(configuration.name); |
744 } | 744 } |
745 } | 745 } |
746 for (UnlinkedExportPublic export in unit.publicNamespace.exports) { | 746 for (UnlinkedExportPublic export in unit.publicNamespace.exports) { |
747 for (UnlinkedConfiguration configuration in export.configurations) { | 747 for (UnlinkedConfiguration configuration in export.configurations) { |
748 nameSet.add(configuration.name); | 748 nameSet.add(configuration.name); |
749 } | 749 } |
750 } | 750 } |
751 } | 751 } |
752 } | 752 } |
753 List<String> sortedNameList = nameSet.toList()..sort(); | 753 List<String> sortedNameList = nameSet.toList()..sort(); |
754 signature.addInt(sortedNameList.length); | 754 signature.addInt(sortedNameList.length); |
755 for (String name in sortedNameList) { | 755 for (String name in sortedNameList) { |
756 signature.addString(name); | 756 signature.addString(name); |
757 signature.addString(getDeclaredVariable(name) ?? ''); | 757 signature.addString(getDeclaredVariable(name) ?? ''); |
758 } | 758 } |
759 } | 759 } |
760 } | 760 } |
761 | 761 |
762 /** | 762 /** |
763 * Specialization of [DependencyWalker] for linking packages. | 763 * Specialization of [DependencyWalker] for linking packages. |
764 */ | 764 */ |
765 class _LinkedWalker extends DependencyWalker<_LinkedNode> { | 765 class _LinkWalker extends DependencyWalker<_LinkNode> { |
766 final _GetDeclaredVariable getDeclaredVariable; | 766 final _GetDeclaredVariable getDeclaredVariable; |
767 final _ListedPackages listedPackages; | 767 final _ListedPackages listedPackages; |
768 final SummaryDataStore store; | 768 final SummaryDataStore store; |
769 final bool strong; | 769 final bool strong; |
770 | 770 |
771 _LinkedWalker( | 771 _LinkWalker( |
772 this.getDeclaredVariable, this.listedPackages, this.store, this.strong); | 772 this.getDeclaredVariable, this.listedPackages, this.store, this.strong); |
773 | 773 |
774 @override | 774 @override |
775 void evaluate(_LinkedNode node) { | 775 void evaluate(_LinkNode node) { |
776 evaluateScc([node]); | 776 evaluateScc([node]); |
777 } | 777 } |
778 | 778 |
779 @override | 779 @override |
780 void evaluateScc(List<_LinkedNode> scc) { | 780 void evaluateScc(List<_LinkNode> scc) { |
781 Map<String, _LinkedNode> uriToNode = <String, _LinkedNode>{}; | 781 Map<String, _LinkNode> uriToNode = <String, _LinkNode>{}; |
782 for (_LinkedNode node in scc) { | 782 for (_LinkNode node in scc) { |
783 for (String uri in node.unlinked.unlinkedUnitUris) { | 783 for (String uri in node.unlinked.unlinkedUnitUris) { |
784 uriToNode[uri] = node; | 784 uriToNode[uri] = node; |
785 } | 785 } |
786 } | 786 } |
787 Set<String> libraryUris = uriToNode.keys.toSet(); | 787 Set<String> libraryUris = uriToNode.keys.toSet(); |
788 // Perform linking. | 788 // Perform linking. |
789 Map<String, LinkedLibraryBuilder> linkedLibraries = | 789 Map<String, LinkedLibraryBuilder> linkedLibraries = |
790 link(libraryUris, (String uri) { | 790 link(libraryUris, (String uri) { |
791 return store.linkedMap[uri]; | 791 return store.linkedMap[uri]; |
792 }, (String uri) { | 792 }, (String uri) { |
793 return store.unlinkedMap[uri]; | 793 return store.unlinkedMap[uri]; |
794 }, getDeclaredVariable, strong); | 794 }, getDeclaredVariable, strong); |
795 // Assemble linked bundles and put them into the store. | 795 // Assemble linked bundles and put them into the store. |
796 for (_LinkedNode node in scc) { | 796 for (_LinkNode node in scc) { |
797 PackageBundleAssembler assembler = new PackageBundleAssembler(); | 797 PackageBundleAssembler assembler = new PackageBundleAssembler(); |
798 linkedLibraries.forEach((uri, linkedLibrary) { | 798 linkedLibraries.forEach((uri, linkedLibrary) { |
799 if (identical(uriToNode[uri], node)) { | 799 if (identical(uriToNode[uri], node)) { |
800 assembler.addLinkedLibrary(uri, linkedLibrary); | 800 assembler.addLinkedLibrary(uri, linkedLibrary); |
801 } | 801 } |
802 }); | 802 }); |
803 List<int> bytes = assembler.assemble().toBuffer(); | 803 List<int> bytes = assembler.assemble().toBuffer(); |
804 node.linkedNewBytes = bytes; | 804 node.linkedNewBytes = bytes; |
805 node.linked = new PackageBundle.fromBuffer(bytes); | 805 node.linked = new PackageBundle.fromBuffer(bytes); |
806 store.addBundle(null, node.linked); | 806 store.addBundle(null, node.linked); |
(...skipping 18 matching lines...) Expand all Loading... |
825 } | 825 } |
826 | 826 |
827 /** | 827 /** |
828 * Check whether the given `package:` [uri] is listed in the package map. | 828 * Check whether the given `package:` [uri] is listed in the package map. |
829 */ | 829 */ |
830 bool isListed(String uri) { | 830 bool isListed(String uri) { |
831 String package = PubSummaryManager.getPackageName(uri); | 831 String package = PubSummaryManager.getPackageName(uri); |
832 return names.contains(package); | 832 return names.contains(package); |
833 } | 833 } |
834 } | 834 } |
OLD | NEW |