4
4
5
5
import 'dart:async' ;
6
6
7
+ import 'package:analyzer/dart/analysis/analysis_context.dart' ;
7
8
import 'package:analyzer/dart/analysis/context_root.dart' ;
8
9
import 'package:analyzer/dart/analysis/results.dart' ;
9
10
import 'package:analyzer/dart/ast/ast.dart' ;
@@ -36,6 +37,8 @@ import 'package:path/path.dart' as p show Context;
36
37
abstract class PackageBuilder {
37
38
// Builds package graph to be used by documentation generator.
38
39
Future <PackageGraph > buildPackageGraph ();
40
+
41
+ Future <void > dispose ();
39
42
}
40
43
41
44
/// A package builder that understands pub package format.
@@ -44,12 +47,49 @@ class PubPackageBuilder implements PackageBuilder {
44
47
final PackageMetaProvider _packageMetaProvider;
45
48
final PackageConfigProvider _packageConfigProvider;
46
49
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 ._(
48
85
this ._config,
49
86
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;
53
93
54
94
@override
55
95
Future <PackageGraph > buildPackageGraph () async {
@@ -68,6 +108,7 @@ class PubPackageBuilder implements PackageBuilder {
68
108
_sdk,
69
109
_embedderSdkUris.isNotEmpty,
70
110
_packageMetaProvider,
111
+ _analysisContext,
71
112
);
72
113
await _getLibraries (newGraph);
73
114
runtimeStats.endPerfTask ();
@@ -84,6 +125,12 @@ class PubPackageBuilder implements PackageBuilder {
84
125
return newGraph;
85
126
}
86
127
128
+ @override
129
+ Future <void > dispose () async {
130
+ // Shutdown macro support.
131
+ await _contextCollection.dispose ();
132
+ }
133
+
87
134
late final DartSdk _sdk = _packageMetaProvider.defaultSdk ??
88
135
FolderBasedDartSdk (
89
136
_resourceProvider, _resourceProvider.getFolder (_config.sdkDir));
@@ -123,22 +170,6 @@ class PubPackageBuilder implements PackageBuilder {
123
170
124
171
late final Map <String , List <Folder >> _packageMap;
125
172
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
-
142
173
List <String > get _sdkFilesToDocument => [
143
174
for (var sdkLib in _sdk.sdkLibraries)
144
175
_sdk.mapDartUri (sdkLib.shortName)! .fullName,
@@ -236,6 +267,8 @@ class PubPackageBuilder implements PackageBuilder {
236
267
}
237
268
var resolvedLibrary = await _resolveLibrary (file);
238
269
if (resolvedLibrary == null ) {
270
+ // `file` did not resolve to a _library_; could be a part, an
271
+ // augmentation, or some other invalid result.
239
272
_knownParts.add (file);
240
273
continue ;
241
274
}
@@ -450,8 +483,6 @@ class PubPackageBuilder implements PackageBuilder {
450
483
specialFiles.difference (files),
451
484
addingSpecials: true ,
452
485
);
453
- // Shutdown macro support.
454
- await _contextCollection.dispose ();
455
486
}
456
487
457
488
/// Throws an exception if any configured-to-be-included files were not found
@@ -502,22 +533,29 @@ class DartDocResolvedLibrary {
502
533
extension on Set <String > {
503
534
/// Adds [element] 's path and all of its part files' paths to `this` , and
504
535
/// 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 ) {
515
550
for (var part in element.parts
516
551
.map ((e) => e.uri)
517
552
.whereType <DirectiveUriWithUnit >()) {
518
553
add (part.source.fullName);
519
554
}
520
555
}
556
+ for (var augmentation in element.augmentationImports) {
557
+ addFilesReferencedBy (augmentation.importedAugmentation);
558
+ }
521
559
}
522
560
}
523
561
}
0 commit comments