-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinit.ps1
More file actions
212 lines (189 loc) · 7.7 KB
/
init.ps1
File metadata and controls
212 lines (189 loc) · 7.7 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
param([switch]$Software)
Start-Transcript -Path "$ENV:TEMP\winconf.log" -Append
$DOTFILES = "$env:userprofile\winconf"
$SCRIPTS_DIR = "$DOTFILES\scripts"
$REPO_URL = 'https://github.com/asolopovas/winconf.git'
$AUTOHOTKEYVERSION = 2
$USER = $env:USERNAME
$PINNED_SOFTWARE = @(
'ScreamingFrog.SEOSpider'
'Adobe.Acrobat.Reader.64-bit'
'Nvidia.CUDA'
'GPSoftware.DirectoryOpus'
)
$ESSENTIAL_SOFTWARE = @(
'AutoHotkey.AutoHotkey'
'Git.Git'
'junegunn.fzf'
"Microsoft.PowerToys"
'Microsoft.PowerShell'
'voidtools.Everything'
'Starship.Starship'
"sharkdp.fd"
"VideoLAN.VLC"
'WinSCP.WinSCP'
)
$SOURCE_FILES = @(
'cleanup'
'inst-paths'
'inst-fonts'
'inst-pwsh'
'inst-terminal'
'inst-ahk'
'wsl-exclusions'
'inst-modules'
'inst-scoop'
)
if ($Software) {
$SOURCE_FILES += 'inst-software'
}
Write-Host "Setting execution policy to RemoteSigned..." -ForegroundColor Yellow
try {
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
} catch {
Write-Host "Warning: Could not set execution policy. Continuing..." -ForegroundColor Yellow
}
function Test-CommandExists {
Param ($command)
return [bool](Get-Command $command -ErrorAction SilentlyContinue)
}
function SourceFile {
param ($file)
Write-Host "`nSourcing $file ..." -ForegroundColor DarkCyan
if ($file -eq 'inst-ahk') {
& "$SCRIPTS_DIR\$file.ps1" -version $AUTOHOTKEYVERSION
} elseif ($file -eq 'inst-modules' -and $isUpdate) {
& "$SCRIPTS_DIR\$file.ps1" -Update
} else {
& "$SCRIPTS_DIR\$file.ps1"
}
}
$isUpdate = Test-Path -Path $DOTFILES
if ($isUpdate) {
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " winconf is already installed." -ForegroundColor Cyan
Write-Host " Running in UPDATE mode." -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "`nThis will:" -ForegroundColor Yellow
Write-Host " - Pull latest winconf changes from git" -ForegroundColor DarkGray
Write-Host " - Upgrade essential software via winget" -ForegroundColor DarkGray
Write-Host " - Update PowerShell modules" -ForegroundColor DarkGray
Write-Host " - Re-run all setup scripts (idempotent)" -ForegroundColor DarkGray
if ($Software) {
Write-Host " - Install/upgrade extended software" -ForegroundColor DarkGray
}
$confirm = Read-Host "`nProceed? (Y/n)"
if ($confirm -and $confirm -notmatch '^[Yy]') {
Write-Host "Cancelled." -ForegroundColor Yellow
Stop-Transcript
exit 0
}
Write-Host "`nPulling latest changes..." -ForegroundColor Green
Set-Location -Path $DOTFILES
git pull
if ($LASTEXITCODE -ne 0) {
Write-Host "Warning: git pull failed. Continuing with local version..." -ForegroundColor Yellow
}
Write-Host "`nChecking for software upgrades..." -ForegroundColor Green
$upgradeOutput = winget upgrade --accept-source-agreements 2>$null | Out-String
$toUpgrade = $ESSENTIAL_SOFTWARE | Where-Object { $upgradeOutput -match [regex]::Escape($_) }
if ($toUpgrade) {
Write-Host " Upgrades available: $($toUpgrade -join ', ')" -ForegroundColor Yellow
$jobs = @()
foreach ($soft in $toUpgrade) {
Write-Host " Upgrading $soft..." -ForegroundColor DarkGray
$jobs += Start-Job -ScriptBlock {
param($id)
winget upgrade --id $id -h --disable-interactivity --accept-source-agreements --accept-package-agreements 2>$null
} -ArgumentList $soft
}
$jobs | Wait-Job | ForEach-Object {
Receive-Job $_ -ErrorAction SilentlyContinue
Remove-Job $_
}
} else {
Write-Host " All essential software is up to date" -ForegroundColor DarkGray
}
} else {
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " winconf FRESH INSTALL" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "`nThis will:" -ForegroundColor Yellow
Write-Host " - Install essential software via winget" -ForegroundColor DarkGray
Write-Host " - Clone winconf repository" -ForegroundColor DarkGray
Write-Host " - Install PowerShell modules" -ForegroundColor DarkGray
Write-Host " - Run all setup scripts" -ForegroundColor DarkGray
Write-Host "`nResetting and updating winget sources..." -ForegroundColor Green
winget source reset --force
winget source update
Write-Host "`nInstalling Git first (required for clone)..." -ForegroundColor Green
winget install --id Git.Git -h --disable-interactivity --accept-source-agreements --accept-package-agreements --force
$remainingSoftware = $ESSENTIAL_SOFTWARE | Where-Object { $_ -ne 'Git.Git' }
Write-Host "`nInstalling remaining software in parallel..." -ForegroundColor Green
$jobs = @()
foreach ($soft in $remainingSoftware) {
Write-Host " Queuing $soft..." -ForegroundColor DarkGray
$jobs += Start-Job -ScriptBlock {
param($id)
winget install --id $id -h --disable-interactivity --accept-source-agreements --accept-package-agreements --force 2>$null
} -ArgumentList $soft
}
$jobs | Wait-Job | ForEach-Object {
Receive-Job $_ -ErrorAction SilentlyContinue
Remove-Job $_
}
Write-Host " Software installation complete" -ForegroundColor Green
if (!(Test-CommandExists git)) {
Write-Host "Adding git to Path" -ForegroundColor Yellow
$gitPath = "$env:ProgramFiles\Git\cmd"
if (Test-Path $gitPath) {
[System.Environment]::SetEnvironmentVariable("Path", $env:Path + ";$gitPath", [System.EnvironmentVariableTarget]::Machine)
$env:PATH += ";$gitPath"
Write-Host "Git added to PATH." -ForegroundColor Green
} else {
Write-Host "Git not found. Please install Git manually." -ForegroundColor Red
exit 1
}
}
git config --global --add safe.directory "$DOTFILES"
Write-Host "`nCloning repository into $DOTFILES..." -ForegroundColor Green
git clone $REPO_URL $DOTFILES
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to clone the repository. Exiting..." -ForegroundColor Red
exit 1
}
Write-Host "Fixing repository ownership for $USER..." -ForegroundColor Yellow
takeown /F $DOTFILES /R /D Y
icacls $DOTFILES /grant "${USER}:(OI)(CI)F" /T /C
}
$modulePath = "$env:USERPROFILE\winconf\powershell\modules"
if (Test-Path $modulePath) {
$env:PSModulePath = "$modulePath;$env:PSModulePath"
Write-Host "Added $modulePath to PSModulePath." -ForegroundColor Green
}
if (!(Test-Path -Path $SCRIPTS_DIR)) {
Write-Host "Scripts directory not found. Cloning might have failed." -ForegroundColor Red
exit 1
}
Write-Host "`nRunning setup scripts..." -ForegroundColor Green
foreach ($file in $SOURCE_FILES) {
SourceFile $file
}
Write-Host "`nApplying winget pins..." -ForegroundColor Green
$existingPins = winget pin list 2>$null | Out-String
foreach ($pin in $PINNED_SOFTWARE) {
if ($existingPins -match [regex]::Escape($pin)) {
Write-Host " Already pinned: $pin" -ForegroundColor DarkGray
} else {
Write-Host " Pinning $pin..." -ForegroundColor DarkGray
winget pin add --id $pin --accept-source-agreements 2>$null
}
}
Write-Host "`n========================================" -ForegroundColor Green
if ($isUpdate) {
Write-Host " Update complete!" -ForegroundColor Green
} else {
Write-Host " Installation complete!" -ForegroundColor Green
}
Write-Host "========================================`n" -ForegroundColor Green
Stop-Transcript