saveUnit static method

Future<void> saveUnit({
  1. required PubspecData pubspecData,
})

Implementation

static Future<void> saveUnit({required PubspecData pubspecData}) async {
  wtLog.startSpinner("Getting ready...");
  final userRepository = UserRepository();
  final dartServerRepository = DartServerRepository();
  await dartServerRepository.initialize();

  final libDirectory = Directory(PathUtils.libPath);
  final testDirectory = Directory(PathUtils.testPath);
  await dartServerRepository.addFilesToRoot(
      [libDirectory.absolute.path, testDirectory.absolute.path]);

  Iterable<FileSystemEntity> fileList = libDirectory
      .listSync(recursive: true)
      .where((element) => element.path.endsWith(".dart"));

  AnalysisContextCollection collection = AnalysisContextCollection(
    includedPaths: [PathUtils.libPath],
    resourceProvider: PhysicalResourceProvider.INSTANCE,
  );

  List<Future> saveFutures = [];
  final testRunInfo = SaveRunInfo.newRun();
  for (var file in fileList) {
    saveFutures.add(
      runSaveUnit(
          file, collection, pubspecData, dartServerRepository, testRunInfo),
    );
  }
  wtLog.startSpinner("Saving Unit Tests");
  await Future.wait(saveFutures);
  String? sessionInfo;
  try {
    //Session Info Failure shouln't abrupt the user experience
    final sessionResult = await userRepository.getSessionInfo('save');
    sessionInfo = sessionResult.info;
  } catch (e) {}
  wtLog.stopSpinner(
      message: '✅ Succeeded. ${testRunInfo.updated} were saved.' +
          (sessionInfo != null ? '\n${sessionInfo}' : ''));
  dartServerRepository.dispose();
}