config-central/update-configs.ps1

64 lines
3.5 KiB
PowerShell

#requires -Version 5.1
[CmdletBinding()]
param(
[switch]$ForceConfigs
)
function Invoke-ConfigUpdate {
param([string]$DataRoot, [switch]$ForceConfigs)
$settingsPath = Join-Path $DataRoot 'settings.json'
if (-not (Test-Path -LiteralPath $settingsPath -PathType Leaf)) {
throw 'Run install.ps1 once on this computer before updating configs.'
}
$settings = Get-Content -LiteralPath $settingsPath -Raw | ConvertFrom-Json
if ($settings.machine -notmatch '^[a-z0-9][a-z0-9-]*$') { throw 'Saved machine selection is invalid. Rerun setup.' }
if ($settings.branch -notmatch '^[A-Za-z0-9][A-Za-z0-9._/-]*$' -or $settings.branch.Contains('..')) {
throw 'Saved repository branch is invalid. Rerun setup.'
}
$repositoryUri = $null
if (-not [Uri]::TryCreate([string]$settings.repositoryUrl, [UriKind]::Absolute, [ref]$repositoryUri) -or $repositoryUri.Scheme -ne 'https') {
throw 'The web updater requires an HTTPS repository URL in settings.json.'
}
$git = Get-Command git.exe -ErrorAction SilentlyContinue
$pwsh = Get-Command pwsh.exe -ErrorAction SilentlyContinue
$gitPath = if ($git) { $git.Source } else { Join-Path $env:ProgramFiles 'Git\cmd\git.exe' }
$pwshPath = if ($pwsh) { $pwsh.Source } else { Join-Path $env:ProgramFiles 'PowerShell\7\pwsh.exe' }
if (-not (Test-Path -LiteralPath $gitPath) -or -not (Test-Path -LiteralPath $pwshPath)) {
throw 'Git or PowerShell 7 is missing. Rerun install.ps1 to repair setup.'
}
$temporary = Join-Path $env:TEMP "config-central-update-$([guid]::NewGuid().ToString('N'))"
$previousGitPrompt = $env:GIT_TERMINAL_PROMPT
try {
$env:GIT_TERMINAL_PROMPT = '0'
Write-Host "Downloading latest configs for '$($settings.machine)' from '$($settings.branch)'..."
& $gitPath clone --quiet --depth 1 --single-branch --no-checkout --branch $settings.branch -- $settings.repositoryUrl $temporary
if ($LASTEXITCODE -ne 0) { throw 'Config download failed. Check the repository URL, network, and Git credentials.' }
$revision = & $gitPath -C $temporary rev-parse --verify 'HEAD^{commit}'
if ($LASTEXITCODE -ne 0 -or $revision -notmatch '^[0-9a-f]{40,64}$') { throw 'Could not resolve the downloaded config revision.' }
& $gitPath -C $temporary checkout --quiet --detach $revision
if ($LASTEXITCODE -ne 0) { throw 'Could not prepare the downloaded configs.' }
$arguments = @('-NoLogo', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', (Join-Path $temporary 'cc.ps1'),
'apply', '-ConfigsOnly', '-Machine', $settings.machine, '-DataRoot', $DataRoot,
'-RepositoryRoot', $temporary, '-Revision', $revision)
if ($ForceConfigs) { $arguments += '-ForceConfigs' }
& $pwshPath @arguments
if ($LASTEXITCODE -ne 0) {
throw "Config update did not fully complete (exit $LASTEXITCODE). Review the results above; backups and logs remain in '$DataRoot'."
}
Write-Host 'Configs are up to date.'
}
finally {
$env:GIT_TERMINAL_PROMPT = $previousGitPrompt
if (Test-Path -LiteralPath $temporary) { Remove-Item -LiteralPath $temporary -Recurse -Force }
}
}
# Keep strict mode and error preferences local to this invocation.
& {
Set-StrictMode -Version 2.0
$ErrorActionPreference = 'Stop'
if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { throw 'Config updates run only on Windows.' }
Invoke-ConfigUpdate -DataRoot (Join-Path $env:LOCALAPPDATA 'ConfigCentral') -ForceConfigs:$ForceConfigs
}