-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.nix
More file actions
71 lines (60 loc) · 1.88 KB
/
formatter.nix
File metadata and controls
71 lines (60 loc) · 1.88 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
{
coreutils,
diffutils,
fd,
nixfmt,
writeShellApplication,
}:
writeShellApplication {
name = "yakumo-formatter";
runtimeInputs = [
coreutils
diffutils
nixfmt
fd
];
# Use double quotes as in ''${var} to escape bash variables in Nix multi-line strings.
text = ''
# Capture args passed by `nix fmt` as it passes the target directory or files
# as args ($@).
targets=("$@")
# If no args are passed, default to the current directory.
if [ ''${#targets[@]} -eq 0 ]; then
targets=(".")
fi
files=()
for target in "''${targets[@]}"; do
if [ -f "$target" ]; then
# Add a Nix file directly to the target list.
if [[ "$target" == *.nix ]]; then
files+=("$target")
fi
elif [ -d "$target" ]; then
# Use `fd` to find all Nix files in a directory.
while IFS= read -r file; do
files+=("$file")
done < <(fd --type file --extension nix --hidden --exclude .git . "$target")
fi
done
total="''${#files[@]}"
if [ "$total" -eq 0 ]; then
echo "🙅 No Nix files found to format."
exit 0
fi
echo "🔍️ Scanned $total Nix file(s)."
echo "✨️ Formatting..."
# Take a snapshot of file hashes BEFORE formatting.
before_state=$(md5sum "''${files[@]}")
# Run the formatter in one go.
nixfmt "''${files[@]}"
# Take a snapshot of file hashes AFTER formatting.
after_state=$(md5sum "''${files[@]}")
# Compare the states to check how many files were actually modified.
modified_count=$(diff <(echo "$before_state") <(echo "$after_state") | grep -c "^>" || true)
if [ "$modified_count" -eq 0 ]; then
echo "⚖️ All $total file(s) were already formatted."
else
echo "✅️ Done! Formatted $modified_count out of $total file(s)."
fi
'';
}