forked from Nexus-Mods/Vortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows_dev_setup.ps1
More file actions
538 lines (452 loc) · 18.3 KB
/
windows_dev_setup.ps1
File metadata and controls
538 lines (452 loc) · 18.3 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# Windows Development Environment Bootstrap
# Installs: Git, Python, CMake, VS Build Tools + Windows SDK, NVM, Node, Yarn
# Then clones/updates Vortex repository
#Requires -RunAsAdministrator
# Configuration
$NODE_VERSION = "22.19"
$YarnVersion = "1.22.19"
$WindowsSDKVer = "19041"
$RepoUrl = "https://github.com/Nexus-Mods/Vortex.git"
$Branch = "master"
$Directory = "C:\vortex"
$RetryAttempts = 3
$RetryDelay = 5
if (Test-Path "$repoPath\package.json") {
$packageJson = Get-Content "$repoPath\package.json"
$j = $packageJson | ConvertFrom-Json
$NODE_VERSION = $j.engines.node
$rawYarnVersion = $j.packageManager
$YarnVersion = $rawYarnVersion.Split("@")[1]
}
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
# Logging function with consistent formatting
function Write-Log {
param(
[string]$Message,
[ValidateSet("INFO", "SUCCESS", "WARN", "ERROR", "STEP")]
[string]$Level = "INFO"
)
$timestamp = Get-Date -Format "HH:mm:ss"
$prefix = switch ($Level) {
"STEP" { "[STEP] "; "Cyan" }
"SUCCESS" { "[OK] "; "Green" }
"WARN" { "[WARN] "; "Yellow" }
"ERROR" { "[ERROR] "; "Red" }
default { "[INFO] "; "White" }
}
Write-Host "$timestamp $($prefix[0])$Message" -ForegroundColor $prefix[1]
}
# Utility Functions
function Test-AdminRights {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Test-WingetAvailable {
if (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
Write-Log "winget not found. Install 'App Installer' from Microsoft Store and retry." "ERROR"
throw "winget is required but not available"
}
}
function Add-ToPathPermanently {
param([string]$Path, [string]$Scope = "Machine")
if (-not $Path -or -not (Test-Path $Path)) {
Write-Log "Skipping invalid path: $Path" "WARN"
return
}
$currentPath = [Environment]::GetEnvironmentVariable("Path", $Scope)
if ($currentPath -split ';' -contains $Path) {
return # Already in PATH
}
$newPath = "$currentPath;$Path"
[Environment]::SetEnvironmentVariable("Path", $newPath, $Scope)
$env:Path = "$Path;$env:Path"
Write-Log "Added to PATH: $Path" "SUCCESS"
}
function Invoke-WithRetry {
param(
[scriptblock]$ScriptBlock,
[string]$Operation,
[int]$MaxAttempts = $RetryAttempts
)
for ($attempt = 1; $attempt -le $MaxAttempts; $attempt++) {
try {
if ($attempt -gt 1) {
Write-Log "$Operation (retry $attempt/$MaxAttempts)" "INFO"
}
& $ScriptBlock
return
}
catch {
if ($attempt -eq $MaxAttempts) {
throw "Failed after $MaxAttempts attempts: $($_.Exception.Message)"
}
Write-Log "$Operation failed, retrying in ${RetryDelay}s: $($_.Exception.Message)" "WARN"
Start-Sleep -Seconds $RetryDelay
}
}
}
# Installation Functions
function Install-Git {
Write-Log "Checking Git installation..." "STEP"
if (Get-Command git -ErrorAction SilentlyContinue) {
$version = git --version
Write-Log "Git already installed: $version" "SUCCESS"
return
}
Invoke-WithRetry -Operation "Git installation" -ScriptBlock {
winget install --id Git.Git -e --accept-package-agreements --accept-source-agreements --silent | Out-Null
Start-Sleep -Seconds 3
# Add Git to PATH
@("$env:ProgramFiles\Git\cmd", "${env:ProgramFiles(x86)}\Git\cmd") | ForEach-Object {
if (Test-Path "$_\git.exe") { Add-ToPathPermanently -Path $_ }
}
# Refresh PATH
$env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [Environment]::GetEnvironmentVariable("Path", "User")
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
throw "Git command not found after installation"
}
}
Write-Log "Git installed: $(git --version)" "SUCCESS"
}
function Install-Python310 {
Write-Log "Checking Python 3.10 installation..." "STEP"
# Check for existing Python 3.10
$pythonCommands = @( { py -3.10 -V }, { python --version }, { python3.10 --version })
foreach ($cmd in $pythonCommands) {
try {
$version = & $cmd 2>&1
if ($version -match 'Python 3\.10\.\d+') {
Write-Log "Python 3.10 already installed: $version" "SUCCESS"
return
}
}
catch { }
}
Invoke-WithRetry -Operation "Python 3.10 installation" -ScriptBlock {
winget install --id Python.Python.3.10 -e --accept-package-agreements --accept-source-agreements --silent | Out-Null
Start-Sleep -Seconds 3
# Verify installation
$pythonFound = $false
foreach ($cmd in $pythonCommands) {
try {
$version = & $cmd 2>&1
if ($version -match 'Python 3\.10\.\d+') {
$pythonFound = $true
break
}
}
catch { }
}
if (-not $pythonFound) {
throw "Python 3.10 verification failed"
}
}
Write-Log "Python 3.10 installed successfully" "SUCCESS"
}
function Install-CMake {
Write-Log "Checking CMake installation..." "STEP"
if (Get-Command cmake -ErrorAction SilentlyContinue) {
$version = cmake --version | Select-Object -First 1
Write-Log "CMake already installed: $version" "SUCCESS"
return
}
Invoke-WithRetry -Operation "CMake installation" -ScriptBlock {
$wingetArgs = @(
'install', '-e', '--id', 'Kitware.CMake',
'--accept-package-agreements', '--accept-source-agreements', '--silent'
)
$proc = Start-Process -FilePath 'winget.exe' -ArgumentList $wingetArgs -NoNewWindow -Wait -PassThru
if ($null -eq $proc -or $proc.ExitCode -ne 0) {
throw "winget failed to install CMake (exit $($proc.ExitCode))"
}
# Wait for cmake to appear on PATH (PATH updates can lag until a new session)
$deadline = (Get-Date).AddMinutes(10)
while (-not (Get-Command cmake -ErrorAction SilentlyContinue)) {
if ((Get-Date) -gt $deadline) { break }
Start-Sleep -Seconds 5
}
if (-not (Get-Command cmake -ErrorAction SilentlyContinue)) {
$possible = @("$Env:ProgramFiles\CMake\bin", "$Env:ProgramFiles(x86)\CMake\bin")
foreach ($pdir in $possible) {
if (Test-Path (Join-Path $pdir 'cmake.exe')) {
$env:PATH = "$pdir;$env:PATH"
break
}
}
if (-not (Get-Command cmake -ErrorAction SilentlyContinue)) {
throw "CMake not available on PATH after installation"
}
}
}
Write-Log "CMake installed: $(cmake --version | Select-Object -First 1)" "SUCCESS"
}
function Install-VisualStudioBuildTools {
Write-Log "Checking Visual Studio Build Tools..." "STEP"
$buildToolsInstalled = $false
$buildToolsPath = $null
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vswhere) {
try {
$buildToolsInstances = & $vswhere -products Microsoft.VisualStudio.Product.BuildTools -format json 2>$null | ConvertFrom-Json
if ($buildToolsInstances -and $buildToolsInstances.Count -gt 0) {
$buildToolsInstalled = $true
$buildToolsPath = $buildToolsInstances[0].installationPath
Write-Log "Build Tools detected at: $buildToolsPath" "SUCCESS"
}
}
catch {}
}
$components = @(
"Microsoft.VisualStudio.Workload.VCTools",
"Microsoft.NetCore.Component.Runtime.6.0",
"Microsoft.NetCore.Component.SDK",
"Microsoft.VisualStudio.Component.VC.ATL"
# "Microsoft.VisualStudio.Component.Windows10SDK.$WindowsSDKVer"
)
if (-not $buildToolsInstalled) {
Write-Log "Installing Visual Studio 2022 Build Tools..." "INFO"
$installArgs = @('--passive', '--norestart')
foreach ($component in $components) { $installArgs += '--add', $component }
$installArgs += '--includeRecommended', '--remove', 'Microsoft.VisualStudio.Component.VC.CMake.Project'
$overrideString = $installArgs -join ' '
Invoke-WithRetry -Operation "Build Tools installation" -ScriptBlock {
$wingetArgs = @(
'install', '-e', '--id', 'Microsoft.VisualStudio.2022.BuildTools',
'--accept-source-agreements', '--accept-package-agreements',
'--override', "`"$overrideString`""
)
$p = Start-Process -FilePath 'winget.exe' -ArgumentList $wingetArgs -NoNewWindow -Wait -PassThru
if ($null -eq $p -or $p.ExitCode -ne 0) { throw "winget Build Tools install failed (exit $($p.ExitCode))" }
}
}
else {
Write-Log "Modifying existing Build Tools to add required components..." "INFO"
$installerPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vs_installer.exe"
if (-not (Test-Path $installerPath)) {
Write-Log "VS Installer not found, components may already be present" "WARN"
return
}
$modifyArgs = @('modify', '--installPath', "`"$buildToolsPath`"")
foreach ($component in $components) { $modifyArgs += '--add', $component }
$modifyArgs += '--includeRecommended', '--passive', '--norestart'
Invoke-WithRetry -Operation "Build Tools modify" -ScriptBlock {
$p = Start-Process -FilePath $installerPath -ArgumentList $modifyArgs -NoNewWindow -Wait -PassThru
if ($null -eq $p -or $p.ExitCode -ne 0) { throw "vs_installer modify failed (exit $($p.ExitCode))" }
}
}
Write-Log "Visual Studio Build Tools ready" "SUCCESS"
}
function Install-NVMAndNode {
Write-Log "Setting up NVM and Node.js $NODE_VERSION..." "STEP"
# Install NVM if not present
if (-not (Get-Command nvm -ErrorAction SilentlyContinue)) {
Write-Log "Installing NVM for Windows..." "INFO"
Invoke-WithRetry -Operation "NVM installation" -ScriptBlock {
winget install --id CoreyButler.NVMforWindows -e --accept-package-agreements --accept-source-agreements --silent | Out-Null
Start-Sleep -Seconds 5
}
}
# Repair NVM settings if needed
$nvmRoot = $env:NVM_HOME
if (-not $nvmRoot -or -not (Test-Path "$nvmRoot\nvm.exe")) {
$possiblePaths = @("$env:APPDATA\nvm", "$env:ProgramFiles\nvm", "${env:ProgramFiles(x86)}\nvm")
foreach ($path in $possiblePaths) {
if (Test-Path "$path\nvm.exe") {
$nvmRoot = $path
break
}
}
}
if ($nvmRoot) {
[Environment]::SetEnvironmentVariable("NVM_HOME", $nvmRoot, "Machine")
[Environment]::SetEnvironmentVariable("NVM_SYMLINK", "C:\Program Files\nodejs", "Machine")
Add-ToPathPermanently -Path $nvmRoot
Add-ToPathPermanently -Path "C:\Program Files\nodejs"
# Ensure settings.txt exists
$settingsPath = "$nvmRoot\settings.txt"
if (-not (Test-Path $settingsPath) -or (Get-Content $settingsPath -Raw -ErrorAction SilentlyContinue).Length -eq 0) {
"root: $nvmRoot`npath: C:\Program Files\nodejs`narch: 64`nproxy: none" | Out-File -FilePath $settingsPath -Encoding ASCII -Force
}
}
# Refresh PATH
$env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [Environment]::GetEnvironmentVariable("Path", "User")
if (-not (Get-Command nvm -ErrorAction SilentlyContinue)) {
throw "NVM installation failed"
}
# Install and activate Node.js
try {
$nvmList = nvm list 2>&1 | Out-String
if ($nvmList -notmatch $NODE_VERSION) {
Write-Log "Installing Node.js $NODE_VERSION..." "INFO"
nvm install $NODE_VERSION | Out-Null
Start-Sleep -Seconds 3
}
nvm use $NODE_VERSION | Out-Null
Start-Sleep -Seconds 2
# Wait for Node to become available
$nodeFound = $false
for ($i = 1; $i -le 5; $i++) {
if (Get-Command node -ErrorAction SilentlyContinue) {
$nodeFound = $true
break
}
Start-Sleep -Seconds 2
}
if (-not $nodeFound) {
throw "Node.js not available after activation"
}
$nodeVersion = node -v
Write-Log "Node.js active: $nodeVersion" "SUCCESS"
}
catch {
throw "Node.js setup failed: $($_.Exception.Message)"
}
}
function Install-Yarn {
Write-Log "Installing Yarn $YarnVersion" "STEP"
try {
$yarnVersion = yarn -v 2>&1
if ($yarnVersion -match '^1\.') {
Write-Log "Yarn $YarnVersion already installed: $yarnVersion" "SUCCESS"
return
}
}
catch { }
Invoke-WithRetry -Operation "Yarn installation" -ScriptBlock {
try {
corepack enable | Out-Null
corepack prepare yarn@${YarnVersion} --activate | Out-Null
}
catch {
npm install -g yarn@${YarnVersion} | Out-Null
}
Start-Sleep -Seconds 2
$version = yarn -v 2>&1
if ($version -notmatch '^1\.') {
throw "Yarn $YarnVersion installation verification failed"
}
}
Write-Log "Yarn installed: $(yarn -v)" "SUCCESS"
}
function Set-NodeGyp {
Write-Log "Configuring node-gyp for Visual Studio 2022..." "STEP"
# Find VS installation path using vswhere
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$vsPath = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools" # Default fallback
if (Test-Path $vswhere) {
try {
$detectedPath = & $vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath 2>$null
if ($detectedPath) { $vsPath = $detectedPath }
}
catch { }
}
# Set environment variables
$gypVars = @{
"npm_config_msvs_version" = "2022"
"GYP_MSVS_VERSION" = "2022"
"GYP_MSVS_OVERRIDE_PATH" = $vsPath
}
foreach ($var in $gypVars.GetEnumerator()) {
[Environment]::SetEnvironmentVariable($var.Key, $var.Value, "Machine")
Set-Item "env:$($var.Key)" $var.Value
}
# Add MSBuild to PATH
$msbuildPath = "$vsPath\MSBuild\Current\Bin"
if (Test-Path $msbuildPath) {
Add-ToPathPermanently -Path $msbuildPath
}
# Create .npmrc
"msvs_version=2022" | Out-File -FilePath "$env:USERPROFILE\.npmrc" -Encoding ASCII -Force
Write-Log "node-gyp configured for VS 2022" "SUCCESS"
}
function Update-Repository {
Write-Log "Setting up Vortex repository..." "STEP"
New-Item -ItemType Directory -Force -Path $Directory | Out-Null
$repoPath = "$Directory\Vortex"
if (Test-Path "$repoPath\.git") {
Write-Log "Updating existing repository..." "INFO"
git -C $repoPath fetch origin
git -C $repoPath checkout $Branch
git -C $repoPath pull --ff-only
}
else {
Write-Log "Cloning repository..." "INFO"
git clone -b $Branch $RepoUrl $repoPath
}
# Update submodules if present
if (Test-Path "$repoPath\.gitmodules") {
git -C $repoPath submodule update --init --recursive
}
# Create project .npmrc
if (-not (Test-Path "$repoPath\.npmrc")) {
"msvs_version=2022" | Out-File -FilePath "$repoPath\.npmrc" -Encoding ASCII -Force
}
Write-Log "Repository ready at: $repoPath" "SUCCESS"
}
function Show-Summary {
Write-Log "" "INFO"
Write-Log "=== INSTALLATION COMPLETE ===" "SUCCESS"
Write-Log "" "INFO"
# Check and display versions
$tools = @{
"Git" = { git --version 2>&1 }
"Python" = { py -3.10 -V 2>&1 }
"CMake" = { (cmake --version | Select-Object -First 1) 2>&1 }
"Node.js" = { node -v 2>&1 }
"NPM" = { npm -v 2>&1 }
"Yarn" = { yarn -v 2>&1 }
}
Write-Log "Installed Tools:" "INFO"
foreach ($tool in $tools.GetEnumerator()) {
try {
$version = & $tool.Value
Write-Host " [OK] $($tool.Key): $version" -ForegroundColor Green
}
catch {
Write-Host " [ERR] $($tool.Key): Not available" -ForegroundColor Red
}
}
Write-Log "" "INFO"
Write-Log "Next Steps:" "INFO"
Write-Host " 1. cd C:\vortex\Vortex" -ForegroundColor Cyan
Write-Host " 2. yarn install" -ForegroundColor Cyan
Write-Host " 3. yarn build" -ForegroundColor Cyan
Write-Log "" "INFO"
Write-Log "Repository location: C:\vortex\Vortex" "INFO"
Write-Log "Bootstrap completed successfully!" "SUCCESS"
}
# Main Execution
try {
Write-Log "Starting Windows Development Environment Bootstrap" "STEP"
Write-Log "This will install: Git, Python 3.10, CMake, VS Build Tools, NVM, Node.js $NODE_VERSION, Yarn" "INFO"
Write-Log "" "INFO"
# Prerequisites check
if (-not (Test-AdminRights)) {
throw "Administrator privileges required. Please run PowerShell as Administrator."
}
Test-WingetAvailable
# Install all components
Install-Git
Install-Python310
Install-CMake
Install-VisualStudioBuildTools
Install-NVMAndNode
Install-Yarn
Set-NodeGyp
Update-Repository
# Final summary
Show-Summary
}
catch {
Write-Log "" "ERROR"
Write-Log "Bootstrap failed: $($_.Exception.Message)" "ERROR"
Write-Log "" "ERROR"
Write-Log "Troubleshooting:" "ERROR"
Write-Log "- Restart computer and run the script again (it handles partial installations)" "ERROR"
Write-Log "- Ensure winget is installed (Microsoft Store > App Installer)" "ERROR"
Write-Log "- Check Windows Defender is not blocking installations" "ERROR"
Write-Log "- Try running individual install commands manually" "ERROR"
exit 1
}