Repository navigation
Expand file tree
/
Copy pathsetup.ps1
More file actions
298 lines (271 loc) · 10.5 KB
/
Copy pathsetup.ps1
File metadata and controls
298 lines (271 loc) · 10.5 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
# scripts/windows/setup.ps1 - Core Windows installer orchestrator (#468 flags-first)
#
# Called by: setup.ps1 (root entry point)
#
# Usage (direct):
# powershell -ExecutionPolicy Bypass -File scripts\windows\setup.ps1 [OPTIONS]
#
# Flags:
# -List Print available tools (alphabetical), exit 0. No install.
# -Help Print usage, exit 0.
# -Only "a,b,c" Install ONLY the listed tools (comma-separated).
# -Skip "a,b,c" Install all default tools EXCEPT the listed ones.
# -Interactive Show the tool picker when a console is available.
# -NonInteractive Never show the tool picker.
# -Only and -Skip are mutually exclusive.
#
# Hidden test seam (not in -Help):
# -ToolsDir <path> Override the tools directory (test use only).
# -SelectionFile <path>
# Read selected tools from a file (test use only).
#
# PS 5.1 ASCII-only: no smart quotes, em-dashes, or non-ASCII characters.
[CmdletBinding()]
param(
[string]$Only = '',
[string]$Skip = '',
[switch]$List,
[switch]$Help,
[switch]$Interactive,
[switch]$NonInteractive,
[string]$ToolsDir = '',
[string]$SelectionFile = ''
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
. "$PSScriptRoot\lib\logging.ps1"
. "$PSScriptRoot\lib\path.ps1"
. "$PSScriptRoot\lib\tui.ps1"
# Dot-source all tool installer modules
. "$PSScriptRoot\tools\winget-check.ps1"
. "$PSScriptRoot\tools\git.ps1"
. "$PSScriptRoot\tools\nvm.ps1"
. "$PSScriptRoot\tools\gh.ps1"
. "$PSScriptRoot\tools\vim.ps1"
. "$PSScriptRoot\tools\copilot.ps1"
. "$PSScriptRoot\tools\dotfiles.ps1"
. "$PSScriptRoot\tools\profile.ps1"
. "$PSScriptRoot\tools\auth.ps1"
. "$PSScriptRoot\tools\git-hook.ps1"
. "$PSScriptRoot\tools\delta.ps1"
. "$PSScriptRoot\tools\lazygit.ps1"
. "$PSScriptRoot\tools\pi.ps1"
. "$PSScriptRoot\tools\herdr.ps1"
# ---------------------------------------------------------------------------
# $DefaultTools -- single ordered source of truth for a no-arg default run.
# Adding a .ps1 file to tools/ + a registry entry makes it AvailableTools
# (selectable via -Only) but does NOT add it here.
# ---------------------------------------------------------------------------
$DefaultTools = @(
'winget-check'
'git'
'nvm'
'gh'
'auth'
'vim'
'copilot'
'dotfiles'
'profile'
'git-hook'
)
# $ToolRegistry maps logical tool names to their install scriptblock.
# copilot-cli is aliased to copilot so both platforms accept the same name.
$ToolRegistry = [ordered]@{
'winget-check' = { Invoke-WingetGate }
'git' = { Install-Git }
'nvm' = { Install-Nvm }
'gh' = { Install-GhCli }
'auth' = { Invoke-GhAuth }
'vim' = { Install-Vim }
'copilot' = { Install-CopilotCli }
'copilot-cli' = { Install-CopilotCli }
'dotfiles' = { Install-Dotfiles }
'profile' = { Write-PowerShellProfile }
'git-hook' = { Install-GitHook }
'delta' = { Install-Delta }
'lazygit' = { Install-Lazygit }
'pi' = { Install-Pi }
'herdr' = { Install-Herdr }
}
# ---------------------------------------------------------------------------
# Test seam: when -ToolsDir is set, build a dynamic registry from stub files
# and load DefaultTools from defaults.txt in that directory.
# ---------------------------------------------------------------------------
if ($ToolsDir) {
$ToolRegistry = [ordered]@{}
foreach ($f in (Get-ChildItem "$ToolsDir\*.ps1" | Sort-Object Name)) {
$name = $f.BaseName
$path = $f.FullName
$ToolRegistry[$name] = [scriptblock]::Create(". '$path'")
}
$defaultsFile = Join-Path $ToolsDir 'defaults.txt'
if (Test-Path $defaultsFile) {
$DefaultTools = Get-Content $defaultsFile | Where-Object { $_ -ne '' }
}
}
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
function Get-AvailableTool {
return ($ToolRegistry.Keys | Sort-Object)
}
function Split-ToolList {
param([string]$ToolList)
if ([string]::IsNullOrEmpty($ToolList)) {
Write-Err "Flag requires at least one tool name."
exit 1
}
$tools = $ToolList.Split(',')
foreach ($t in $tools) {
if ([string]::IsNullOrEmpty($t)) {
Write-Err "Empty tool name in list (check commas)."
exit 1
}
}
return $tools
}
function Invoke-Tool {
param([string]$Name)
if (-not $ToolRegistry.Contains($Name)) {
Write-Warn "Tool not in registry, skipping: $Name"
return
}
Write-Info "Installing: $Name"
& $ToolRegistry[$Name]
Write-Ok "$Name -- done"
}
function Show-Help {
Write-Output "Usage: setup.ps1 [OPTIONS]"
Write-Output ""
Write-Output "Install developer tools on Windows."
Write-Output ""
Write-Output "Options:"
Write-Output " -List Print available tools (alphabetical order), exit 0."
Write-Output " -Help Print this help message, exit 0."
Write-Output " -Only 'a,b,c' Install ONLY the listed tools (comma-separated)."
Write-Output " -Skip 'a,b,c' Install all default tools EXCEPT the listed ones."
Write-Output " -Interactive Show the tool picker when a console is available."
Write-Output " -NonInteractive Never show the tool picker."
Write-Output ""
Write-Output "Notes:"
Write-Output " -Only and -Skip are mutually exclusive."
Write-Output " Unknown tool names exit with an error and print the available list."
Write-Output " A no-arg run installs all default tools in the defined order."
}
# ---------------------------------------------------------------------------
# --help / --list handling
# ---------------------------------------------------------------------------
if ($Help) {
Show-Help
exit 0
}
if ($List) {
Get-AvailableTool | ForEach-Object { Write-Output $_ }
exit 0
}
# ---------------------------------------------------------------------------
# Mutual exclusion
# ---------------------------------------------------------------------------
if ($PSBoundParameters.ContainsKey('Only') -and $PSBoundParameters.ContainsKey('Skip')) {
Write-Err "-Only and -Skip are mutually exclusive."
exit 1
}
if ($Interactive -and $NonInteractive) {
Write-Err "-Interactive and -NonInteractive are mutually exclusive."
exit 1
}
if ($NonInteractive -and $PSBoundParameters.ContainsKey('SelectionFile')) {
Write-Err "-NonInteractive and -SelectionFile are mutually exclusive."
exit 1
}
# ---------------------------------------------------------------------------
# Interactive guard. Explicit flags and headless environments suppress the menu.
# ---------------------------------------------------------------------------
function Test-ShouldShowMenu {
param(
[bool]$NonInteractiveRequested,
[bool]$OnlySet,
[bool]$SkipSet,
[bool]$InteractiveRequested,
[bool]$SelectionFileSet
)
if ($NonInteractiveRequested -or $OnlySet -or $SkipSet) { return $false }
# -Interactive + -SelectionFile: bypass CI/TTY detection so CI can test the menu path.
if ($InteractiveRequested -and $SelectionFileSet) { return $true }
if ($env:SETUP_NON_INTERACTIVE -eq '1' -or $env:CI -or $env:GITHUB_ACTIONS) { return $false }
if ([Console]::IsInputRedirected -or -not [Environment]::UserInteractive) { return $false }
if ($null -eq $Host.UI.RawUI) { return $false }
return $true
}
# ---------------------------------------------------------------------------
# Determine final toolset: menu path (interactive) or flag path (non-interactive)
# ---------------------------------------------------------------------------
$Available = Get-AvailableTool
[string[]]$FinalTools = @()
if (Test-ShouldShowMenu `
-NonInteractiveRequested $NonInteractive.IsPresent `
-OnlySet ($PSBoundParameters.ContainsKey('Only')) `
-SkipSet ($PSBoundParameters.ContainsKey('Skip')) `
-InteractiveRequested $Interactive.IsPresent `
-SelectionFileSet ($PSBoundParameters.ContainsKey('SelectionFile'))) {
# Interactive path: selection-file seam (CI/test) or live menu
if ($PSBoundParameters.ContainsKey('SelectionFile') -and $SelectionFile) {
[string[]]$selectedNames = @(Get-Content -LiteralPath $SelectionFile |
Where-Object { -not [string]::IsNullOrEmpty($_) })
} else {
[string[]]$selectedNames = Show-ToolMenu -DefaultTools $DefaultTools -Available $Available
}
if ($null -eq $selectedNames) {
Write-TuiLine -Text 'Install cancelled.' -Color (Get-TuiRoleColor -Role 'Cancel')
exit 0
}
if ($selectedNames.Count -eq 0) {
Write-Output 'Nothing selected, exiting.'
exit 0
}
$FinalTools = Resolve-FinalToolset -DefaultTools $DefaultTools `
-Only ($selectedNames -join ',') -OnlySet $true
} else {
# Non-interactive path: validate tool names then resolve
if ($PSBoundParameters.ContainsKey('Only')) {
$names = Split-ToolList -ToolList $Only
foreach ($name in $names) {
if ($Available -notcontains $name) {
Write-Err "Unknown tool: $name"
Write-Err "Available tools: $($Available -join ', ')"
Write-Err "Use -List to see all available tools."
exit 1
}
}
$FinalTools = Resolve-FinalToolset -DefaultTools $DefaultTools `
-Only $Only -OnlySet $true
} elseif ($PSBoundParameters.ContainsKey('Skip')) {
$names = Split-ToolList -ToolList $Skip
foreach ($name in $names) {
if ($Available -notcontains $name) {
Write-Err "Unknown tool: $name"
Write-Err "Available tools: $($Available -join ', ')"
exit 1
}
}
$FinalTools = Resolve-FinalToolset -DefaultTools $DefaultTools `
-Skip $Skip -SkipSet $true
} else {
$FinalTools = Resolve-FinalToolset -DefaultTools $DefaultTools
}
}
# ---------------------------------------------------------------------------
# Main dispatch
# ---------------------------------------------------------------------------
function Main {
Write-Info "Starting Windows setup..."
foreach ($tool in $FinalTools) {
Invoke-Tool -Name $tool
}
Write-Ok ""
Write-Ok "Setup complete!"
Write-Info "Next steps:"
Write-Info " 1. Restart your terminal to apply PATH changes"
Write-Info " 2. Run: gh auth login"
}
Main