123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #------------------------------------------------------------------------------
- # Makefile to generate and run my personal blog
- #
- # It takes care of properly generating all files whitin the './posts' and the
- # './pages' directory, with the './parts' directory contaning the dependencies
- # (files which are part of other files)
- #
- # TODO:
- # - Implement the run recipe
- # - Add a rule for the stylesheet
- # So that it only copies to the build if it changes and dosen't trigger a
- # whole rebuild.
- # - Implement dependencies on the './pages' directory
- #------------------------------------------------------------------------------
- # Default domain and port to be used to generate the site and to run the
- # server. To use others, specify them via the command line
- DOMAIN := https://localhost
- PORT := 3000
- .PHONY: all run clean
- # Tooling definition
- mkpage := resources/mkpage.pl
- macros := resources/macros.m4
- stylesheet := resources/site/stylesheet.css
- server := resources/server/server.js
- cert := resources/site/certificate.pem
- key := resources/site/private-key.pem
- tmp := $(shell mktemp)
- # Templates for automatic recipe generation
- define template_no_deps =
- $(1): $(2) $(mkpage) $(macros)
- M4PATH=$(m4path) m4 $(macros) $(2) >$(tmp)
- $(if $(findstring index.html,$(1)),\
- $(mkpage) -d $(DOMAIN):$(PORT) $(tmp) >$(1),\
- $(mkpage) $(tmp) >$(1)\
- )
- endef
- define template_post_with_deps =
- $(1): $(2) $(3) $(mkpage) $(macros)
- M4PATH=$(m4path) m4 $(macros) $(2) >$(tmp)
- $(mkpage) $(tmp) >$(1)
- endef
- # Get the basename of a file without the intermediary suffix
- # $1: File
- # $2: Sufix to be removed
- prepare_file = $(notdir $(subst $(2),,$(1)))
- # Function for recipe generation for files without dependencies
- # $1: file list of targets
- # $2: Suffix to be removed of targets
- define gen_recipes_no_deps
- $(foreach f,$(1),\
- $(eval $(call template_no_deps,\
- $(call prepare_file,$(f),$(2)),$(f))))
- endef
- # Get all pages and posts
- pages != find ./pages -name '*.page.html'
- posts != find . -wholename './parts' -prune -o -name '*.post.html' -print
- # Remove the intermediary suffixes from the files, leaving only the '.html'
- posts_target := $(foreach post,$(posts),$(call prepare_file,$(post),.post))
- pages_target := $(foreach page,$(pages),$(call prepare_file,$(page),.page))
- # Posts and their dependencies for recipe generation
- posts_with_deps := $(shell resources/post_deps.bash -d ./posts )
- posts_without_deps := $(shell resources/post_deps.bash ./posts)
- deps := $(foreach post,$(posts_with_deps),\
- $(wordlist 2,$(words $(subst :, ,$(post))),$(subst :, ,$(post))))
- # Fetch each subdirectory from './posts' to populate M4PATH
- # This allows the 'include' macro to work properly
- m4path := $(shell find ./parts -type d -exec printf {}: \;)
- # Look into the build and ./parts directories for files
- vpath %.html ./build
- vpath %.html $(shell find ./parts -type d)
- build: $(pages_target) $(posts_target)
- [[ -d build ]] || mkdir build
- mv *.html build/
- cp $(stylesheet) build/
- cp $(cert) build/
- cp $(key) build/
- rm $(tmp)
- $(call gen_recipes_no_deps,$(pages),.page)
- $(call gen_recipes_no_deps,$(posts_without_deps),.post)
- # Generates recipes for posts with dependencies
- $(foreach post,$(posts_with_deps),\
- $(eval $(call template_post_with_deps,\
- $(notdir $(subst .post,,$(firstword $(subst :, ,$(post))))),\
- $(firstword $(subst :, ,$(post))),\
- $(wordlist 2,$(words $(subst :, ,$(post))),$(subst :, ,$(post)))\
- ))\
- )
- # Start server
- run:
- @if [[ -d build ]] ; then \
- $(server) -c ./build/certificate.pem -k ./build/private-key.pem -p $(PORT) ./build;\
- else \
- echo 'No build directory found. Please run: make or make build' ;\
- fi
- clean:
- [[ -d build ]] && rm -r build
|