public/Repair-WpcSerializedYaml.ps1

function Repair-WpcSerializedYaml {
    param(
        [Parameter(Mandatory = $true)]
        [string] $YamlText,
        $TemplateNode
    )

    $normalizedYaml = $YamlText
    $normalizedYaml = Convert-WpcScalarKeyToSequence -YamlText $normalizedYaml -KeyName 'depends_on'
    $normalizedYaml = Convert-WpcScalarKeyToSequence -YamlText $normalizedYaml -KeyName 'path'
    $normalizedYaml = Normalize-WpcSequenceIndentation -YamlText $normalizedYaml -KeyName 'depends_on'
    $normalizedYaml = Normalize-WpcSequenceIndentation -YamlText $normalizedYaml -KeyName 'path'
    $normalizedYaml = $normalizedYaml -replace "(?m)^steps:\r?\n name:", "steps:`n- name:"

    $templateHasWhen = $TemplateNode -is [System.Collections.IDictionary] -and $TemplateNode.Contains('when')
    $templateWhenIsArray = $templateHasWhen -and
        $TemplateNode['when'] -is [System.Collections.IEnumerable] -and
        $TemplateNode['when'] -isnot [System.Collections.IDictionary] -and
        $TemplateNode['when'] -isnot [string]
    $templateWhenIsMapping = $templateHasWhen -and -not $templateWhenIsArray

    if (-not $templateHasWhen) {
        return $normalizedYaml
    }

    $normalizedLines = [System.Collections.Generic.List[string]]::new()
    $lines = @($normalizedYaml -split "`r?`n")
    $lineIndex = 0
    while ($lineIndex -lt $lines.Count) {
        $currentLine = $lines[$lineIndex]
        if ($currentLine -ne 'when:') {
            $normalizedLines.Add($currentLine)
            $lineIndex++
            continue
        }

        $normalizedLines.Add($currentLine)
        $lineIndex++
        if ($lineIndex -ge $lines.Count) {
            break
        }

        if ($templateWhenIsMapping) {
            $firstWhenLine = $lines[$lineIndex]
            if ($firstWhenLine.StartsWith(' - ')) {
                $normalizedLines.Add(" $($firstWhenLine.Substring(4))")
                $lineIndex++
                while ($lineIndex -lt $lines.Count) {
                    $currentWhenLine = $lines[$lineIndex]
                    if ($currentWhenLine -match '^\S') {
                        break
                    }

                    if ($currentWhenLine.StartsWith(' - ')) {
                        break
                    }

                    $normalizedLines.Add($currentWhenLine)
                    $lineIndex++
                }
                continue
            }

            $normalizedLines.Add($firstWhenLine)
            $lineIndex++
            continue
        }

        $firstWhenLine = $lines[$lineIndex]
        if ($firstWhenLine -match '^\s*-\s') {
            while ($lineIndex -lt $lines.Count) {
                $currentWhenLine = $lines[$lineIndex]
                if ($currentWhenLine -match '^\S') {
                    break
                }
                $normalizedLines.Add($currentWhenLine)
                $lineIndex++
            }
            continue
        }

        $isFirstWhenEntryLine = $true
        while ($lineIndex -lt $lines.Count) {
            $currentWhenLine = $lines[$lineIndex]
            if ($currentWhenLine -match '^\S') {
                break
            }

            if ($isFirstWhenEntryLine) {
                $normalizedLines.Add(($currentWhenLine -replace '^ ', ' - '))
                $isFirstWhenEntryLine = $false
            } else {
                $normalizedLines.Add((" $currentWhenLine").TrimEnd())
            }
            $lineIndex++
        }
    }

    return $normalizedLines -join "`n"
}