|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Publish browser-hosted (Blazor WebAssembly) output for free static hosting targets. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + Produces deterministic validation output (paths, hashes, base href) and prepares |
| 7 | + publish artefacts for GitHub Pages / Azure Static Web Apps. |
| 8 | +
|
| 9 | +.NOTES |
| 10 | + Keep minimal-change; do not modify publish output manually. |
| 11 | +#> |
| 12 | + |
| 13 | +[CmdletBinding()] |
| 14 | +param( |
| 15 | + [Parameter(Mandatory = $true)] |
| 16 | + [ValidateSet('github-pages', 'azure-swa')] |
| 17 | + [string] $Platform, |
| 18 | + |
| 19 | + [string] $BasePath, |
| 20 | + |
| 21 | + [ValidateSet('Debug', 'Release')] |
| 22 | + [string] $Configuration = 'Release' |
| 23 | +) |
| 24 | + |
| 25 | +Set-StrictMode -Version Latest |
| 26 | +$ErrorActionPreference = 'Stop' |
| 27 | + |
| 28 | +function Normalize-BasePath { |
| 29 | + param([Parameter(Mandatory = $true)][string] $Value) |
| 30 | + |
| 31 | + $v = $Value.Trim() |
| 32 | + if (-not $v.StartsWith('/')) { $v = '/' + $v } |
| 33 | + if (-not $v.EndsWith('/')) { $v = $v + '/' } |
| 34 | + return $v |
| 35 | +} |
| 36 | + |
| 37 | +function Replace-BaseHrefInFile { |
| 38 | + param( |
| 39 | + [Parameter(Mandatory = $true)][string] $FilePath, |
| 40 | + [Parameter(Mandatory = $true)][string] $NewBaseHref |
| 41 | + ) |
| 42 | + |
| 43 | + if (-not (Test-Path -LiteralPath $FilePath)) { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + $content = Get-Content -LiteralPath $FilePath -Raw |
| 48 | + $replacement = '<base href="' + $NewBaseHref + '" />' |
| 49 | + $updated = $content -replace '<base\s+href="[^"]*"\s*/>', $replacement |
| 50 | + Set-Content -LiteralPath $FilePath -Value $updated -NoNewline |
| 51 | +} |
| 52 | + |
| 53 | +function Upsert-CspMetaInFile { |
| 54 | + param( |
| 55 | + [Parameter(Mandatory = $true)][string] $FilePath, |
| 56 | + [Parameter(Mandatory = $true)][string] $Csp |
| 57 | + ) |
| 58 | + |
| 59 | + if (-not (Test-Path -LiteralPath $FilePath)) { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + $content = Get-Content -LiteralPath $FilePath -Raw |
| 64 | + |
| 65 | + $metaRegex = '<meta\s+http-equiv="Content-Security-Policy"\s+content="[^"]*"\s*/?>' |
| 66 | + $metaReplacement = '<meta http-equiv="Content-Security-Policy" content="' + $Csp + '" />' |
| 67 | + |
| 68 | + if ($content -match $metaRegex) { |
| 69 | + $updated = $content -replace $metaRegex, $metaReplacement |
| 70 | + Set-Content -LiteralPath $FilePath -Value $updated -NoNewline |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + # Insert CSP meta before the first <base ...> (or as first element inside <head> if <base> not found). |
| 75 | + if ($content -match '<base\s+href=') { |
| 76 | + $updated = $content -replace '(<base\s+href=)', ($metaReplacement + "`r`n " + '$1') |
| 77 | + Set-Content -LiteralPath $FilePath -Value $updated -NoNewline |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + if ($content -match '<head\s*>') { |
| 82 | + $updated = $content -replace '(<head\s*>)', ('$1' + "`r`n " + $metaReplacement) |
| 83 | + Set-Content -LiteralPath $FilePath -Value $updated -NoNewline |
| 84 | + return |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +Write-Host "[publish-browser-hosted] Platform=$Platform Configuration=$Configuration" |
| 89 | + |
| 90 | +$repoRoot = Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..') |
| 91 | +$wasmProject = Join-Path $repoRoot 'CovenantPromptKeyWebAssembly' |
| 92 | +$wasmProject = Join-Path $wasmProject 'CovenantPromptKeyWebAssembly.csproj' |
| 93 | + |
| 94 | +if (-not (Test-Path -LiteralPath $wasmProject)) { |
| 95 | + throw "WASM project not found: $wasmProject" |
| 96 | +} |
| 97 | + |
| 98 | +$outputDir = Join-Path $repoRoot 'ReleaseDownload' |
| 99 | +$outputDir = Join-Path $outputDir 'browser-hosted' |
| 100 | +$outputDir = Join-Path $outputDir $Platform |
| 101 | +$outputDir = Join-Path $outputDir $Configuration |
| 102 | +New-Item -ItemType Directory -Path $outputDir -Force | Out-Null |
| 103 | + |
| 104 | +Write-Host "[publish-browser-hosted] Publishing: $wasmProject" |
| 105 | +dotnet publish $wasmProject -c $Configuration -o $outputDir | Out-Host |
| 106 | + |
| 107 | +$wwwrootDir = Join-Path $outputDir 'wwwroot' |
| 108 | +if (-not (Test-Path -LiteralPath $wwwrootDir)) { |
| 109 | + throw "Publish output missing wwwroot: $wwwrootDir" |
| 110 | +} |
| 111 | + |
| 112 | +$cspBaseline = "default-src 'self'; base-uri 'self'; object-src 'none'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self';" |
| 113 | + |
| 114 | +Upsert-CspMetaInFile -FilePath (Join-Path $wwwrootDir 'index.html') -Csp $cspBaseline |
| 115 | +Upsert-CspMetaInFile -FilePath (Join-Path $wwwrootDir '404.html') -Csp $cspBaseline |
| 116 | + |
| 117 | +if ($Platform -eq 'github-pages') { |
| 118 | + if ([string]::IsNullOrWhiteSpace($BasePath)) { |
| 119 | + throw "BasePath is required for github-pages (e.g. '/CovenantPromptKey/')" |
| 120 | + } |
| 121 | + |
| 122 | + $baseHref = Normalize-BasePath -Value $BasePath |
| 123 | + Replace-BaseHrefInFile -FilePath (Join-Path $wwwrootDir 'index.html') -NewBaseHref $baseHref |
| 124 | + Replace-BaseHrefInFile -FilePath (Join-Path $wwwrootDir '404.html') -NewBaseHref $baseHref |
| 125 | +} |
| 126 | + |
| 127 | +if ($Platform -eq 'azure-swa') { |
| 128 | + $swaConfigSrc = Join-Path $repoRoot 'CovenantPromptKeyWebAssembly' |
| 129 | + $swaConfigSrc = Join-Path $swaConfigSrc 'staticwebapp.config.json' |
| 130 | + $swaConfigDst = Join-Path $wwwrootDir 'staticwebapp.config.json' |
| 131 | + Copy-Item -LiteralPath $swaConfigSrc -Destination $swaConfigDst -Force |
| 132 | +} |
| 133 | + |
| 134 | +$indexPath = Join-Path $wwwrootDir 'index.html' |
| 135 | +$swAssetsPath = Join-Path $wwwrootDir 'service-worker-assets.js' |
| 136 | + |
| 137 | +$indexHash = if (Test-Path -LiteralPath $indexPath) { (Get-FileHash -Algorithm SHA256 -LiteralPath $indexPath).Hash } else { '' } |
| 138 | +$swAssetsHash = if (Test-Path -LiteralPath $swAssetsPath) { (Get-FileHash -Algorithm SHA256 -LiteralPath $swAssetsPath).Hash } else { '' } |
| 139 | + |
| 140 | +Write-Host "[publish-browser-hosted] OutputDir=$outputDir" |
| 141 | +Write-Host "[publish-browser-hosted] WwwrootDir=$wwwrootDir" |
| 142 | +Write-Host "[publish-browser-hosted] IndexHtmlSha256=$indexHash" |
| 143 | +Write-Host "[publish-browser-hosted] ServiceWorkerAssetsSha256=$swAssetsHash" |
| 144 | + |
| 145 | +exit 0 |
0 commit comments