param( [string]$Repository = 'https://github.com/DD4SN/DD4SN.github.io.git' ) $ErrorActionPreference = 'Stop' $projectRoot = Split-Path -Parent $PSScriptRoot $siteSource = Join-Path $projectRoot 'pages-dist' $workflowSource = Join-Path $projectRoot 'pages/deploy.yml' $temporaryRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("holodori-pages-{0}" -f [guid]::NewGuid().ToString('N')) $checkout = Join-Path $temporaryRoot 'repository' try { & npm --prefix $projectRoot run build:pages if ($LASTEXITCODE -ne 0) { throw 'Pages build failed.' } New-Item -ItemType Directory -Path $temporaryRoot | Out-Null & git clone $Repository $checkout if ($LASTEXITCODE -ne 0) { throw 'Pages repository clone failed.' } $resolvedCheckout = (Resolve-Path -LiteralPath $checkout).Path $resolvedTemporaryRoot = (Resolve-Path -LiteralPath $temporaryRoot).Path if (-not $resolvedCheckout.StartsWith($resolvedTemporaryRoot, [System.StringComparison]::OrdinalIgnoreCase)) { throw 'Refusing to update a checkout outside the temporary deployment directory.' } $oldSiteTarget = Join-Path $resolvedCheckout 'site' if (Test-Path -LiteralPath $oldSiteTarget) { Remove-Item -LiteralPath $oldSiteTarget -Recurse -Force } foreach ($generatedPath in @('assets', 'icons', 'index.html', 'README.txt', 'VERSION.txt', '.nojekyll')) { $target = Join-Path $resolvedCheckout $generatedPath if (Test-Path -LiteralPath $target) { Remove-Item -LiteralPath $target -Recurse -Force } } Copy-Item -Path (Join-Path $siteSource '*') -Destination $resolvedCheckout -Recurse -Force New-Item -ItemType File -Path (Join-Path $resolvedCheckout '.nojekyll') -Force | Out-Null $workflowTarget = Join-Path $resolvedCheckout '.github/workflows' New-Item -ItemType Directory -Path $workflowTarget -Force | Out-Null Copy-Item -LiteralPath $workflowSource -Destination (Join-Path $workflowTarget 'deploy.yml') -Force Push-Location $resolvedCheckout try { & git add --all -- . if ($LASTEXITCODE -ne 0) { throw 'Failed to stage Pages files.' } & git diff --cached --quiet if ($LASTEXITCODE -eq 0) { Write-Output 'GitHub Pages output is already up to date.' return } $package = Get-Content -LiteralPath (Join-Path $projectRoot 'package.json') -Raw | ConvertFrom-Json & git commit -m ("deploy: HolodoriCalc v{0}" -f $package.version) if ($LASTEXITCODE -ne 0) { throw 'Pages commit failed.' } & git push origin HEAD:main if ($LASTEXITCODE -ne 0) { throw 'Pages push failed.' } } finally { Pop-Location } } finally { if (Test-Path -LiteralPath $temporaryRoot) { $resolvedTemporaryRoot = (Resolve-Path -LiteralPath $temporaryRoot).Path $systemTemporaryRoot = [System.IO.Path]::GetFullPath([System.IO.Path]::GetTempPath()).TrimEnd('\') if ($resolvedTemporaryRoot.StartsWith($systemTemporaryRoot, [System.StringComparison]::OrdinalIgnoreCase) -and (Split-Path -Leaf $resolvedTemporaryRoot).StartsWith('holodori-pages-')) { Remove-Item -LiteralPath $resolvedTemporaryRoot -Recurse -Force } } }