runSaveUnit static method

Future<void> runSaveUnit(
  1. FileSystemEntity file,
  2. AnalysisContextCollection collection,
  3. PubspecData pubspecData,
  4. DartServerRepository dartServerRepository,
  5. TestRunInfo testRunInfo,
)

Implementation

static Future<void> runSaveUnit(
  FileSystemEntity file,
  AnalysisContextCollection collection,
  PubspecData pubspecData,
  DartServerRepository dartServerRepository,
  TestRunInfo testRunInfo,
) async {
  try {
    final resolvedUnit = await file.resolveFile(collection);
    CompilationUnit unit = resolvedUnit.unit;

    final saver = UnitSaver(
        dartServerRepository: dartServerRepository,
        cacheManager: CacheManager());
    saver.init();

    List<Future> saveFutures = [];

    for (final declaration
        in unit.declarations.whereType<ClassDeclaration>()) {
      final element = declaration.declaredElement!;

      if (!isAnnotationPresent(element)) continue;

      if (element.checkIfAWidgetOrState) continue;

      saveFutures.add(saver.saveForClass(
          classElement: element,
          annotation: element.metadata.firstWhere((element) =>
              element.element!.displayName ==
              Welltested().runtimeType.toString()),
          pubspecData: pubspecData,
          classAstNode: declaration,
          compilationUnit: unit,
          testRunInfo: testRunInfo,
          classFile: file as File));
    }
    for (final declaration
        in unit.declarations.whereType<ExtensionDeclaration>()) {
      final element = declaration.declaredElement!;

      if (!isAnnotationPresent(element)) continue;

      saveFutures.add(saver.saveForExtension(
          compilationUnit: unit,
          annotation: element.metadata.firstWhere((element) =>
              element.element!.displayName ==
              Welltested().runtimeType.toString()),
          extAstNode: declaration,
          extElement: element,
          pubspecData: pubspecData,
          extFile: file as File,
          testRunInfo: testRunInfo));
    }
    for (final declaration
        in unit.declarations.whereType<FunctionDeclaration>()) {
      if (!isAnnotationPresent(declaration.declaredElement as Element))
        continue;
      if (!(declaration.declaredElement != null &&
          declaration.declaredElement is FunctionElement)) {
        saveFutures.add(saver.saveForGlobalExecutableElement(
          methodElement: declaration.declaredElement!,
          methodAstNode: declaration,
          compilationUnit: unit,
          annotation: declaration.declaredElement!.metadata.firstWhere(
              (element) =>
                  element.element!.displayName ==
                  Welltested().runtimeType.toString()),
          pubspecData: pubspecData,
          methodFile: file as File,
          testRunInfo: testRunInfo,
        ));
        continue;
      }
      final element = declaration.declaredElement! as FunctionElement;
      saveFutures.add(saver.saveForGlobalMethod(
        methodElement: element,
        methodAstNode: declaration,
        compilationUnit: unit,
        annotation: element.metadata.firstWhere((element) =>
            element.element!.displayName ==
            Welltested().runtimeType.toString()),
        pubspecData: pubspecData,
        methodFile: file as File,
        testRunInfo: testRunInfo,
      ));
    }
    await Future.wait(saveFutures);
  } catch (e, stackTrace) {
    wtTelemetry.trackError(
        severity: Severity.error, error: e, stackTrace: stackTrace);
    wtLog.error('Unable to resolve file ${file.path}', verbose: true);
  }
}