PipelineStepModel.fromYaml constructor

PipelineStepModel.fromYaml(
  1. Map<String, dynamic> yamlMap
)

Implementation

factory PipelineStepModel.fromYaml(Map<String, dynamic> yamlMap) {
  final stepMap = Map<String, dynamic>.from(yamlMap);
  if (yamlMap['name'] == null || yamlMap['command'] == null) {
    print(
        '❌ Pipeline step is missing required fields. Each step must have "name" and "command".');
    if (yamlMap['name'] == null) {
      print('   → Missing: "name" (a descriptive name for this step)');
    }
    if (yamlMap['command'] == null) {
      print('   → Missing: "command" (the shell command to execute)');
    }
    print('   Step data: $yamlMap');
    exit(1);
  }

  // Parse env map safely
  Map<String, String>? envMap;
  if (stepMap['env'] != null) {
    try {
      envMap = Map<String, String>.from(
        (stepMap['env'] as Map).map(
          (key, value) => MapEntry(key.toString(), value.toString()),
        ),
      );
    } catch (e) {
      print(
          '⚠️ Warning: Step "${yamlMap['name']}" has invalid "env" format. Expected key-value pairs.');
      print('   Example: env: { MY_VAR: "value" }');
    }
  }

  // Parse and validate timeout
  int? timeout;
  if (stepMap['timeout'] != null) {
    timeout = _parseIntSafe(stepMap['timeout'], 'timeout', yamlMap['name']);
    if (timeout != null && timeout <= 0) {
      print(
          '⚠️ Warning: Step "${yamlMap['name']}" has timeout: $timeout — must be a positive number. Ignoring timeout.');
      timeout = null;
    }
  }

  // Parse and validate retry
  int retry = 0;
  if (stepMap['retry'] != null) {
    retry = _parseIntSafe(stepMap['retry'], 'retry', yamlMap['name']) ?? 0;
    if (retry < 0) {
      print(
          '⚠️ Warning: Step "${yamlMap['name']}" has retry: $retry — must be >= 0. Defaulting to 0.');
      retry = 0;
    }
  }

  // Parse and validate retry_delay
  int retryDelay = 5;
  if (stepMap['retry_delay'] != null) {
    retryDelay = _parseIntSafe(
            stepMap['retry_delay'], 'retry_delay', yamlMap['name']) ??
        5;
    if (retryDelay <= 0) {
      print(
          '⚠️ Warning: Step "${yamlMap['name']}" has retry_delay: $retryDelay — must be > 0. Defaulting to 5.');
      retryDelay = 5;
    }
  }

  return PipelineStepModel(
    name: yamlMap['name'],
    command: yamlMap['command'],
    dependsOn: List<String>.from(stepMap['depends_on'] ?? []),
    description: yamlMap['description'],
    uploadOutput: yamlMap['upload_output'] ?? false,
    outputPath: yamlMap['output_path'],
    notifySlack: yamlMap['notify_slack'] ?? false,
    notifyTeams: yamlMap['notify_teams'] ?? false,
    stopOnFailure: yamlMap['stop_on_failure'] ?? true,
    customExitCondition: yamlMap['custom_exit_condition'],
    env: envMap,
    workingDirectory: yamlMap['working_directory'],
    timeout: timeout,
    retry: retry,
    retryDelay: retryDelay,
    continueOnError: yamlMap['continue_on_error'] ?? false,
    allowFailure: yamlMap['allow_failure'] ?? false,
    condition: yamlMap['condition'],
  );
}