Skip to content

Commit 08b2c10

Browse files
authored
Reduce use of generators in finding files to document (#3961)
1 parent 5e26cb6 commit 08b2c10

File tree

1 file changed

+60
-63
lines changed

1 file changed

+60
-63
lines changed

lib/src/model/package_builder.dart

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,8 @@ class PubPackageBuilder implements PackageBuilder {
227227
/// Uses [processedLibraries] to prevent calling [addLibrary] more than once
228228
/// with the same [LibraryElement]. Adds each [LibraryElement] found to
229229
/// [processedLibraries].
230-
///
231-
/// [addingSpecials] indicates that only [SpecialClass]es are being resolved
232-
/// in this round.
233-
Future<void> _discoverLibraries(
234-
void Function(DartDocResolvedLibrary) addLibrary,
235-
Set<LibraryElement> processedLibraries,
236-
Set<String> files) async {
230+
Future<void> _discoverLibraries(PackageGraph uninitializedPackageGraph,
231+
Set<LibraryElement> processedLibraries, Set<String> files) async {
237232
files = {...files};
238233
// Discover Dart libraries in a loop. In each iteration of the loop, we take
239234
// a set of files (starting with the ones passed into the function), resolve
@@ -288,7 +283,7 @@ class PubPackageBuilder implements PackageBuilder {
288283
if (processedLibraries.contains(resolvedLibrary.element)) {
289284
continue;
290285
}
291-
addLibrary(resolvedLibrary);
286+
uninitializedPackageGraph.addLibraryToGraph(resolvedLibrary);
292287
processedLibraries.add(resolvedLibrary.element);
293288
}
294289
files.addAll(newFiles);
@@ -305,17 +300,13 @@ class PubPackageBuilder implements PackageBuilder {
305300
// (so we can generate the right hyperlinks), it's vital that we add all
306301
// libraries in dependent packages. So if the analyzer discovers some
307302
// files in a package we haven't seen yet, add files for that package.
308-
for (var meta in packages.difference(knownPackages)) {
309-
if (meta.isSdk) {
303+
for (var packageMeta in packages.difference(knownPackages)) {
304+
if (packageMeta.isSdk) {
310305
if (!_skipUnreachableSdkLibraries) {
311306
files.addAll(_sdkFilesToDocument);
312307
}
313308
} else {
314-
files.addAll(await _findFilesToDocumentInPackage(
315-
meta.dir.path,
316-
includeDependencies: false,
317-
filterExcludes: false,
318-
).toList());
309+
files.addAll(_findFilesToDocumentInPackage({packageMeta.dir.path}));
319310
}
320311
}
321312
knownPackages.addAll(packages);
@@ -325,40 +316,19 @@ class PubPackageBuilder implements PackageBuilder {
325316

326317
/// Returns all top level library files in the 'lib/' directory of the given
327318
/// package root directory.
328-
///
329-
/// If [includeDependencies], then all top level library files in the 'lib/'
330-
/// directory of every package in [basePackageDir]'s package config are also
331-
/// included.
332-
Stream<String> _findFilesToDocumentInPackage(
333-
String basePackageDir, {
334-
required bool includeDependencies,
335-
required bool filterExcludes,
336-
}) async* {
337-
var packageDirs = {basePackageDir};
338-
339-
if (includeDependencies) {
340-
var packageConfig = (await _packageConfigProvider
341-
.findPackageConfig(_resourceProvider.getFolder(basePackageDir)))!;
342-
for (var package in packageConfig.packages) {
343-
if (filterExcludes && _config.exclude.contains(package.name)) {
344-
continue;
345-
}
346-
packageDirs.add(_pathContext.dirname(
347-
_pathContext.fromUri(packageConfig[package.name]!.packageUriRoot)));
348-
}
349-
}
350-
319+
List<String> _findFilesToDocumentInPackage(Set<String> packageRoots) {
351320
var sep = _pathContext.separator;
352321
var packagesWithSeparators = '${sep}packages$sep';
353-
for (var packageDir in packageDirs) {
354-
var packageLibDir = _pathContext.join(packageDir, 'lib');
322+
var filesToDocument = <String>[];
323+
for (var packageRoot in packageRoots) {
324+
var packageLibDir = _pathContext.join(packageRoot, 'lib');
355325
var packageLibSrcDir = _pathContext.join(packageLibDir, 'src');
356326
var packageDirContainsPackages =
357-
packageDir.contains(packagesWithSeparators);
327+
packageRoot.contains(packagesWithSeparators);
358328
// To avoid analyzing package files twice, only files with paths not
359329
// containing '/packages/' will be added. The only exception is if the
360330
// file to analyze already has a '/packages/' in its path.
361-
for (var filePath in _listDir(packageDir, const {})) {
331+
for (var filePath in _listDir(packageRoot, const {})) {
362332
if (!filePath.endsWith('.dart')) continue;
363333
if (!packageDirContainsPackages &&
364334
filePath.contains(packagesWithSeparators)) {
@@ -373,30 +343,32 @@ class PubPackageBuilder implements PackageBuilder {
373343
continue;
374344
}
375345

376-
yield filePath;
346+
filesToDocument.add(filePath);
377347
}
378348
}
349+
return filesToDocument;
379350
}
380351

381352
/// Lists the files in [directory].
382353
///
383354
/// Excludes files and directories beginning with `.`.
384355
///
385356
/// The returned paths are guaranteed to begin with [directory].
386-
Iterable<String> _listDir(
387-
String directory, Set<String> listedDirectories) sync* {
357+
List<String> _listDir(String directory, Set<String> listedDirectories) {
388358
// Avoid recursive symlinks.
389359
var resolvedPath =
390360
_resourceProvider.getFolder(directory).resolveSymbolicLinksSync().path;
391361
if (listedDirectories.contains(resolvedPath)) {
392-
return;
362+
return const [];
393363
}
394364

395365
listedDirectories = {
396366
...listedDirectories,
397367
resolvedPath,
398368
};
399369

370+
var dirs = <String>[];
371+
400372
for (var resource
401373
in _packageDirList(_resourceProvider.getFolder(directory))) {
402374
// Skip hidden files and directories.
@@ -405,13 +377,15 @@ class PubPackageBuilder implements PackageBuilder {
405377
}
406378

407379
if (resource is File) {
408-
yield resource.path;
380+
dirs.add(resource.path);
409381
continue;
410382
}
411383
if (resource is Folder) {
412-
yield* _listDir(resource.path, listedDirectories);
384+
dirs.addAll(_listDir(resource.path, listedDirectories));
413385
}
414386
}
387+
388+
return dirs;
415389
}
416390

417391
/// Calculates 'includeExternal' based on a list of files.
@@ -434,22 +408,45 @@ class PubPackageBuilder implements PackageBuilder {
434408
/// This takes into account the 'auto-include-dependencies' option, the
435409
/// 'exclude' option, and the 'include-external' option.
436410
Future<Set<String>> _getFilesToDocument() async {
437-
var files = _config.topLevelPackageMeta.isSdk
438-
? _sdkFilesToDocument
439-
: await _findFilesToDocumentInPackage(
440-
_config.inputDir,
441-
includeDependencies: _config.autoIncludeDependencies,
442-
filterExcludes: true,
443-
).toList();
444-
var externals = _includedExternalsFrom(files);
445-
if (externals.isNotEmpty) {
446-
includeExternalsWasSpecified = true;
411+
if (_config.topLevelPackageMeta.isSdk) {
412+
return _sdkFilesToDocument
413+
.map((s) => _pathContext.absolute(_resourceProvider.getFile(s).path))
414+
.toSet();
415+
} else {
416+
var packagesToDocument = await _findPackagesToDocument(
417+
_config.inputDir,
418+
);
419+
var files = _findFilesToDocumentInPackage(packagesToDocument).toList();
420+
var externals = _includedExternalsFrom(files);
421+
if (externals.isNotEmpty) {
422+
includeExternalsWasSpecified = true;
423+
files = [...files, ...externals];
424+
}
425+
return {
426+
...files.map(
427+
(s) => _pathContext.absolute(_resourceProvider.getFile(s).path)),
428+
..._embedderSdkFiles,
429+
};
447430
}
448-
files = [...files, ...externals];
431+
}
432+
433+
/// Returns a set of package roots that are to be documented.
434+
///
435+
/// If `_config.autoIncludeDependencies` is `true`, then every package in
436+
/// [basePackageRoot]'s package config is included.
437+
Future<Set<String>> _findPackagesToDocument(String basePackageRoot) async {
438+
if (!_config.autoIncludeDependencies) {
439+
return {basePackageRoot};
440+
}
441+
442+
var packageConfig = (await _packageConfigProvider
443+
.findPackageConfig(_resourceProvider.getFolder(basePackageRoot)))!;
449444
return {
450-
...files
451-
.map((s) => _pathContext.absolute(_resourceProvider.getFile(s).path)),
452-
..._embedderSdkFiles,
445+
basePackageRoot,
446+
for (var package in packageConfig.packages)
447+
if (!_config.exclude.contains(package.name))
448+
_pathContext.dirname(_pathContext
449+
.fromUri(packageConfig[package.name]!.packageUriRoot)),
453450
};
454451
}
455452

@@ -477,7 +474,7 @@ class PubPackageBuilder implements PackageBuilder {
477474
logInfo('Discovering libraries...');
478475
var foundLibraries = <LibraryElement>{};
479476
await _discoverLibraries(
480-
uninitializedPackageGraph.addLibraryToGraph,
477+
uninitializedPackageGraph,
481478
foundLibraries,
482479
files,
483480
);

0 commit comments

Comments
 (0)