Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
350 lines (305 loc) · 10.4 KB
/
Copy pathinstall.ps1
File metadata and controls
350 lines (305 loc) · 10.4 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Requires a git checkout of this repository. Builds the Windows directory
# package from that checkout, copies it to %LOCALAPPDATA%\Programs\dsh, and
# adds that folder to the user PATH. There is no download URL: this script
# never fetches a remote payload.
#
# git clone <this-repo>
# cd <this-repo>
# powershell -ExecutionPolicy Bypass -File .\scripts\install\install.ps1
[CmdletBinding()]
param(
[string]$InstallDir = $env:DSH_INSTALL_DIR,
[string]$PackageDir = "",
[switch]$SkipPack,
[switch]$SkipBuild,
[switch]$SkipZip,
[switch]$SkipPath,
[switch]$SkipVerify,
[switch]$DryRun
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$NonInteractive = $env:DSH_NON_INTERACTIVE -match "^(?i:1|true|yes)$"
function Write-Step {
param([string]$Message)
Write-Host "==> $Message"
}
function Write-WarningStep {
param([string]$Message)
Write-Warning $Message
}
function Path-Contains {
param(
[string]$PathValue,
[string]$Entry
)
if ([string]::IsNullOrWhiteSpace($PathValue)) {
return $false
}
$needle = $Entry.TrimEnd("\")
foreach ($segment in $PathValue.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)) {
if ($segment.TrimEnd("\") -ieq $needle) {
return $true
}
}
return $false
}
function Add-PathEntry {
param(
[string]$PathValue,
[string]$Entry
)
if (Path-Contains -PathValue $PathValue -Entry $Entry) {
return $PathValue
}
if ([string]::IsNullOrWhiteSpace($PathValue)) {
return $Entry
}
return "$PathValue;$Entry"
}
function Invoke-WithInstallLock {
param(
[string]$LockPath,
[scriptblock]$Script
)
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $LockPath) | Out-Null
$lock = $null
while ($null -eq $lock) {
try {
$lock = [System.IO.File]::Open(
$LockPath,
[System.IO.FileMode]::OpenOrCreate,
[System.IO.FileAccess]::ReadWrite,
[System.IO.FileShare]::None
)
} catch [System.IO.IOException] {
Start-Sleep -Milliseconds 250
}
}
try {
& $Script
} finally {
$lock.Dispose()
}
}
function Resolve-RepoRoot {
$scriptDir = $PSScriptRoot
if ([string]::IsNullOrWhiteSpace($scriptDir)) {
throw "install.ps1 must be run from a file path so it can find the repository root."
}
$candidate = (Resolve-Path (Join-Path $scriptDir "..\..")).Path
$workspace = Join-Path $candidate "pnpm-workspace.yaml"
$cliManifest = Join-Path $candidate "apps\cli\package.json"
$packer = Join-Path $candidate "scripts\pack-windows-cli.ts"
if (-not (Test-Path -LiteralPath $workspace) -or -not (Test-Path -LiteralPath $cliManifest) -or -not (Test-Path -LiteralPath $packer)) {
throw "install.ps1 must run from a DeepSeek Harness checkout. Missing $workspace, $cliManifest, or $packer."
}
return $candidate
}
function Test-NodeVersion {
$raw = (& node -p "process.versions.node" 2>$null)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($raw)) {
throw "install.ps1 requires Node.js ^22.19.0 or >=24.0.0 on PATH. Install Node, then re-run."
}
$parts = $raw.Trim().Split(".")
if ($parts.Length -lt 2) {
throw "install.ps1 could not parse Node.js version $raw."
}
$major = [int]$parts[0]
$minor = [int]$parts[1]
if ($major -eq 22 -and $minor -ge 19) {
return $raw.Trim()
}
if ($major -ge 24) {
return $raw.Trim()
}
throw "install.ps1 requires Node.js ^22.19.0 or >=24.0.0; found $raw."
}
function Assert-Pnpm {
$pnpm = Get-Command pnpm -ErrorAction SilentlyContinue
if ($null -ne $pnpm) {
return
}
$corepack = Get-Command corepack -ErrorAction SilentlyContinue
if ($null -eq $corepack) {
throw "install.ps1 requires pnpm 11.7.0. Enable Corepack (corepack enable) or install pnpm, then re-run."
}
Write-Step "Activating pnpm through Corepack"
if ($DryRun) {
Write-Host "==> [dry-run] corepack enable"
Write-Host "==> [dry-run] corepack prepare pnpm@11.7.0 --activate"
return
}
& corepack enable
if ($LASTEXITCODE -ne 0) {
throw "install.ps1: corepack enable failed."
}
& corepack prepare pnpm@11.7.0 --activate
if ($LASTEXITCODE -ne 0) {
throw "install.ps1: corepack prepare pnpm@11.7.0 --activate failed."
}
}
function Copy-PackageTree {
param(
[string]$Source,
[string]$Destination
)
# Copy-Item hits MAX_PATH on nested node_modules; robocopy copies those names.
if (Test-Path -LiteralPath $Destination) {
Remove-Item -LiteralPath $Destination -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $Destination | Out-Null
& robocopy $Source $Destination /E /COPY:DAT /R:1 /W:1 /NFL /NDL /NJH /NJS /NP
if ($LASTEXITCODE -ge 8) {
throw "install.ps1: robocopy failed with exit code $LASTEXITCODE while copying $Source to $Destination."
}
}
function Test-PackageComplete {
param([string]$Dir)
$required = @(
"node.exe",
"dsh.cmd",
"deepseek.cmd",
"lib\bin.js",
"dsh-install.json",
"package.json"
)
foreach ($name in $required) {
if (-not (Test-Path -LiteralPath (Join-Path $Dir $name) -PathType Leaf)) {
return $false
}
}
return $true
}
if ($env:OS -ne "Windows_NT") {
Write-Error "install.ps1 supports Windows only."
exit 1
}
if (-not [Environment]::Is64BitOperatingSystem) {
Write-Error "dsh requires a 64-bit version of Windows."
exit 1
}
$architecture = $env:PROCESSOR_ARCHITECTURE
if ($architecture -notin @("AMD64", "ARM64")) {
Write-Error "Unsupported architecture: $architecture. dsh supports AMD64 and ARM64."
exit 1
}
$repoRoot = Resolve-RepoRoot
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
$InstallDir = Join-Path $env:LOCALAPPDATA "Programs\dsh"
}
if ([string]::IsNullOrWhiteSpace($PackageDir)) {
$PackageDir = Join-Path $repoRoot "dist-windows\dsh"
}
if (-not $SkipPack) {
$nodeVersion = Test-NodeVersion
Write-Step "Detected Node.js $nodeVersion"
Assert-Pnpm
$nodeModules = Join-Path $repoRoot "node_modules"
if (-not (Test-Path -LiteralPath $nodeModules)) {
Write-Step "Installing workspace dependencies"
if ($DryRun) {
Write-Host "==> [dry-run] pnpm install --frozen-lockfile"
} else {
Push-Location $repoRoot
try {
& pnpm install --frozen-lockfile
if ($LASTEXITCODE -ne 0) {
throw "install.ps1: pnpm install --frozen-lockfile failed."
}
} finally {
Pop-Location
}
}
}
}
$needPack = -not $SkipPack
if ($needPack -and (Test-PackageComplete -Dir $PackageDir) -and -not $SkipBuild) {
Write-Step "Existing package at $PackageDir will be rebuilt"
} elseif ($needPack -and (Test-PackageComplete -Dir $PackageDir) -and $SkipBuild) {
Write-Step "Reusing existing package at $PackageDir"
$needPack = $false
}
if ($needPack) {
Write-Step "Packing the Windows directory package from this checkout"
$packArgs = @("--import", "tsx/esm", "scripts/pack-windows-cli.ts")
if ($SkipBuild) {
$packArgs += "--skip-build"
}
if ($SkipZip) {
$packArgs += "--skip-zip"
}
if ($DryRun) {
$packArgs += "--dry-run"
Write-Host "==> [dry-run] node $($packArgs -join ' ')"
} else {
Push-Location $repoRoot
try {
# Call tsx through Node. A pnpm exec subprocess can prune the
# checkout to production dependencies when CI=true.
& node @packArgs
if ($LASTEXITCODE -ne 0) {
throw "install.ps1: pack-windows-cli failed."
}
} finally {
Pop-Location
}
}
}
if (-not $DryRun -and -not (Test-PackageComplete -Dir $PackageDir)) {
throw "install.ps1: packed tree at $PackageDir is incomplete. Re-run without -SkipPack."
}
$lockPath = Join-Path $env:LOCALAPPDATA "Programs\dsh-install.lock"
$installParent = Split-Path -Parent $InstallDir
$stagingDir = Join-Path $installParent (".staging-dsh." + $PID)
Write-Step "Installing to $InstallDir"
if ($DryRun) {
Write-Host "==> [dry-run] copy $PackageDir -> $InstallDir"
} else {
Invoke-WithInstallLock -LockPath $lockPath -Script {
New-Item -ItemType Directory -Force -Path $installParent | Out-Null
Copy-PackageTree -Source $PackageDir -Destination $stagingDir
if (Test-Path -LiteralPath $InstallDir) {
Remove-Item -LiteralPath $InstallDir -Recurse -Force
}
Move-Item -LiteralPath $stagingDir -Destination $InstallDir
}
}
if (-not $SkipVerify -and -not $DryRun) {
$launcher = Join-Path $InstallDir "deepseek.cmd"
Write-Step "Verifying $launcher --version"
& $launcher --version
if ($LASTEXITCODE -ne 0) {
throw "install.ps1: installed deepseek --version failed."
}
}
if (-not $SkipPath) {
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$newUserPath = Add-PathEntry -PathValue $userPath -Entry $InstallDir
if ($DryRun) {
Write-Host "==> [dry-run] add $InstallDir to the user PATH"
} elseif ($newUserPath -cne $userPath) {
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
Write-Step "PATH updated for future PowerShell sessions."
} else {
Write-Step "$InstallDir is already on the user PATH."
}
if (-not (Path-Contains -PathValue $env:Path -Entry $InstallDir)) {
if ([string]::IsNullOrWhiteSpace($env:Path)) {
$env:Path = $InstallDir
} else {
$env:Path = "$env:Path;$InstallDir"
}
}
}
Write-Step "Current session: deepseek"
Write-Step "New windows: open a new PowerShell window and run: deepseek"
Write-Host "DeepSeek CLI installed from this checkout into $InstallDir"
if (-not $NonInteractive -and -not [Console]::IsInputRedirected -and -not [Console]::IsOutputRedirected) {
$choice = Read-Host "Start DeepSeek CLI now? [y/N]"
if ($choice -match "^(?i:y(?:es)?)$" -and -not $DryRun) {
Write-Step "Launching DeepSeek CLI"
& (Join-Path $InstallDir "deepseek.cmd")
}
}