makefile

# ---------------------------------------
# Author: David Marsh <rdmarsh@gmail.com>
# ---------------------------------------
#
# This Makefile is used to generate files for XXX
#   for usage: make help
#   more info: make about
#
# Copyright (C) 2022 David Marsh rdmarsh@gmail.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

# MAKE FLAGS
#MAKEFLAGS += -j4

# FILE EXTENSIONS
# ---------------------------------------
# change only if needed

JSN ?= json
J2 ?= j2

# SOURCE DIR and FILENAMES
# ---------------------------------------
# change only if needed

name := XXX
prog := $(name).py

bakdir := ../$(name)_back
cfgdir := ~/.$(name)
cmddir := _cmds
defdir := _defs
jnjdir := _jnja
jsndir := _json

# EXECUTABLE AND PROGRAM FLAGS
# ---------------------------------------
# change only if needed

CURL ?= curl
JQ ?= jq
AWK ?= awk 
JINJA ?= jinja2

# for testing non-required commands
GREP ?= grep
GREPFLAGS += -l

# for backup
TAR ?= tar 
TARFLAGS += -cvf

# pip for install
PIP ?= pip 

# BUILD SOURCE AND TARGTS
# ---------------------------------------
# do not change

XXXSOURCES := $(wildcard $(defdir)/[A-Z]*.$(JSN))
XXXTARGETS := $(patsubst $(defdir)/%.$(JSN),$(cmddir)/%.py,$(CMDSOURCES))
NONXXXTARGETS := $(filter-out $(XXXSOURCES),$(XXXTARGETS))

# COLOUR OUTPUT
# ---------------------------------------
# do not change

NO_COLOR=\x1b[0m
OK_COLOR=\x1b[32;01m
OK_STRING=$(OK_COLOR)[OK]$(NO_COLOR)

# TARGETS
# ---------------------------------------
# do not change

.PHONY: all
all: init ## Build everything
    @echo XXX
    @echo "$@ $(OK_STRING)"

.PHONY: init
init: | PYTHON-exists JINJA-exists ## Check prerequisites, initialise dirs, get swagger file, create definition files
    @echo "$@ $(OK_STRING)"

.PHONY: PYTHON-exists CURL-exists JINJA-exists JQ-exists PIP-exists
PYTHON-exists: ; which python3
CURL-exists: ; which $(CURL)
JINJA-exists: ; which $(JINJA)
JQ-exists: ; which $(JQ)
PIP-exists: ; which $(PIP)

$(bakdir) $(cmddir) $(defdir) $(cfgdir):
    mkdir -p $@
    chown $$(id -u):$$(id -g) $@
    chmod 700 $@
    @echo "$@ $(OK_STRING)"

# BUILD AND INSTALL COMMANDS
# =======================================
# do not change


# TESTS
# =======================================
# do not change

.PHONY: test
test:
    @echo "$@ $(OK_STRING)"

# BACKUP
# =======================================
# do not change

.PHONY: back
back: nomac $(bakdir) ## TAR and backup (eg ../name_backup/name.YYYY-MM-DD.tar.gz)
    $(TAR) $(TARFLAGS) $(bakdir)/$(name).$(shell date +%Y-%m-%d).tar.gz .
    @echo "$@ $(OK_STRING)"

.PHONY: clean
clean: nomac ## Remove generated files
    $(RM) -r __pycache__
    $(RM) -r $(cmddir) $(defdir)
    $(RM) $(CMDTARGETS)
    $(RM) $(name)
    $(RM) $(prog)
    $(RM) engine.py
    @echo "$@ $(OK_STRING)"

.PHONY: nomac
nomac: ## Remove unneeded mac files
    $(RM) .DS_Store
    @echo "$@ $(OK_STRING)"

# ABOUT COPY HELP
# =======================================
# do not change

.PHONY: about
about: ## About this Makefile
    @echo
    @echo 'This Makefile is used to generate files for $(name)'
    @echo
    @echo 'Run "make help" to for how to run'
    @echo

.PHONY: copying
copying: ## Copyright notice
    @echo
    @echo 'Copyright (C) 2022 David Marsh'
    @echo 'rdmarsh@gmail.com'
    @echo
    @echo 'This program is free software: you can redistribute it and/or modify'
    @echo 'it under the terms of the GNU General Public License as published by'
    @echo 'the Free Software Foundation, either version 3 of the License, or'
    @echo '(at your option) any later version.'
    @echo
    @echo 'This program comes with ABSOLUTELY NO WARRANTY.'
    @echo 'This is free software, and you are welcome to redistribute it'
    @echo 'under certain conditions. View the file "LICENSE" for details.'
    @echo

.PHONY: help
help: ## Show this help
    @awk 'BEGIN {FS = ":.*##"; printf "\nUsage: make [flags] [option]\n"} /^[$$()% \.0-9a-zA-Z_-]+:.*?##/ { printf "  \033[36mmake %-12s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
    @echo
    @echo 'Useful make flags:'
    @echo '  make -n  dry run'
    @echo '  make -j  run simultaneous jobs'
    @echo '  make -B  force make target'
    @echo