param([string]$Version = '', [string]$Directory = 'artifacts/releases', [switch]$Publish) $ErrorActionPreference = 'Stop' Set-Location (Split-Path $PSScriptRoot) if (!$Version) { [xml]$props = Get-Content Directory.Build.props; $Version = $props.Project.PropertyGroup.Version } if ($Version -notmatch '^\d+\.\d+\.\d+$') { throw 'Use a stable version such as 1.0.0.' } if (!$env:ASTROFRAME_RELEASE_TOKEN) { throw 'Set ASTROFRAME_RELEASE_TOKEN to a Gitea token with repository write permission. Never put it in source control.' } $headers = @{ Authorization = "token $env:ASTROFRAME_RELEASE_TOKEN" } $api = 'https://git.elijahkuntz.com/api/v1/repos/Elijah/AstroFrame' $files = @() foreach ($channel in @('win-x64','linux-x64')) { $folder = Join-Path $Directory $channel $feedPath = Join-Path $folder "releases.$channel.json" if (!(Test-Path $feedPath)) { throw "Missing release feed: $feedPath" } $feed = Get-Content $feedPath -Raw | ConvertFrom-Json $latest = @($feed.Assets | Where-Object { $_.Type -eq 'Full' -and $_.Version -eq $Version }) if ($latest.Count -ne 1) { throw "No full $Version release for $channel" } $files += @(Get-ChildItem -LiteralPath $folder -File | Where-Object { $_.Extension -in @('.exe','.AppImage','.nupkg','.json','.sh','.png','.zip') -or $_.Name -like 'RELEASES-*' }) } foreach ($file in $files) { if ($file.Extension -eq '.nupkg' -and $file.Name -notlike "AstroFrame-$Version-*") { continue } if ($file.Length -eq 0) { throw "Empty release asset: $($file.Name)" } } $commit = (& git rev-parse HEAD).Trim() if ($LASTEXITCODE) { throw 'Cannot identify the release commit.' } if (@(& git status --porcelain).Count -gt 0) { throw 'Commit the release source and push it before publishing.' } Invoke-RestMethod "$api/commits/$commit" -Headers $headers | Out-Null $tag = "v$Version" $release = $null try { $release = Invoke-RestMethod "$api/releases/tags/$tag" -Headers $headers } catch { if ([int]$_.Exception.Response.StatusCode -ne 404) { throw } } if ($release -and !$release.draft) { throw "$tag is already published. Release assets are immutable; build a new version." } if (!$release) { $body = @{ tag_name=$tag; target_commitish=$commit; name="AstroFrame $Version"; body="AstroFrame $Version for Windows and Linux. See the repository README for installation and update instructions."; draft=$true; prerelease=$false } | ConvertTo-Json $release = Invoke-RestMethod "$api/releases" -Method Post -Headers $headers -ContentType 'application/json' -Body $body } $attached = @(Invoke-RestMethod "$api/releases/$($release.id)/assets" -Headers $headers) foreach ($file in $files) { if ($file.Extension -eq '.nupkg' -and $file.Name -notlike "AstroFrame-$Version-*") { continue } if ($attached.name -contains $file.Name) { throw "Draft already contains $($file.Name). Remove the conflicting asset explicitly before retrying." } $name = [Uri]::EscapeDataString($file.Name) Invoke-RestMethod "$api/releases/$($release.id)/assets?name=$name" -Method Post -Headers $headers -Form @{ attachment=$file } | Out-Null Write-Output "Attached $($file.Name)" } if ($Publish) { Invoke-RestMethod "$api/releases/$($release.id)" -Method Patch -Headers $headers -ContentType 'application/json' -Body '{"draft":false}' | Out-Null Write-Output "Published $($release.html_url)" } else { Write-Output "Draft release ready: $($release.html_url). Review it, then publish in Gitea." }