Files

114 lines
3.5 KiB
PowerShell

<#
.SYNOPSIS
Packages the modular SciFi-XZBT project into a single, standalone, offline HTML file.
.EXAMPLE
.\package.ps1
.EXAMPLE
.\package.ps1 -Force
#>
[CmdletBinding()]
param(
[string]$OutputFile = "",
[switch]$Force
)
$rootDir = (Resolve-Path "$PSScriptRoot\..").Path
$indexFile = Join-Path $rootDir "index.html"
if (-not (Test-Path $indexFile)) {
Write-Error "index.html not found in $rootDir"
exit 1
}
# Resolve the current commit hash so the output filename is tied to the
# exact source snapshot it was built from, e.g. SciFiAmbientDisplay_V977d348.html
$commitHash = $null
try {
$commitHash = (git -C $rootDir rev-parse --short HEAD 2>$null)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($commitHash)) {
$commitHash = $null
} else {
$commitHash = $commitHash.Trim()
}
} catch {
$commitHash = $null
}
if ([string]::IsNullOrWhiteSpace($commitHash)) {
Write-Error "Unable to resolve the current git commit hash (is git installed and is $rootDir a git repository?). Aborting so the build isn't silently mislabeled."
exit 1
}
if ([string]::IsNullOrWhiteSpace($OutputFile)) {
$OutputFile = Join-Path $rootDir "dist\SciFiAmbientDisplay_V$commitHash.html"
}
if (Test-Path $OutputFile) {
Write-Error "Output file already exists: $OutputFile"
if ($Force) {
Write-Warning "-Force specified; overwriting existing file."
} else {
$response = Read-Host "This build has already been generated. Replace it? (y/N)"
if ($response -notmatch '^(y|yes)$') {
Write-Host "Aborted. No changes made." -ForegroundColor Yellow
exit 1
}
}
}
Write-Host "Packaging SciFi Ambient Display from $rootDir..." -ForegroundColor Cyan
Write-Host " -> Commit: $commitHash" -ForegroundColor Cyan
$indexLines = [System.IO.File]::ReadAllLines($indexFile)
$output = [System.Collections.Generic.List[string]]::new()
foreach ($line in $indexLines) {
# Inline CSS stylesheet
if ($line -match '<link\s+rel="stylesheet"\s+href="([^"]+)"\s*/?>') {
$cssRel = $matches[1]
$cssPath = Join-Path $rootDir $cssRel
if (Test-Path $cssPath) {
Write-Host " -> Inlining CSS: $cssRel" -ForegroundColor Green
$output.Add(" <style>")
$cssContent = [System.IO.File]::ReadAllLines($cssPath)
foreach ($cl in $cssContent) { $output.Add($cl) }
$output.Add(" </style>")
} else {
Write-Warning "CSS file not found: $cssPath"
$output.Add($line)
}
continue
}
# Inline JS scripts
if ($line -match '<script\s+src="([^"]+)"></script>') {
$jsRel = $matches[1]
$jsPath = Join-Path $rootDir $jsRel
if (Test-Path $jsPath) {
Write-Host " -> Inlining JS: $jsRel" -ForegroundColor Green
$output.Add(" <script>")
$jsContent = [System.IO.File]::ReadAllLines($jsPath)
foreach ($jl in $jsContent) { $output.Add($jl) }
$output.Add(" </script>")
} else {
Write-Warning "JS file not found: $jsPath"
$output.Add($line)
}
continue
}
$output.Add($line)
}
$outDir = [System.IO.Path]::GetDirectoryName($OutputFile)
if (-not (Test-Path $outDir)) {
New-Item -ItemType Directory -Path $outDir -Force | Out-Null
}
[System.IO.File]::WriteAllLines($OutputFile, $output)
$fileSize = (Get-Item $OutputFile).Length
$fileSizeKb = [math]::Round($fileSize / 1KB, 2)
Write-Host "Build Successful!" -ForegroundColor Green
Write-Host "Output: $OutputFile ($fileSizeKb KB, $($output.Count) lines)" -ForegroundColor Yellow