diff --git a/TASKS.md b/TASKS.md index 72faa2e..00e51f4 100644 --- a/TASKS.md +++ b/TASKS.md @@ -180,3 +180,4 @@ open until it is run on disposable machines with the required account setups. ## Setup progress feedback - [x] Show elapsed spinner feedback during the initial apply, keep redirected output plain, and replay child results and diagnostics after completion. +- [x] Show elapsed feedback during `cc update-software`, preserve plain/JSON output, and accept `cc software-update` as an alias (192 Linux Pester tests pass; 5 Windows-only skips). diff --git a/cc.ps1 b/cc.ps1 index f4c11b1..4161f8e 100644 --- a/cc.ps1 +++ b/cc.ps1 @@ -1,6 +1,6 @@ [CmdletBinding()] param( - [Parameter(Position = 0)][ValidateSet('doctor', 'plan', 'apply', 'sync', 'update-software', 'status', 'configs', 'capture', 'restore', 'schedule', 'unschedule', 'menu')][string]$Command = 'menu', + [Parameter(Position = 0)][ValidateSet('doctor', 'plan', 'apply', 'sync', 'update-software', 'software-update', 'status', 'configs', 'capture', 'restore', 'schedule', 'unschedule', 'menu')][string]$Command = 'menu', [string]$Machine, [string]$Config, [string]$OutputPath, @@ -40,6 +40,7 @@ Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' try { +$Command = if ($Command -eq 'software-update') { 'update-software' } else { $Command } foreach ($module in @('State', 'Manifest', 'Repository', 'Packages', 'Adapters', 'Configs', 'Elevation', 'Scheduling', 'Core')) { $path = Join-Path $PSScriptRoot "src/$module.psm1" if (Test-Path -LiteralPath $path) { Import-Module $path -Force } @@ -57,7 +58,7 @@ foreach ($module in @('State', 'Manifest', 'Repository', 'Packages', 'Adapters', Show-ConfigCentralMenu -Machine $Machine -DataRoot $DataRoot -RepositoryRoot $RepositoryRoot exit 0 } - $result = Invoke-ConfigCentral -Command $Command -Machine $Machine -Config $Config -ConfigsOnly:$ConfigsOnly -ForceConfigs:$ForceConfigs -OutputPath $OutputPath -Paths $paths -RepositoryRoot $RepositoryRoot -FromSnapshot:$FromSnapshot -Revision $Revision + $result = Invoke-ConfigCentral -Command $Command -Machine $Machine -Config $Config -ConfigsOnly:$ConfigsOnly -ForceConfigs:$ForceConfigs -OutputPath $OutputPath -Paths $paths -RepositoryRoot $RepositoryRoot -FromSnapshot:$FromSnapshot -Revision $Revision -ShowProgress:(-not $Json) if ($Json) { $result | ConvertTo-Json -Depth 32 } else { Write-ConfigCentralResult $result } exit ([int]$result.exitCode) diff --git a/src/Core.psm1 b/src/Core.psm1 index 582e95e..ef843e5 100644 --- a/src/Core.psm1 +++ b/src/Core.psm1 @@ -138,15 +138,61 @@ function Invoke-ApplyPlan { finally { Exit-ApplyLock -Lock $lock -Paths $Paths } } +function Invoke-ConfigCentralChildProcess { + param( + [Parameter(Mandatory)][string]$FilePath, + [Parameter(Mandatory)][string[]]$Arguments, + [string]$Status, + [switch]$ShowProgress + ) + $startInfo = [Diagnostics.ProcessStartInfo]::new() + $startInfo.FileName = $FilePath + $startInfo.UseShellExecute = $false + $startInfo.RedirectStandardOutput = $true + $startInfo.RedirectStandardError = $true + foreach ($argument in $Arguments) { [void]$startInfo.ArgumentList.Add($argument) } + $process = [Diagnostics.Process]::new() + $process.StartInfo = $startInfo + $stopwatch = [Diagnostics.Stopwatch]::StartNew() + $animate = $ShowProgress -and -not [Console]::IsOutputRedirected + $started = $false + try { + if ($ShowProgress -and $Status) { Write-Host $Status } + if (-not $process.Start()) { throw "Could not start '$FilePath'." } + $started = $true + $outputTask = $process.StandardOutput.ReadToEndAsync() + $errorTask = $process.StandardError.ReadToEndAsync() + $frames = @('|', '/', '-', '\') + $frame = 0 + while (-not $process.WaitForExit(125)) { + if ($animate) { + Write-Host -NoNewline ("`r{0} Updating software... {1}s" -f $frames[$frame % $frames.Count], [int]$stopwatch.Elapsed.TotalSeconds) + $frame++ + } + } + if ($animate) { Write-Host -NoNewline ("`r{0}`r" -f (' ' * 56)) } + [pscustomobject]@{ + exitCode = $process.ExitCode + output = $outputTask.GetAwaiter().GetResult() + error = $errorTask.GetAwaiter().GetResult() + } + } + finally { + $stopwatch.Stop() + if ($started -and -not $process.HasExited) { $process.WaitForExit() } + $process.Dispose() + } +} + function Invoke-SnapshotCommand { - param([string]$Snapshot, [string]$Machine, $Paths, [string]$Revision, [ValidateSet('apply', 'update-software')][string]$Command, [switch]$ConfigsOnly, [switch]$ForceConfigs) + param([string]$Snapshot, [string]$Machine, $Paths, [string]$Revision, [ValidateSet('apply', 'update-software')][string]$Command, [switch]$ConfigsOnly, [switch]$ForceConfigs, [switch]$ShowProgress) $arguments = @('-NoLogo', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', (Join-Path $Snapshot 'cc.ps1'), $Command, '-Machine', $Machine, '-DataRoot', $Paths.Root, '-RepositoryRoot', $Snapshot, '-Revision', $Revision, '-FromSnapshot', '-Json') if ($ConfigsOnly) { $arguments += '-ConfigsOnly' } if ($ForceConfigs) { $arguments += '-ForceConfigs' } - $text = & pwsh.exe @arguments - $exitCode = $LASTEXITCODE - try { $child = ($text -join [Environment]::NewLine) | ConvertFrom-Json -Depth 32 } - catch { throw "Snapshot command returned invalid output (exit $exitCode): $($text -join [Environment]::NewLine)" } + $status = "Refreshing Config Central and updating software for '$Machine'. This can take several minutes..." + $process = Invoke-ConfigCentralChildProcess -FilePath 'pwsh.exe' -Arguments $arguments -Status $status -ShowProgress:$ShowProgress + try { $child = $process.output | ConvertFrom-Json -Depth 32 } + catch { throw "Snapshot command returned invalid output (exit $($process.exitCode)): $($process.output)$($process.error)" } return $child } @@ -154,7 +200,7 @@ function Invoke-ConfigCentral { param( [string]$Command, [string]$Machine, [string]$Config, [switch]$ConfigsOnly, [switch]$ForceConfigs, [string]$OutputPath, $Paths, [string]$RepositoryRoot, [switch]$FromSnapshot, - [string]$Revision + [string]$Revision, [switch]$ShowProgress ) if ($Command -eq 'doctor') { return Invoke-Doctor -Machine $Machine -RepositoryRoot $RepositoryRoot -Paths $Paths } if ($Command -eq 'configs') { @@ -200,7 +246,7 @@ function Invoke-ConfigCentral { $settings = Read-JsonState -Path $Paths.Settings -Default ([ordered]@{}) $remoteRevision = Resolve-RemoteRevision -RepositoryRoot $RepositoryRoot -Settings $settings $snapshot = New-RepositorySnapshot -RepositoryRoot $RepositoryRoot -Revision $remoteRevision -Paths $Paths - $result = Invoke-SnapshotCommand -Snapshot $snapshot -Machine $Machine -Paths $Paths -Revision $remoteRevision -Command update-software + $result = Invoke-SnapshotCommand -Snapshot $snapshot -Machine $Machine -Paths $Paths -Revision $remoteRevision -Command update-software -ShowProgress:$ShowProgress try { $selfUpdate = Update-RepositoryCheckout -RepositoryRoot $RepositoryRoot -Revision $remoteRevision } catch { $selfUpdate = [pscustomobject]@{ name = 'Config Central'; status = 'Failed'; message = $_.Exception.Message } diff --git a/tests/Cli.Tests.ps1 b/tests/Cli.Tests.ps1 index eaeaac0..c75a845 100644 --- a/tests/Cli.Tests.ps1 +++ b/tests/Cli.Tests.ps1 @@ -23,4 +23,10 @@ Describe 'Software update CLI' { ($output | Out-String) | Should -Match 'No repository branch is configured' ($output | Out-String) | Should -Not -Match 'ValidateSet|Cannot validate argument' } + It 'accepts software-update as an alias for the canonical command' { + $output = & (Join-Path $PSHOME $(if ($IsWindows) { 'pwsh.exe' } else { 'pwsh' })) -NoProfile -File "$PSScriptRoot/../cc.ps1" software-update -Machine missing-test-machine -DataRoot "$TestDrive/software-alias-data" 2>&1 + $LASTEXITCODE | Should -Be 2 + ($output | Out-String) | Should -Match 'No repository branch is configured' + ($output | Out-String) | Should -Not -Match 'ValidateSet|Cannot validate argument' + } } diff --git a/tests/Core.Tests.ps1 b/tests/Core.Tests.ps1 index a4907d2..befe47b 100644 --- a/tests/Core.Tests.ps1 +++ b/tests/Core.Tests.ps1 @@ -47,6 +47,30 @@ Describe 'Core workflow policy' { } } +Describe 'Child process progress feedback' { + It 'captures child output and reports a plain start status' { + InModuleScope Core { + Mock Write-Host {} + $powerShell = (Get-Process -Id $PID).Path + $arguments = @('-NoProfile', '-Command', '[Console]::Out.Write("child-output"); [Console]::Error.Write("child-error"); exit 7') + $result = Invoke-ConfigCentralChildProcess -FilePath $powerShell -Arguments $arguments -Status 'Updating test software...' -ShowProgress + $result.exitCode | Should -Be 7 + $result.output | Should -Be 'child-output' + $result.error | Should -Be 'child-error' + Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -eq 'Updating test software...' } + } + } + It 'keeps machine-readable child runs free of progress output' { + InModuleScope Core { + Mock Write-Host {} + $powerShell = (Get-Process -Id $PID).Path + $result = Invoke-ConfigCentralChildProcess -FilePath $powerShell -Arguments @('-NoProfile', '-Command', '[Console]::Out.Write("{}")') -Status 'Hidden status' + $result.output | Should -Be '{}' + Should -Invoke Write-Host -Times 0 + } + } +} + Describe 'Result status colors' { It 'colors statuses without coloring item names and messages' { InModuleScope Core { @@ -167,13 +191,13 @@ Describe 'Software update command' { Mock Invoke-SnapshotCommand { [pscustomobject]@{ command = $Command; revision = $Revision; operations = @(); succeeded = $true; exitCode = 0 } } Mock Update-RepositoryCheckout { [pscustomobject]@{ name = 'Config Central'; status = 'Succeeded'; message = 'Updated' } } $paths = Get-ConfigCentralPaths -DataRoot "$Working/software-refresh" - $result = Invoke-ConfigCentral -Command update-software -Machine test -Paths $paths -RepositoryRoot $Working + $result = Invoke-ConfigCentral -Command update-software -Machine test -Paths $paths -RepositoryRoot $Working -ShowProgress $result.command | Should -Be 'update-software' $result.revision | Should -Be 'remote-revision' $result.operations[0].name | Should -Be 'Config Central' Should -Invoke Resolve-RemoteRevision -Times 1 -Exactly Should -Invoke New-RepositorySnapshot -Times 1 -Exactly -ParameterFilter { $Revision -eq 'remote-revision' } - Should -Invoke Invoke-SnapshotCommand -Times 1 -Exactly -ParameterFilter { $Command -eq 'update-software' -and $Revision -eq 'remote-revision' } + Should -Invoke Invoke-SnapshotCommand -Times 1 -Exactly -ParameterFilter { $Command -eq 'update-software' -and $Revision -eq 'remote-revision' -and $ShowProgress } Should -Invoke Update-RepositoryCheckout -Times 1 -Exactly -ParameterFilter { $Revision -eq 'remote-revision' } } }