Makefile 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #------------------------------------------------------------------------------
  2. # Makefile to generate and run my personal blog
  3. #
  4. # It takes care of properly generating all files whitin the './posts' and the
  5. # './pages' directory, with the './parts' directory contaning the dependencies
  6. # (files which are part of other files)
  7. #
  8. # TODO:
  9. # - Implement the run recipe
  10. # - Add a rule for the stylesheet
  11. # So that it only copies to the build if it changes and dosen't trigger a
  12. # whole rebuild.
  13. # - Implement dependencies on the './pages' directory
  14. #------------------------------------------------------------------------------
  15. # Default domain and port to be used to generate the site and to run the
  16. # server. To use others, specify them via the command line
  17. DOMAIN := https://localhost
  18. PORT := 3000
  19. .PHONY: all run clean
  20. # Tooling definition
  21. mkpage := resources/mkpage.pl
  22. macros := resources/macros.m4
  23. stylesheet := resources/site/stylesheet.css
  24. server := resources/server/server.js
  25. cert := resources/site/certificate.pem
  26. key := resources/site/private-key.pem
  27. tmp := $(shell mktemp)
  28. # Templates for automatic recipe generation
  29. define template_no_deps =
  30. $(1): $(2) $(mkpage) $(macros)
  31. M4PATH=$(m4path) m4 $(macros) $(2) >$(tmp)
  32. $(if $(findstring index.html,$(1)),\
  33. $(mkpage) -d $(DOMAIN):$(PORT) $(tmp) >$(1),\
  34. $(mkpage) $(tmp) >$(1)\
  35. )
  36. endef
  37. define template_post_with_deps =
  38. $(1): $(2) $(3) $(mkpage) $(macros)
  39. M4PATH=$(m4path) m4 $(macros) $(2) >$(tmp)
  40. $(mkpage) $(tmp) >$(1)
  41. endef
  42. # Get the basename of a file without the intermediary suffix
  43. # $1: File
  44. # $2: Sufix to be removed
  45. prepare_file = $(notdir $(subst $(2),,$(1)))
  46. # Function for recipe generation for files without dependencies
  47. # $1: file list of targets
  48. # $2: Suffix to be removed of targets
  49. define gen_recipes_no_deps
  50. $(foreach f,$(1),\
  51. $(eval $(call template_no_deps,\
  52. $(call prepare_file,$(f),$(2)),$(f))))
  53. endef
  54. # Get all pages and posts
  55. pages != find ./pages -name '*.page.html'
  56. posts != find . -wholename './parts' -prune -o -name '*.post.html' -print
  57. # Remove the intermediary suffixes from the files, leaving only the '.html'
  58. posts_target := $(foreach post,$(posts),$(call prepare_file,$(post),.post))
  59. pages_target := $(foreach page,$(pages),$(call prepare_file,$(page),.page))
  60. # Posts and their dependencies for recipe generation
  61. posts_with_deps := $(shell resources/post_deps.bash -d ./posts )
  62. posts_without_deps := $(shell resources/post_deps.bash ./posts)
  63. deps := $(foreach post,$(posts_with_deps),\
  64. $(wordlist 2,$(words $(subst :, ,$(post))),$(subst :, ,$(post))))
  65. # Fetch each subdirectory from './posts' to populate M4PATH
  66. # This allows the 'include' macro to work properly
  67. m4path := $(shell find ./parts -type d -exec printf {}: \;)
  68. # Look into the build and ./parts directories for files
  69. vpath %.html ./build
  70. vpath %.html $(shell find ./parts -type d)
  71. build: $(pages_target) $(posts_target)
  72. [[ -d build ]] || mkdir build
  73. mv *.html build/
  74. cp $(stylesheet) build/
  75. cp $(cert) build/
  76. cp $(key) build/
  77. rm $(tmp)
  78. $(call gen_recipes_no_deps,$(pages),.page)
  79. $(call gen_recipes_no_deps,$(posts_without_deps),.post)
  80. # Generates recipes for posts with dependencies
  81. $(foreach post,$(posts_with_deps),\
  82. $(eval $(call template_post_with_deps,\
  83. $(notdir $(subst .post,,$(firstword $(subst :, ,$(post))))),\
  84. $(firstword $(subst :, ,$(post))),\
  85. $(wordlist 2,$(words $(subst :, ,$(post))),$(subst :, ,$(post)))\
  86. ))\
  87. )
  88. # Start server
  89. run:
  90. @if [[ -d build ]] ; then \
  91. $(server) -c ./build/certificate.pem -k ./build/private-key.pem -p $(PORT) ./build;\
  92. else \
  93. echo 'No build directory found. Please run: make or make build' ;\
  94. fi
  95. clean:
  96. [[ -d build ]] && rm -r build