|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +# Copyright (c) 2021-2026 Claudio Satriano <satriano@ipgp.fr> |
| 3 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +<# |
| 6 | +.SYNOPSIS |
| 7 | +Install or update SeisCat with uv on Windows. |
| 8 | +
|
| 9 | +.DESCRIPTION |
| 10 | +This script: |
| 11 | +1) Checks if uv is installed and installs it if missing. |
| 12 | +2) Installs/updates SeisCat with optional plotting dependencies. |
| 13 | +3) Installs/updates argcomplete. |
| 14 | +4) Runs activate-global-python-argcomplete. |
| 15 | +
|
| 16 | +.PARAMETER DryRun |
| 17 | +Print planned commands without executing them. |
| 18 | +
|
| 19 | +.PARAMETER Help |
| 20 | +Show usage help and exit. |
| 21 | +
|
| 22 | +.EXAMPLE |
| 23 | +pwsh -NoProfile -File scripts/install_seiscat_uv.ps1 |
| 24 | +
|
| 25 | +.EXAMPLE |
| 26 | +pwsh -NoProfile -File scripts/install_seiscat_uv.ps1 -DryRun |
| 27 | +
|
| 28 | +.EXAMPLE |
| 29 | +pwsh -NoProfile -File scripts/install_seiscat_uv.ps1 -Help |
| 30 | +#> |
| 31 | + |
| 32 | +param( |
| 33 | + [switch]$DryRun, |
| 34 | + [Alias('h')][switch]$Help |
| 35 | +) |
| 36 | + |
| 37 | +Set-StrictMode -Version Latest |
| 38 | +$ErrorActionPreference = 'Stop' |
| 39 | + |
| 40 | +function Write-Info { |
| 41 | + param([Parameter(Mandatory = $true)][string]$Message) |
| 42 | + Write-Host "[INFO] $Message" -ForegroundColor Cyan |
| 43 | +} |
| 44 | + |
| 45 | +function Write-Ok { |
| 46 | + param([Parameter(Mandatory = $true)][string]$Message) |
| 47 | + Write-Host "[OK] $Message" -ForegroundColor Green |
| 48 | +} |
| 49 | + |
| 50 | +function Write-WarnMsg { |
| 51 | + param([Parameter(Mandatory = $true)][string]$Message) |
| 52 | + Write-Host "[WARN] $Message" -ForegroundColor Yellow |
| 53 | +} |
| 54 | + |
| 55 | +function Fail { |
| 56 | + param([Parameter(Mandatory = $true)][string]$Message) |
| 57 | + Write-Host "[ERROR] $Message" -ForegroundColor Red |
| 58 | + exit 1 |
| 59 | +} |
| 60 | + |
| 61 | +function Show-Usage { |
| 62 | + @' |
| 63 | +Usage: |
| 64 | + pwsh -NoProfile -File scripts/install_seiscat_uv.ps1 [-DryRun] [-Help] |
| 65 | +
|
| 66 | +Options: |
| 67 | + -DryRun Print all steps and commands without executing them. |
| 68 | + -Help Show this help message and exit. |
| 69 | +'@ | Write-Host |
| 70 | +} |
| 71 | + |
| 72 | +function Test-Command { |
| 73 | + param([Parameter(Mandatory = $true)][string]$Name) |
| 74 | + return $null -ne (Get-Command $Name -ErrorAction SilentlyContinue) |
| 75 | +} |
| 76 | + |
| 77 | +function Invoke-StepCommand { |
| 78 | + param( |
| 79 | + [Parameter(Mandatory = $true)][string]$Display, |
| 80 | + [Parameter(Mandatory = $true)][scriptblock]$ScriptBlock |
| 81 | + ) |
| 82 | + |
| 83 | + Write-Info "Running: $Display" |
| 84 | + if ($DryRun) { |
| 85 | + Write-WarnMsg "Dry-run mode enabled: command not executed." |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + & $ScriptBlock |
| 90 | +} |
| 91 | + |
| 92 | +function Update-PathForCurrentSession { |
| 93 | + $candidateDirs = @( |
| 94 | + (Join-Path $HOME ".local\\bin"), |
| 95 | + (Join-Path $HOME ".cargo\\bin") |
| 96 | + ) |
| 97 | + |
| 98 | + foreach ($dir in $candidateDirs) { |
| 99 | + if ((Test-Path $dir) -and ($env:PATH -notlike "*$dir*")) { |
| 100 | + $env:PATH = "$dir;$env:PATH" |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +function Install-Uv { |
| 106 | + Write-Info "uv is not installed. Installing uv..." |
| 107 | + |
| 108 | + if (Test-Command "winget") { |
| 109 | + Write-Info "Installing uv with winget..." |
| 110 | + Invoke-StepCommand -Display "winget install --id Astral-sh.uv -e --accept-package-agreements --accept-source-agreements" -ScriptBlock { |
| 111 | + winget install --id Astral-sh.uv -e --accept-package-agreements --accept-source-agreements |
| 112 | + } |
| 113 | + } |
| 114 | + elseif (Test-Command "scoop") { |
| 115 | + Write-Info "Installing uv with scoop..." |
| 116 | + Invoke-StepCommand -Display "scoop install uv" -ScriptBlock { |
| 117 | + scoop install uv |
| 118 | + } |
| 119 | + } |
| 120 | + elseif (Test-Command "choco") { |
| 121 | + Write-Info "Installing uv with chocolatey..." |
| 122 | + Invoke-StepCommand -Display "choco install uv -y" -ScriptBlock { |
| 123 | + choco install uv -y |
| 124 | + } |
| 125 | + } |
| 126 | + else { |
| 127 | + Write-Info "No package manager detected. Installing uv via official install script..." |
| 128 | + Invoke-StepCommand -Display "Invoke-RestMethod -Uri https://astral.sh/uv/install.ps1 | run" -ScriptBlock { |
| 129 | + & ([scriptblock]::Create((Invoke-RestMethod -Uri "https://astral.sh/uv/install.ps1"))) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + Update-PathForCurrentSession |
| 134 | + |
| 135 | + if ($DryRun) { |
| 136 | + Write-Ok "uv installation skipped in dry-run mode." |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + if (-not (Test-Command "uv")) { |
| 141 | + Fail "uv installation did not complete successfully. Open a new terminal and retry." |
| 142 | + } |
| 143 | + |
| 144 | + Write-Ok "uv installed successfully: $(uv --version)" |
| 145 | +} |
| 146 | + |
| 147 | +function Activate-Argcomplete { |
| 148 | + Write-Info "Step 4/4: Running activate-global-python-argcomplete..." |
| 149 | + |
| 150 | + $activationCmd = Get-Command "activate-global-python-argcomplete" -ErrorAction SilentlyContinue |
| 151 | + |
| 152 | + if ($null -ne $activationCmd) { |
| 153 | + try { |
| 154 | + Invoke-StepCommand -Display "activate-global-python-argcomplete --user" -ScriptBlock { |
| 155 | + & $activationCmd.Source --user |
| 156 | + } |
| 157 | + Write-Ok "Global argcomplete activation completed (--user mode)." |
| 158 | + } |
| 159 | + catch { |
| 160 | + Write-WarnMsg "activate-global-python-argcomplete --user failed. Retrying without --user..." |
| 161 | + Invoke-StepCommand -Display "activate-global-python-argcomplete" -ScriptBlock { |
| 162 | + & $activationCmd.Source |
| 163 | + } |
| 164 | + Write-Ok "Global argcomplete activation completed." |
| 165 | + } |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + Write-WarnMsg "activate-global-python-argcomplete is not on PATH. Trying via uv tool run..." |
| 170 | + try { |
| 171 | + Invoke-StepCommand -Display "uv tool run --from argcomplete activate-global-python-argcomplete --user" -ScriptBlock { |
| 172 | + uv tool run --from argcomplete activate-global-python-argcomplete --user |
| 173 | + } |
| 174 | + Write-Ok "Global argcomplete activation completed via uv tool run (--user mode)." |
| 175 | + } |
| 176 | + catch { |
| 177 | + Write-WarnMsg "argcomplete activation command failed on this shell." |
| 178 | + Write-WarnMsg "On Windows, this command mainly targets bash-like shells." |
| 179 | + Write-WarnMsg "If you use Git Bash, run activate-global-python-argcomplete there." |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +if ($Help) { |
| 184 | + Show-Usage |
| 185 | + exit 0 |
| 186 | +} |
| 187 | + |
| 188 | +Write-Info "Starting SeisCat installation with uv for Windows..." |
| 189 | + |
| 190 | +if ($DryRun) { |
| 191 | + Write-WarnMsg "Dry-run mode enabled: no commands will be executed and no files will be modified." |
| 192 | +} |
| 193 | + |
| 194 | +Write-Info "Step 1/4: Checking if uv is installed..." |
| 195 | +if (Test-Command "uv") { |
| 196 | + Write-Ok "uv is already installed: $(uv --version)" |
| 197 | +} |
| 198 | +else { |
| 199 | + Install-Uv |
| 200 | +} |
| 201 | + |
| 202 | +Write-Info "Step 2/4: Installing/updating seiscat with plotly, cartopy, folium, and pandas support using uv tool install..." |
| 203 | +Invoke-StepCommand -Display "uv tool install seiscat --with plotly --with cartopy --with folium --with pandas --upgrade --force" -ScriptBlock { |
| 204 | + uv tool install seiscat --with plotly --with cartopy --with folium --with pandas --upgrade --force |
| 205 | +} |
| 206 | +Write-Ok "seiscat installed/updated with plotly, cartopy, folium, and pandas support." |
| 207 | + |
| 208 | +Write-Info "Step 3/4: Installing/updating argcomplete using uv tool install..." |
| 209 | +Invoke-StepCommand -Display "uv tool install argcomplete --upgrade --force" -ScriptBlock { |
| 210 | + uv tool install argcomplete --upgrade --force |
| 211 | +} |
| 212 | +Write-Ok "argcomplete installed/updated successfully." |
| 213 | + |
| 214 | +Activate-Argcomplete |
| 215 | + |
| 216 | +Write-Ok "All steps completed." |
| 217 | +Write-Info "If seiscat is not found in this terminal, open a new terminal window and try again." |
0 commit comments