Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
executable file
·222 lines (202 loc) · 9.59 KB
/
Copy pathinstall.ps1
File metadata and controls
executable file
·222 lines (202 loc) · 9.59 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# LLMHarbor Windows installer
# Usage:
# irm https://raw.githubusercontent.com/PLASMA-FR/LLMHarbor/main/install.ps1 | iex
$ErrorActionPreference = "Stop"
Set-StrictMode -Version 2.0
$RepoUrl = if ($env:LLMHARBOR_REPO) { $env:LLMHARBOR_REPO } else { "https://github.com/PLASMA-FR/LLMHarbor.git" }
$InstallDir = if ($env:LLMHARBOR_HOME) { $env:LLMHARBOR_HOME } else { Join-Path $env:USERPROFILE ".llmharbor\app" }
$BinDir = if ($env:LLMHARBOR_BIN_DIR) { $env:LLMHARBOR_BIN_DIR } else { Join-Path $env:LOCALAPPDATA "LLMHarbor\bin" }
$CommandPath = Join-Path $BinDir "llmharbor.ps1"
$CmdShimPath = Join-Path $BinDir "llmharbor.cmd"
$MinimumNodeMessage = "Node.js ^22.12.0 or ^24.0.0"
function Fail([string]$Message) {
[Console]::Error.WriteLine("Error: $Message")
exit 1
}
function Require-Command([string]$Name, [string]$Hint) {
if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) {
if ($Hint) { Fail "Missing required command: $Name. $Hint" }
Fail "Missing required command: $Name"
}
}
function Invoke-Native([string]$File, [string[]]$Arguments) {
& $File @Arguments
if ($LASTEXITCODE -ne 0) { Fail "$File exited with code $LASTEXITCODE" }
}
function Test-SupportedNode {
$Version = (& node -p "process.versions.node" 2>$null)
if ($LASTEXITCODE -ne 0 -or -not $Version) { return $false }
$Parts = $Version.Trim().Split(".")
if ($Parts.Count -lt 2) { return $false }
$Major = [int]$Parts[0]
$Minor = [int]$Parts[1]
return (($Major -eq 22 -and $Minor -ge 12) -or $Major -eq 24)
}
function Write-Utf8NoBom([string]$Path, [string]$Content) {
$Encoding = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($Path, $Content, $Encoding)
}
function Write-ManagedShim([string]$Path, [string]$Content) {
$Directory = Split-Path $Path -Parent
$TemporaryPath = Join-Path $Directory ".llmharbor-shim-$PID-$([Guid]::NewGuid().ToString('N')).tmp"
try {
Write-Utf8NoBom $TemporaryPath $Content
Move-Item -LiteralPath $TemporaryPath -Destination $Path -Force
} finally {
Remove-Item -LiteralPath $TemporaryPath -Force -ErrorAction SilentlyContinue
}
}
function Test-PathEntry([string]$PathValue, [string]$Entry) {
if ([string]::IsNullOrWhiteSpace($PathValue)) { return $false }
foreach ($Candidate in $PathValue.Split(';')) {
if ([string]::Equals($Candidate.TrimEnd('\'), $Entry.TrimEnd('\'), [StringComparison]::OrdinalIgnoreCase)) {
return $true
}
}
return $false
}
function Get-CanonicalPath([string]$PathValue) {
if (Test-Path -LiteralPath $PathValue) { return (Resolve-Path -LiteralPath $PathValue).Path }
return [System.IO.Path]::GetFullPath($PathValue)
}
function Assert-ExistingInstallIdle([string]$Root) {
$PidPath = Join-Path $Root ".llmharbor\llmharbor.pid"
if (-not (Test-Path -LiteralPath $PidPath -PathType Leaf)) { return }
$PidText = (Get-Content -LiteralPath $PidPath -Raw).Trim()
$ProcessId = 0
if ([int]::TryParse($PidText, [ref]$ProcessId) -and $ProcessId -gt 1 -and
(Get-Process -Id $ProcessId -ErrorAction SilentlyContinue)) {
Fail "LLMHarbor is running with PID $ProcessId. Stop it before rerunning the installer."
}
}
function Test-ManagedPowerShellShim([string]$Path) {
if (-not (Test-Path -LiteralPath $Path)) { return $true }
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { return $false }
$Item = Get-Item -LiteralPath $Path -Force
if (($Item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) { return $false }
$Content = Get-Content -LiteralPath $Path -Raw
return $Content -match '(?m)^# LLMHarbor (Windows command line|managed command shim)'
}
function Test-ManagedCmdShim([string]$Path) {
if (-not (Test-Path -LiteralPath $Path)) { return $true }
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { return $false }
$Item = Get-Item -LiteralPath $Path -Force
if (($Item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) { return $false }
$Content = Get-Content -LiteralPath $Path -Raw
return $Content -match '(?im)^(?:rem LLMHarbor managed command shim\r?\n)?@echo off' -and
$Content -match '(?i)powershell\.exe.+llmharbor\.ps1'
}
Require-Command "git" "Install Git for Windows: https://git-scm.com/download/win"
Require-Command "node" "Install Node.js LTS: https://nodejs.org/"
Require-Command "npm" "Install Node.js LTS: https://nodejs.org/"
if (-not (Test-SupportedNode)) {
$Version = (& node -p "process.versions.node" 2>$null)
if (-not $Version) { $Version = "unknown" }
Fail "$MinimumNodeMessage is required; found Node.js $Version"
}
$InstallDir = Get-CanonicalPath $InstallDir
$BinDir = Get-CanonicalPath $BinDir
$CommandPath = Join-Path $BinDir "llmharbor.ps1"
$CmdShimPath = Join-Path $BinDir "llmharbor.cmd"
$ProspectiveInstalledCli = Join-Path $InstallDir "bin\llmharbor.ps1"
if ([string]::Equals(
[System.IO.Path]::GetFullPath($CommandPath),
[System.IO.Path]::GetFullPath($ProspectiveInstalledCli),
[StringComparison]::OrdinalIgnoreCase)) {
Fail "Command directory cannot be the repository's bin directory; it would overwrite the installed CLI: $CommandPath"
}
if (-not (Test-ManagedPowerShellShim $CommandPath)) {
Fail "$CommandPath already exists and is not an LLMHarbor-managed command; refusing to update or overwrite it"
}
if (-not (Test-ManagedCmdShim $CmdShimPath)) {
Fail "$CmdShimPath already exists and is not an LLMHarbor-managed command; refusing to update or overwrite it"
}
if (Test-Path -LiteralPath (Join-Path $InstallDir ".git") -PathType Container) {
$InstallDir = (Resolve-Path -LiteralPath $InstallDir).Path
Assert-ExistingInstallIdle $InstallDir
$InstalledCli = Join-Path $InstallDir "bin\llmharbor.ps1"
if (-not (Test-Path -LiteralPath $InstalledCli -PathType Leaf)) { Fail "Installed CLI is missing: $InstalledCli" }
Write-Host "Updating LLMHarbor through its lifecycle-aware CLI in $InstallDir"
$PreviousHome = $env:LLMHARBOR_HOME
$env:LLMHARBOR_HOME = $InstallDir
try {
& $InstalledCli update
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
} finally {
$env:LLMHARBOR_HOME = $PreviousHome
}
} elseif (Test-Path -LiteralPath $InstallDir) {
Fail "$InstallDir already exists but is not a git checkout. Set LLMHARBOR_HOME to another directory."
} else {
Write-Host "Cloning LLMHarbor into $InstallDir"
$ParentDir = Split-Path $InstallDir -Parent
if (-not $ParentDir) { Fail "Install directory must have a parent directory: $InstallDir" }
New-Item -ItemType Directory -Force -Path $ParentDir | Out-Null
Invoke-Native "git" @("clone", "--", $RepoUrl, $InstallDir)
$InstallDir = (Resolve-Path -LiteralPath $InstallDir).Path
$InstalledCli = Join-Path $InstallDir "bin\llmharbor.ps1"
if (-not (Test-Path -LiteralPath $InstalledCli -PathType Leaf)) { Fail "Installed CLI is missing: $InstalledCli" }
Write-Host "Installing dependencies and building production assets"
$PreviousHome = $env:LLMHARBOR_HOME
$env:LLMHARBOR_HOME = $InstallDir
try {
& $InstalledCli install
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
} finally {
$env:LLMHARBOR_HOME = $PreviousHome
}
}
# Keep the PATH entry as a tiny wrapper instead of copying the implementation.
# Updates then take effect immediately and project-root discovery stays correct.
New-Item -ItemType Directory -Force -Path $BinDir | Out-Null
$BinDir = (Resolve-Path -LiteralPath $BinDir).Path
$CommandPath = Join-Path $BinDir "llmharbor.ps1"
$CmdShimPath = Join-Path $BinDir "llmharbor.cmd"
$InstalledCli = (Resolve-Path -LiteralPath (Join-Path $InstallDir "bin\llmharbor.ps1")).Path
if ([string]::Equals(
[System.IO.Path]::GetFullPath($CommandPath),
[System.IO.Path]::GetFullPath($InstalledCli),
[StringComparison]::OrdinalIgnoreCase)) {
Fail "Command directory cannot be the repository's bin directory; it would overwrite the installed CLI: $CommandPath"
}
if (-not (Test-ManagedPowerShellShim $CommandPath)) {
Fail "$CommandPath already exists and is not an LLMHarbor-managed command; refusing to overwrite it"
}
if (-not (Test-ManagedCmdShim $CmdShimPath)) {
Fail "$CmdShimPath already exists and is not an LLMHarbor-managed command; refusing to overwrite it"
}
$EscapedInstallDir = $InstallDir.Replace("'", "''")
$EscapedInstalledCli = $InstalledCli.Replace("'", "''")
$PowerShellShim = @"
# LLMHarbor managed command shim
if (-not `$env:LLMHARBOR_HOME) { `$env:LLMHARBOR_HOME = '$EscapedInstallDir' }
& '$EscapedInstalledCli' @args
if (`$?) { exit 0 } else { exit 1 }
"@
Write-ManagedShim $CommandPath ($PowerShellShim + "`r`n")
$CmdShim = "rem LLMHarbor managed command shim`r`n@echo off`r`npowershell.exe -NoProfile -ExecutionPolicy Bypass -File `"%~dp0llmharbor.ps1`" %*`r`n"
Write-ManagedShim $CmdShimPath $CmdShim
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
if (-not (Test-PathEntry $UserPath $BinDir)) {
$NewUserPath = if ([string]::IsNullOrWhiteSpace($UserPath)) { $BinDir } else { $UserPath.TrimEnd(';') + ";" + $BinDir }
[Environment]::SetEnvironmentVariable("Path", $NewUserPath, "User")
$ProcessPath = [Environment]::GetEnvironmentVariable("PATH", "Process")
if (-not (Test-PathEntry $ProcessPath $BinDir)) {
$NewProcessPath = if ([string]::IsNullOrWhiteSpace($ProcessPath)) { $BinDir } else { $ProcessPath.TrimEnd(';') + ";" + $BinDir }
[Environment]::SetEnvironmentVariable("PATH", $NewProcessPath, "Process")
}
$PathNote = "Added $BinDir to your user PATH. Open a new terminal if llmharbor is not found."
} else {
$PathNote = "$BinDir is already on your user PATH."
}
Write-Host ""
Write-Host "LLMHarbor installed."
Write-Host ""
Write-Host "Command:"
Write-Host " $CmdShimPath"
Write-Host ""
Write-Host "Next steps:"
Write-Host " llmharbor start"
Write-Host " llmharbor open"
Write-Host ""
Write-Host $PathNote