rimworld-blax-slaves/Tools/workshop-upload.ps1

161 lines
9.0 KiB
PowerShell
Raw Normal View History

<#
Updates the Steam Workshop item from the command line, through the running Steam client: no password,
no game window, no dev mode. RimWorld may stay open.
.\Tools\workshop-upload.ps1 dry run: builds, stages a clean copy, checks the item and
that this Steam account owns it. Uploads nothing.
.\Tools\workshop-upload.ps1 -Publish uploads the staged copy. The change note is the CHANGELOG.md
entry for the version in About.xml.
-DescriptionFile docs\workshop-description.txt also replaces the Workshop description (BBCode)
-DescriptionOnly with -DescriptionFile: replaces only the description. No build,
no content upload, no change note.
Every [img] in the description must load with an Access-Control-Allow-Origin header: Steam loads
description images as cross-origin requests and shows a broken image otherwise. -Publish refuses
when one doesn't.
-Publish refuses unless the shipped files are committed and pushed, the top CHANGELOG.md entry matches
About.xml's modVersion, and two Tests\run-tests.ps1 runs passed after the last build: one with every
installed DLC (BlaxSlavesTestResults.all.txt) and one with -NoDlc (BlaxSlavesTestResults.nodlc.txt).
-SkipChecks skips those checks. The in-game button (Tools\workshop-stage.ps1) still works as a fallback.
#>
param(
[string]$RimWorldDir = 'E:\SteamLibrary\steamapps\common\RimWorld',
[switch]$Publish,
[string]$DescriptionFile,
[switch]$DescriptionOnly,
[string]$TestResultsDir = (Join-Path $env:TEMP 'BlaxSlavesTest'),
[switch]$SkipChecks
)
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$ship = 'About', '1.7', 'Defs', 'Patches', 'Languages', 'Textures', 'Sounds'
$toolProj = Join-Path $PSScriptRoot 'WorkshopUpload\WorkshopUpload.csproj'
$toolExe = Join-Path $PSScriptRoot 'WorkshopUpload\bin\Release\WorkshopUpload.exe'
if ($DescriptionOnly -and -not $DescriptionFile) { throw '-DescriptionOnly needs -DescriptionFile.' }
# Steam adds crossorigin to description images, so a host without Access-Control-Allow-Origin shows a broken image.
function Test-DescriptionImages([string]$file) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$found = @()
$text = [IO.File]::ReadAllText((Resolve-Path $file).Path, [Text.Encoding]::UTF8)
foreach ($m in [regex]::Matches($text, '\[img\](.*?)\[/img\]')) {
$url = $m.Groups[1].Value.Trim()
try {
$r = Invoke-WebRequest -Uri $url -UseBasicParsing -Headers @{ Origin = 'https://steamcommunity.com' }
$type = "$($r.Headers['Content-Type'])"
$acao = "$($r.Headers['Access-Control-Allow-Origin'])"
if ($type -notlike 'image/*') { $found += "$url is not an image ($type)." }
elseif ($acao -ne '*' -and $acao -ne 'https://steamcommunity.com') { $found += "$url sends no Access-Control-Allow-Origin header; Steam would show it broken." }
}
catch { $found += "$url does not load: $($_.Exception.Message)" }
}
$found
}
$itemId = (Get-Content -LiteralPath (Join-Path $repo 'About\PublishedFileId.txt') -Raw).Trim()
$about = [xml](Get-Content -LiteralPath (Join-Path $repo 'About\About.xml') -Raw -Encoding UTF8)
$packageId = $about.ModMetaData.packageId.Trim()
$modVersion = "$($about.ModMetaData.modVersion)".Trim()
# Same tags the in-game uploader sets: "Mod" plus Major.Minor of every supported version.
$tags = @('Mod') + @($about.ModMetaData.supportedVersions.li | ForEach-Object { $v = [version]"$_".Trim(); "$($v.Major).$($v.Minor)" })
# Change note: every CHANGELOG.md entry newer than the last version published from here
# (Tools\workshop-published.txt, written after each successful -Publish).
$publishedFile = Join-Path $PSScriptRoot 'workshop-published.txt'
$lastPublished = if (Test-Path $publishedFile) { [version](Get-Content -LiteralPath $publishedFile -Raw).Trim() } else { [version]'0.0' }
$sections = [ordered]@{}
$current = $null
foreach ($line in Get-Content -LiteralPath (Join-Path $repo 'CHANGELOG.md') -Encoding UTF8) {
if ($line -match '^##\s+(.+)$') { $current = $Matches[1].Trim(); $sections[$current] = New-Object System.Collections.ArrayList; continue }
if ($current -and $line.Trim()) { [void]$sections[$current].Add($line.TrimEnd()) }
}
if ($sections.Count -eq 0) { throw 'CHANGELOG.md has no "## <version>" entry.' }
$topVersion = @($sections.Keys)[0]
$noteLines = @()
foreach ($v in $sections.Keys) {
if ([version]$v -le $lastPublished) { break }
if ($noteLines) { $noteLines += '' }
$noteLines += @("$($about.ModMetaData.name) $v") + $sections[$v]
}
if (-not $noteLines) { $noteLines = @("$($about.ModMetaData.name) $topVersion") + $sections[$topVersion] }
$note = ($noteLines -join "`n").Trim()
if (-not $DescriptionOnly) {
foreach ($proj in Get-ChildItem (Join-Path $repo 'Source\*\*.csproj')) {
& dotnet build $proj.FullName -c Release -nologo -v q "-p:RimWorldDir=$RimWorldDir"
if ($LASTEXITCODE -ne 0) { throw "build failed: $($proj.Name)" }
}
}
& dotnet build $toolProj -c Release -nologo -v q "-p:RimWorldDir=$RimWorldDir"
if ($LASTEXITCODE -ne 0) { throw 'build failed: WorkshopUpload' }
$imageProblems = @()
if ($DescriptionFile) {
$imageProblems = @(Test-DescriptionImages $DescriptionFile)
foreach ($p in $imageProblems) { Write-Warning $p }
}
if ($Publish -and -not $SkipChecks) {
$problems = @() + $imageProblems
$ahead = & git -C $repo rev-list --count '@{u}..HEAD' 2>$null
if ($LASTEXITCODE -ne 0) { $problems += 'The branch has no upstream to compare with.' }
elseif ([int]$ahead -gt 0) { $problems += "$ahead commit(s) not pushed yet, so the Workshop would show what the repo doesn't." }
if ($DescriptionOnly) {
$dirty = & git -C $repo status --porcelain -- (Resolve-Path $DescriptionFile).Path
if ($dirty) { $problems += "The description file has uncommitted changes: $dirty" }
}
else {
if ($topVersion -ne $modVersion) { $problems += "CHANGELOG.md's top entry is $topVersion, About.xml says $modVersion." }
$dirty = & git -C $repo status --porcelain -- $ship
if ($dirty) { $problems += "Uncommitted changes in shipped files:`n$($dirty -join "`n")" }
$dll = Get-ChildItem (Join-Path $repo '1.7\Assemblies\*.dll') | Sort-Object LastWriteTime -Descending | Select-Object -First 1
# The mod must work with and without DLCs: a passing run with every installed DLC and one with none.
foreach ($set in @(@{ name = 'all'; run = '.\Tests\run-tests.ps1' }, @{ name = 'nodlc'; run = '.\Tests\run-tests.ps1 -NoDlc' })) {
$file = Join-Path $TestResultsDir "BlaxSlavesTestResults.$($set.name).txt"
if (-not (Test-Path $file)) { $problems += "No test results at $file. Run $($set.run) first."; continue }
$res = Get-Item $file
$lines = Get-Content -LiteralPath $file -Encoding UTF8
if (-not ($lines -match '^END$') -or ($lines -match '^FAIL ')) { $problems += "The last $($set.run) run failed or did not finish." }
elseif ($dll -and $res.LastWriteTime -lt $dll.LastWriteTime) { $problems += "The last $($set.run) run ($($res.LastWriteTime)) is older than the build ($($dll.LastWriteTime)). Run it again." }
}
}
if ($problems) { throw ("Not publishing:`n- " + ($problems -join "`n- ")) }
}
if ($DescriptionOnly) {
$toolArgs = @('--item', $itemId, '--description-file', (Resolve-Path $DescriptionFile).Path)
}
else {
# Clean copy outside the repo: only what players need, no PDBs. TEMP has no spaces in its path.
$root = Join-Path $env:TEMP 'BlaxWorkshopUpload'
$stage = Join-Path $root $packageId
if (Test-Path $stage) { Remove-Item $stage -Recurse -Force }
New-Item -ItemType Directory $stage -Force | Out-Null
foreach ($name in $ship) {
$src = Join-Path $repo $name
if (Test-Path $src) { Copy-Item $src (Join-Path $stage $name) -Recurse }
}
Get-ChildItem $stage -Recurse -File -Filter *.pdb | Remove-Item
$noteFile = Join-Path $root "$packageId.note.txt"
[IO.File]::WriteAllText($noteFile, $note, (New-Object Text.UTF8Encoding $false))
$toolArgs = @('--item', $itemId, '--content', $stage, '--note-file', $noteFile, '--tags', ($tags -join ','))
if ($DescriptionFile) { $toolArgs += @('--description-file', (Resolve-Path $DescriptionFile).Path) }
}
if ($Publish) { $toolArgs += '--publish' }
# steam_api writes status lines to stderr; under 'Stop' PowerShell 5.1 would turn them into errors.
$ErrorActionPreference = 'Continue'
Push-Location (Split-Path $toolExe)
try {
& $toolExe @toolArgs 2>&1 | ForEach-Object { "$_" } | Where-Object { $_ -notmatch '^(Setting breakpad|SteamInternal_)' }
$code = $LASTEXITCODE
}
finally { Pop-Location }
if ($Publish -and $code -eq 0 -and -not $DescriptionOnly) {
[IO.File]::WriteAllText($publishedFile, "$topVersion`n")
Write-Host "Recorded $topVersion in Tools\workshop-published.txt. Commit it."
}
exit $code