|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Generates the repository Codecov components configuration from a JSON map. |
| 4 | + .DESCRIPTION |
| 5 | + Produces .codecov.yml from DevOps/Quality/codecov-components-map.json. |
| 6 | + When -Validate is provided, compares generated content to the checked-in |
| 7 | + file and fails if drift is detected. |
| 8 | +#> |
| 9 | +[CmdletBinding()] |
| 10 | +param( |
| 11 | + [string]$MapPath, |
| 12 | + [string]$OutputPath, |
| 13 | + [switch]$Validate |
| 14 | +) |
| 15 | + |
| 16 | +$repoRoot = Resolve-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '..\..') |
| 17 | + |
| 18 | +if (-not $MapPath) { |
| 19 | + $MapPath = Join-Path -Path $PSScriptRoot -ChildPath 'codecov-components-map.json' |
| 20 | +} |
| 21 | +if (-not $OutputPath) { |
| 22 | + $OutputPath = Join-Path -Path $repoRoot -ChildPath '.codecov.yml' |
| 23 | +} |
| 24 | + |
| 25 | +if (-not (Test-Path -Path $MapPath)) { |
| 26 | + throw ('Codecov component map file not found: {0}' -f $MapPath) |
| 27 | +} |
| 28 | + |
| 29 | +$map = Get-Content -Path $MapPath -Raw | ConvertFrom-Json |
| 30 | +if ($null -eq $map.components -or $map.components.Count -eq 0) { |
| 31 | + throw 'Codecov component map does not contain any components.' |
| 32 | +} |
| 33 | + |
| 34 | +$generatedLines = @( |
| 35 | + '# This file is generated by DevOps/Quality/Generate-CodecovComponents.ps1', |
| 36 | + '# Source of truth: DevOps/Quality/codecov-components-map.json', |
| 37 | + 'component_management:', |
| 38 | + ' individual_components:' |
| 39 | +) |
| 40 | + |
| 41 | +foreach ($component in $map.components) { |
| 42 | + if ([string]::IsNullOrWhiteSpace($component.id)) { |
| 43 | + throw 'A component id is missing in the map file.' |
| 44 | + } |
| 45 | + if ([string]::IsNullOrWhiteSpace($component.name)) { |
| 46 | + throw ('Component ''{0}'' is missing a name in the map file.' -f $component.id) |
| 47 | + } |
| 48 | + if ($null -eq $component.paths -or $component.paths.Count -eq 0) { |
| 49 | + throw ('Component ''{0}'' does not define any paths in the map file.' -f $component.id) |
| 50 | + } |
| 51 | + |
| 52 | + $generatedLines += (' - component_id: {0}' -f $component.id) |
| 53 | + $generatedLines += (' name: {0}' -f $component.name) |
| 54 | + $generatedLines += ' paths:' |
| 55 | + |
| 56 | + foreach ($pathPattern in $component.paths) { |
| 57 | + if ([string]::IsNullOrWhiteSpace($pathPattern)) { |
| 58 | + continue |
| 59 | + } |
| 60 | + $generatedLines += (' - {0}' -f $pathPattern) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +$generatedContent = ($generatedLines -join "`n") + "`n" |
| 65 | + |
| 66 | +function Normalize-TextForComparison { |
| 67 | + param( |
| 68 | + [string]$Text |
| 69 | + ) |
| 70 | + |
| 71 | + if ($null -eq $Text) { |
| 72 | + return '' |
| 73 | + } |
| 74 | + |
| 75 | + $normalized = $Text -replace "`r`n", "`n" |
| 76 | + if ($normalized.Length -gt 0 -and $normalized[0] -eq [char]0xFEFF) { |
| 77 | + $normalized = $normalized.Substring(1) |
| 78 | + } |
| 79 | + |
| 80 | + return $normalized.TrimEnd("`n") |
| 81 | +} |
| 82 | + |
| 83 | +if ($Validate) { |
| 84 | + if (-not (Test-Path -Path $OutputPath)) { |
| 85 | + throw ('Codecov config file is missing: {0}' -f $OutputPath) |
| 86 | + } |
| 87 | + |
| 88 | + $existingContent = Get-Content -Path $OutputPath -Raw |
| 89 | + if ((Normalize-TextForComparison -Text $existingContent) -ne (Normalize-TextForComparison -Text $generatedContent)) { |
| 90 | + throw 'Codecov component config drift detected. Run: pwsh -File .\DevOps\Quality\Generate-CodecovComponents.ps1' |
| 91 | + } |
| 92 | + |
| 93 | + Write-Host 'Codecov component config validation passed.' -ForegroundColor Green |
| 94 | + return |
| 95 | +} |
| 96 | + |
| 97 | +Set-Content -Path $OutputPath -Value $generatedContent -Encoding utf8 |
| 98 | +Write-Host ('Generated Codecov component config at: {0}' -f $OutputPath) -ForegroundColor Green |
0 commit comments