-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup-usb.ps1
More file actions
417 lines (368 loc) · 17 KB
/
Copy pathsetup-usb.ps1
File metadata and controls
417 lines (368 loc) · 17 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#Requires -Version 5.1
<#
.SYNOPSIS
Setup script per preparare la chiavetta USB con Claude Code Portable.
Esegui questo script UNA VOLTA dal tuo PC principale per configurare la chiavetta.
.PARAMETER UsbDrive
Lettera del drive USB (es. "E", "F")
.PARAMETER NodeVersion
Versione di Node.js da scaricare (default: 20.11.1)
.EXAMPLE
.\setup-usb.ps1 -UsbDrive E
#>
param(
[Parameter(Mandatory = $true)]
[ValidatePattern('^[A-Z]$')]
[string]$UsbDrive,
[string]$NodeVersion = "20.11.1"
)
$ErrorActionPreference = "Stop"
$UsbRoot = "${UsbDrive}:\"
# --- Progress Bar ---
$totalSteps = 9
$currentStep = 0
function Show-SetupProgress {
param(
[int]$Step,
[string]$Activity,
[string]$Status = "",
[int]$PercentWithinStep = -1
)
$overallPercent = [math]::Floor(($Step - 1) / $totalSteps * 100)
if ($PercentWithinStep -ge 0) {
$stepContribution = [math]::Floor($PercentWithinStep / $totalSteps)
$overallPercent = [math]::Min($overallPercent + $stepContribution, 100)
}
# Barra ASCII visuale
$barWidth = 40
$filled = [math]::Floor($overallPercent / 100 * $barWidth)
$empty = $barWidth - $filled
$bar = "[" + ("#" * $filled) + ("-" * $empty) + "]"
# Sovrascrivi la riga di progresso
$progressLine = "`r $bar $overallPercent% - Step $Step/$totalSteps"
if ($Status) { $progressLine += " - $Status" }
Write-Host $progressLine -NoNewline -ForegroundColor Cyan
# Write-Progress nativo per terminali che lo supportano
$progressParams = @{
Activity = "Wolfix USB Setup"
Status = "[$Step/$totalSteps] $Activity"
PercentComplete = $overallPercent
}
if ($Status) { $progressParams["CurrentOperation"] = $Status }
Write-Progress @progressParams
}
function Complete-Step {
param([int]$Step, [string]$Activity)
$overallPercent = [math]::Floor($Step / $totalSteps * 100)
$barWidth = 40
$filled = [math]::Floor($overallPercent / 100 * $barWidth)
$empty = $barWidth - $filled
$bar = "[" + ("#" * $filled) + ("-" * $empty) + "]"
Write-Host "`r $bar $overallPercent% - Step $Step/$totalSteps - Completato! " -ForegroundColor Green
}
function Invoke-DownloadWithProgress {
param(
[string]$Uri,
[string]$OutFile,
[int]$Step,
[string]$Label,
[int]$MaxRetries = 2
)
for ($i = 1; $i -le $MaxRetries; $i++) {
try {
$webRequest = [System.Net.HttpWebRequest]::Create($Uri)
$webRequest.Timeout = 120000
$response = $webRequest.GetResponse()
$totalBytes = $response.ContentLength
$stream = $response.GetResponseStream()
$fileStream = [System.IO.File]::Create($OutFile)
$buffer = New-Object byte[] 65536
$downloaded = 0
$lastUpdate = [DateTime]::Now
while (($bytesRead = $stream.Read($buffer, 0, $buffer.Length)) -gt 0) {
$fileStream.Write($buffer, 0, $bytesRead)
$downloaded += $bytesRead
$now = [DateTime]::Now
if (($now - $lastUpdate).TotalMilliseconds -ge 200) {
$lastUpdate = $now
if ($totalBytes -gt 0) {
$dlPercent = [math]::Floor($downloaded / $totalBytes * 100)
$dlMB = [math]::Round($downloaded / 1MB, 1)
$totalMB = [math]::Round($totalBytes / 1MB, 1)
Show-SetupProgress -Step $Step -Activity $Label -Status "${dlMB}MB / ${totalMB}MB ($dlPercent%)" -PercentWithinStep $dlPercent
}
}
}
$fileStream.Close()
$stream.Close()
$response.Close()
return
} catch {
if ($i -eq $MaxRetries) { throw }
Write-Host ""
Write-Host " [RETRY] Tentativo $i fallito, riprovo..." -ForegroundColor Yellow
}
}
}
# --- Validazione ---
if (-not (Test-Path $UsbRoot)) {
Write-Error "Drive ${UsbDrive}: non trovato. Inserisci la chiavetta USB."
exit 1
}
$freeSpace = (Get-PSDrive $UsbDrive).Free
$requiredSpace = 800MB
if ($freeSpace -lt $requiredSpace) {
Write-Error "Spazio insufficiente. Servono almeno 800MB liberi. Disponibili: $([math]::Round($freeSpace / 1MB))MB"
exit 1
}
Write-Host ""
Write-Host " +============================================+" -ForegroundColor Green
Write-Host " | WOLFIX USB SETUP |" -ForegroundColor Green
Write-Host " | Claude Code Portable Installer |" -ForegroundColor Green
Write-Host " +============================================+" -ForegroundColor Green
Write-Host ""
Write-Host " Drive USB: ${UsbDrive}: Node.js: $NodeVersion" -ForegroundColor Yellow
Write-Host ""
# --- Creazione struttura directory ---
$currentStep = 1
Show-SetupProgress -Step $currentStep -Activity "Creazione directory" -Status "Preparazione struttura..."
Write-Host ""
Write-Host " [1/9] Creazione struttura directory..." -ForegroundColor Green
$directories = @(
"runtime\node-win-x64",
"runtime\node-linux-x64",
"runtime\node-darwin-x64",
"runtime\node-darwin-arm64",
"runtime\git-win-x64",
"claude-code",
"config",
"config\rules",
"toolkit\prompts",
"toolkit\scripts",
"toolkit\logs"
)
foreach ($dir in $directories) {
$fullPath = Join-Path $UsbRoot $dir
if (-not (Test-Path $fullPath)) {
New-Item -ItemType Directory -Path $fullPath -Force | Out-Null
}
}
Complete-Step -Step $currentStep -Activity "Creazione directory"
# --- Download Node.js Windows ---
$currentStep = 2
Show-SetupProgress -Step $currentStep -Activity "Node.js Windows" -Status "Avvio download..."
Write-Host ""
Write-Host " [2/9] Download Node.js $NodeVersion per Windows x64..." -ForegroundColor Green
$nodeWinZip = Join-Path $env:TEMP "node-win-x64.zip"
$nodeWinUrl = "https://nodejs.org/dist/v${NodeVersion}/node-v${NodeVersion}-win-x64.zip"
$nodeWinDest = Join-Path $UsbRoot "runtime\node-win-x64"
if (-not (Test-Path (Join-Path $nodeWinDest "node.exe"))) {
Invoke-DownloadWithProgress -Uri $nodeWinUrl -OutFile $nodeWinZip -Step $currentStep -Label "Node.js Windows"
Write-Host ""
Show-SetupProgress -Step $currentStep -Activity "Node.js Windows" -Status "Estrazione..." -PercentWithinStep 80
Write-Host ""
Expand-Archive -Path $nodeWinZip -DestinationPath (Join-Path $env:TEMP "node-win-extract") -Force
$extractedDir = Get-ChildItem (Join-Path $env:TEMP "node-win-extract") | Select-Object -First 1
Copy-Item -Path "$($extractedDir.FullName)\*" -Destination $nodeWinDest -Recurse -Force
Remove-Item $nodeWinZip -Force -ErrorAction SilentlyContinue
Remove-Item (Join-Path $env:TEMP "node-win-extract") -Recurse -Force -ErrorAction SilentlyContinue
} else {
Write-Host " Gia' presente, skip." -ForegroundColor Yellow
}
Complete-Step -Step $currentStep -Activity "Node.js Windows"
# --- Download Node.js Linux ---
$currentStep = 3
Show-SetupProgress -Step $currentStep -Activity "Node.js Linux" -Status "Avvio download..."
Write-Host ""
Write-Host " [3/9] Download Node.js $NodeVersion per Linux x64..." -ForegroundColor Green
$nodeLinuxTar = Join-Path $env:TEMP "node-linux-x64.tar.xz"
$nodeLinuxUrl = "https://nodejs.org/dist/v${NodeVersion}/node-v${NodeVersion}-linux-x64.tar.xz"
$nodeLinuxDest = Join-Path $UsbRoot "runtime\node-linux-x64"
if (-not (Test-Path (Join-Path $nodeLinuxDest "bin"))) {
Invoke-DownloadWithProgress -Uri $nodeLinuxUrl -OutFile $nodeLinuxTar -Step $currentStep -Label "Node.js Linux"
Write-Host ""
Write-Host " NOTA: Estrai manualmente il .tar.xz su Linux con:"
Write-Host " tar -xf node-linux-x64.tar.xz -C /path/to/usb/runtime/node-linux-x64 --strip-components=1" -ForegroundColor Yellow
Copy-Item $nodeLinuxTar -Destination $nodeLinuxDest -Force
Remove-Item $nodeLinuxTar -Force -ErrorAction SilentlyContinue
} else {
Write-Host " Gia' presente, skip." -ForegroundColor Yellow
}
Complete-Step -Step $currentStep -Activity "Node.js Linux"
# --- Download Node.js macOS x64 ---
$currentStep = 4
Show-SetupProgress -Step $currentStep -Activity "Node.js macOS x64" -Status "Avvio download..."
Write-Host ""
Write-Host " [4/9] Download Node.js $NodeVersion per macOS x64..." -ForegroundColor Green
$nodeMacX64Tar = Join-Path $env:TEMP "node-mac-x64.tar.gz"
$nodeMacX64Url = "https://nodejs.org/dist/v${NodeVersion}/node-v${NodeVersion}-darwin-x64.tar.gz"
$nodeMacX64Dest = Join-Path $UsbRoot "runtime\node-darwin-x64"
if (-not (Test-Path (Join-Path $nodeMacX64Dest "bin"))) {
Invoke-DownloadWithProgress -Uri $nodeMacX64Url -OutFile $nodeMacX64Tar -Step $currentStep -Label "Node.js macOS x64"
Write-Host ""
Copy-Item $nodeMacX64Tar -Destination $nodeMacX64Dest -Force
Remove-Item $nodeMacX64Tar -Force -ErrorAction SilentlyContinue
} else {
Write-Host " Gia' presente, skip." -ForegroundColor Yellow
}
Complete-Step -Step $currentStep -Activity "Node.js macOS x64"
# --- Download Node.js macOS ARM64 ---
$currentStep = 5
Show-SetupProgress -Step $currentStep -Activity "Node.js macOS ARM64" -Status "Avvio download..."
Write-Host ""
Write-Host " [5/9] Download Node.js $NodeVersion per macOS ARM64..." -ForegroundColor Green
$nodeMacArm64Tar = Join-Path $env:TEMP "node-mac-arm64.tar.gz"
$nodeMacArm64Url = "https://nodejs.org/dist/v${NodeVersion}/node-v${NodeVersion}-darwin-arm64.tar.gz"
$nodeMacArm64Dest = Join-Path $UsbRoot "runtime\node-darwin-arm64"
if (-not (Test-Path (Join-Path $nodeMacArm64Dest "bin"))) {
Invoke-DownloadWithProgress -Uri $nodeMacArm64Url -OutFile $nodeMacArm64Tar -Step $currentStep -Label "Node.js macOS ARM64"
Write-Host ""
Copy-Item $nodeMacArm64Tar -Destination $nodeMacArm64Dest -Force
Remove-Item $nodeMacArm64Tar -Force -ErrorAction SilentlyContinue
} else {
Write-Host " Gia' presente, skip." -ForegroundColor Yellow
}
Complete-Step -Step $currentStep -Activity "Node.js macOS ARM64"
# --- Download Git Portable per Windows ---
$currentStep = 6
Show-SetupProgress -Step $currentStep -Activity "Git Portable" -Status "Avvio download..."
Write-Host ""
Write-Host " [6/9] Download Git Portable per Windows..." -ForegroundColor Green
$gitVersion = "2.47.1"
$gitPortableUrl = "https://github.com/git-for-windows/git/releases/download/v${gitVersion}.windows.1/PortableGit-${gitVersion}-64-bit.7z.exe"
$gitDest = Join-Path $UsbRoot "runtime\git-win-x64"
if (-not (Test-Path (Join-Path $gitDest "bin\bash.exe"))) {
$gitInstaller = Join-Path $env:TEMP "PortableGit.exe"
Invoke-DownloadWithProgress -Uri $gitPortableUrl -OutFile $gitInstaller -Step $currentStep -Label "Git Portable" -MaxRetries 2
Write-Host ""
# Estrai prima in locale (exFAT non supporta estrazione diretta)
$gitTempDir = Join-Path $env:TEMP "git-portable-extract"
Show-SetupProgress -Step $currentStep -Activity "Git Portable" -Status "Estrazione in locale..." -PercentWithinStep 60
Write-Host ""
if (Test-Path $gitTempDir) { Remove-Item $gitTempDir -Recurse -Force }
New-Item -ItemType Directory -Path $gitTempDir -Force | Out-Null
& $gitInstaller -o"$gitTempDir" -y 2>&1 | Out-Null
Show-SetupProgress -Step $currentStep -Activity "Git Portable" -Status "Copia su USB..." -PercentWithinStep 80
Write-Host ""
if (-not (Test-Path $gitDest)) { New-Item -ItemType Directory -Path $gitDest -Force | Out-Null }
& robocopy $gitTempDir $gitDest /E /NFL /NDL /NP 2>&1 | Out-Null
Remove-Item $gitInstaller -Force -ErrorAction SilentlyContinue
Remove-Item $gitTempDir -Recurse -Force -ErrorAction SilentlyContinue
} else {
Write-Host " Gia' presente, skip." -ForegroundColor Yellow
}
Complete-Step -Step $currentStep -Activity "Git Portable"
# --- Installazione Claude Code ---
$currentStep = 7
Show-SetupProgress -Step $currentStep -Activity "Claude Code" -Status "Installazione npm..."
Write-Host ""
Write-Host " [7/9] Installazione Claude Code..." -ForegroundColor Green
$nodePath = Join-Path $UsbRoot "runtime\node-win-x64\node.exe"
$npmPath = Join-Path $UsbRoot "runtime\node-win-x64\npm.cmd"
$claudeCodeDir = Join-Path $UsbRoot "claude-code"
$claudeTempDir = Join-Path $env:TEMP "claude-code-install"
$env:PATH = "$(Join-Path $UsbRoot 'runtime\node-win-x64');$env:PATH"
# Installa prima in locale (exFAT corrompe npm install diretto)
Show-SetupProgress -Step $currentStep -Activity "Claude Code" -Status "npm install (attendere qualche minuto)..." -PercentWithinStep 10
Write-Host ""
if (Test-Path $claudeTempDir) { Remove-Item $claudeTempDir -Recurse -Force }
& $npmPath install -g @anthropic-ai/claude-code --prefix $claudeTempDir 2>&1 | ForEach-Object {
if ($_ -match "added|updated|claude") { Write-Host " $_" -ForegroundColor Gray }
}
# Copia sulla chiavetta con robocopy (affidabile su exFAT)
Show-SetupProgress -Step $currentStep -Activity "Claude Code" -Status "Copia su USB..." -PercentWithinStep 70
Write-Host ""
& robocopy $claudeTempDir $claudeCodeDir /E /NFL /NDL /NP /IS /IT 2>&1 | Out-Null
Remove-Item $claudeTempDir -Recurse -Force -ErrorAction SilentlyContinue
Complete-Step -Step $currentStep -Activity "Claude Code"
# --- Login Claude ---
$currentStep = 8
Show-SetupProgress -Step $currentStep -Activity "Autenticazione" -Status "Login..."
Write-Host ""
Write-Host " [8/9] Configurazione autenticazione..." -ForegroundColor Green
$env:CLAUDE_CONFIG_DIR = Join-Path $UsbRoot "config"
$claudeBin = Join-Path $claudeCodeDir "bin\claude.cmd"
if (Test-Path $claudeBin) {
Write-Host " Avvio login... Segui le istruzioni nel browser." -ForegroundColor Yellow
& $claudeBin login
} else {
Write-Host " ATTENZIONE: claude.cmd non trovato in $claudeBin" -ForegroundColor Red
Write-Host " Esegui il login manualmente dopo il setup." -ForegroundColor Yellow
}
Complete-Step -Step $currentStep -Activity "Autenticazione"
# --- Copia launcher e toolkit ---
$currentStep = 9
Show-SetupProgress -Step $currentStep -Activity "Launcher e toolkit" -Status "Copia file..."
Write-Host ""
Write-Host " [9/9] Copia launcher e toolkit sulla chiavetta..." -ForegroundColor Green
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$filesToCopy = @(
"launch.bat",
"launch.ps1",
"launch.sh",
"wolfix-eject.ps1",
"toolkit\prompts\windows-health.md",
"toolkit\prompts\linux-health.md",
"toolkit\prompts\esxi-health.md",
"toolkit\prompts\vmware-health.md",
"toolkit\prompts\server-2008-2012.md",
"toolkit\scripts\collect-win.ps1",
"toolkit\scripts\collect-linux.sh",
"toolkit\scripts\collect-esxi.sh",
"toolkit\prompts\macos-health.md",
"toolkit\scripts\collect-macos.sh",
"toolkit\scripts\log-session.ps1",
"VERSION",
"README.md"
)
foreach ($file in $filesToCopy) {
$source = Join-Path $scriptDir $file
$dest = Join-Path $UsbRoot $file
if (Test-Path $source) {
$destDir = Split-Path $dest -Parent
if (-not (Test-Path $destDir)) { New-Item -ItemType Directory -Path $destDir -Force | Out-Null }
Copy-Item $source $dest -Force
Write-Host " Copiato: $file" -ForegroundColor Gray
}
}
Complete-Step -Step $currentStep -Activity "Launcher e toolkit"
# --- Generate SHA256SUMS ---
Write-Host " Generating SHA256SUMS..." -ForegroundColor Gray
$hashFiles = @("launch.bat", "launch.sh", "launch.ps1", "wolfix-eject.ps1")
$hashLines = @()
foreach ($hf in $hashFiles) {
$hfPath = Join-Path $UsbRoot $hf
if (Test-Path $hfPath) {
$hash = (Get-FileHash -Path $hfPath -Algorithm SHA256).Hash.ToLower()
$hashLines += "$hash $hf"
}
}
if ($hashLines.Count -gt 0) {
$hashLines -join "`n" | Set-Content -Path (Join-Path $UsbRoot "SHA256SUMS") -NoNewline -Encoding UTF8
Write-Host " SHA256SUMS generato con $($hashLines.Count) file." -ForegroundColor Gray
}
Write-Progress -Activity "Wolfix USB Setup" -Completed
# --- Riepilogo ---
Write-Host ""
Write-Host " [########################################] 100% - Setup completato!" -ForegroundColor Green
Write-Host ""
Write-Host " +============================================+" -ForegroundColor Cyan
Write-Host " | SETUP COMPLETATO CON SUCCESSO |" -ForegroundColor Cyan
Write-Host " +============================================+" -ForegroundColor Cyan
Write-Host ""
Write-Host "Struttura chiavetta ${UsbDrive}:\" -ForegroundColor Yellow
Write-Host " runtime\ - Node.js portable (Win + Linux + macOS)"
Write-Host " runtime\git\ - Git Portable (per Windows senza Git)"
Write-Host " claude-code\ - Claude Code CLI"
Write-Host " config\ - Configurazione e credenziali"
Write-Host " toolkit\ - Prompt diagnostici e script"
Write-Host ""
Write-Host "[NOTE] macOS: tar.gz files will be extracted automatically by launch.sh on first run." -ForegroundColor Gray
Write-Host ""
Write-Host "Per usare la chiavetta:" -ForegroundColor Yellow
Write-Host " Windows: Doppio click su launch.bat (o launch.ps1)"
Write-Host " Linux: bash launch.sh"
Write-Host " macOS: bash launch.sh"
Write-Host ""
Write-Host "IMPORTANTE: La chiavetta contiene le tue credenziali." -ForegroundColor Red
Write-Host "Considera di cifrarla con BitLocker o VeraCrypt." -ForegroundColor Red