|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +[CmdletBinding()] |
| 3 | +param( |
| 4 | + [string] $RepoRoot = (Get-Location).Path, |
| 5 | + [datetime] $NowUtc = ([datetime]::UtcNow), |
| 6 | + [string] $OutputPath = $env:GITHUB_OUTPUT |
| 7 | +) |
| 8 | + |
| 9 | +$ErrorActionPreference = "Stop" |
| 10 | +Set-StrictMode -Version Latest |
| 11 | + |
| 12 | +function Invoke-Git { |
| 13 | + param([Parameter(Mandatory=$true)][string[]] $Arguments, [switch] $AllowFailure) |
| 14 | + |
| 15 | + $previousPreference = $ErrorActionPreference |
| 16 | + $ErrorActionPreference = "Continue" |
| 17 | + try { |
| 18 | + $output = & git @Arguments 2>$null |
| 19 | + $exitCode = $LASTEXITCODE |
| 20 | + } |
| 21 | + finally { |
| 22 | + $ErrorActionPreference = $previousPreference |
| 23 | + } |
| 24 | + |
| 25 | + if ($exitCode -ne 0) { |
| 26 | + if ($AllowFailure) { |
| 27 | + return $null |
| 28 | + } |
| 29 | + throw "git $($Arguments -join ' ') failed with exit code $exitCode" |
| 30 | + } |
| 31 | + return @($output) |
| 32 | +} |
| 33 | + |
| 34 | +function Get-FirstOutput { |
| 35 | + param($Value) |
| 36 | + |
| 37 | + $items = @($Value) |
| 38 | + if ($items.Count -eq 0) { |
| 39 | + return $null |
| 40 | + } |
| 41 | + return [string]$items[0] |
| 42 | +} |
| 43 | + |
| 44 | +function Get-CentralTimeZone { |
| 45 | + foreach ($id in @("Central Standard Time", "America/Chicago")) { |
| 46 | + try { |
| 47 | + return [System.TimeZoneInfo]::FindSystemTimeZoneById($id) |
| 48 | + } |
| 49 | + catch { |
| 50 | + } |
| 51 | + } |
| 52 | + throw "Could not resolve the America/Chicago release time zone." |
| 53 | +} |
| 54 | + |
| 55 | +function Get-DateStamp { |
| 56 | + param([datetime] $Utc) |
| 57 | + |
| 58 | + if ($Utc.Kind -ne [System.DateTimeKind]::Utc) { |
| 59 | + $Utc = $Utc.ToUniversalTime() |
| 60 | + } |
| 61 | + $central = [System.TimeZoneInfo]::ConvertTimeFromUtc($Utc, (Get-CentralTimeZone)) |
| 62 | + return $central.ToString("yyyy.M.d", [System.Globalization.CultureInfo]::InvariantCulture) |
| 63 | +} |
| 64 | + |
| 65 | +function Get-NextRevision { |
| 66 | + param([string] $DateStamp) |
| 67 | + |
| 68 | + $escapedDate = [regex]::Escape($DateStamp) |
| 69 | + $pattern = "^v$escapedDate\.(\d+)(-[A-Za-z0-9][A-Za-z0-9.-]*)?$" |
| 70 | + $tags = @(Invoke-Git -Arguments @("tag", "--list", "v$DateStamp.*")) |
| 71 | + $highest = -1 |
| 72 | + foreach ($tag in $tags) { |
| 73 | + if ($tag -match $pattern) { |
| 74 | + $value = [int]$Matches[1] |
| 75 | + if ($value -gt $highest) { |
| 76 | + $highest = $value |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return $highest + 1 |
| 81 | +} |
| 82 | + |
| 83 | +function Write-OutputValue { |
| 84 | + param([string] $Name, [string] $Value) |
| 85 | + |
| 86 | + $line = "$Name=$Value" |
| 87 | + Write-Host $line |
| 88 | + if (-not [string]::IsNullOrWhiteSpace($OutputPath)) { |
| 89 | + Add-Content -LiteralPath $OutputPath -Value $line -Encoding UTF8 |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +$resolvedRoot = (Resolve-Path -LiteralPath $RepoRoot).Path |
| 94 | +Push-Location $resolvedRoot |
| 95 | +try { |
| 96 | + $head = (Get-FirstOutput -Value (Invoke-Git -Arguments @("rev-parse", "HEAD"))).Trim() |
| 97 | + $latestTagOutput = Get-FirstOutput -Value (Invoke-Git -Arguments @("describe", "--tags", "--abbrev=0", "--match", "v*", "HEAD") -AllowFailure) |
| 98 | + $latestTag = "" |
| 99 | + if (-not [string]::IsNullOrWhiteSpace($latestTagOutput)) { |
| 100 | + $latestTag = $latestTagOutput.Trim() |
| 101 | + } |
| 102 | + |
| 103 | + if ([string]::IsNullOrWhiteSpace($latestTag)) { |
| 104 | + $commitCount = [int]((Get-FirstOutput -Value (Invoke-Git -Arguments @("rev-list", "--count", "--no-merges", "HEAD"))).Trim()) |
| 105 | + } else { |
| 106 | + $commitCount = [int]((Get-FirstOutput -Value (Invoke-Git -Arguments @("rev-list", "--count", "--no-merges", "$latestTag..HEAD"))).Trim()) |
| 107 | + } |
| 108 | + |
| 109 | + $hasChanges = $commitCount -gt 0 |
| 110 | + $dateStamp = Get-DateStamp -Utc $NowUtc |
| 111 | + $revision = Get-NextRevision -DateStamp $dateStamp |
| 112 | + $nextTag = "v$dateStamp.$revision-beta" |
| 113 | + |
| 114 | + Write-OutputValue -Name "has_changes" -Value $hasChanges.ToString().ToLowerInvariant() |
| 115 | + Write-OutputValue -Name "commit_count" -Value ([string]$commitCount) |
| 116 | + Write-OutputValue -Name "latest_tag" -Value $latestTag |
| 117 | + Write-OutputValue -Name "head_sha" -Value $head |
| 118 | + Write-OutputValue -Name "next_tag" -Value $nextTag |
| 119 | +} |
| 120 | +finally { |
| 121 | + Pop-Location |
| 122 | +} |
0 commit comments