-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
1909 lines (1647 loc) · 65.3 KB
/
install.ps1
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Requires -RunAsAdministrator
# Ensure $PROFILE is set correctly
if (-not $PROFILE) {
$PROFILE = Join-Path ([Environment]::GetFolderPath("MyDocuments")) "WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
}
# Script-wide variables
$script:Config = $null
$script:CONFIG_PATHS = $null
$SCRIPT_LOG_PATH = Join-Path $env:TEMP "windows_setup_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
$INSTALLATION_STATE = @{
Steps = @{}
BackupPaths = @{}
StartTime = $null
}
$INSTALLED_COMPONENTS = @{}
# Get the directory where the script is located
$SCRIPT_DIR = $PSScriptRoot
if (-not $SCRIPT_DIR) {
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Definition
}
# Store the original directory
$ORIGINAL_DIR = Get-Location
function Install-BasicPrograms {
if (Test-InstallationState "basic_programs") {
Write-ColorOutput "Basic programs already installed" "Status"
return
}
Write-ColorOutput "Installing basic programs..." "Status"
$programs = @(
@{Name = "git"; Alias = "git"},
@{Name = "powershell-core"; Alias = "pwsh"},
@{Name = "starship"; Alias = "starship"},
@{Name = "fzf"; Alias = "fzf"},
@{Name = "ag"; Alias = "ag"},
@{Name = "bat"; Alias = "bat"}
)
$failed_installs = @()
$installed = @()
foreach ($program in $programs) {
if (-not (Get-Command -Name $program.Alias -ErrorAction SilentlyContinue)) {
try {
choco install $program.Name -y
# Refresh PATH after each installation
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" +
[System.Environment]::GetEnvironmentVariable("Path","User")
if (Get-Command -Name $program.Alias -ErrorAction SilentlyContinue) {
$installed += $program.Name
} else {
$failed_installs += $program.Name
}
}
catch {
$failed_installs += $program.Name
}
} else {
Write-ColorOutput "$($program.Name) is already installed" "Status"
}
}
if ($installed.Count -gt 0) {
Write-ColorOutput "Successfully installed: $($installed -join ', ')" "Success"
}
if ($failed_installs.Count -gt 0) {
Write-ColorOutput "Failed to install: $($failed_installs -join ', ')" "Error"
return $false
}
Save-InstallationState "basic_programs"
Write-ColorOutput "Basic programs installation completed" "Success"
return $true
}
# Function to install Chocolatey
function Install-Chocolatey {
Write-ColorOutput "Installing Chocolatey..." "Status"
if (-not (Get-Command -Name choco -ErrorAction SilentlyContinue)) {
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Write-ColorOutput "Chocolatey installed successfully" "Success"
} else {
Write-ColorOutput "Chocolatey is already installed" "Status"
}
}
function Install-GitSSH {
[CmdletBinding()]
param()
if (Test-InstallationState "git_ssh") {
Write-ColorOutput "Git and SSH already configured" "Status"
return $true
}
Write-ColorOutput "Setting up Git and SSH..." "Status"
# Install Git using Chocolatey if not present
if (-not (Test-Command "git")) {
try {
Write-ColorOutput "Installing Git..." "Status"
Invoke-SafeCommand { choco install git -y } -ErrorMessage "Failed to install Git"
RefreshPath
if (-not (Test-Command "git")) {
throw "Git installation failed verification"
}
}
catch {
Write-ColorOutput "Failed to install Git: $_" "Error"
return $false
}
}
else {
$gitVersion = Invoke-SafeCommand { git --version } -ErrorMessage "Failed to get Git version"
Write-ColorOutput "Git $gitVersion already installed" "Status"
}
# Configure Git global settings
try {
# Get current Git configuration
$currentEmail = Invoke-SafeCommand { git config --global user.email } -ErrorMessage "Failed to get Git email"
$currentName = Invoke-SafeCommand { git config --global user.name } -ErrorMessage "Failed to get Git name"
# Configure email if not set
if ([string]::IsNullOrEmpty($currentEmail)) {
$GIT_EMAIL = Read-Host "Enter your Git email"
if ([string]::IsNullOrWhiteSpace($GIT_EMAIL)) {
Write-ColorOutput "Git email is required" "Error"
return $false
}
Invoke-SafeCommand { git config --global user.email $GIT_EMAIL } -ErrorMessage "Failed to set Git email"
Write-ColorOutput "Git email configured" "Success"
} else {
$GIT_EMAIL = $currentEmail
Write-ColorOutput "Using existing Git email: $GIT_EMAIL" "Status"
}
# Configure name if not set
if ([string]::IsNullOrEmpty($currentName)) {
$GIT_NAME = Read-Host "Enter your Git name"
if ([string]::IsNullOrWhiteSpace($GIT_NAME)) {
Write-ColorOutput "Git name is required" "Error"
return $false
}
Invoke-SafeCommand { git config --global user.name $GIT_NAME } -ErrorMessage "Failed to set Git name"
Write-ColorOutput "Git name configured" "Success"
} else {
$GIT_NAME = $currentName
Write-ColorOutput "Using existing Git name: $GIT_NAME" "Status"
}
# Additional Git configurations
Invoke-SafeCommand {
# Set default branch name
git config --global init.defaultBranch main
# Configure line endings
git config --global core.autocrlf true
# Configure credential helper
git config --global credential.helper manager-core
} -ErrorMessage "Failed to set additional Git configurations"
}
catch {
Write-ColorOutput "Failed to configure Git: $_" "Error"
return $false
}
# Setup SSH
try {
if (-not (Test-Path "~/.ssh/id_ed25519")) {
# Create .ssh directory
$sshDir = Join-Path $HOME ".ssh"
New-SafeDirectory -Path $sshDir -Force
# Generate SSH key
Write-ColorOutput "We'll now set up an SSH key for GitHub..." "Status"
Write-ColorOutput "This key will allow secure communication with GitHub." "Status"
Write-Host "`nPress Enter to continue..." -ForegroundColor Cyan
Read-Host
Write-ColorOutput "Generating new SSH key..." "Status"
# Enable OpenSSH features if needed
try {
$opensshFeature = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
if ($opensshFeature.State -ne "Installed") {
Write-ColorOutput "Installing OpenSSH..." "Status"
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
}
}
catch {
Write-ColorOutput "Note: OpenSSH feature check failed, continuing anyway..." "Warn"
}
# Configure and start ssh-agent with retry logic
$maxRetries = 3
$retryCount = 0
$success = $false
while (-not $success -and $retryCount -lt $maxRetries) {
try {
# Try to enable the service first
$null = sc.exe config ssh-agent start= auto
$sshAgentService = Get-Service -Name ssh-agent -ErrorAction Stop
if ($sshAgentService.StartType -ne 'Automatic') {
Set-Service -Name ssh-agent -StartupType Automatic
}
if ($sshAgentService.Status -ne 'Running') {
Start-Service ssh-agent
}
$success = $true
Write-ColorOutput "SSH agent service configured and started" "Success"
}
catch {
$retryCount++
if ($retryCount -eq $maxRetries) {
throw "Failed to configure SSH agent service after $maxRetries attempts: $_"
}
Write-ColorOutput "Failed to configure SSH agent. Retrying in 5 seconds..." "Warn"
Start-Sleep -Seconds 5
}
}
# Generate the key
$sshKeyPath = Join-Path $sshDir "id_ed25519"
$sshArgs = @(
"-t", "ed25519",
"-C", $GIT_EMAIL,
"-f", $sshKeyPath,
"-N", '""'
)
$result = Start-Process -FilePath "ssh-keygen" -ArgumentList $sshArgs -NoNewWindow -Wait -PassThru
if ($result.ExitCode -ne 0) {
throw "SSH key generation failed with exit code: $($result.ExitCode)"
}
# Add SSH key to agent
$null = Start-Process -FilePath "ssh-add" -ArgumentList $sshKeyPath -NoNewWindow -Wait -PassThru
# Create SSH config
$sshConfig = @"
Host github.com
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
"@
Set-Content -Path (Join-Path $sshDir "config") -Value $sshConfig -Force
Write-ColorOutput "SSH config created" "Success"
# Guide user through GitHub setup
Write-ColorOutput "`nNow we'll add this SSH key to your GitHub account." "Status"
Write-ColorOutput "Please follow these steps:" "Status"
Write-ColorOutput "1. Open GitHub.com and sign in" "Status"
Write-ColorOutput "2. Click your profile picture → Settings" "Status"
Write-ColorOutput "3. In the left sidebar, click 'SSH and GPG keys'" "Status"
Write-ColorOutput "4. Click 'New SSH key' or 'Add SSH key'" "Status"
Write-ColorOutput "5. Give your key a descriptive title (e.g., 'Work Laptop')" "Status"
Write-ColorOutput "6. Copy the following public key:" "Status"
Write-Host "`n"
Get-Content "$HOME/.ssh/id_ed25519.pub"
Write-Host "`n"
do {
$response = Read-Host "Have you added the key to GitHub? (y/n)"
if ($response -eq 'n') {
Write-ColorOutput "Please add the key to continue. Press Enter when ready to test the connection..." "Status"
Read-Host
}
} while ($response -ne 'y')
# Test connection with retries
$maxRetries = 3
$retryCount = 0
$success = $false
while (-not $success -and $retryCount -lt $maxRetries) {
try {
Write-ColorOutput "Testing GitHub SSH connection (Attempt $($retryCount + 1) of $maxRetries)..." "Status"
$output = ssh -T [email protected] -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=10 2>&1
if ($output -match "success" -or $output -match "authenticated") {
$success = $true
Write-ColorOutput "SSH connection test successful" "Success"
} else {
throw "Connection test failed: $output"
}
}
catch {
$retryCount++
if ($retryCount -eq $maxRetries) {
Write-ColorOutput "GitHub SSH connection test failed: $_" "Error"
Write-ColorOutput "Please verify that you added the SSH key to your GitHub account correctly" "Error"
Write-ColorOutput "You can try the following:" "Status"
Write-ColorOutput "1. Check if the key is visible in GitHub settings" "Status"
Write-ColorOutput "2. Ensure you copied the entire key" "Status"
Write-ColorOutput "3. Try running 'ssh -vT [email protected]' for more details" "Status"
return $false
}
Write-ColorOutput "Connection attempt failed. Retrying in 5 seconds..." "Status"
Start-Sleep -Seconds 5
}
}
}
else {
Write-ColorOutput "SSH key already exists" "Status"
# Test existing connection
try {
Write-ColorOutput "Testing existing GitHub SSH connection..." "Status"
$output = ssh -T [email protected] -o BatchMode=yes -o ConnectTimeout=10 2>&1
if ($output -match "success" -or $output -match "authenticated") {
Write-ColorOutput "Existing SSH connection verified" "Success"
} else {
throw "Connection test failed: $output"
}
}
catch {
Write-ColorOutput "Existing SSH key is not working with GitHub: $_" "Error"
Write-ColorOutput "Would you like to generate a new SSH key? (y/n)" "Status"
$response = Read-Host
if ($response -eq 'y') {
Remove-Item "~/.ssh/id_ed25519*"
return Install-GitSSH # Recursive call to generate new key
} else {
return $false
}
}
}
Save-InstallationState "git_ssh"
Write-ColorOutput "Git and SSH setup completed" "Success"
return $true
}
catch {
Write-ColorOutput "Failed during SSH setup: $_" "Error"
return $false
}
}
## Then update the Install-CliTools function:
function Install-CliTools {
[CmdletBinding()]
param()
if (Test-InstallationState "cli_tools") {
Write-ColorOutput "CLI tools already installed and configured" "Status"
return $true
}
Write-ColorOutput "Installing CLI tools..." "Status"
$failed = @()
$installed = @()
$skipped = @()
foreach ($tool in $Config.CliTools) {
try {
if (-not (Test-Command $tool.Name)) {
Write-ColorOutput "Installing $($tool.Name)..." "Status"
Invoke-SafeCommand {
choco install $tool.Name -y
} -ErrorMessage "Failed to install $($tool.Name)"
RefreshPath
if (Test-Command $tool.Name) {
$installed += $tool.Name
Write-ColorOutput "$($tool.Name) installed successfully" "Success"
} else {
throw "Installation verification failed"
}
} else {
Write-ColorOutput "$($tool.Name) is already installed" "Status"
$skipped += $tool.Name
}
# Configure if needed
if ($tool.ConfigCheck -and $tool.ConfigText) {
if (-not (Test-Path $PROFILE)) {
New-Item -Path $PROFILE -ItemType File -Force | Out-Null
Write-ColorOutput "Created new PowerShell profile" "Status"
}
if (-not (Select-String -Path $PROFILE -Pattern $tool.ConfigCheck -Quiet -ErrorAction SilentlyContinue)) {
Add-Content -Path $PROFILE -Value "`n$($tool.ConfigText)"
Write-ColorOutput "$($tool.Name) configuration added to profile" "Success"
} else {
Write-ColorOutput "$($tool.Name) already configured in profile" "Status"
}
}
}
catch {
$failed += $tool.Name
Write-ColorOutput "Failed to setup $($tool.Name): $_" "Error"
if ($tool.Required) {
return $false
}
}
}
# Summary
if ($installed.Count -gt 0) {
Write-ColorOutput "Installed: $($installed -join ', ')" "Success"
}
if ($skipped.Count -gt 0) {
Write-ColorOutput "Already installed: $($skipped -join ', ')" "Status"
}
if ($failed.Count -gt 0) {
Write-ColorOutput "Failed to install: $($failed -join ', ')" "Warn"
}
# Reload profile
try {
Write-ColorOutput "Reloading PowerShell profile..." "Status"
. $PROFILE
Write-ColorOutput "PowerShell profile reloaded" "Success"
}
catch {
Write-ColorOutput "Failed to reload PowerShell profile: $_" "Warn"
Write-ColorOutput "Please restart your PowerShell session to apply changes" "Warn"
}
Save-InstallationState "cli_tools"
Write-ColorOutput "CLI tools configuration completed" "Success"
return $true
}
# Function to install fonts
function Install-NerdFonts {
Write-ColorOutput "Installing JetBrainsMono Nerd Font..." "Status"
$fontUrl = "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/JetBrainsMono.zip"
$fontsFolder = "$env:LOCALAPPDATA\Microsoft\Windows\Fonts"
$downloadPath = "$env:TEMP\JetBrainsMono.zip"
# Download and extract font
Invoke-WebRequest -Uri $fontUrl -OutFile $downloadPath
Expand-Archive -Path $downloadPath -DestinationPath "$env:TEMP\JetBrainsMono" -Force
# Install fonts
$fonts = Get-ChildItem -Path "$env:TEMP\JetBrainsMono" -Filter "*.ttf"
foreach ($font in $fonts) {
Copy-Item $font.FullName $fontsFolder
}
Write-ColorOutput "Fonts installed successfully" "Success"
}
# Function to install and configure Starship
function Install-Starship {
if (Test-InstallationState "starship") {
Write-ColorOutput "Starship already installed and configured" "Status"
return $true
}
Write-ColorOutput "Installing and configuring Starship..." "Status"
try {
# Install Starship using Chocolatey
choco install starship -y
RefreshPath
if (-not (Get-Command -Name starship -ErrorAction SilentlyContinue)) {
Write-ColorOutput "Starship installation failed" "Error"
return $false
}
# Create Starship config directory
$starshipDir = "$env:USERPROFILE\.starship"
if (-not (Test-Path $starshipDir)) {
New-Item -ItemType Directory -Path $starshipDir -Force | Out-Null
}
# Backup existing configuration
$configPath = "$starshipDir\starship.toml"
if (Test-Path $configPath) {
Move-Item $configPath "$configPath.backup" -Force
}
# Clone dotfiles repository temporarily to get starship config
$tempPath = "$env:TEMP\dotfiles"
if (Test-Path $tempPath) {
Remove-Item $tempPath -Recurse -Force
}
git clone https://github.com/JDLanctot/dotfiles.git $tempPath
if (Test-Path "$tempPath\.config\starship.toml") {
Copy-Item "$tempPath\.config\starship.toml" $configPath -Force
Write-ColorOutput "Starship configuration installed" "Success"
}
else {
Write-ColorOutput "starship.toml not found in dotfiles" "Error"
return $false
}
# Update PowerShell profile
if (-not (Test-Path $PROFILE)) {
New-Item -path $PROFILE -type file -force | Out-Null
}
$profileContent = Get-Content $PROFILE -Raw
$initCommand = 'Invoke-Expression (&starship init powershell)'
if ($profileContent -notmatch [regex]::Escape($initCommand)) {
Add-Content $PROFILE "`n$initCommand"
Write-ColorOutput "Starship initialization added to PowerShell profile" "Success"
}
# Cleanup
Remove-Item $tempPath -Recurse -Force
Save-InstallationState "starship"
Write-ColorOutput "Starship setup completed" "Success"
return $true
}
catch {
Write-ColorOutput "Failed to install/configure Starship: $_" "Error"
return $false
}
}
# Function to install and configure Neovim
function Install-Neovim {
if (Test-InstallationState "neovim") {
Write-ColorOutput "Neovim already installed and configured" "Status"
return $true
}
Write-ColorOutput "Installing and configuring Neovim..." "Status"
try {
# Install Neovim
choco install neovim -y
RefreshPath
if (-not (Get-Command -Name nvim -ErrorAction SilentlyContinue)) {
Write-ColorOutput "Neovim installation failed" "Error"
return $false
}
# Add Neovim to Path
$neovimPath = "C:\tools\neovim\Neovim\bin"
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*$neovimPath*") {
[Environment]::SetEnvironmentVariable(
"Path",
"$userPath;$neovimPath",
"User"
)
}
# Configure Neovim
$nvimConfigPath = "$env:LOCALAPPDATA\nvim"
$nvimBackupPath = "$nvimConfigPath.backup"
# Backup existing configuration
if (Test-Path $nvimConfigPath) {
if (Test-Path $nvimBackupPath) {
Remove-Item $nvimBackupPath -Recurse -Force
}
Move-Item $nvimConfigPath $nvimBackupPath
}
# Clone dotfiles directly into nvim config directory
Push-Location $env:LOCALAPPDATA
try {
git clone https://github.com/JDLanctot/dotfiles.git nvim
Push-Location nvim
try {
# Move Neovim configuration files to root and clean up others
if (Test-Path ".config\nvim") {
Move-Item .config\nvim\* . -Force
# Clean up unnecessary files and directories
Remove-Item .config -Recurse -Force
Remove-Item .zsh -Recurse -Force
Remove-Item .bashrc -Force
Remove-Item .zshrc -Force
Remove-Item .zshenv -Force
Remove-Item README.md -Force
Remove-Item .git -Recurse -Force
Remove-Item .julia -Recurse -Force -ErrorAction SilentlyContinue
Write-ColorOutput "Neovim configuration installed" "Success"
# Install plugins
Write-ColorOutput "Installing Neovim plugins (this may take a while)..." "Status"
$result = Start-Process -Wait -NoNewWindow nvim -ArgumentList "--headless", "+Lazy! sync", "+qa" -PassThru
if ($result.ExitCode -ne 0) {
throw "Plugin installation failed"
}
}
else {
throw "Neovim configuration not found in dotfiles"
}
}
finally {
Pop-Location
}
}
catch {
# If anything fails, restore backup and clean up
if (Test-Path $nvimConfigPath) {
Remove-Item $nvimConfigPath -Recurse -Force
}
if (Test-Path $nvimBackupPath) {
Move-Item $nvimBackupPath $nvimConfigPath
}
Write-ColorOutput "Failed during Neovim configuration: $_" "Error"
return $false
}
finally {
Pop-Location
}
# Remove backup if everything succeeded
if (Test-Path $nvimBackupPath) {
Remove-Item $nvimBackupPath -Recurse -Force
}
Save-InstallationState "neovim"
Write-ColorOutput "Neovim setup completed" "Success"
return $true
}
catch {
Write-ColorOutput "Failed to install/configure Neovim: $_" "Error"
return $false
}
}
# Function to install Julia
function Install-Julia {
if (Test-InstallationState "julia") {
Write-ColorOutput "Julia already installed" "Status"
return $true
}
Write-ColorOutput "Installing Julia..." "Status"
# Install Julia using Chocolatey
try {
choco install julia -y
RefreshPath
if (-not (Get-Command -Name julia -ErrorAction SilentlyContinue)) {
Write-ColorOutput "Julia installation failed" "Error"
return $false
}
# Setup Julia configuration
$juliaConfigPath = "$env:USERPROFILE\.julia\config"
New-SafeDirectory -Path $juliaConfigPath
# Copy startup.jl from dotfiles
$tempPath = "$env:TEMP\dotfiles"
if (-not (Test-Path "$juliaConfigPath\startup.jl")) {
if (-not (Test-Path $tempPath)) {
git clone https://github.com/JDLanctot/dotfiles.git $tempPath
}
if (Test-Path "$tempPath\.julia\config\startup.jl") {
Copy-Item "$tempPath\.julia\config\startup.jl" $juliaConfigPath -Force
Write-ColorOutput "Julia configuration installed" "Success"
}
else {
Write-ColorOutput "Julia startup.jl not found in dotfiles" "Error"
}
# Cleanup
if (Test-Path $tempPath) {
Remove-Item $tempPath -Recurse -Force
}
}
$juliaVersion = (julia --version)
Write-ColorOutput "Julia $juliaVersion installed and configured" "Success"
Save-InstallationState "julia"
return $true
}
catch {
Write-ColorOutput "Failed to install Julia: $_" "Error"
return $false
}
}
# Function to install Zig
function Install-Zig {
if (Test-InstallationState "zig") {
Write-ColorOutput "Zig already installed" "Status"
return $true
}
Write-ColorOutput "Installing Zig..." "Status"
try {
# Install Zig using Chocolatey
choco install zig -y
RefreshPath
if (-not (Get-Command -Name zig -ErrorAction SilentlyContinue)) {
Write-ColorOutput "Zig installation failed" "Error"
return $false
}
# Add Zig to Path if not already present
$zigPath = "C:\ProgramData\chocolatey\bin"
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*$zigPath*") {
[Environment]::SetEnvironmentVariable(
"Path",
"$userPath;$zigPath",
"User"
)
}
$zigVersion = (zig version)
Write-ColorOutput "Zig $zigVersion installed" "Success"
Save-InstallationState "zig"
return $true
}
catch {
Write-ColorOutput "Failed to install Zig: $_" "Error"
return $false
}
}
# Function to install Node.js and pnpm
function Install-Node {
if (Test-InstallationState "nodejs") {
Write-ColorOutput "Node.js and pnpm already installed" "Status"
return $true
}
Write-ColorOutput "Installing Node.js and pnpm..." "Status"
# Install Node.js using Chocolatey
if (-not (Get-Command -Name node -ErrorAction SilentlyContinue)) {
try {
choco install nodejs-lts -y
RefreshPath
if (-not (Get-Command -Name node -ErrorAction SilentlyContinue)) {
Write-ColorOutput "Node.js installation failed" "Error"
return $false
}
$nodeVersion = (node --version)
Write-ColorOutput "Node.js $nodeVersion installed" "Success"
}
catch {
Write-ColorOutput "Failed to install Node.js: $_" "Error"
return $false
}
}
else {
$nodeVersion = (node --version)
Write-ColorOutput "Node.js $nodeVersion is already installed" "Status"
}
# Install pnpm
if (-not (Get-Command -Name pnpm -ErrorAction SilentlyContinue)) {
try {
npm install -g pnpm
RefreshPath
if (-not (Get-Command -Name pnpm -ErrorAction SilentlyContinue)) {
Write-ColorOutput "pnpm installation failed" "Error"
return $false
}
$pnpmVersion = (pnpm --version)
Write-ColorOutput "pnpm $pnpmVersion installed" "Success"
}
catch {
Write-ColorOutput "Failed to install pnpm: $_" "Error"
return $false
}
}
else {
$pnpmVersion = (pnpm --version)
Write-ColorOutput "pnpm $pnpmVersion is already installed" "Status"
}
# Install neovim
if (-not (Get-Command -Name neovim -ErrorAction SilentlyContinue)) {
try {
npm install -g neovim
RefreshPath
if (-not (Get-Command -Name neovim -ErrorAction SilentlyContinue)) {
Write-ColorOutput "neovim installation failed" "Error"
return $false
}
$neovimVersion = (neovim --version)
Write-ColorOutput "neovim $neovimVersion installed" "Success"
}
catch {
Write-ColorOutput "Failed to install neovim: $_" "Error"
return $false
}
}
else {
$neovimVersion = (neovim --version)
Write-ColorOutput "neovim $neovimVersion is already installed" "Status"
}
Save-InstallationState "nodejs"
Write-ColorOutput "Node.js environment setup completed" "Success"
return $true
}
function Install-Miniconda {
[CmdletBinding()]
param()
if (Test-InstallationState "miniconda") {
Write-ColorOutput "Miniconda already installed and configured" "Status"
return $true
}
Write-ColorOutput "Installing Miniconda..." "Status"
try {
# Check if conda is already installed
if (Get-Command conda -ErrorAction SilentlyContinue) {
Write-ColorOutput "Conda is already installed" "Status"
$condaVersion = (conda --version)
Write-ColorOutput "Version: $condaVersion" "Status"
}
else {
# Download Miniconda installer
$installerUrl = "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe"
$installerPath = Join-Path $env:TEMP "miniconda.exe"
Write-ColorOutput "Downloading Miniconda installer..." "Status"
try {
Invoke-SafeCommand {
Invoke-WebRequest -Uri $installerUrl -OutFile $installerPath -UseBasicParsing
} -ErrorMessage "Failed to download Miniconda installer"
}
catch {
# Fallback to curl if WebRequest fails
Write-ColorOutput "Trying alternative download method..." "Status"
Invoke-SafeCommand {
curl.exe $installerUrl -o $installerPath
} -ErrorMessage "Failed to download Miniconda installer using curl"
}
if (-not (Test-Path $installerPath)) {
throw "Installer download failed"
}
# Install Miniconda silently
Write-ColorOutput "Installing Miniconda (this may take a few minutes)..." "Status"
$installArgs = @(
"/S", # Silent installation
"/AddToPath=1", # Add to PATH
"/RegisterPython=1", # Register as system Python
"/D=$env:USERPROFILE\Miniconda3" # Installation directory
)
$result = Start-Process -FilePath $installerPath -ArgumentList $installArgs -Wait -PassThru
if ($result.ExitCode -ne 0) {
throw "Installation failed with exit code: $($result.ExitCode)"
}
# Clean up installer
Remove-Item $installerPath -Force -ErrorAction SilentlyContinue
# Refresh environment variables
RefreshPath
# Verify installation
if (-not (Get-Command conda -ErrorAction SilentlyContinue)) {
throw "Conda command not found after installation"
}
}
# Initialize conda for PowerShell
Write-ColorOutput "Initializing conda for PowerShell..." "Status"
# Create a temporary script to run conda init
$initScript = Join-Path $env:TEMP "conda-init.ps1"
@"
`$env:Path = "${env:USERPROFILE}\Miniconda3;${env:USERPROFILE}\Miniconda3\Scripts;${env:USERPROFILE}\Miniconda3\Library\bin;$env:Path"
conda init powershell
"@ | Set-Content $initScript
# Execute the init script in a new PowerShell process
$initResult = Start-Process powershell -ArgumentList "-File `"$initScript`"" -Wait -PassThru
if ($initResult.ExitCode -ne 0) {
throw "Conda initialization failed"
}
# Clean up
Remove-Item $initScript -Force -ErrorAction SilentlyContinue
# Verify conda initialization
if (-not (Select-String -Path $PROFILE -Pattern "conda initialize" -Quiet -ErrorAction SilentlyContinue)) {
Write-ColorOutput "Warning: Conda initialization not found in PowerShell profile" "Warn"
Write-ColorOutput "You may need to manually run 'conda init powershell' in a new terminal" "Warn"
} else {
Write-ColorOutput "Conda initialization added to PowerShell profile" "Success"
}
Save-InstallationState "miniconda"
Write-ColorOutput "Miniconda installation completed" "Success"
Write-ColorOutput "Please restart your terminal to use conda" "Status"
return $true
}
catch {
Write-ColorOutput "Failed to install Miniconda: $_" "Error"
return $false
}
}
# Function to setup PowerShell profile
function Install-PowerShellProfile {
if (Test-InstallationState "powershell_profile") {
Write-ColorOutput "PowerShell profile already configured" "Status"
return $true
}
Write-ColorOutput "Setting up PowerShell profile..." "Status"
try {
# Create profile directory if it doesn't exist
$profileDir = Split-Path $PROFILE -Parent
if (-not (Test-Path $profileDir)) {
New-Item -Path $profileDir -ItemType Directory -Force | Out-Null
}
# Backup existing profile
if (Test-Path $PROFILE) {
Copy-Item $PROFILE "$PROFILE.backup" -Force
}
# Get profile from dotfiles
$tempPath = "$env:TEMP\dotfiles"
if (-not (Test-Path $tempPath)) {
git clone https://github.com/JDLanctot/dotfiles.git $tempPath
}
if (Test-Path "$tempPath\.windows\profile.ps1") {
Copy-Item "$tempPath\.windows\profile.ps1" $PROFILE -Force
Write-ColorOutput "PowerShell profile installed" "Success"
# Verify essential configurations
$profileContent = Get-Content $PROFILE -Raw
$requiredConfigs = @(
@{Name = "Starship initialization"; Pattern = "Invoke-Expression \(&starship init powershell\)"}
@{Name = "PSFzf import"; Pattern = "Import-Module PSFzf"}
@{Name = "Neovim alias"; Pattern = "Set-Alias.*vim.*nvim"}
)
$missingConfigs = @()
foreach ($config in $requiredConfigs) {
if ($profileContent -notmatch $config.Pattern) {
$missingConfigs += $config.Name
}
}
if ($missingConfigs.Count -gt 0) {
Write-ColorOutput "Warning: Missing configurations in profile:" "Error"
foreach ($config in $missingConfigs) {
Write-ColorOutput "- $config" "Error"
}
return $false
}
Write-ColorOutput "All essential configurations verified in profile" "Success"
}
else {
Write-ColorOutput "PowerShell profile not found in dotfiles" "Error"
return $false
}
Save-InstallationState "powershell_profile"
Write-ColorOutput "PowerShell profile setup completed" "Success"
return $true
}
catch {
Write-ColorOutput "Failed to configure PowerShell profile: $_" "Error"
if (Test-Path "$PROFILE.backup") {
Move-Item "$PROFILE.backup" $PROFILE -Force