-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
168 lines (143 loc) · 6.07 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
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
.PHONY: requires_nix_shell format-staged \
unreachable-commit-staged format-whitespace-staged format-nix-staged \
format-hs-staged format-cabal-staged format-purs-staged format-js-staged \
format-dhall-staged check-format-hs-staged check-format-cabal-staged \
check-format-purs-staged check-format-js-staged check-format-dhall-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 `*.purs`...'
@$(MAKE) --no-print-directory format-purs-staged
@echo 'Formatting `*.js`...'
@$(MAKE) --no-print-directory format-js-staged
@echo 'Formatting `*.dhall`...'
@$(MAKE) --no-print-directory format-dhall-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
format-purs-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.purs$$'\
| while IFS= read -r -d '' FILE; do test -f $$FILE && printf "$$FILE\0"; done\
| xargs -0 -r purs-tidy format-in-place
check-format-purs-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.purs$$'\
| xargs -0 -r purs-tidy check
format-js-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.js$$'\
| while IFS= read -r -d '' FILE; do test -f $$FILE && printf "$$FILE\0"; done\
| xargs -0 -r eslint --fix
check-format-js-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.js$$'\
| xargs -0 -r eslint
format-dhall-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.dhall$$'\
| while IFS= read -r -d '' FILE; do test -f $$FILE && printf "$$FILE\0"; done\
| xargs -0 -r dhall lint
check-format-dhall-staged: requires_nix_shell
@git diff -z --name-only --diff-filter=d --cached HEAD\
| grep -Ez '^.*\.dhall$$'\
| xargs -0 -r dhall lint --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 --