-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwin.ps1
More file actions
208 lines (182 loc) · 7.26 KB
/
win.ps1
File metadata and controls
208 lines (182 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env pwsh
# win.ps1 - Run bash scripts from PowerShell via Git Bash
# Usage: .\win.ps1 ./test-scenario.sh --pipelines-collab --interactive
$GitBash = "C:\Program Files\Git\bin\bash.exe"
if (-not (Test-Path $GitBash)) {
Write-Host "Git Bash not found at $GitBash" -ForegroundColor Red
exit 1
}
$useDesktop = $false
$desktopWait = $false
$sessionId = $null
$psexecPath = $null
$forwardArgs = @()
function Get-ActiveSessionId {
$sessions = @()
try { $sessions = & query session 2>$null } catch { $sessions = @() }
foreach ($line in $sessions) {
if ($line -match '\s+(\d+)\s+Active') {
return $Matches[1]
}
}
$explorer = Get-Process explorer -ErrorAction SilentlyContinue | Select-Object -First 1
if ($explorer) { return $explorer.SessionId }
return $null
}
for ($i = 0; $i -lt $args.Count; $i++) {
$arg = $args[$i]
switch ($arg) {
"--desktop" { $useDesktop = $true; continue }
"--desktop-wait" { $useDesktop = $true; $desktopWait = $true; continue }
"--session" {
if ($i + 1 -ge $args.Count) { Write-Host "Missing value for --session" -ForegroundColor Red; exit 1 }
$sessionId = $args[++$i]; continue
}
"--psexec" {
if ($i + 1 -ge $args.Count) { Write-Host "Missing value for --psexec" -ForegroundColor Red; exit 1 }
$psexecPath = $args[++$i]; continue
}
default { $forwardArgs += $arg }
}
}
if ($useDesktop) {
if (-not $psexecPath) {
$psexecCmd = Get-Command psexec -ErrorAction SilentlyContinue
if ($psexecCmd) { $psexecPath = $psexecCmd.Source }
}
if (-not $psexecPath) {
$defaultPsexec = "C:\Tools\PSTools\PsExec.exe"
if (Test-Path $defaultPsexec) { $psexecPath = $defaultPsexec }
}
if (-not $psexecPath -or -not (Test-Path $psexecPath)) {
Write-Host "PsExec not found. Install Sysinternals PsExec and ensure it's on PATH or pass --psexec <path> (default: C:\\Tools\\PSTools\\PsExec.exe)." -ForegroundColor Red
exit 1
}
if (-not $sessionId) { $sessionId = Get-ActiveSessionId }
if (-not $sessionId) {
Write-Host "Could not determine active desktop session. Pass --session <id>." -ForegroundColor Red
exit 1
}
$wd = (Get-Location).Path
$psArgs = @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", $PSCommandPath) + $forwardArgs
$psexecArgs = @("-accepteula", "-i", $sessionId, "-w", $wd)
if (-not $desktopWait) { $psexecArgs += "-d" }
$psexecArgs += @("powershell.exe") + $psArgs
& $psexecPath @psexecArgs
exit $LASTEXITCODE
}
if ($forwardArgs.Count -eq 0) {
Write-Host "Usage: .\win.ps1 [--desktop [--session <id>] [--desktop-wait] [--psexec <path>]] <script> [args...]" -ForegroundColor Yellow
Write-Host "Example: .\win.ps1 ./test-scenario.sh --pipelines-collab --interactive"
Write-Host "Example (desktop): .\win.ps1 --desktop --session 1 ./test-scenario.sh --pipelines-collab --interactive"
exit 1
}
# Stop any running bv.exe from this workspace to avoid build locks.
function Stop-ProcessByPathPrefix {
param([string]$Prefix)
$stopped = $false
Get-Process -ErrorAction SilentlyContinue | ForEach-Object {
try {
if ($_.Path -and $_.Path.StartsWith($Prefix, [System.StringComparison]::OrdinalIgnoreCase)) {
Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue
$stopped = $true
}
} catch { }
}
return $stopped
}
$killedNames = @("bv", "bv.exe", "bv-desktop", "bv-desktop.exe")
foreach ($name in $killedNames) {
try { Stop-Process -Name $name -Force -ErrorAction SilentlyContinue } catch { }
try { & taskkill /F /IM $name /T 2>$null | Out-Null } catch { }
}
$workspaceRoot = (Get-Location).Path
$targetRoots = @(
(Join-Path $workspaceRoot "biovault\\cli\\target"),
(Join-Path $workspaceRoot "src-tauri\\target")
)
foreach ($root in $targetRoots) {
if (Test-Path $root) {
if (Stop-ProcessByPathPrefix $root) {
Write-Host "Stopped running binaries from $root" -ForegroundColor DarkYellow
}
}
}
# Convert Windows path to Unix path for the script
$script = $forwardArgs[0]
$scriptArgs = if ($forwardArgs.Count -gt 1) { @($forwardArgs[1..($forwardArgs.Count-1)]) } else { @() }
# Get current directory in Unix format
$unixPath = (Get-Location).Path -replace '\\', '/' -replace '^([A-Za-z]):', '/$1'
$unixPath = $unixPath.ToLower() -replace '^/([a-z])', '/$1'
function Convert-ToUnixPath {
param([string]$Path)
$resolved = Resolve-Path -Path $Path -ErrorAction Stop
$unix = $resolved.Path -replace '\\', '/' -replace '^([A-Za-z]):', '/$1'
return ($unix.ToLower() -replace '^/([a-z])', '/$1')
}
# Convert script path to Unix format (resolve relative paths)
$scriptUnix = Convert-ToUnixPath $script
# Key Windows tool paths to add (converted to Unix format)
# Order matters - put preferred paths first
$toolPaths = @(
"$env:USERPROFILE\.local\bin"
"$env:USERPROFILE\.cargo\bin"
"$env:LOCALAPPDATA\Programs\Python\Python311"
"$env:LOCALAPPDATA\Programs\Python\Python311\Scripts"
"$env:PROGRAMFILES\nodejs"
"$env:PROGRAMFILES\Go\bin"
"$env:USERPROFILE\go\bin"
"$env:LOCALAPPDATA\Microsoft\WinGet\Packages\Google.Protobuf_Microsoft.Winget.Source_8wekyb3d8bbwe\bin"
"$env:LOCALAPPDATA\Microsoft\WinGet\Links"
"$env:PROGRAMFILES\Bun\bin"
"$env:USERPROFILE\.bun\bin"
)
# Find uv-managed Python and add to paths
$uvPythonBase = "$env:APPDATA\uv\python"
if (Test-Path $uvPythonBase) {
$pythonDirs = Get-ChildItem -Path $uvPythonBase -Directory -Filter "cpython-3.11*" | Select-Object -First 1
if ($pythonDirs) {
$toolPaths = @($pythonDirs.FullName) + @("$($pythonDirs.FullName)\Scripts") + $toolPaths
}
}
$unixToolPaths = @()
foreach ($p in $toolPaths) {
$expanded = [Environment]::ExpandEnvironmentVariables($p)
if (Test-Path $expanded) {
if ($expanded -match '^([A-Za-z]):(.*)') {
$drive = $Matches[1].ToLower()
$rest = $Matches[2] -replace '\\', '/'
$unixToolPaths += "/$drive$rest"
}
}
}
$extraPath = ($unixToolPaths -join ':')
# Build the command with PATH additions
$cmd = @"
export PATH="$extraPath`:`$PATH"
cd '$unixPath'
# On Windows, python3 might not exist but python does
# Create function wrappers that work in subshells (aliases don't)
if ! command -v python3 &> /dev/null && command -v python &> /dev/null; then
python3() { python "`$@"; }
export -f python3
fi
$scriptUnix "`$@"
"@
Write-Host "Running: $script $($scriptArgs -join ' ')" -ForegroundColor Cyan
Write-Host "PATH additions: $extraPath" -ForegroundColor DarkGray
Write-Host ""
# Debug: show command being run
# Write-Host "CMD: $cmd" -ForegroundColor DarkGray
# Write command to temp file and execute
$tempScript = [System.IO.Path]::GetTempFileName() + ".sh"
[System.IO.File]::WriteAllText($tempScript, $cmd, [System.Text.UTF8Encoding]::new($false))
# Convert temp path to Unix format for bash
$unixTempScript = $tempScript -replace '\\', '/' -replace '^([A-Za-z]):', '/$1'
$unixTempScript = $unixTempScript.ToLower() -replace '^/([a-z])', '/$1'
$scriptArgsList = @($scriptArgs)
try {
& $GitBash $unixTempScript @scriptArgsList
} finally {
Remove-Item $tempScript -ErrorAction SilentlyContinue
}