-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfmt.nu
More file actions
executable file
·23 lines (20 loc) · 892 Bytes
/
Copy pathfmt.nu
File metadata and controls
executable file
·23 lines (20 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env nu
# stage 1: format workspace crates
cargo fmt
# stage 2: strip trailing whitespace and ensure every file ends with a newline,
# skipping anything matched by fmt-exclude
let exclude = (try { open --raw fmt-exclude } catch { "" } | lines | each {|l| $l | str trim } | where {|l| $l != "" and not ($l | str starts-with "#")})
let excluded = ($exclude | each {|p| if ($p | str ends-with "/") { glob $"($p)**" -D } else { glob $p -D } } | flatten | path expand | uniq)
idx init . --wait
for f in (idx files .) {
let f = $f.full_path
if ($excluded | any {|e| $e == ($f | path expand) }) {
continue
}
let content = (open --raw $f)
let clean = ($content | str replace --all --regex "(?m)[ \t]+$" "")
let clean = if ($clean | str ends-with "\n") { $clean } else { $clean + "\n" }
if $clean != $content {
$clean | save --force $f
}
}