-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.ps1
More file actions
164 lines (148 loc) · 6.56 KB
/
Copy pathrun.ps1
File metadata and controls
164 lines (148 loc) · 6.56 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
# AgentHub desktop launcher (PowerShell)
$ErrorActionPreference = "Stop"
Set-Location -Path $PSScriptRoot
# Double-click / Explorer cmd often inherits a stale PATH. Merge Machine + User
# and put cargo/pnpm first so `tauri:dev` finds the same tools as a terminal.
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
$extra = @(
(Join-Path $env:USERPROFILE '.cargo\bin'),
(Join-Path $env:LOCALAPPDATA 'pnpm')
) | Where-Object { $_ -and (Test-Path $_) }
$env:Path = (@($extra + $machinePath + $userPath + $env:Path) | Where-Object { $_ }) -join ';'
$runtime = Get-Content -LiteralPath (Join-Path $PSScriptRoot 'scripts\dev-runtime.json') -Raw | ConvertFrom-Json
$DevPort = [int]$runtime.port
$AutoFreePort = $false
$StopOnly = $false
$forwardArgs = @()
foreach ($a in $args) {
switch -Regex ($a) {
'^(?i)(--restart|-restart)$' { $AutoFreePort = $true }
'^(?i)(--stop|-stop)$' { $StopOnly = $true; $AutoFreePort = $true }
default { $forwardArgs += $a }
}
}
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " AgentHub Desktop Launcher" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Working dir: $PWD"
Write-Host ""
function Fail([string]$msg) {
Write-Host "[ERROR] $msg" -ForegroundColor Red
Write-Host ""
Write-Host "Press any key to close..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
function Get-ListenersOnPort([int]$Port) {
# Prefer Get-NetTCPConnection; fall back to netstat parsing.
$rows = @()
try {
$conns = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction Stop
foreach ($c in $conns) {
$p = Get-Process -Id $c.OwningProcess -ErrorAction SilentlyContinue
$rows += [pscustomobject]@{
Pid = $c.OwningProcess
Name = if ($p) { $p.ProcessName } else { '?' }
Path = if ($p) { $p.Path } else { $null }
}
}
} catch {
$lines = netstat -ano -p tcp 2>$null | Select-String ":$Port\s+.*LISTENING\s+(\d+)\s*$"
foreach ($m in $lines) {
if ($m.Line -match 'LISTENING\s+(\d+)\s*$') {
$procId = [int]$Matches[1]
$p = Get-Process -Id $procId -ErrorAction SilentlyContinue
$rows += [pscustomobject]@{
Pid = $procId
Name = if ($p) { $p.ProcessName } else { '?' }
Path = if ($p) { $p.Path } else { $null }
}
}
}
}
$rows | Sort-Object Pid -Unique
}
function Ensure-DevPortFree([int]$Port, [bool]$Force = $false) {
$holders = @(Get-ListenersOnPort $Port)
if ($holders.Count -eq 0) { return }
Write-Host "[WARN] Port $Port is already in use (Vite/tauri:dev needs it exclusive)." -ForegroundColor Yellow
foreach ($h in $holders) {
Write-Host (" - PID {0} {1} {2}" -f $h.Pid, $h.Name, $h.Path) -ForegroundColor DarkYellow
}
Write-Host ""
if (-not $Force) {
Write-Host "Common causes: previous tauri:dev still running, or pnpm dev / dev:mock." -ForegroundColor DarkGray
Write-Host "Press Y to stop those process(es) and continue, or any other key to abort." -ForegroundColor Yellow
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
if ($key.Character -ne 'y' -and $key.Character -ne 'Y') {
Fail "Aborted: free port $Port then retry (e.g. stop old node/vite / close other AgentHub dev)."
}
} else {
Write-Host "[INFO] -Restart/-Stop: stopping those process(es)." -ForegroundColor DarkGray
}
foreach ($h in $holders) {
try {
Stop-Process -Id $h.Pid -Force -ErrorAction Stop
Write-Host "[INFO] Stopped PID $($h.Pid) ($($h.Name))" -ForegroundColor Green
} catch {
Fail "Could not stop PID $($h.Pid): $($_.Exception.Message)"
}
}
Start-Sleep -Milliseconds 400
$still = @(Get-ListenersOnPort $Port)
if ($still.Count -gt 0) {
Fail "Port $Port still busy after kill. Close the process manually and retry."
}
}
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
Fail "Node.js not found. Install: https://nodejs.org/"
}
if (-not (Get-Command cargo -ErrorAction SilentlyContinue)) {
Fail "Rust/Cargo not found. Install: https://rustup.rs/ and ensure %USERPROFILE%\.cargo\bin is in PATH"
}
if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) {
Write-Host "[INFO] pnpm not found, installing via npm..." -ForegroundColor Yellow
npm install -g pnpm
if ($LASTEXITCODE -ne 0) { Fail "pnpm install failed" }
}
if (-not (Test-Path "node_modules")) {
Write-Host "[INFO] Installing deps: pnpm install..." -ForegroundColor Yellow
pnpm install
if ($LASTEXITCODE -ne 0) { Fail "pnpm install failed" }
Write-Host ""
}
Write-Host ("[INFO] node {0} | pnpm {1}" -f (node -v), (pnpm -v)) -ForegroundColor DarkGray
Ensure-DevPortFree -Port $DevPort -Force $AutoFreePort
# Installed release app may already be open; single-instance plugin focuses it.
$runningGui = Get-Process -Name "agenthub-gui" -ErrorAction SilentlyContinue
if ($runningGui) {
if ($AutoFreePort) {
Write-Host "[INFO] Stopping agenthub-gui (PID(s): $($runningGui.Id -join ', '))." -ForegroundColor DarkGray
$runningGui | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds 400
} else {
Write-Host "[WARN] agenthub-gui already running (PID(s): $($runningGui.Id -join ', '))." -ForegroundColor Yellow
Write-Host " Second instances may exit immediately (single-instance). Close it if dev fails to open." -ForegroundColor DarkGray
}
}
if ($StopOnly) {
Write-Host "[INFO] Stopped leftover desktop/dev processes." -ForegroundColor Green
exit 0
}
Write-Host "[START] pnpm tauri:dev" -ForegroundColor Green
Write-Host "[INFO] Vite + Tauri desktop (real backend, not browser mock)" -ForegroundColor DarkGray
Write-Host "[INFO] First build may take a while. Ctrl+C to stop." -ForegroundColor DarkGray
Write-Host ""
pnpm tauri:dev
if ($LASTEXITCODE -ne 0) {
Fail @"
tauri:dev exited with code $LASTEXITCODE
If you saw "Port $DevPort is already in use":
- Close other pnpm dev / tauri:dev / AgentHub windows, then retry
- Or: Get-NetTCPConnection -LocalPort $DevPort | % { Stop-Process -Id `$_.OwningProcess -Force }
If cargo/node not found when double-clicking:
- Reopen terminal after install, or add cargo/node to user PATH
"@
}