executeScript method

TaskEither<SlidyError, String> executeScript(
  1. String command,
  2. SlidyPipelineV1 pipeline
)

Implementation

TaskEither<SlidyError, String> executeScript(String command, SlidyPipelineV1 pipeline) {
  return TaskEither(() async {
    var script = pipeline.scripts[command];
    if (script == null) {
      return Left(SlidyError('Command not found'));
    }

    var resolvedScript = script.copyWith(
      name: resolveVariables(script.name, pipeline).getOrElse((l) => script.name),
      description: resolveVariables(script.description, pipeline).getOrElse((l) => script.description),
      workingDirectory: resolveVariables(script.workingDirectory, pipeline).getOrElse((l) => script.workingDirectory),
    );

    final steps = resolvedScript.steps.map((step) {
      final stepDir = resolveVariables(step.workingDirectory, pipeline).getOrElse((l) => script.workingDirectory);
      final workingDirectory = resolveWorkingDirectory(resolvedScript.workingDirectory, stepDir);
      return step.copyWith(
        workingDirectory: workingDirectory,
        environment: {
          ...script.environment ?? {},
          ...step.environment ?? {},
        },
      );
    }).toList();

    resolvedScript = resolvedScript.copyWith(steps: steps);

    print('Script:  ${output.green(script.name)}');
    if (script.description.isNotEmpty) {
      print('Description:  ${output.green(script.description)}');
    }
    print('\n---------------- STEPS --------------\n');

    for (var step in resolvedScript.steps) {
      final pipelineVarUpdate = pipeline.copyWith(
        systemVariables: {
          ...Platform.environment,
          ...step.environment ?? {},
        },
      );
      step = step.copyWith(
        name: step.name == null ? null : resolveVariables(step.name!, pipelineVarUpdate).getOrElse((l) => step.name!),
        description: resolveVariables(step.description, pipelineVarUpdate).getOrElse((l) => step.description),
        run: resolveVariables(step.run, pipelineVarUpdate).getOrElse((l) => step.run),
        condition: step.condition == null ? null : resolveVariables(step.condition!, pipelineVarUpdate).getOrElse((l) => step.condition!),
      );

      if (!conditionEval.call(step.condition)) {
        output.msg('Step: ${step.name} condition false');
        print('\n----------- END STEP ----------\n');
        continue;
      }

      if (step.name != null) {
        print('Step:  ${output.green(step.name!)}');
      }
      if (step.description.isNotEmpty) {
        print('Description:  ${output.green(step.description)}');
      }
      if (step.name != null || step.description.isNotEmpty) {
        print('\n');
      }
      final result = await executor(step);
      if (!result) {
        return Left(SlidyError('Step \'${step.name}\' failure.'));
      }
      print('\n----------- END STEP ----------\n');
    }
    return Right('Success!');
  });
}