photomk/Makefile
2025-02-19 18:40:40 +01:00

70 lines
2.1 KiB
Makefile

.PHONY: all, clean-dist, clean
ifeq ($(origin IMAGE_DIR),undefined)
$(error $$IMAGE_DIR is not set)
endif
# Files for distribution
DIST_DIR ?= dist
# Temporary build artifacts
BUILD_DIR ?= build
THUMB_DIR := $(DIST_DIR)/thumbs
images := $(shell find $(IMAGE_DIR) -iname '*.jpg')
image_names_cs := $(notdir $(images))
image_names := $(shell echo $(image_names_cs) | tr A-Z a-z)
dist_images := $(addprefix $(DIST_DIR)/,$(image_names))
dist_thumbs := $(patsubst $(DIST_DIR)/%.jpg,$(THUMB_DIR)/%.webp,$(dist_images))
extra_files := $(wildcard extra/*)
dist_extra_files := $(extra_files:%=$(DIST_DIR)/%)
define copy_image_rule =
$(DIST_DIR)/$(1): $(shell find $(IMAGE_DIR) -iname $(1))
@mkdir -p $(DIST_DIR)
cp --preserve $$< $$@
endef
build_metas := $(dist_images:$(DIST_DIR)/%.jpg=$(BUILD_DIR)/%.meta.json)
dist_html := $(dist_images:$(DIST_DIR)/%.jpg=$(DIST_DIR)/%.html)
### RULES ###
all: $(dist_images) $(dist_thumbs) $(dist_extra_files) $(dist_html) $(DIST_DIR)/all.zip $(DIST_DIR)/index.html
clean-dist:
rm -rf $(DIST_DIR)
clean: clean-dist
rm -rf $(BUILD_DIR)
$(foreach f,$(image_names),$(eval $(call copy_image_rule,$(f))))
$(THUMB_DIR)/%.webp: $(DIST_DIR)/%.jpg
@mkdir -p $(THUMB_DIR)
magick $< -strip -thumbnail 300x300 $@
$(BUILD_DIR)/index.json: $(build_metas) build-index.py
./build-index.py $(filter-out build-index.py,$^) > $@
$(BUILD_DIR)/%.meta.json: $(DIST_DIR)/%.jpg $(THUMB_DIR)/%.webp
@mkdir -p $(BUILD_DIR)
squeezer --root $(DIST_DIR) --dist-path $(patsubst $(BUILD_DIR)/%.meta.json,$(DIST_DIR)/%.html,$@) --key $(patsubst $(BUILD_DIR)/%.meta.json,%,$@) --thumbnail $(patsubst $(BUILD_DIR)/%.meta.json,$(THUMB_DIR)/%.webp,$@) $< > $@
$(DIST_DIR)/index.html: $(BUILD_DIR)/index.json index.hbs
@mkdir -p $(DIST_DIR)
handlebars-cli index.hbs @$(BUILD_DIR)/index.json > $@
$(DIST_DIR)/%.html: $(BUILD_DIR)/%.meta.json $(BUILD_DIR)/index.json photo.hbs
@mkdir -p $(DIST_DIR)
cat $(BUILD_DIR)/index.json | jq .index.$(patsubst $(BUILD_DIR)/%.meta.json,%,$<) | handlebars-cli photo.hbs @/dev/stdin > $@
$(DIST_DIR)/extra/%: extra/%
@mkdir -p $(DIST_DIR)/extra
cp -r $< $@
$(DIST_DIR)/all.zip: $(dist_images)
@mkdir -p $(DIST_DIR)
zip --junk-paths $@ $^