runCliAnalysis method

Future<Iterable<UnusedFilesFileReport>> runCliAnalysis(
  1. Iterable<String> folders,
  2. String rootFolder,
  3. UnusedFilesConfig config, {
  4. String? sdkPath,
})

Returns a list of unused files reports for analyzing all files in the given folders. The analysis is configured with the config.

Implementation

Future<Iterable<UnusedFilesFileReport>> runCliAnalysis(
  Iterable<String> folders,
  String rootFolder,
  UnusedFilesConfig config, {
  String? sdkPath,
}) async {
  final collection =
      createAnalysisContextCollection(folders, rootFolder, sdkPath);

  final unusedFiles = <String>{};

  for (final context in collection.contexts) {
    final unusedFilesAnalysisConfig =
        _getAnalysisConfig(context, rootFolder, config);

    final filePaths = getFilePaths(
      folders,
      context,
      rootFolder,
      unusedFilesAnalysisConfig.globalExcludes,
    );

    unusedFiles.addAll(filePaths);

    final analyzedFiles =
        filePaths.intersection(context.contextRoot.analyzedFiles().toSet());
    for (final filePath in analyzedFiles) {
      _logger?.infoVerbose('Analyzing $filePath');

      final unit = await context.currentSession.getResolvedUnit(filePath);
      unusedFiles.removeAll(_analyzeFile(filePath, unit, config.isMonorepo));
    }
  }

  return unusedFiles.map((path) {
    final relativePath = relative(path, from: rootFolder);

    return UnusedFilesFileReport(
      path: path,
      relativePath: relativePath,
    );
  }).toSet();
}