-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
279 lines (259 loc) · 10.8 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
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
MAKEFILE := $(lastword $(MAKEFILE_LIST))
MAKE_COMMAND := $(MAKE) -C $(abspath $(dir $(MAKEFILE))) -f $(abspath $(MAKEFILE))
CHANGED_FILES ?= $(shell git diff --name-only origin/HEAD)
REPO_ROOT ?= $(shell git rev-parse --show-toplevel)
.PHONY: ssh git
# from <https://stackoverflow.com/questions/2131213/can-you-make-valid-makefiles-without-tab-characters/60363121#60363121>
.RECIPEPREFIX := $(.RECIPEPREFIX) # switch to using spaces instead of tabs for recipe separators
.PHONY: help
help: ## Miscellaneous: Returns this Makefile's commands and their descriptions in a formatted table
@ci/scripts/makefile_help.sh $(MAKEFILE_LIST) 1
CONFIG_FILE := "config.new.$$$$"
ssh: ## Generators: Generates "${HOME}/.ssh/config" file using 'includes' directives
ifeq ($(notdir $(CURDIR)), .ssh)
$(info running $@)
$(eval TEMPLATE := "config.template")
$(eval DIR := "config.d")
@if [ -d $(DIR) ]; then \
cp -ai $(TEMPLATE) $(CONFIG_FILE); \
$(info going through all Include lines and replace them with the file contents) \
for line in $(shell grep -n Include $(TEMPLATE) | sed -re 's;Include ;;' | sort -r); do \
lineno=$${line%%:*}; \
file=$${line##*:}; \
#echo "lineno = '$$lineno'; file = '$$file'"; \
if [ -f "$$file" ]; then \
sed -i -re "$${lineno}r $$file" $(CONFIG_FILE); \
fi; \
sed -i -e "$${lineno}d" $(CONFIG_FILE); \
done; \
$(info ) \
$(info config file created here:) \
$(realpath $(info $(CONFIG_FILE))) \
else \
echo $(DIR) "doesn't exist!"; \
fi
else
@# Makefile path determination from
@#<https://stackoverflow.com/questions/18136918/how-to-get-current-relative-directory-of-your-makefile/18137056#18137056>
@$(MAKE) -sC .ssh -f $(abspath $(lastword $(MAKEFILE_LIST))) ssh
endif
git: ## Generators: Generates "${HOME}/.gitconfig" file using 'includes' directives
ifeq ($(notdir $(CURDIR)), .gitconfig.d)
$(info running $@)
$(eval TEMPLATE := "config.template")
$(eval DIR := ".gitconfig.d")
@if [ -d $(DIR) ]; then \
cp -ai $(TEMPLATE) $(CONFIG_FILE); \
$(info going through all 'include' lines and replace them with the file contents) \
for line in $(shell grep -n ^include $(TEMPLATE) | sed -re 's;^include ;;' | sort -r); do \
lineno=$${line%%:*}; \
file=$${line##*:}; \
#echo "lineno = '$$lineno'; file = '$$file'"; \
if [ -f "$$file" ]; then \
sed -i -re "$${lineno}r $$file" $(CONFIG_FILE); \
fi; \
sed -i -e "$${lineno}d" $(CONFIG_FILE); \
done; \
$(info ) \
$(info config file created here:) \
realpath $(CONFIG_FILE); \
else \
echo $(DIR) "doesn't exist!"; \
fi
else
@# Makefile path determination from
@#<https://stackoverflow.com/questions/18136918/how-to-get-current-relative-directory-of-your-makefile/18137056#18137056>
@$(MAKE) -sC .gitconfig.d -f $(abspath $(lastword $(MAKEFILE_LIST))) git
endif
.PHONY: dev
dev: ## Aliases: Runs 'git-hooks'
@$(MAKE_COMMAND) git-hooks
git-hooks:
@$(MAKE_COMMAND) $(subst .githooks, .git/hooks, $(wildcard .githooks/*))
.git/hooks/%: .githooks/% ## Automatic: Creates symlinks for all git hooks from '.githooks' to '.git/hooks'
@[ -h $@ ] || ln -siv ../../$< $@
.PHONY: check-defs
check-defs: $(shell find -regex '.*\.md\(\.tpl\)?') ## Miscellaneous: Checks all Markdown files for unused definitions
@/bin/bash -c '\
for file in $^; do \
for footnote in $$(\
cat $$file | \
sed -rne '\''s;^(\[[^]]+\]):.*;\1;p'\'' | \
sed -re '\''s;[][*];\\&;g'\'' | \
sed -re "s;^;[^^];" | \
sed -re "s; ;\o1;g" \
); do \
footnote=$$(echo "$$footnote" | tr "\1" " "); \
egrep -iq --color=always "$$footnote" $$file || \
footnotes="$$footnotes\n'\''$$(echo $$footnote | sed -re "s;^\[\^\^\];;" -e "s;[*^\];;g")'\'' is unused"; \
done; \
if [[ -n "$$footnotes" ]]; then \
echo -n "$$file:" && \
echo -e "$$footnotes" && \
echo && unset footnotes; \
fi; \
for def in $$(\
cat $$file | \
sed -rne '\''s;.*\[[^]]+\]\[([^]]+)\].*;\1;gp'\'' | \
sed -re '\''s;[][*];\\&;g'\'' | \
sed -re "s; ;\o1;g"; \
cat $$file | \
sed -rne '\''s;.*\[([^]]+)\]\[\].*;\1;gp'\'' | \
sed -re '\''s;[][*];\\&;g'\'' | \
sed -re "s; ;\o1;g"; \
); do \
def=$$(echo "$$def" | tr "\1" " "); \
# echo "def = '\''$$def'\''"; \
egrep -iq --color=always "^\[$$def\]:" $$file || \
defs="$$defs\n'\''$$def'\'' is undefined"; \
done; \
if [[ -n "$$defs" ]]; then \
echo -n "$$file:" && \
echo -e "$$defs" && \
echo && unset defs; \
fi; \
done; \
'
.PHONY: check-md-links
check-md-links: $(shell find -regex '.*\.md\(\.tpl\)?') ## Miscellaneous: Checks all Markdown files for unused definitions
-@echo
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo $(shell echo '$@' | tr '[:lower:]' '[:upper:]')
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo
@for file in $^; do \
refs="$$(grep -Eo '\[[^]]+\]\[[^]]*\]' $${file} | sort -u | wc -l)"; \
defs="$$(grep -Eo '^\[[^]]+\]: ' $${file} | sort -u | wc -l)"; \
if [ "$${refs}" != "$${defs}" ]; then \
errors="$$((errors + 1))"; \
echo "$${file} has $${refs} reference(s) but $${defs} definition(s)"; \
fi; \
done; \
if [ -n "$${errors}" ]; then \
echo "$${errors} error(s) found"; \
fi
.PHONY: generate-docs
generate-docs: check-md-links $(shell find -regex '.*\.md\(\.tpl\)?') ## Generators: Regenerates Markdown files (including ToC and 'make help' output)
-@echo
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo $(shell echo '$@' | tr '[:lower:]' '[:upper:]')
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo
@echo "Generating docs..."
@for file in $^; do \
[ -s $${file} ] || continue; \
TEMP_FILE="$$(mktemp -p $(REPO_ROOT))"; \
OUTPUT_FILENAME="$${file%%.tpl}"; \
MAKEFILE_HELP="$$($(REPO_ROOT)/ci/scripts/makefile_help.sh $(MAKEFILE_LIST))" \
TOC="$$( \
sed -rne 's;^(#{2,4}) (.*);\1- [\2](\#\L\2);p' $(REPO_ROOT)/$${file} | \
sed -Ee 's;^(#+);\1\1;' | \
awk 'BEGIN{FS=OFS="-"} {gsub(/#/, " ", $$1)} $$1' | \
awk 'BEGIN{FS="[]][(]"; OFS="]("} {gsub(/[ ]/, "-", $$2)} {gsub(/[/()`.]/, "", $$2)}; $$2=$$2")"' | \
sed -e 's;^ ;;' \
)" \
envsubst \
'\
$${MAKEFILE_HELP} \
$${TOC} \
' \
< $(REPO_ROOT)/$${file} \
| grep -v '^ *<!-- # vim:' | tee $${TEMP_FILE} >/dev/null; \
if diff $${TEMP_FILE} $(REPO_ROOT)/$${OUTPUT_FILENAME} >/dev/null 2>&1; then \
echo "no changes for $${file}"; \
rm $${TEMP_FILE}; \
else \
mv -fu $${TEMP_FILE} $(REPO_ROOT)/$${OUTPUT_FILENAME}; \
fi; \
done
.PHONY: gh-act-install
gh-act-install: ## Linting: Install 'gh' and the 'nektos/act' extension
-@echo
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo $(shell echo '$@' | tr '[:lower:]' '[:upper:]')
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo
-@if command -v -- gh >/dev/null 2>&1; then \
echo "$(shell gh --version | head -n1) installed"; \
if gh act -h >/dev/null 2>&1; then \
echo "$(shell gh act --version) installed"; \
else \
echo "installing 'act'..." && \
gh extension install nektos/gh-act; \
fi; \
else \
echo "error: install 'gh', then re-run 'make $@'"; \
echo "https://github.com/cli/cli/blob/trunk/docs/install_linux.md#debian-ubuntu-linux-raspberry-pi-os-apt"; \
fi
.PHONY: lint
lint: gh-act-install ## Linting: Run MegaLinter with nektos/act
-@echo
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo $(shell echo '$@' | tr '[:lower:]' '[:upper:]')
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo
-gh act --artifact-server-path /tmp/artifacts -W .github/workflows/pre-commit.yml
.PHONY: lint-cleanup
lint-cleanup: ## Linting: Clean up any leftover docker continers from linting
-@echo
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo $(shell echo '$@' | tr '[:lower:]' '[:upper:]')
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo
-for container in $$(docker container ls -a --format='{{.Names}}' | grep -E '^act-'); do \
docker container stop "$${container}"; \
docker container rm "$${container}"; \
done && \
for volume in $$(docker volume ls --format='{{.Name}}' | grep -E '^act-'); do \
docker volume rm "$${volume}"; \
done
.PHONY: pre-commit-install
pre-commit-install: ## Linting: Install pre-commit
-@echo
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo $(shell echo '$@' | tr '[:lower:]' '[:upper:]')
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo
-@command -v -- pipenv >/dev/null 2>&1 || pip3 install pipenv
-@if command -v -- pipenv >/dev/null 2>&1; then \
echo "$(shell pipenv --version) installed"; \
if pipenv run pre-commit -V >/dev/null 2>&1; then \
echo "$(shell pipenv run pre-commit -V) installed"; \
else \
echo "installing pre-commit..." && \
pipenv install pre-commit; \
fi; \
else \
echo "error: install 'pipenv', then re-run 'make $@'"; \
fi
.PHONY: pre-commit-install-hooks
pre-commit-install-hooks: pre-commit-install ## Linting: Install pre-commit hooks
-@echo
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo $(shell echo '$@' | tr '[:lower:]' '[:upper:]')
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo
-pipenv run pre-commit install
.PHONY: pre-commit
pre-commit: pre-commit-install ## Linting: Lints all files changed between the default branch and the current branch
-@echo
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo $(shell echo '$@' | tr '[:lower:]' '[:upper:]')
-@echo '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
-@echo
-pipenv run pre-commit run -v --show-diff-on-failure --color=always --files $(CHANGED_FILES)
#test:
# @DIR="config.d"; \
# CONFIG_FILE="config.new"; \
# cd ".ssh"; \
# if [ -d "$$DIR" ]; then \
# if [ -f "$$DIR/$$CONFIG_FILE" ]; then \
# rm "$$DIR/$$CONFIG_FILE"; \
# fi; \
# for FILE in "$$DIR"/*; do \
# cat $$FILE >> "$$DIR"/$$CONFIG_FILE; \
# echo >> "$$DIR"/$$CONFIG_FILE; \
# done; \
# else \
# echo "$$DIR doesn't exist!"; \
# fi