-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
128 lines (109 loc) · 4.53 KB
/
Makefile
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
.PHONY: requires_nix_shell format-staged \
unreachable-commit-staged format-whitespace-staged format-nix-staged \
format-hs-staged format-cabal-staged \
check-format-hs-staged check-format-cabal-staged \
check-format-nix-staged check-format-whitespace
current-system := $(shell nix eval --impure --expr builtins.currentSystem)
# Target to use as dependency to fail if not inside nix-shell.
requires_nix_shell:
@ [ "$(IN_NIX_SHELL)" ] || { \
echo "The $(MAKECMDGOALS) target must be run from inside a nix shell"; \
echo " run 'nix develop' first"; \
false; \
}
NIX_SOURCES := $(shell fd -enix)
# `format-staged` is a .PHONY rule which formats only git's staged files
# relative to the current HEAD. Precisely, this uses targets of the form
# `format-*-staged` to format the various file types.
#
# Moreover, before running the formatters, this runs the target
# `unreachable-commit-staged` to save a snapshot of the staged files before
# running the formatters in case the formatters corrupt your work.
format-staged: unreachable-commit-staged requires_nix_shell
@echo 'Formatting `*.hs`...'
@$(MAKE) --no-print-directory format-hs-staged
@echo 'Formatting `*.cabal`...'
@$(MAKE) --no-print-directory format-cabal-staged
@echo 'Formatting whitespace...'
@$(MAKE) --no-print-directory format-whitespace-staged
@echo 'Formatting `*.nix` files...'
@$(MAKE) --no-print-directory format-nix-staged
# `unreachable-commit-staged` constructs an unreachable git commit object of the
# current staged files.
#
# This internal commit object can be viewed with
# ```
# git log $(git cat-file --batch-check --batch-all | awk '$2 == "commit" {print $1}')
# ```
# which reads all commit objects (including the unreachable commit we just
# made) to `git log` where (by default) the most recent commit is shown first.
#
# If we'd like to reset to the aforementioned snapshot, one could execute
# ```
# git reset --hard <snapshot hash>
# ```
# and fix the commit message accordingly.
#
# Note: these commit objects will be gc'd as the are unreachable -- see `git
# gc`
unreachable-commit-staged:
@echo 'Creating a git commit object to snapshot the current staged files...'
@git stash create --staged -m "WIP: autogenerated 'unreachable-commit-staged' commit"
format-whitespace-staged:
@git diff -z --name-only --diff-filter=d --cached HEAD ':!*.golden' \
| while IFS= read -r -d '' FILE; do test -f $$FILE && printf "$$FILE\0"; done\
| while IFS= read -r -d '' file; do\
TMP=$$(mktemp);\
git stripspace <$$file >$$TMP;\
cat $$TMP > $$file;\
rm $$TMP;\
done
format-nix-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.nix$$'\
| while IFS= read -r -d '' FILE; do test -f $$FILE && printf "$$FILE\0"; done\
| xargs -0 -r nixpkgs-fmt
check-format-nix-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.nix$$'\
| xargs -0 -r nixpkgs-fmt --check
FOURMOLU_EXTENSIONS := \
-o -XBangPatterns \
-o -XTypeApplications \
-o -XTemplateHaskell \
-o -XImportQualifiedPost \
-o -XPatternSynonyms \
-o -fplugin=RecordDotPreprocessor
format-hs-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.hs$$'\
| while IFS= read -r -d '' FILE; do test -f $$FILE && printf "$$FILE\0"; done\
| xargs -0 -r fourmolu $(FOURMOLU_EXTENSIONS) --mode inplace --check-idempotence
format-hs: requires_nix_shell
@git ls-files -z\
| grep -Ez '^.*\.hs$$'\
| while IFS= read -r -d '' FILE; do test -f $$FILE && printf "$$FILE\0"; done\
| xargs -0 -r fourmolu $(FOURMOLU_EXTENSIONS) --mode inplace --check-idempotence
check-format-hs-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.hs$$'\
| xargs -0 -r fourmolu $(FOURMOLU_EXTENSIONS) --mode check --check-idempotence
format-cabal-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.cabal$$'\
| while IFS= read -r -d '' FILE; do test -f $$FILE && printf "$$FILE\0"; done\
| xargs -0 -r cabal-fmt --inplace
check-format-cabal-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.cabal$$'\
| xargs -0 -r cabal-fmt --check
nixpkgsfmt: requires_nix_shell
nixpkgs-fmt $(NIX_SOURCES)
nixpkgsfmt_check: requires_nix_shell
nixpkgs-fmt --check $(NIX_SOURCES)
lock: requires_nix_shell
nix flake lock
lock_check: requires_nix_shell
@nix flake lock --no-update-lock-file
check-format-whitespace: requires_nix_shell
@git diff --check --cached HEAD --