add linter and precommit hook installer to Makefile

This commit is contained in:
2026-02-18 17:41:22 +01:00
parent 660b87e709
commit d78c32ed16

View File

@@ -16,6 +16,9 @@ ASSETS_LUA = inc/meta/meta.assets.lua
ASSETS_DIR = assets ASSETS_DIR = assets
ASSET_TYPES = tiles sprites sfx music ASSET_TYPES = tiles sprites sfx music
LINT_TMP_LUA := /tmp/_lint_combined.lua
LINT_TMP_MAP := /tmp/_lint_map.txt
# CI/CD variables # CI/CD variables
VERSION_FILE = .version VERSION_FILE = .version
GAME_LANG ?= lua GAME_LANG ?= lua
@@ -31,10 +34,10 @@ build: $(OUTPUT)
$(OUTPUT): $(SRC) $(ORDER) $(OUTPUT): $(SRC) $(ORDER)
@rm -f $(OUTPUT) @rm -f $(OUTPUT)
@while read f; do \ @sed 's/\r$$//' $(ORDER) | while read f; do \
cat "$(SRC_DIR)/$$f" >> $(OUTPUT); \ cat "$(SRC_DIR)/$$f" >> $(OUTPUT); \
echo "" >> $(OUTPUT); \ echo "" >> $(OUTPUT); \
done < $(ORDER) done
export: build export: build
@if [ -z "$(VERSION)" ]; then \ @if [ -z "$(VERSION)" ]; then \
@@ -58,25 +61,98 @@ watch:
make build make build
fswatch -o $(SRC_DIR) $(ORDER) assets | while read; do make build; done fswatch -o $(SRC_DIR) $(ORDER) assets | while read; do make build; done
import_assets: import_assets: $(OUTPUT)
@for t in $(ASSET_TYPES); do \ @TIC_CMD="load $(OUTPUT) &"; \
for t in $(ASSET_TYPES); do \
for f in $(ASSETS_DIR)/$$t/*.png; do \ for f in $(ASSETS_DIR)/$$t/*.png; do \
[ -e "$$f" ] || continue; \ [ -e "$$f" ] || continue; \
echo "==> Importing $$f as $$t..."; \ echo "==> Importing $$f as $$t..."; \
tic80 --cli --skip --fs=. --cmd="import $$t $$f & exit"; \ TIC_CMD="$${TIC_CMD} & import $$t $$f"; \
done; \ done; \
done done; \
TIC_CMD="$$TIC_CMD save & exit"; \
echo $$TIC_CMD; \
tic80 --cli --skip --fs=. --cmd="$$TIC_CMD"
export_assets: build # export helper function
define f_export_asset_awk
cat $(2) | awk '/-- <$(1)>/,/<\/$(1)>/' >> $(3)
endef
lint:
@echo "==> Merging..."
@rm -f $(LINT_TMP_LUA) $(LINT_TMP_MAP)
@touch $(LINT_TMP_LUA)
@line=1; \
while IFS= read -r f || [ -n "$$f" ]; do \
[ -z "$$f" ] && continue; \
before=$$(wc -l < $(LINT_TMP_LUA)); \
cat "$(SRC_DIR)/$$f" >> $(LINT_TMP_LUA); \
printf '\n' >> $(LINT_TMP_LUA); \
after=$$(wc -l < $(LINT_TMP_LUA)); \
linecount=$$((after - before)); \
echo "$$line $$linecount $(SRC_DIR)/$$f" >> $(LINT_TMP_MAP); \
line=$$((line + linecount)); \
done < $(ORDER)
@echo "==> luacheck..."
@LINT_OUTPUT=$$(luacheck --no-max-line-length $(LINT_TMP_LUA) 2>&1 | awk -v map=$(LINT_TMP_MAP) ' \
BEGIN { \
NR_map = 0; \
while ((getline line < map) > 0) { \
n = split(line, a, " "); \
start[NR_map] = a[1]+0; \
count[NR_map] = a[2]+0; \
fname[NR_map] = a[3]; \
NR_map++; \
} \
} \
/^[^:]+:[0-9]+:[0-9]+:/ { \
colon1 = index($$0, ":"); \
rest1 = substr($$0, colon1+1); \
colon2 = index(rest1, ":"); \
absline = substr(rest1, 1, colon2-1) + 0; \
rest2 = substr(rest1, colon2+1); \
colon3 = index(rest2, ":"); \
col = substr(rest2, 1, colon3-1); \
rest = substr(rest2, colon3); \
found = 0; \
for (i = 0; i < NR_map; i++) { \
end_line = start[i] + count[i] -1; \
if (absline >= start[i] && absline <= end_line) { \
relline = absline - start[i] + 1; \
print fname[i] ":" relline ":" col ":" rest; \
found = 1; \
break; \
} \
} \
if (!found) print $$0; \
next; \
} \
{ print } \
'); \
echo "$$LINT_OUTPUT"; \
NUM_ISSUES=$$(echo "$$LINT_OUTPUT" | grep -cE "^[^:]+:[0-9]+:[0-9]+:"); \
if [ "$$NUM_ISSUES" -gt 0 ]; then \
echo "Total: $$NUM_ISSUES issue(s) found, commit aborted."; \
exit 1; \
else \
echo "Checking /tmp/_lint_combined.lua OK"; \
echo "Total: 0 warnings / 0 errors in 1 file"; \
fi
@rm -f $(LINT_TMP_LUA) $(LINT_TMP_MAP)
export_assets:
# $(OUTPUT) would be a circular dependency
@test -e $(OUTPUT)
@echo "==> Exporting TIC-80 asset sections" @echo "==> Exporting TIC-80 asset sections"
@mkdir -p inc/meta @mkdir -p inc/meta
@sed -n '/^-- <PALETTE>/,/^-- <\/PALETTE>/p;\ @echo -n '' > $(ASSETS_LUA)
/^-- <TILES>/,/^-- <\/TILES>/p;\ @$(call f_export_asset_awk,PALETTE,$(OUTPUT),$(ASSETS_LUA))
/^-- <SPRITES>/,/^-- <\/SPRITES>/p;\ @$(call f_export_asset_awk,TILES,$(OUTPUT),$(ASSETS_LUA))
/^-- <MAP>/,/^-- <\/MAP>/p;\ @$(call f_export_asset_awk,SPRITES,$(OUTPUT),$(ASSETS_LUA))
/^-- <SFX>/,/^-- <\/SFX>/p;\ @$(call f_export_asset_awk,MAP,$(OUTPUT),$(ASSETS_LUA))
/^-- <MUSIC>/,/^-- <\/MUSIC>/p' \ @$(call f_export_asset_awk,SFX,$(OUTPUT),$(ASSETS_LUA))
$(OUTPUT) > $(ASSETS_LUA) @$(call f_export_asset_awk,WAVES,$(OUTPUT),$(ASSETS_LUA))
clean: clean:
@rm -f $(PROJECT)-*.tic $(PROJECT)-*.html.zip $(OUTPUT) @rm -f $(PROJECT)-*.tic $(PROJECT)-*.html.zip $(OUTPUT)
@@ -114,5 +190,25 @@ ci-update:
echo "==> Triggering update for version $$VERSION"; \ echo "==> Triggering update for version $$VERSION"; \
curl "$(UPDATE_SERVER)/update?secret=$(UPDATE_SECRET)&name=$(PROJECT)&platform=tic80&version=$$VERSION" curl "$(UPDATE_SERVER)/update?secret=$(UPDATE_SECRET)&name=$(PROJECT)&platform=tic80&version=$$VERSION"
.PHONY: all build export watch import_assets export_assets clean ci-version ci-export ci-upload ci-update install_precommit_hook:
@echo "Installing Git pre-commit hook (lint check)..."
@mkdir -p .git/hooks
@printf '#!/bin/bash\n' > .git/hooks/pre-commit
@printf 'echo "Running lint before commit..."\n' >> .git/hooks/pre-commit
@printf 'make lint\n' >> .git/hooks/pre-commit
@printf 'if [ $$? -ne 0 ]; then\n' >> .git/hooks/pre-commit
@printf ' echo "Lint failed! Commit aborted."\n' >> .git/hooks/pre-commit
@printf ' exit 1\n' >> .git/hooks/pre-commit
@printf 'fi\n' >> .git/hooks/pre-commit
@chmod +x .git/hooks/pre-commit
@echo "Pre-commit hook installed successfully."
.PHONY: all build export watch import_assets export_assets clean lint ci-version ci-export ci-upload ci-update install_precommit_hook
#-- <WAVES>
#-- 000:224578acdeeeeddcba95434567653100
#-- </WAVES>
#
#-- <SFX>
#-- 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000
#-- </SFX>