|
| 1 | +param( |
| 2 | + [Parameter(Mandatory = $true)] |
| 3 | + [ValidatePattern('^\d+\.\d+\.\d+$')] |
| 4 | + [string] $Version, |
| 5 | + |
| 6 | + [string] $Branch = 'main', |
| 7 | + |
| 8 | + [switch] $Push, |
| 9 | + |
| 10 | + [switch] $AllowNonMainBranch |
| 11 | +) |
| 12 | + |
| 13 | +Set-StrictMode -Version Latest |
| 14 | +$ErrorActionPreference = 'Stop' |
| 15 | + |
| 16 | +$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path |
| 17 | +$versionHeader = Join-Path $repoRoot 'include\.internal\version' |
| 18 | +$tagName = "v$Version" |
| 19 | + |
| 20 | +function Invoke-Git { |
| 21 | + param( |
| 22 | + [Parameter(ValueFromRemainingArguments = $true)] |
| 23 | + [string[]] $Arguments |
| 24 | + ) |
| 25 | + |
| 26 | + & git @Arguments |
| 27 | + if ($LASTEXITCODE -ne 0) { |
| 28 | + throw "git $($Arguments -join ' ') failed with exit code $LASTEXITCODE." |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function Test-GitRefExists { |
| 33 | + param([string] $Ref) |
| 34 | + |
| 35 | + & git rev-parse --verify --quiet $Ref *> $null |
| 36 | + return $LASTEXITCODE -eq 0 |
| 37 | +} |
| 38 | + |
| 39 | +Push-Location $repoRoot |
| 40 | +try { |
| 41 | + Invoke-Git rev-parse --show-toplevel *> $null |
| 42 | + |
| 43 | + $currentBranch = (& git branch --show-current).Trim() |
| 44 | + if (-not $AllowNonMainBranch -and $currentBranch -ne $Branch) { |
| 45 | + throw "Release preparation must run on '$Branch'. Current branch is '$currentBranch'." |
| 46 | + } |
| 47 | + |
| 48 | + Invoke-Git fetch origin $Branch --tags |
| 49 | + if (-not $AllowNonMainBranch) { |
| 50 | + $localHead = (& git rev-parse HEAD).Trim() |
| 51 | + $remoteHead = (& git rev-parse "origin/$Branch").Trim() |
| 52 | + if ($localHead -ne $remoteHead) { |
| 53 | + throw "Local '$Branch' must match origin/$Branch before preparing a release." |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + $status = (& git status --porcelain).Trim() |
| 58 | + if (-not [string]::IsNullOrWhiteSpace($status)) { |
| 59 | + throw "Working tree must be clean before preparing a release." |
| 60 | + } |
| 61 | + |
| 62 | + if (Test-GitRefExists "refs/tags/$tagName") { |
| 63 | + throw "Local tag already exists: $tagName" |
| 64 | + } |
| 65 | + |
| 66 | + & git ls-remote --exit-code --tags origin $tagName *> $null |
| 67 | + if ($LASTEXITCODE -eq 0) { |
| 68 | + throw "Remote tag already exists on origin: $tagName" |
| 69 | + } |
| 70 | + if ($LASTEXITCODE -ne 2) { |
| 71 | + throw "Could not check whether remote tag exists on origin: $tagName" |
| 72 | + } |
| 73 | + |
| 74 | + $parts = $Version.Split('.') |
| 75 | + $major = $parts[0] |
| 76 | + $minor = $parts[1] |
| 77 | + $patch = $parts[2] |
| 78 | + |
| 79 | + if (-not (Test-Path $versionHeader)) { |
| 80 | + throw "Version header was not found: $versionHeader" |
| 81 | + } |
| 82 | + |
| 83 | + $currentVersion = (& (Join-Path $repoRoot 'scripts\nuget\Get-CrtSysVersion.ps1')).Trim() |
| 84 | + if ($currentVersion -eq $Version) { |
| 85 | + throw "Version header is already $Version." |
| 86 | + } |
| 87 | + |
| 88 | + $content = Get-Content -LiteralPath $versionHeader -Raw |
| 89 | + $content = [regex]::Replace( |
| 90 | + $content, |
| 91 | + '(?m)^(#define\s+CRTSYS_VERSION_MAJOR\s+)\d+(\s*)$', |
| 92 | + { param($match) "$($match.Groups[1].Value)$major$($match.Groups[2].Value)" }) |
| 93 | + $content = [regex]::Replace( |
| 94 | + $content, |
| 95 | + '(?m)^(#define\s+CRTSYS_VERSION_MINOR\s+)\d+(\s*)$', |
| 96 | + { param($match) "$($match.Groups[1].Value)$minor$($match.Groups[2].Value)" }) |
| 97 | + $content = [regex]::Replace( |
| 98 | + $content, |
| 99 | + '(?m)^(#define\s+CRTSYS_VERSION_PATCH\s+)\d+(\s*)$', |
| 100 | + { param($match) "$($match.Groups[1].Value)$patch$($match.Groups[2].Value)" }) |
| 101 | + |
| 102 | + $utf8NoBom = [System.Text.UTF8Encoding]::new($false) |
| 103 | + [System.IO.File]::WriteAllText($versionHeader, $content, $utf8NoBom) |
| 104 | + |
| 105 | + $resolvedVersion = (& (Join-Path $repoRoot 'scripts\nuget\Get-CrtSysVersion.ps1')).Trim() |
| 106 | + if ($resolvedVersion -ne $Version) { |
| 107 | + throw "Version header update failed. Expected $Version, got $resolvedVersion." |
| 108 | + } |
| 109 | + |
| 110 | + Invoke-Git diff --check |
| 111 | + Invoke-Git add -- include/.internal/version |
| 112 | + Invoke-Git commit -m "release crtsys $Version" |
| 113 | + Invoke-Git tag -a $tagName -m "crtsys $Version" |
| 114 | + |
| 115 | + if ($Push) { |
| 116 | + Invoke-Git push origin $currentBranch |
| 117 | + Invoke-Git push origin $tagName |
| 118 | + Write-Host "Pushed $currentBranch and $tagName. The Package workflow should start from the tag." |
| 119 | + } else { |
| 120 | + Write-Host "Prepared release commit and tag locally." |
| 121 | + Write-Host "Push with:" |
| 122 | + Write-Host " git push origin $currentBranch" |
| 123 | + Write-Host " git push origin $tagName" |
| 124 | + } |
| 125 | +} catch { |
| 126 | + Write-Error $_ |
| 127 | + exit 1 |
| 128 | +} finally { |
| 129 | + Pop-Location |
| 130 | +} |
0 commit comments