run method

  1. @override
Future? run()
override

Executes the run command to process distribution tasks.

This method performs the following operations:

  • Parses the configuration file specified by --config option
  • Filters tasks and jobs based on the --operation key
  • Executes build operations for each selected job
  • Executes publish operations for each selected job
  • Logs progress and results throughout the process

The execution can be filtered by operation key:

  • No operation key: Runs all tasks and jobs
  • Task key only: Runs all jobs within that task
  • Task.Job key: Runs only the specific job

Implementation

@override
Future? run() async {
  final configParser = await configParserBuilder();
  if (configParser == null) {
    logger.logError("Configuration file is not valid or not found.");
    exit(1);
  }
  try {
    logger.logInfo(
      'Running application with configuration: ${argResults!['config'] as String}',
    );
    logger.logInfo("======= Running tasks =======");
    for (var task in configParser.tasks) {
      logger.logInfo(task.name);
      if (task.description != null) logger.logInfo('${task.description}');
      logger.logEmpty();
      List<Job> jobs = List.from(task.jobs);
      if (operationKey.isEmpty) {
        if (task.workflows != null) {
          logger.logInfo(
            "Workflows:\n${task.workflows?.map((e) => "- $e").join("\n")}",
          );
          logger.logEmpty();
          jobs = [];
          for (var workflow in task.workflows!) {
            final job = task.jobs
                .where((job) => job.key == workflow)
                .firstOrNull;
            if (job != null) {
              jobs.add(job);
            } else {
              logger.logError("No job found with the key: $workflow");
              exit(1);
            }
          }
        }
      }
      List<int> results = [];
      for (var value in jobs) {
        logger.logInfo(
          "[Job] : ${value.name} ${value.description != null ? "(${value.description})" : ""}",
        );

        /// Check if both builder and publisher are null
        if (value.builder == null && value.publisher == null) {
          logger.logEmpty();
          logger.logError(
            "No builder or publisher found for the job: ${value.name}",
          );
          break;
        }

        //Run Builder Job
        if (value.builder != null) {
          results.add(
            await runBuilder(value.builder!, configParser).then((value) {
              if (value != 0) {
                logger.logError("Build failed with exit code: $value");
              }
              return value;
            }),
          );
        }

        //Run Publisher Job
        if (value.publisher != null) {
          results.add(
            await runPublisher(value.publisher!, configParser).then((value) {
              if (value != 0) {
                if (logger.isVerbose) {
                  logger.logInfo("Publish failed with exit code: $value");
                } else {
                  logger.logInfo(
                    "Publish failed with exit code: $value, details is available in distribution.log",
                  );
                }
              }
              return value;
            }),
          );
        }
      }
      if (results.isEmpty) {
        logger.logError("No results found for the task: ${task.name}");
        continue;
      }
      if (results.any((element) => element != 0)) {
        logger.logError(
          "Task ${task.name} failed with exit code: ${results.reduce((value, element) => value + element)}",
        );
        continue;
      }
      logger.logSuccess("==============================");
    }
  } catch (e, s) {
    logger.logError('Error: $e\n$s');
    logger.logError('Please check your configuration file and try again.');
  } finally {
    logger.logSuccess("======= Finished tasks =======");
  }

  return;
}