Skip to content

Commit 70a811a

Browse files
committed
Reduce use of generators in finding files to document
1 parent e129586 commit 70a811a

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
@@ -222,13 +222,8 @@ class PubPackageBuilder implements PackageBuilder {
222222
/// Uses [processedLibraries] to prevent calling [addLibrary] more than once
223223
/// with the same [LibraryElement]. Adds each [LibraryElement] found to
224224
/// [processedLibraries].
225-
///
226-
/// [addingSpecials] indicates that only [SpecialClass]es are being resolved
227-
/// in this round.
228-
Future<void> _discoverLibraries(
229-
void Function(DartDocResolvedLibrary) addLibrary,
230-
Set<LibraryElement> processedLibraries,
231-
Set<String> files) async {
225+
Future<void> _discoverLibraries(PackageGraph uninitializedPackageGraph,
226+
Set<LibraryElement> processedLibraries, Set<String> files) async {
232227
files = {...files};
233228
// Discover Dart libraries in a loop. In each iteration of the loop, we take
234229
// a set of files (starting with the ones passed into the function), resolve
@@ -283,7 +278,7 @@ class PubPackageBuilder implements PackageBuilder {
283278
if (processedLibraries.contains(resolvedLibrary.element)) {
284279
continue;
285280
}
286-
addLibrary(resolvedLibrary);
281+
uninitializedPackageGraph.addLibraryToGraph(resolvedLibrary);
287282
processedLibraries.add(resolvedLibrary.element);
288283
}
289284
files.addAll(newFiles);
@@ -300,17 +295,13 @@ class PubPackageBuilder implements PackageBuilder {
300295
// (so we can generate the right hyperlinks), it's vital that we add all
301296
// libraries in dependent packages. So if the analyzer discovers some
302297
// files in a package we haven't seen yet, add files for that package.
303-
for (var meta in packages.difference(knownPackages)) {
304-
if (meta.isSdk) {
298+
for (var packageMeta in packages.difference(knownPackages)) {
299+
if (packageMeta.isSdk) {
305300
if (!_skipUnreachableSdkLibraries) {
306301
files.addAll(_sdkFilesToDocument);
307302
}
308303
} else {
309-
files.addAll(await _findFilesToDocumentInPackage(
310-
meta.dir.path,
311-
includeDependencies: false,
312-
filterExcludes: false,
313-
).toList());
304+
files.addAll(_findFilesToDocumentInPackage({packageMeta.dir.path}));
314305
}
315306
}
316307
knownPackages.addAll(packages);
@@ -320,40 +311,19 @@ class PubPackageBuilder implements PackageBuilder {
320311

321312
/// Returns all top level library files in the 'lib/' directory of the given
322313
/// package root directory.
323-
///
324-
/// If [includeDependencies], then all top level library files in the 'lib/'
325-
/// directory of every package in [basePackageDir]'s package config are also
326-
/// included.
327-
Stream<String> _findFilesToDocumentInPackage(
328-
String basePackageDir, {
329-
required bool includeDependencies,
330-
required bool filterExcludes,
331-
}) async* {
332-
var packageDirs = {basePackageDir};
333-
334-
if (includeDependencies) {
335-
var packageConfig = (await _packageConfigProvider
336-
.findPackageConfig(_resourceProvider.getFolder(basePackageDir)))!;
337-
for (var package in packageConfig.packages) {
338-
if (filterExcludes && _config.exclude.contains(package.name)) {
339-
continue;
340-
}
341-
packageDirs.add(_pathContext.dirname(
342-
_pathContext.fromUri(packageConfig[package.name]!.packageUriRoot)));
343-
}
344-
}
345-
314+
List<String> _findFilesToDocumentInPackage(Set<String> packageRoots) {
346315
var sep = _pathContext.separator;
347316
var packagesWithSeparators = '${sep}packages$sep';
348-
for (var packageDir in packageDirs) {
349-
var packageLibDir = _pathContext.join(packageDir, 'lib');
317+
var filesToDocument = <String>[];
318+
for (var packageRoot in packageRoots) {
319+
var packageLibDir = _pathContext.join(packageRoot, 'lib');
350320
var packageLibSrcDir = _pathContext.join(packageLibDir, 'src');
351321
var packageDirContainsPackages =
352-
packageDir.contains(packagesWithSeparators);
322+
packageRoot.contains(packagesWithSeparators);
353323
// To avoid analyzing package files twice, only files with paths not
354324
// containing '/packages/' will be added. The only exception is if the
355325
// file to analyze already has a '/packages/' in its path.
356-
for (var filePath in _listDir(packageDir, const {})) {
326+
for (var filePath in _listDir(packageRoot, const {})) {
357327
if (!filePath.endsWith('.dart')) continue;
358328
if (!packageDirContainsPackages &&
359329
filePath.contains(packagesWithSeparators)) {
@@ -368,30 +338,32 @@ class PubPackageBuilder implements PackageBuilder {
368338
continue;
369339
}
370340

371-
yield filePath;
341+
filesToDocument.add(filePath);
372342
}
373343
}
344+
return filesToDocument;
374345
}
375346

376347
/// Lists the files in [directory].
377348
///
378349
/// Excludes files and directories beginning with `.`.
379350
///
380351
/// The returned paths are guaranteed to begin with [directory].
381-
Iterable<String> _listDir(
382-
String directory, Set<String> listedDirectories) sync* {
352+
List<String> _listDir(String directory, Set<String> listedDirectories) {
383353
// Avoid recursive symlinks.
384354
var resolvedPath =
385355
_resourceProvider.getFolder(directory).resolveSymbolicLinksSync().path;
386356
if (listedDirectories.contains(resolvedPath)) {
387-
return;
357+
return const [];
388358
}
389359

390360
listedDirectories = {
391361
...listedDirectories,
392362
resolvedPath,
393363
};
394364

365+
var dirs = <String>[];
366+
395367
for (var resource
396368
in _packageDirList(_resourceProvider.getFolder(directory))) {
397369
// Skip hidden files and directories.
@@ -400,13 +372,15 @@ class PubPackageBuilder implements PackageBuilder {
400372
}
401373

402374
if (resource is File) {
403-
yield resource.path;
375+
dirs.add(resource.path);
404376
continue;
405377
}
406378
if (resource is Folder) {
407-
yield* _listDir(resource.path, listedDirectories);
379+
dirs.addAll(_listDir(resource.path, listedDirectories));
408380
}
409381
}
382+
383+
return dirs;
410384
}
411385

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

@@ -472,7 +469,7 @@ class PubPackageBuilder implements PackageBuilder {
472469
logInfo('Discovering libraries...');
473470
var foundLibraries = <LibraryElement>{};
474471
await _discoverLibraries(
475-
uninitializedPackageGraph.addLibraryToGraph,
472+
uninitializedPackageGraph,
476473
foundLibraries,
477474
files,
478475
);

0 commit comments

Comments
 (0)