-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
229 lines (216 loc) · 9.64 KB
/
Makefile
File metadata and controls
229 lines (216 loc) · 9.64 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
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
# Scriptorium Makefile
# A collection of useful shell functions, scripts and utilities targeting Bash 4+
.PHONY: help build install clean test lint fmt new-script
# Directories
SCRIPTS_DIR := scripts
DIST_DIR := dist
TEMPLATES_DIR := templates
# Find all script directories with main.sh
SCRIPT_DIRS := $(dir $(wildcard $(SCRIPTS_DIR)/*/main.sh))
SCRIPT_NAMES := $(notdir $(patsubst %/,%,$(SCRIPT_DIRS)))
# Lint/format excludes and options
SHFMT_EXCLUDES := ! -name '*_spec.sh' ! -name '*options.sh'
SHFMT_OPTS := -i 0 -ci
SHELLCHECK_FORMAT ?=
help:
@echo "Scriptorium - Shell Script Collection"
@echo ""
@echo "Available targets:"
@echo " build [NAME=x] - Build all scripts or a specific one to dist/"
@echo " install DESTDIR=x [NAME=y] - Install scripts to DESTDIR"
@echo " clean - Remove generated files in dist/"
@echo " test [NAME=x] [KCOV=0] - Run tests with coverage (all, or specific script)"
@echo " lint [NAME=x] - Run shellcheck and shfmt check"
@echo " fmt [NAME=x] - Format shell scripts with shfmt"
@echo " new-script NAME=x - Create a new script from templates"
# Build: all scripts or single script
build:
ifdef NAME
@if [ ! -d "$(SCRIPTS_DIR)/$(NAME)" ]; then \
echo "Error: script '$(NAME)' not found in $(SCRIPTS_DIR)/"; \
exit 1; \
fi
@echo "Building $(NAME) -> $(DIST_DIR)/$(NAME)/"
@mkdir -p $(DIST_DIR)/$(NAME)/bin
@mkdir -p $(DIST_DIR)/$(NAME)/man/man1 $(DIST_DIR)/$(NAME)/man/man5
@mkdir -p $(DIST_DIR)/$(NAME)/completions/bash $(DIST_DIR)/$(NAME)/completions/zsh
@if [ -f "$(SCRIPTS_DIR)/$(NAME)/main.sh" ]; then \
echo " Bundling -> $(DIST_DIR)/$(NAME)/bin/$(NAME)"; \
bash utils/bundle.sh --strip-comments --keep-comments '@.*-kcov-' "$(SCRIPTS_DIR)/$(NAME)/main.sh" > "$(DIST_DIR)/$(NAME)/bin/$(NAME)"; \
chmod +x "$(DIST_DIR)/$(NAME)/bin/$(NAME)"; \
fi
@for adoc in $(SCRIPTS_DIR)/$(NAME)/docs/*.adoc; do \
[ -f "$$adoc" ] || continue; \
filename=$$(basename "$$adoc" .adoc); \
section=$$(grep -m1 '^= .*([0-9])$$' "$$adoc" | sed 's/.*(\([0-9]\))$$/\1/' || echo "1"); \
[ -z "$$section" ] && section=1; \
echo " $$adoc -> $(DIST_DIR)/$(NAME)/man/man$$section/$$filename.$$section"; \
asciidoctor -b manpage -D $(DIST_DIR)/$(NAME)/man/man$$section "$$adoc"; \
done
@if [ -d "$(SCRIPTS_DIR)/$(NAME)/completions" ]; then \
for f in $(SCRIPTS_DIR)/$(NAME)/completions/*.bash; do \
[ -f "$$f" ] && cp "$$f" $(DIST_DIR)/$(NAME)/completions/bash/ && echo " $$f -> $(DIST_DIR)/$(NAME)/completions/bash/$$(basename $$f)"; \
done; \
for f in $(SCRIPTS_DIR)/$(NAME)/completions/_*; do \
[ -f "$$f" ] && cp "$$f" $(DIST_DIR)/$(NAME)/completions/zsh/ && echo " $$f -> $(DIST_DIR)/$(NAME)/completions/zsh/$$(basename $$f)"; \
done; \
fi
else
@echo "Building all scripts..."
@for name in $(SCRIPT_NAMES); do \
$(MAKE) --no-print-directory build NAME=$$name; \
done
@echo "Done."
endif
# Install: all scripts or single script to DESTDIR
install:
ifndef DESTDIR
$(error DESTDIR is required. Usage: make install DESTDIR=<path> [NAME=<script>])
endif
ifdef NAME
@$(MAKE) --no-print-directory build NAME=$(NAME)
@echo "Installing $(NAME) to $(DESTDIR)..."
@mkdir -p $(DESTDIR)/bin
@mkdir -p $(DESTDIR)/share/man/man1 $(DESTDIR)/share/man/man5
@mkdir -p $(DESTDIR)/share/bash-completion/completions
@mkdir -p $(DESTDIR)/share/zsh/site-functions
@install -m755 $(DIST_DIR)/$(NAME)/bin/* $(DESTDIR)/bin/
@for section in 1 5; do \
if [ -d "$(DIST_DIR)/$(NAME)/man/man$$section" ] && [ -n "$$(ls -A $(DIST_DIR)/$(NAME)/man/man$$section 2>/dev/null)" ]; then \
install -m644 $(DIST_DIR)/$(NAME)/man/man$$section/* $(DESTDIR)/share/man/man$$section/; \
fi; \
done
@if [ -d "$(DIST_DIR)/$(NAME)/completions/bash" ] && [ -n "$$(ls -A $(DIST_DIR)/$(NAME)/completions/bash 2>/dev/null)" ]; then \
for f in $(DIST_DIR)/$(NAME)/completions/bash/*.bash; do \
[ -f "$$f" ] && install -m644 "$$f" "$(DESTDIR)/share/bash-completion/completions/$$(basename "$$f" .bash)"; \
done; \
fi
@if [ -d "$(DIST_DIR)/$(NAME)/completions/zsh" ] && [ -n "$$(ls -A $(DIST_DIR)/$(NAME)/completions/zsh 2>/dev/null)" ]; then \
install -m644 $(DIST_DIR)/$(NAME)/completions/zsh/_* $(DESTDIR)/share/zsh/site-functions/ 2>/dev/null || true; \
fi
@echo "Done."
else
@$(MAKE) --no-print-directory build
@echo "Installing to $(DESTDIR)..."
@mkdir -p $(DESTDIR)/bin
@mkdir -p $(DESTDIR)/share/man/man1 $(DESTDIR)/share/man/man5
@mkdir -p $(DESTDIR)/share/bash-completion/completions
@mkdir -p $(DESTDIR)/share/zsh/site-functions
@mkdir -p $(DESTDIR)/share/scriptorium
@for name in $(SCRIPT_NAMES); do \
if [ -d "$(DIST_DIR)/$$name/bin" ]; then \
install -m755 $(DIST_DIR)/$$name/bin/* $(DESTDIR)/bin/ 2>/dev/null || true; \
fi; \
for section in 1 5; do \
if [ -d "$(DIST_DIR)/$$name/man/man$$section" ] && [ -n "$$(ls -A $(DIST_DIR)/$$name/man/man$$section 2>/dev/null)" ]; then \
install -m644 $(DIST_DIR)/$$name/man/man$$section/* $(DESTDIR)/share/man/man$$section/; \
fi; \
done; \
if [ -d "$(DIST_DIR)/$$name/completions/bash" ] && [ -n "$$(ls -A $(DIST_DIR)/$$name/completions/bash 2>/dev/null)" ]; then \
for f in $(DIST_DIR)/$$name/completions/bash/*.bash; do \
[ -f "$$f" ] && install -m644 "$$f" "$(DESTDIR)/share/bash-completion/completions/$$(basename "$$f" .bash)"; \
done; \
fi; \
if [ -d "$(DIST_DIR)/$$name/completions/zsh" ] && [ -n "$$(ls -A $(DIST_DIR)/$$name/completions/zsh 2>/dev/null)" ]; then \
install -m644 $(DIST_DIR)/$$name/completions/zsh/_* $(DESTDIR)/share/zsh/site-functions/ 2>/dev/null || true; \
fi; \
done
@install -m644 scriptorium.plugin.sh $(DESTDIR)/share/scriptorium/
@install -m644 scriptorium.plugin.zsh $(DESTDIR)/share/scriptorium/
@echo "Done."
endif
clean:
@echo "Cleaning generated files..."
@rm -rf $(DIST_DIR)/*
@echo "Done."
# Test: all, specific script, or utils (with coverage by default, KCOV=0 to disable)
KCOV ?= 1
KCOV_OPTS = $(if $(filter 1,$(KCOV)),--kcov --kcov-options="--include-pattern=$(DIST_DIR)/$(1) --exclude-region=@start-kcov-exclude:@end-kcov-exclude",)
test:
ifdef NAME
@if [ "$(NAME)" = "utils" ]; then \
echo "Running tests for utils..."; \
shellspec utils; \
else \
if [ ! -d "$(SCRIPTS_DIR)/$(NAME)" ]; then \
echo "Error: script '$(NAME)' not found in $(SCRIPTS_DIR)/"; \
exit 1; \
fi; \
$(MAKE) --no-print-directory build NAME=$(NAME); \
echo "Running tests for $(NAME)..."; \
shellspec $(call KCOV_OPTS,$(NAME)/bin) \
--helperdir scripts --require spec_helper $(SCRIPTS_DIR)/$(NAME); \
fi
else
@echo "Running all tests..."
@$(MAKE) --no-print-directory build
@shellspec $(call KCOV_OPTS,) \
--helperdir scripts --require spec_helper scripts
@shellspec utils
endif
# Lint: all, specific script, or utils
lint:
ifdef NAME
@if [ "$(NAME)" = "utils" ]; then \
echo "Linting utils..."; \
find utils/ -name '*.sh' -type f | xargs shellcheck -x -P SCRIPTDIR -S info $(SHELLCHECK_FORMAT); \
find utils/ -name '*.sh' -type f $(SHFMT_EXCLUDES) | xargs shfmt -d $(SHFMT_OPTS); \
else \
if [ ! -d "$(SCRIPTS_DIR)/$(NAME)" ]; then \
echo "Error: script '$(NAME)' not found in $(SCRIPTS_DIR)/"; \
exit 1; \
fi; \
echo "Linting $(NAME)..."; \
find $(SCRIPTS_DIR)/$(NAME)/ -name '*.sh' -type f | xargs shellcheck -x -P SCRIPTDIR -S info $(SHELLCHECK_FORMAT); \
find $(SCRIPTS_DIR)/$(NAME)/ -name '*.sh' -type f $(SHFMT_EXCLUDES) | xargs shfmt -d $(SHFMT_OPTS); \
fi
@echo "All checks passed."
else
@echo "Running shellcheck..."
@find $(SCRIPTS_DIR)/ utils/ -name '*.sh' -type f | xargs shellcheck -x -P SCRIPTDIR -S info $(SHELLCHECK_FORMAT)
@echo "Checking formatting with shfmt..."
@find $(SCRIPTS_DIR)/ utils/ -name '*.sh' -type f $(SHFMT_EXCLUDES) | xargs shfmt -d $(SHFMT_OPTS)
@echo "All checks passed."
endif
# Format: all, specific script, or utils
fmt:
ifdef NAME
@if [ "$(NAME)" = "utils" ]; then \
echo "Formatting utils..."; \
find utils/ -name '*.sh' -type f $(SHFMT_EXCLUDES) | xargs shfmt -w $(SHFMT_OPTS); \
else \
if [ ! -d "$(SCRIPTS_DIR)/$(NAME)" ]; then \
echo "Error: script '$(NAME)' not found in $(SCRIPTS_DIR)/"; \
exit 1; \
fi; \
echo "Formatting $(NAME)..."; \
find $(SCRIPTS_DIR)/$(NAME)/ -name '*.sh' -type f $(SHFMT_EXCLUDES) | xargs shfmt -w $(SHFMT_OPTS); \
fi
@echo "Done."
else
@echo "Formatting shell scripts..."
@find $(SCRIPTS_DIR)/ utils/ -name '*.sh' -type f $(SHFMT_EXCLUDES) | xargs shfmt -w $(SHFMT_OPTS)
@echo "Done."
endif
# Create a new script from templates
new-script:
ifndef NAME
$(error NAME is required. Usage: make new-script NAME=<script-name>)
endif
@if [ -d "$(SCRIPTS_DIR)/$(NAME)" ]; then \
echo "Error: script '$(NAME)' already exists in $(SCRIPTS_DIR)/"; \
exit 1; \
fi
@echo "Creating new script: $(NAME)"
@mkdir -p $(SCRIPTS_DIR)/$(NAME)/docs $(SCRIPTS_DIR)/$(NAME)/completions
@sed 's|<name>|$(NAME)|g' $(TEMPLATES_DIR)/main.sh > $(SCRIPTS_DIR)/$(NAME)/main.sh
@sed 's|<name>|$(NAME)|g' $(TEMPLATES_DIR)/options.sh > $(SCRIPTS_DIR)/$(NAME)/options.sh
@sed 's|<name>|$(NAME)|g; s|<version>|0.1.0|g; s|<description>|TODO: Add description|g' $(TEMPLATES_DIR)/default.nix > $(SCRIPTS_DIR)/$(NAME)/default.nix
@NAME_UPPER=$$(echo $(NAME) | tr '[:lower:]' '[:upper:]'); \
sed "s|<name>|$(NAME)|g; s|<NAME>|$$NAME_UPPER|g; s|<description>|TODO: Add description|g" $(TEMPLATES_DIR)/docs/command.adoc > $(SCRIPTS_DIR)/$(NAME)/docs/$(NAME).adoc
@sed 's|<name>|$(NAME)|g' $(TEMPLATES_DIR)/completions/command.bash > $(SCRIPTS_DIR)/$(NAME)/completions/$(NAME).bash
@sed 's|<name>|$(NAME)|g' $(TEMPLATES_DIR)/completions/_command > $(SCRIPTS_DIR)/$(NAME)/completions/_$(NAME)
@sed 's|<name>|$(NAME)|g' $(TEMPLATES_DIR)/main_spec.sh > $(SCRIPTS_DIR)/$(NAME)/main_spec.sh
@echo "Created $(SCRIPTS_DIR)/$(NAME)/"
@echo ""
@echo "See CONTRIBUTING.md for next steps."