101 lines
5.2 KiB
PowerShell
101 lines
5.2 KiB
PowerShell
#requires -Version 5.1
|
|
[CmdletBinding()]
|
|
param(
|
|
[switch]$ForceConfigs = $true
|
|
)
|
|
|
|
# Kept identical in standalone entrypoints; verified by Output.Tests.ps1.
|
|
function Write-ConfigCentralMessage {
|
|
param([string]$Message, [ValidateSet('Info', 'Success', 'Warning', 'Error')][string]$Level = 'Info')
|
|
$color = switch ($Level) {
|
|
'Success' { 'Green' }
|
|
'Warning' { 'Yellow' }
|
|
'Error' { 'Red' }
|
|
default { 'Cyan' }
|
|
}
|
|
$redirected = if ($Level -eq 'Error') { [Console]::IsErrorRedirected } else { [Console]::IsOutputRedirected }
|
|
$useColor = -not $redirected -and $null -eq [Environment]::GetEnvironmentVariable('NO_COLOR')
|
|
if ($Level -eq 'Error') {
|
|
$previous = $null
|
|
try {
|
|
if ($useColor) { $previous = [Console]::ForegroundColor; [Console]::ForegroundColor = $color }
|
|
[Console]::Error.WriteLine($Message)
|
|
}
|
|
finally { if ($null -ne $previous) { [Console]::ForegroundColor = $previous } }
|
|
}
|
|
elseif ($useColor) { Write-Host $Message -ForegroundColor $color }
|
|
else { Write-Host $Message }
|
|
}
|
|
|
|
function Invoke-ConfigUpdate {
|
|
param([string]$DataRoot, [switch]$ForceConfigs = $true)
|
|
$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) {
|
|
$failure = [Exception]::new("Config update did not fully complete (exit $LASTEXITCODE). Review the item results above; backups and logs remain in '$DataRoot'.")
|
|
$failure.Data['ExitCode'] = [int]$LASTEXITCODE
|
|
throw $failure
|
|
}
|
|
Write-ConfigCentralMessage -Level Success -Message 'Configs are up to date.'
|
|
}
|
|
finally {
|
|
$env:GIT_TERMINAL_PROMPT = $previousGitPrompt
|
|
if (Test-Path -LiteralPath $temporary) { Remove-Item -LiteralPath $temporary -Recurse -Force }
|
|
}
|
|
}
|
|
|
|
try {
|
|
# 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
|
|
}
|
|
|
|
$global:LASTEXITCODE = 0
|
|
}
|
|
catch {
|
|
Write-ConfigCentralMessage -Level Error -Message "Config update could not complete: $($_.Exception.Message)"
|
|
Write-Host 'Correct the reported issue and rerun the command. Completed changes are kept.'
|
|
$code = if ($_.Exception.Data.Contains('ExitCode')) { [int]$_.Exception.Data['ExitCode'] } else { 2 }
|
|
if ($MyInvocation.MyCommand.PSObject.Properties['Path']) { exit $code }
|
|
$global:LASTEXITCODE = $code
|
|
return
|
|
}
|