Repository navigation
Expand file tree
/
Copy pathsetup.nu
More file actions
447 lines (381 loc) · 14.8 KB
/
Copy pathsetup.nu
File metadata and controls
447 lines (381 loc) · 14.8 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
#!/usr/bin/env nu
# =============================================================================
# Platform detection stuffs
# =============================================================================
def get-platform [] {
match $nu.os-info.name {
"windows" => "windows"
"macos" => "macos"
"linux" => {
if ("/proc/version" | path exists) {
let ver = (open /proc/version | str downcase)
if ($ver | str contains "microsoft") { "wsl" } else { "linux" }
} else { "linux" }
}
_ => "unknown"
}
}
# A destination is a path, a {platform: path} record, or a list of those.
def resolve-dests [dest_data: any, platform: string] {
let entries = if (($dest_data | describe) =~ '^(list|table)') { $dest_data } else { [$dest_data] }
$entries | each {|d|
if ($d | describe | str starts-with "record") {
if ($platform in ($d | columns)) { $d | get $platform } else { null }
} else { $d }
} | compact
}
def is-unix [platform: string] { $platform in ["wsl", "macos", "linux"] }
# =============================================================================
# Package manager helpers
# =============================================================================
def get-manager [platform: string] {
if $platform == "windows" {
"winget"
} else if (which brew | is-not-empty) {
"brew"
} else if (which apt | is-not-empty) {
"apt"
} else {
null
}
}
def pkg-is-installed [manager: string, id: string] {
match $manager {
"winget" => {
let list = (^winget list --disable-interactivity | str downcase)
$list | str contains ($id | str downcase)
}
"brew" => {
let installed = (^brew list --formula --full-name | lines) ++ (^brew list --cask --full-name | lines)
($id | str downcase) in ($installed | each { str downcase })
}
"apt" => {
let installed = (^dpkg-query -W -f '${Package}\n' | lines | each { str trim })
($id | str downcase) in ($installed | each { str downcase })
}
}
}
def pkg-install [manager: string, id: string, args?: string] {
match $manager {
"winget" => {
if ($args | is-empty) {
^winget install $id --accept-source-agreements --accept-package-agreements --source winget --silent | complete
} else {
^winget install $id --accept-source-agreements --accept-package-agreements --source winget --silent $args | complete
}
}
"brew" => { ^brew install $id | complete }
"apt" => { ^sudo apt install -y $id | complete }
}
}
# =============================================================================
# Section filtering
# =============================================================================
def sections-for-platform [platform: string, personal: bool] {
mut sections = ["common"]
if $platform == "windows" {
$sections = ($sections | append ["gui_only" "windows_only"])
} else {
$sections = ($sections | append "unix_only")
if $platform == "macos" { $sections = ($sections | append ["gui_only" "macos_only"]) }
if $platform == "wsl" { $sections = ($sections | append "wsl_only") }
}
if $personal { $sections = ($sections | append "personal") }
$sections
}
def pkg-id-for [pkg: record, platform: string, manager: string] {
# Try manager key first (brew/winget), then platform key
if ($manager in ($pkg | columns)) {
$pkg | get $manager
} else if ($platform in ($pkg | columns)) {
$pkg | get $platform
} else {
null
}
}
# Collect all packages relevant to this platform as a flat list of {name, pkg, id}
def pkgs-for-platform [all_pkgs: record, sections: list<string>, platform: string, manager: string] {
mut out = []
for section in $sections {
if not ($section in ($all_pkgs | columns)) { continue }
let pkgs = $all_pkgs | get $section
for col in ($pkgs | columns) {
let pkg = $pkgs | get $col
let id = pkg-id-for $pkg $platform $manager
if ($id != null) {
$out = ($out | append { name: $col, pkg: $pkg, id: $id })
}
}
}
$out
}
# =============================================================================
# Commands
# =============================================================================
# Install packages from config
def "main packages" [
--dry-run (-n) # Preview without installing
--personal (-p)
--config (-c): string = "config.toml" # Config file path
] {
let platform = get-platform
let manager = get-manager $platform
if ($manager == null) {
print $"(ansi red)Error:(ansi reset) No package manager found"
return
}
let cfg = open $config
let sections = sections-for-platform $platform $personal
let all_pkgs = $cfg.packages
let pad = 22
let entries = pkgs-for-platform $all_pkgs $sections $platform $manager
print $"Packages \((ansi cyan)($platform)(ansi reset) / (ansi cyan)($manager)(ansi reset)\)\n"
# Update package cache once before installing
if (not $dry_run and $manager == "brew") {
print $" (ansi dark_gray)updating brew cache...(ansi reset)" --no-newline
^brew update out+err> /dev/null
print $"(char cr) (ansi dark_gray)brew cache updated (ansi reset)"
}
mut installed_count = 0
mut skipped_count = 0
mut failed_count = 0
for entry in $entries {
let name_padded = ($entry.name | fill -w $pad -c ' ' -a l)
if (pkg-is-installed $manager $entry.id) {
print $" (ansi dark_gray)✓ ($name_padded) skip(ansi reset)"
$skipped_count = $skipped_count + 1
continue
}
if $dry_run {
print $" (ansi cyan)○ ($name_padded) install(ansi reset)"
$installed_count = $installed_count + 1
continue
}
# Show installing indicator, then overwrite with result
print $" (ansi dark_gray)◌ ($name_padded) installing...(ansi reset)" --no-newline
let args = if ("windows_args" in ($entry.pkg | columns)) { $entry.pkg.windows_args } else { null }
let result = (pkg-install $manager $entry.id $args)
let ok = ($result.exit_code == 0)
if $ok {
print $"(char cr) (ansi green)✓ ($name_padded) installed(ansi reset) "
$installed_count = $installed_count + 1
} else {
print $"(char cr) (ansi red)✗ ($name_padded) failed(ansi reset) "
$failed_count = $failed_count + 1
let log = ([($result.stdout | default "") ($result.stderr | default "")] | str join "\n" | str trim)
if (not ($log | is-empty)) {
print $" (ansi dark_gray)($log)(ansi reset)"
}
}
}
print $"\n (ansi green)($installed_count) installed(ansi reset) · ($skipped_count) skip · (ansi red)($failed_count) failed(ansi reset)"
}
# Copy configuration files
def "main files" [
--dry-run (-n)
--config (-c): string = "config.toml"
] {
let platform = get-platform
let cfg = open $config
let files = $cfg.files
print $"Platform: (ansi cyan)($platform)(ansi reset)\n"
mut results = []
for entry in ($files | transpose source dest_data) {
let source = $entry.source
let dests = (resolve-dests $entry.dest_data $platform)
if ($dests | is-empty) {
$results = ($results | append { source: $source, dest: "-", action: "SKIP (no dest)" })
continue
}
for dest in $dests {
if not ($source | path exists) {
$results = ($results | append { source: $source, dest: $dest, action: "FAIL (not found)" })
continue
}
if $dry_run {
$results = ($results | append { source: $source, dest: $dest, action: "COPY" })
} else {
let result = try {
let dest_expanded = ($dest | path expand)
let dest_dir = if (($dest_expanded | str ends-with "/") or ($dest_expanded | str ends-with "\\")) {
$dest_expanded
} else {
$dest_expanded | path dirname
}
mkdir $dest_dir
cp $source $dest_expanded
"OK"
} catch {|e| $"FAIL: ($e.msg)" }
$results = ($results | append { source: $source, dest: $dest, action: $result })
}
}
}
print ($results | table)
}
# Reverse of `files`: pull the live configs back into files/ so edits made on the machine get committed
def "main collect" [
--dry-run (-n)
--config (-c): string = "config.toml"
] {
let platform = get-platform
let cfg = open $config
print $"Platform: (ansi cyan)($platform)(ansi reset)\n"
mut results = []
for entry in ($cfg.files | transpose source dest_data) {
# With several destinations, the first one is the copy that gets edited.
let dest = (resolve-dests $entry.dest_data $platform | get -o 0)
if ($dest == null) {
$results = ($results | append { dest: "-", source: $entry.source, action: "SKIP (no dest)" })
continue
}
# a trailing slash means the file kept its own name inside that dir
let live = if (($dest | str ends-with "/") or ($dest | str ends-with "\\")) {
$dest | path expand | path join ($entry.source | path basename)
} else {
$dest | path expand
}
if not ($live | path exists) {
$results = ($results | append { dest: $dest, source: $entry.source, action: "SKIP (not on machine)" })
continue
}
if $dry_run {
$results = ($results | append { dest: $dest, source: $entry.source, action: "COLLECT" })
} else {
let result = try {
mkdir ($entry.source | path dirname)
cp $live $entry.source
"OK"
} catch {|e| $"FAIL: ($e.msg)" }
$results = ($results | append { dest: $dest, source: $entry.source, action: $result })
}
}
print ($results | table)
print $"\n(ansi yellow)Review with `git diff files/` before committing.(ansi reset)"
}
# Generate init files for starship and zoxide
def "main init" [
--dry-run (-n)
--config (-c): string = "config.toml"
] {
let nuconfig_dir = $nu.default-config-dir
mkdir $nuconfig_dir
# Ensure source targets exist even before the tools are installed.
for f in ["starship.nu" "zoxide.nu"] {
let path = $"($nuconfig_dir)/($f)"
if not ($path | path exists) {
"" | save --force $path
}
}
if not $dry_run {
try {
print " Generating starship init..."
^starship init nu | save --force $"($nuconfig_dir)/starship.nu"
} catch { print " (starship not found yet)" }
try {
print " Generating zoxide init..."
^zoxide init nushell | save --force $"($nuconfig_dir)/zoxide.nu"
} catch { print " (zoxide not found yet)" }
}
}
# Run post-install commands
def "main post-install" [
--dry-run (-n)
--config (-c): string = "config.toml"
] {
let platform = get-platform
let cfg = open $config
let post = if ("post_install" in ($cfg | columns)) { $cfg.post_install } else { null }
if ($post == null) {
print "No post-install commands defined."
return
}
mut commands = []
# Collect "all" platform commands
if ("all" in ($post | columns)) {
let all_cmds = $post.all
for col in ($all_cmds | columns) {
$commands = ($commands | append { name: $col, cmd: ($all_cmds | get $col) })
}
}
# Collect platform-specific commands
let platform_keys = match $platform {
"macos" => ["unix" "macos"]
"wsl" => ["unix" "wsl"]
"linux" => ["unix" "linux"]
_ => [$platform]
}
for platform_key in $platform_keys {
if ($platform_key in ($post | columns)) {
let plat_cmds = $post | get $platform_key
for col in ($plat_cmds | columns) {
$commands = ($commands | append { name: $col, cmd: ($plat_cmds | get $col) })
}
}
}
if ($commands | is-empty) {
print "No post-install commands for this platform."
return
}
for entry in $commands {
if $dry_run {
print $" [DRY RUN] ($entry.name): ($entry.cmd)"
} else {
print $" ($entry.name)... " --no-newline
try {
nu -c $entry.cmd
print $"(ansi green)OK(ansi reset)"
} catch {
print $"(ansi yellow)SKIP(ansi reset)"
}
}
}
}
# Show installation status
def "main status" [
--personal (-p)
--config (-c): string = "config.toml"
] {
let platform = get-platform
let manager = get-manager $platform
if ($manager == null) {
print $"(ansi red)Error:(ansi reset) No package manager found"
return
}
let cfg = open $config
let sections = sections-for-platform $platform $personal
let all_pkgs = $cfg.packages
let pad = 22
let entries = pkgs-for-platform $all_pkgs $sections $platform $manager
print $"Status \((ansi cyan)($platform)(ansi reset) / (ansi cyan)($manager)(ansi reset)\)\n"
mut installed_count = 0
mut total = 0
for entry in $entries {
let name_padded = ($entry.name | fill -w $pad -c ' ' -a l)
$total = $total + 1
if (pkg-is-installed $manager $entry.id) {
print $" (ansi green)✓(ansi reset) ($name_padded) (ansi dark_gray)installed(ansi reset)"
$installed_count = $installed_count + 1
} else {
print $" (ansi red)✗(ansi reset) ($name_padded) (ansi dark_gray)missing(ansi reset)"
}
}
print $"\n ($installed_count)/($total) installed"
}
# Run full setup (packages + files + init + post-install)
def main [
--dry-run (-n) # Preview without making changes
--personal (-p) # Include personal
--config (-c): string = "config.toml" # Config file path
] {
print $"(ansi blue_bold)Dev Environment Setup(ansi reset)\n"
if $dry_run { print $"(ansi yellow)DRY RUN MODE(ansi reset)\n" }
print "── Packages ──"
main packages --dry-run=$dry_run --personal=$personal --config $config
print "\n── Files ──"
main files --dry-run=$dry_run --config $config
print "\n── Init ──"
main init --dry-run=$dry_run --config $config
print "\n── Post-install ──"
main post-install --dry-run=$dry_run --config $config
print $"\n(ansi green_bold)Done!(ansi reset)"
}