diff --git a/scripts/install.ps1 b/scripts/install.ps1 index a2f7dea..1c60a31 100644 --- a/scripts/install.ps1 +++ b/scripts/install.ps1 @@ -101,12 +101,52 @@ function Assert-OutsideRepo([string]$Path) { function Test-HerdrApp { return [bool](Get-Command 'herdr-app' -ErrorAction SilentlyContinue) } +# Runs herdr-app and waits for it. +# +# herdr-app.exe is a program of the GUI subsystem: PowerShell does not wait for +# it and does not set $LASTEXITCODE (whatever the last console program left +# there stays - 1 after robocopy means "copied"). So it is started with +# Start-Process, waited for, and its exit code and output are read from the +# process and from two temporary files. +# +# Returns ExitCode, Output (stdout) and Error (stderr). +function Invoke-HerdrApp([string[]]$Arguments) { + $exe = (Get-Command 'herdr-app' -ErrorAction Stop).Source + $outFile = [IO.Path]::GetTempFileName() + $errFile = [IO.Path]::GetTempFileName() + try { + $quoted = @($Arguments | ForEach-Object { + if ($_ -match '[\s"]') { '"' + ($_ -replace '"', '\"') + '"' } else { $_ } + }) + $process = Start-Process -FilePath $exe -ArgumentList $quoted -NoNewWindow -PassThru ` + -RedirectStandardOutput $outFile -RedirectStandardError $errFile + # Holding the handle keeps ExitCode readable after the process ended. + $null = $process.Handle + $process.WaitForExit() + return [pscustomobject]@{ + ExitCode = $process.ExitCode + Output = [IO.File]::ReadAllText($outFile) + Error = [IO.File]::ReadAllText($errFile) + } + } finally { + Remove-Item -LiteralPath $outFile, $errFile -Force -ErrorAction SilentlyContinue + } +} + +# Prints what herdr-app said (stdout, then stderr). +function Write-HerdrOutput($Result) { + foreach ($text in @($Result.Output, $Result.Error)) { + if ($text -and $text.Trim() -ne '') { Write-Host ($text.TrimEnd()) } + } +} + # The name of the layout the window shows now, or nothing when it cannot be told. function Get-ActiveLayout { if (-not (Test-HerdrApp)) { return $null } try { - $status = (& herdr-app status | Out-String) | ConvertFrom-Json - return [string]$status.layout + $result = Invoke-HerdrApp @('status') + if ($result.ExitCode -ne 0) { return $null } + return [string]($result.Output | ConvertFrom-Json).layout } catch { return $null } @@ -117,9 +157,11 @@ function Switch-Layout([string]$LayoutName) { Write-Warning "herdr-app is not on the PATH - switch the layout to '$LayoutName' by hand (ctrl-shift-l)." return } - & herdr-app do layout.switch --name $LayoutName - if ($LASTEXITCODE -ne 0) { - Write-Warning "layout.switch to '$LayoutName' failed (exit $LASTEXITCODE) - switch it by hand (ctrl-shift-l)." + # `--name `, not a JSON argument: quotes inside an argument do not survive. + $result = Invoke-HerdrApp @('do', 'layout.switch', '--name', $LayoutName) + Write-HerdrOutput $result + if ($result.ExitCode -ne 0) { + Write-Warning "layout.switch to '$LayoutName' failed (exit $($result.ExitCode)) - switch it by hand (ctrl-shift-l)." } } @@ -287,7 +329,11 @@ if (Test-Path -LiteralPath $OldLayout) { New-Item -ItemType Directory -Force -Path $ModsDir | Out-Null if (Test-Junction $ModDst) { Remove-Junction $ModDst } robocopy $ModSrc $ModDst /MIR /XF gpui-kit.d.ts jsconfig.json /NFL /NDL /NJH /NJS /NP | Out-Null -if ($LASTEXITCODE -ge 8) { throw "robocopy failed (exit $LASTEXITCODE)" } +$robocopyExit = $LASTEXITCODE +# 0-7 are all "fine" for robocopy (1 = files were copied); nothing below may +# read this code as if it were the result of its own command. +$global:LASTEXITCODE = 0 +if ($robocopyExit -ge 8) { throw "robocopy failed (exit $robocopyExit)" } Write-Host "Mod copied: $ModDst" # 4. The layout: an existing one that differs is saved first, an identical one stays. @@ -312,8 +358,9 @@ Update-Keys # 6. Check the mod; switch the layout only where the person has not chosen another one. if (Test-HerdrApp) { - & herdr-app check $ModDst - if ($LASTEXITCODE -ne 0) { Write-Warning "herdr-app check reported a problem (exit $LASTEXITCODE)." } + $check = Invoke-HerdrApp @('check', $ModDst) + Write-HerdrOutput $check + if ($check.ExitCode -ne 0) { Write-Warning "herdr-app check reported a problem (exit $($check.ExitCode))." } } else { Write-Warning 'herdr-app is not on the PATH - the mod was not checked.' }