diff --git a/TASKS.md b/TASKS.md index b9bf4f6..50d7bbc 100644 --- a/TASKS.md +++ b/TASKS.md @@ -174,3 +174,7 @@ open until it is run on disposable machines with the required account setups. ## Requested Beffroi tools - [x] Add verified PPTX Reporter 0.2.0 portable installation and update PDF Presenter to the stable 0.1.91 release. + +## Setup progress feedback + +- [x] Show elapsed spinner feedback during the initial apply, keep redirected output plain, and replay child results and diagnostics after completion. diff --git a/bootstrap.ps1 b/bootstrap.ps1 index d6d21cb..dd11cc4 100644 --- a/bootstrap.ps1 +++ b/bootstrap.ps1 @@ -83,6 +83,46 @@ function Assert-BootstrapPowerShell { } } +function Invoke-BootstrapApply { + param( + [Parameter(Mandatory = $true)][string]$PowerShell, + [Parameter(Mandatory = $true)][string[]]$Arguments, + [Parameter(Mandatory = $true)][string]$Machine + ) + $outputPath = [System.IO.Path]::GetTempFileName() + $errorPath = [System.IO.Path]::GetTempFileName() + $process = $null + $stopwatch = [Diagnostics.Stopwatch]::StartNew() + $animate = -not [Console]::IsOutputRedirected + try { + $quotedArguments = @($Arguments | ForEach-Object { + if ($_ -match '"') { throw 'Bootstrap apply arguments cannot contain quotation marks.' } + '"' + $_ + '"' + }) + Write-ConfigCentralMessage -Message "Applying software and configs for '$Machine'. This can take several minutes..." + $process = Start-Process -FilePath $PowerShell -ArgumentList $quotedArguments -PassThru -NoNewWindow -RedirectStandardOutput $outputPath -RedirectStandardError $errorPath + $frames = @('|', '/', '-', '\') + $frame = 0 + while (-not $process.WaitForExit(125)) { + if ($animate) { + Write-Host -NoNewline ("`r{0} Working... {1}s" -f $frames[$frame % $frames.Count], [int]$stopwatch.Elapsed.TotalSeconds) + $frame++ + } + } + if ($animate) { Write-Host -NoNewline ("`r{0}`r" -f (' ' * 48)) } + $output = Get-Content -LiteralPath $outputPath -Raw -ErrorAction SilentlyContinue + $errors = Get-Content -LiteralPath $errorPath -Raw -ErrorAction SilentlyContinue + if ($output) { [Console]::Out.Write($output) } + if ($errors) { [Console]::Error.Write($errors) } + return $process.ExitCode + } + finally { + $stopwatch.Stop() + if ($process -and -not $process.HasExited) { $process.WaitForExit() } + Remove-Item -LiteralPath $outputPath, $errorPath -Force -ErrorAction SilentlyContinue + } +} + function Find-GitPath { $command = Get-Command git.exe -ErrorAction SilentlyContinue if ($command) { return $command.Source } @@ -218,8 +258,8 @@ if (-not $SkipApply) { (Join-Path $Checkout 'cc.ps1'), 'apply', '-Machine', $Machine, '-DataRoot', $dataRoot, '-RepositoryRoot', $Checkout) if (-not $PreserveLocalConfigs) { $applyArguments += '-ForceConfigs' } - & $powerShell7 @applyArguments - exit $LASTEXITCODE + $applyExitCode = Invoke-BootstrapApply -PowerShell $powerShell7 -Arguments $applyArguments -Machine $Machine + exit $applyExitCode } $global:LASTEXITCODE = 0 diff --git a/tests/Bootstrap.Tests.ps1 b/tests/Bootstrap.Tests.ps1 index ac2eb6d..15cfedd 100644 --- a/tests/Bootstrap.Tests.ps1 +++ b/tests/Bootstrap.Tests.ps1 @@ -87,3 +87,37 @@ Describe 'Bootstrap PowerShell prerequisites' { { Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Throw '*could not start*' } } + +Describe 'Bootstrap first-apply feedback' { + BeforeAll { + $fn = $bootstrapAst.Find({ + param($node) + $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Invoke-BootstrapApply' + }, $true) + . ([scriptblock]::Create($fn.Extent.Text)) + } + + It 'shows the active machine and preserves child output, diagnostics, and exit code' { + $powerShell = (Get-Process -Id $PID).Path + Mock Write-ConfigCentralMessage + $originalOutput = [Console]::Out + $originalError = [Console]::Error + $capturedOutput = [IO.StringWriter]::new() + $capturedError = [IO.StringWriter]::new() + try { + [Console]::SetOut($capturedOutput) + [Console]::SetError($capturedError) + $code = Invoke-BootstrapApply -PowerShell $powerShell -Arguments @('-NoProfile', '-Command', 'Write-Output child-output; [Console]::Error.WriteLine(123); exit 7') -Machine 'test-machine' + } + finally { + [Console]::SetOut($originalOutput) + [Console]::SetError($originalError) + } + $code | Should -Be 7 + $capturedOutput.ToString() | Should -Match 'child-output' + $capturedError.ToString() | Should -Match '123' + Should -Invoke Write-ConfigCentralMessage -Times 1 -Exactly -ParameterFilter { + $Message -match "Applying software and configs for 'test-machine'" + } + } +} diff --git a/tests/Installer.Tests.ps1 b/tests/Installer.Tests.ps1 index 3d7b4dc..ed59bb9 100644 --- a/tests/Installer.Tests.ps1 +++ b/tests/Installer.Tests.ps1 @@ -75,7 +75,7 @@ Describe 'Setup config replacement policy' { $installerText = Get-Content "$PSScriptRoot/../install.ps1" -Raw $bootstrapText = Get-Content "$PSScriptRoot/../bootstrap.ps1" -Raw $installerArguments = [regex]::Match($installerText, '(?s) \$arguments = @\(.*?(?= & \$windowsPowerShell)').Value - $bootstrapArguments = [regex]::Match($bootstrapText, '(?s) \$applyArguments = @\(.*?(?= & \$powerShell7)').Value + $bootstrapArguments = [regex]::Match($bootstrapText, '(?s) \$applyArguments = @\(.*?(?= \$applyExitCode = Invoke-BootstrapApply)').Value $installerArguments | Should -Not -BeNullOrEmpty $bootstrapArguments | Should -Not -BeNullOrEmpty $bootstrapPath = 'bootstrap.ps1'; $repository = 'https://example.test/repo.git'