44
55import 'dart:async' ;
66
7+ import 'package:analyzer/dart/analysis/analysis_context.dart' ;
78import 'package:analyzer/dart/analysis/context_root.dart' ;
89import 'package:analyzer/dart/analysis/results.dart' ;
910import 'package:analyzer/dart/ast/ast.dart' ;
@@ -36,6 +37,8 @@ import 'package:path/path.dart' as p show Context;
3637abstract class PackageBuilder {
3738 // Builds package graph to be used by documentation generator.
3839 Future <PackageGraph > buildPackageGraph ();
40+
41+ Future <void > dispose ();
3942}
4043
4144/// A package builder that understands pub package format.
@@ -44,12 +47,49 @@ class PubPackageBuilder implements PackageBuilder {
4447 final PackageMetaProvider _packageMetaProvider;
4548 final PackageConfigProvider _packageConfigProvider;
4649
47- PubPackageBuilder (
50+ final AnalysisContextCollectionImpl _contextCollection;
51+ final AnalysisContext _analysisContext;
52+
53+ factory PubPackageBuilder (
54+ DartdocOptionContext config,
55+ PackageMetaProvider packageMetaProvider,
56+ PackageConfigProvider packageConfigProvider, {
57+ @visibleForTesting bool skipUnreachableSdkLibraries = false ,
58+ }) {
59+ var contextCollection = AnalysisContextCollectionImpl (
60+ includedPaths: [config.inputDir],
61+ // TODO(jcollins-g): should we pass excluded directories here instead
62+ // of handling it ourselves?
63+ resourceProvider: packageMetaProvider.resourceProvider,
64+ sdkPath: config.sdkDir,
65+ updateAnalysisOptions2: ({
66+ required AnalysisOptionsImpl analysisOptions,
67+ required ContextRoot contextRoot,
68+ required DartSdk sdk,
69+ }) =>
70+ analysisOptions
71+ ..warning = false
72+ ..lint = false ,
73+ );
74+ return PubPackageBuilder ._(
75+ config,
76+ packageMetaProvider,
77+ packageConfigProvider,
78+ contextCollection,
79+ analysisContext: contextCollection.contextFor (config.inputDir),
80+ skipUnreachableSdkLibraries: skipUnreachableSdkLibraries,
81+ );
82+ }
83+
84+ PubPackageBuilder ._(
4885 this ._config,
4986 this ._packageMetaProvider,
50- this ._packageConfigProvider, {
51- @visibleForTesting bool skipUnreachableSdkLibraries = false ,
52- }) : _skipUnreachableSdkLibraries = skipUnreachableSdkLibraries;
87+ this ._packageConfigProvider,
88+ this ._contextCollection, {
89+ required AnalysisContext analysisContext,
90+ required bool skipUnreachableSdkLibraries,
91+ }) : _analysisContext = analysisContext,
92+ _skipUnreachableSdkLibraries = skipUnreachableSdkLibraries;
5393
5494 @override
5595 Future <PackageGraph > buildPackageGraph () async {
@@ -68,6 +108,7 @@ class PubPackageBuilder implements PackageBuilder {
68108 _sdk,
69109 _embedderSdkUris.isNotEmpty,
70110 _packageMetaProvider,
111+ _analysisContext,
71112 );
72113 await _getLibraries (newGraph);
73114 runtimeStats.endPerfTask ();
@@ -84,6 +125,12 @@ class PubPackageBuilder implements PackageBuilder {
84125 return newGraph;
85126 }
86127
128+ @override
129+ Future <void > dispose () async {
130+ // Shutdown macro support.
131+ await _contextCollection.dispose ();
132+ }
133+
87134 late final DartSdk _sdk = _packageMetaProvider.defaultSdk ??
88135 FolderBasedDartSdk (
89136 _resourceProvider, _resourceProvider.getFolder (_config.sdkDir));
@@ -123,22 +170,6 @@ class PubPackageBuilder implements PackageBuilder {
123170
124171 late final Map <String , List <Folder >> _packageMap;
125172
126- late final _contextCollection = AnalysisContextCollectionImpl (
127- includedPaths: [_config.inputDir],
128- // TODO(jcollins-g): should we pass excluded directories here instead of
129- // handling it ourselves?
130- resourceProvider: _resourceProvider,
131- sdkPath: _config.sdkDir,
132- updateAnalysisOptions2: ({
133- required AnalysisOptionsImpl analysisOptions,
134- required ContextRoot contextRoot,
135- required DartSdk sdk,
136- }) =>
137- analysisOptions
138- ..warning = false
139- ..lint = false ,
140- );
141-
142173 List <String > get _sdkFilesToDocument => [
143174 for (var sdkLib in _sdk.sdkLibraries)
144175 _sdk.mapDartUri (sdkLib.shortName)! .fullName,
@@ -236,6 +267,8 @@ class PubPackageBuilder implements PackageBuilder {
236267 }
237268 var resolvedLibrary = await _resolveLibrary (file);
238269 if (resolvedLibrary == null ) {
270+ // `file` did not resolve to a _library_; could be a part, an
271+ // augmentation, or some other invalid result.
239272 _knownParts.add (file);
240273 continue ;
241274 }
@@ -450,8 +483,6 @@ class PubPackageBuilder implements PackageBuilder {
450483 specialFiles.difference (files),
451484 addingSpecials: true ,
452485 );
453- // Shutdown macro support.
454- await _contextCollection.dispose ();
455486 }
456487
457488 /// Throws an exception if any configured-to-be-included files were not found
@@ -502,22 +533,29 @@ class DartDocResolvedLibrary {
502533extension on Set <String > {
503534 /// Adds [element] 's path and all of its part files' paths to `this` , and
504535 /// recursively adds the paths of all imported and exported libraries.
505- void addFilesReferencedBy (LibraryElement ? element) {
506- if (element != null ) {
507- var path = element.source.fullName;
508- if (add (path)) {
509- for (var import in element.libraryImports) {
510- addFilesReferencedBy (import.importedLibrary);
511- }
512- for (var export in element.libraryExports) {
513- addFilesReferencedBy (export.exportedLibrary);
514- }
536+ void addFilesReferencedBy (LibraryOrAugmentationElement ? element) {
537+ if (element == null ) return ;
538+
539+ var path = element.source? .fullName;
540+ if (path == null ) return ;
541+
542+ if (add (path)) {
543+ for (var import in element.libraryImports) {
544+ addFilesReferencedBy (import.importedLibrary);
545+ }
546+ for (var export in element.libraryExports) {
547+ addFilesReferencedBy (export.exportedLibrary);
548+ }
549+ if (element is LibraryElement ) {
515550 for (var part in element.parts
516551 .map ((e) => e.uri)
517552 .whereType <DirectiveUriWithUnit >()) {
518553 add (part.source.fullName);
519554 }
520555 }
556+ for (var augmentation in element.augmentationImports) {
557+ addFilesReferencedBy (augmentation.importedAugmentation);
558+ }
521559 }
522560 }
523561}
0 commit comments