Skip to content

Commit 925776f

Browse files
committed
Improve Windows installer command output
1 parent 8022cfa commit 925776f

1 file changed

Lines changed: 57 additions & 11 deletions

File tree

install-windows.ps1

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,57 @@ function Wait-BeforeExit {
5656
}
5757
}
5858

59+
function Format-CommandForDisplay {
60+
param(
61+
[Parameter(Mandatory = $true)]
62+
[string]$FilePath,
63+
64+
[Parameter()]
65+
[string[]]$Arguments = @()
66+
)
67+
68+
$parts = @($FilePath) + @($Arguments)
69+
$quotedParts = foreach ($part in $parts) {
70+
if ($part -match '[\s"]') {
71+
'"' + ($part -replace '"', '\"') + '"'
72+
}
73+
else {
74+
$part
75+
}
76+
}
77+
78+
return ($quotedParts -join ' ')
79+
}
80+
81+
function Invoke-NativeCommand {
82+
param(
83+
[Parameter(Mandatory = $true)]
84+
[string]$FilePath,
85+
86+
[Parameter()]
87+
[string[]]$Arguments = @(),
88+
89+
[Parameter()]
90+
[string]$FailureMessage = 'Command failed.'
91+
)
92+
93+
Write-Host ("Running: {0}" -f (Format-CommandForDisplay -FilePath $FilePath -Arguments $Arguments)) -ForegroundColor DarkGray
94+
$output = & $FilePath @Arguments 2>&1
95+
$exitCode = $LASTEXITCODE
96+
97+
if ($output) {
98+
foreach ($line in @($output)) {
99+
Write-Host $line
100+
}
101+
}
102+
103+
if ($exitCode -ne 0) {
104+
Die $FailureMessage
105+
}
106+
107+
return @($output)
108+
}
109+
59110
function Die($Message) {
60111
Write-Host "Error: $Message" -ForegroundColor Red
61112
Wait-BeforeExit
@@ -146,24 +197,19 @@ then run this installer again.
146197
Write-Host "Using Python: $python"
147198

148199
Log 'Checking built-in venv support'
149-
& $python -m venv --help > $null 2>&1
150-
if ($LASTEXITCODE -ne 0) {
151-
Die 'This Python does not support the built-in venv module. Install a full Python distribution and try again.'
152-
}
200+
Invoke-NativeCommand -FilePath $python -Arguments @('-c', 'import venv') -FailureMessage 'This Python does not support the built-in venv module. Install a full Python distribution and try again.' | Out-Null
153201

154202
Log 'Creating private Python environment'
155203
if (-not (Test-Path $AppHome)) {
156204
New-Item -ItemType Directory -Path $AppHome -Force | Out-Null
157205
}
158-
& $python -m venv $VenvDir
159-
if ($LASTEXITCODE -ne 0) { Die 'Failed to create virtual environment.' }
206+
Invoke-NativeCommand -FilePath $python -Arguments @('-m', 'venv', $VenvDir) -FailureMessage 'Failed to create virtual environment.' | Out-Null
160207

161208
$venvPython = Join-Path $ScriptsDir 'python.exe'
162209

163210
Log 'Installing / upgrading Slicer URI Bridge'
164-
& $venvPython -m pip install --upgrade pip 2>&1 | Out-Null
165-
& $venvPython -m pip install --upgrade $ProjectSpec
166-
if ($LASTEXITCODE -ne 0) { Die 'pip install failed.' }
211+
Invoke-NativeCommand -FilePath $venvPython -Arguments @('-m', 'pip', 'install', '--upgrade', 'pip') -FailureMessage 'Failed to upgrade pip.' | Out-Null
212+
Invoke-NativeCommand -FilePath $venvPython -Arguments @('-m', 'pip', 'install', '--upgrade', $ProjectSpec) -FailureMessage 'pip install failed.' | Out-Null
167213

168214
Log 'Adding Scripts directory to user PATH'
169215
$added = Add-ToUserPath $ScriptsDir
@@ -175,10 +221,10 @@ then run this installer again.
175221
}
176222

177223
Log 'Creating config if needed'
178-
& $BridgeExe init-config 2>&1 | Out-Null
224+
Invoke-NativeCommand -FilePath $BridgeExe -Arguments @('init-config') -FailureMessage 'Failed to create config.' | Out-Null
179225

180226
Log 'Registering URI handlers'
181-
& $BridgeExe register --auto
227+
Invoke-NativeCommand -FilePath $BridgeExe -Arguments @('register', '--auto') -FailureMessage 'Failed to register URI handlers.' | Out-Null
182228

183229
$configDir = Join-Path $env:APPDATA 'slicer-uri-bridge'
184230

0 commit comments

Comments
 (0)