getEngineStepsToTopLevelCodemod creates a codemod for moving engine.steps to the top-level steps field
()
| 10 | |
| 11 | // getEngineStepsToTopLevelCodemod creates a codemod for moving engine.steps to the top-level steps field |
| 12 | func getEngineStepsToTopLevelCodemod() Codemod { |
| 13 | return Codemod{ |
| 14 | ID: "engine-steps-to-top-level", |
| 15 | Name: "Move engine.steps to top-level steps", |
| 16 | Description: "Moves the 'steps' field from under 'engine' to the top-level 'steps' field, as 'engine.steps' is no longer supported", |
| 17 | IntroducedIn: "0.11.0", |
| 18 | Apply: func(content string, frontmatter map[string]any) (string, bool, error) { |
| 19 | // Check if engine.steps exists in frontmatter |
| 20 | engineValue, hasEngine := frontmatter["engine"] |
| 21 | if !hasEngine { |
| 22 | return content, false, nil |
| 23 | } |
| 24 | |
| 25 | engineMap, isMap := engineValue.(map[string]any) |
| 26 | if !isMap { |
| 27 | // engine is a string, no steps to move |
| 28 | return content, false, nil |
| 29 | } |
| 30 | |
| 31 | if _, hasSteps := engineMap["steps"]; !hasSteps { |
| 32 | return content, false, nil |
| 33 | } |
| 34 | |
| 35 | // Determine if existing top-level steps is a sequence |
| 36 | hasTopLevelSteps := false |
| 37 | if stepsVal, exists := frontmatter["steps"]; exists { |
| 38 | if _, isSlice := stepsVal.([]any); isSlice { |
| 39 | hasTopLevelSteps = true |
| 40 | engineStepsCodemodLog.Print("Found existing top-level 'steps'") |
| 41 | } else { |
| 42 | engineStepsCodemodLog.Print("Top-level 'steps' exists but is not a sequence; treating as absent") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return applyFrontmatterLineTransform(content, func(frontmatterLines []string) ([]string, bool) { |
| 47 | // Find engine block and the steps field within it |
| 48 | engineIndent := "" |
| 49 | stepsStartIdx := -1 |
| 50 | inEngineBlock := false |
| 51 | |
| 52 | for i, line := range frontmatterLines { |
| 53 | trimmed := strings.TrimSpace(line) |
| 54 | |
| 55 | if isTopLevelKey(line) && strings.HasPrefix(trimmed, "engine:") { |
| 56 | engineIndent = getIndentation(line) |
| 57 | inEngineBlock = true |
| 58 | engineStepsCodemodLog.Printf("Found 'engine:' block at line %d", i+1) |
| 59 | continue |
| 60 | } |
| 61 | |
| 62 | // Check if we've exited the engine block |
| 63 | if inEngineBlock && len(trimmed) > 0 && !strings.HasPrefix(trimmed, "#") { |
| 64 | lineIndent := getIndentation(line) |
| 65 | if len(lineIndent) <= len(engineIndent) { |
| 66 | inEngineBlock = false |
| 67 | } |
| 68 | } |
| 69 |