2021-06-14 12:52:48 +02:00
|
|
|
.DEFAULT_GOAL := help
|
|
|
|
.PHONY: FORCE
|
2021-02-09 17:05:00 +01:00
|
|
|
comma ?= ,
|
|
|
|
dollar ?= $
|
|
|
|
dquote ?= "
|
|
|
|
quote ?= '
|
2021-06-12 03:13:01 +02:00
|
|
|
lbracket ?= (
|
|
|
|
rbracket ?= )
|
2021-05-17 03:40:02 +02:00
|
|
|
APP ?= $(if $(wildcard .git),$(notdir $(CURDIR)))
|
2022-11-19 02:03:37 +01:00
|
|
|
APP_LOAD ?= $(if $(SUBREPO),subrepo) $(if $(filter .,$(MYOS)),myos)
|
2022-11-06 00:27:14 +01:00
|
|
|
APP_NAME ?= $(subst _,,$(subst -,,$(subst .,,$(call LOWERCASE,$(APP)))))
|
2021-05-25 14:55:16 +02:00
|
|
|
APPS ?= $(if $(MONOREPO),$(sort $(patsubst $(MONOREPO_DIR)/%/.git,%,$(wildcard $(MONOREPO_DIR)/*/.git))))
|
|
|
|
APPS_NAME ?= $(foreach app,$(APPS),$(or $(shell awk -F '=' '$$1 == "APP" {print $$2}' $(or $(wildcard $(MONOREPO_DIR)/$(app)/.env),$(wildcard $(MONOREPO_DIR)/$(app)/.env.$(ENV)),$(MONOREPO_DIR)/$(app)/.env.dist) 2>/dev/null),$(app)))
|
2021-06-09 00:44:36 +02:00
|
|
|
BRANCH ?= $(GIT_BRANCH)
|
2021-06-16 13:19:52 +02:00
|
|
|
CMD_APK_INSTALL ?= $(if $(shell type -p apk),apk --no-cache --update add)
|
|
|
|
CMD_APK_REMOVE ?= $(if $(shell type -p apk),apk --no-cache del)
|
|
|
|
CMD_APT_INSTALL ?= $(if $(shell type -p apt-get),apt-get update && apt-get -fy install)
|
|
|
|
CMD_APT_REMOVE ?= $(if $(shell type -p apt-get),apt-get -fy remove)
|
2022-11-16 09:46:00 +01:00
|
|
|
CMDARGS ?= apps-install install-app
|
2021-07-11 09:56:03 +02:00
|
|
|
COLOR_BLUE ?= \033[01;34m
|
|
|
|
COLOR_BROWN ?= \033[33m
|
|
|
|
COLOR_CYAN ?= \033[36m
|
|
|
|
COLOR_DGRAY ?= \033[30m
|
|
|
|
COLOR_ERROR ?= $(COLOR_RED)
|
|
|
|
COLOR_GRAY ?= \033[37m
|
|
|
|
COLOR_GREEN ?= \033[32m
|
2021-06-14 12:52:48 +02:00
|
|
|
COLOR_HIGHLIGHT ?= $(COLOR_GREEN)
|
2021-07-11 09:56:03 +02:00
|
|
|
COLOR_INFO ?= $(COLOR_BROWN)
|
|
|
|
COLOR_RED ?= \033[31m
|
|
|
|
COLOR_RESET ?= \033[0m
|
2021-06-14 12:52:48 +02:00
|
|
|
COLOR_VALUE ?= $(COLOR_CYAN)
|
|
|
|
COLOR_WARNING ?= $(COLOR_YELLOW)
|
|
|
|
COLOR_YELLOW ?= \033[01;33m
|
2021-06-09 00:44:36 +02:00
|
|
|
COMMIT ?= $(or $(SUBREPO_COMMIT),$(GIT_COMMIT))
|
|
|
|
CONFIG ?= $(RELATIVE)config
|
2021-06-16 13:19:52 +02:00
|
|
|
CONFIG_REPOSITORY ?= $(CONFIG_REPOSITORY_URL)
|
|
|
|
CONFIG_REPOSITORY_HOST ?= $(shell printf '$(CONFIG_REPOSITORY_URI)\n' |sed 's|/.*||;s|.*@||')
|
|
|
|
CONFIG_REPOSITORY_PATH ?= $(shell printf '$(CONFIG_REPOSITORY_URI)\n' |sed 's|[^/]*/||;')
|
|
|
|
CONFIG_REPOSITORY_SCHEME ?= $(shell printf '$(CONFIG_REPOSITORY_URL)\n' |sed 's|://.*||;')
|
|
|
|
CONFIG_REPOSITORY_URI ?= $(shell printf '$(CONFIG_REPOSITORY_URL)\n' |sed 's|.*://||;')
|
|
|
|
CONFIG_REPOSITORY_URL ?= $(call pop,$(APP_UPSTREAM_REPOSITORY))/$(notdir $(CONFIG))
|
|
|
|
CONTEXT ?= ENV $(shell awk 'BEGIN {FS="="}; $$1 !~ /^(\#|$$)/ {print $$1}' .env.dist 2>/dev/null)
|
2021-07-12 21:04:06 +02:00
|
|
|
CONTEXT_DEBUG ?= MAKEFILE_LIST DOCKER_ENV_ARGS ENV_ARGS APPS GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME MAKE_DIR MAKE_SUBDIRS MAKE_CMD_ARGS MAKE_ENV_ARGS UID USER
|
2021-06-12 03:13:01 +02:00
|
|
|
DEBUG ?=
|
2021-06-16 13:19:52 +02:00
|
|
|
DOCKER ?= $(shell type -p docker)
|
2021-05-17 15:28:24 +02:00
|
|
|
DOMAIN ?= localhost
|
2021-02-09 17:05:00 +01:00
|
|
|
DRONE ?= false
|
|
|
|
DRYRUN ?= false
|
|
|
|
DRYRUN_RECURSIVE ?= false
|
2021-06-13 02:09:57 +02:00
|
|
|
ELAPSED_TIME = $(shell $(call TIME))
|
2022-11-16 09:46:00 +01:00
|
|
|
ENV ?= master
|
2021-07-12 21:04:06 +02:00
|
|
|
ENV_ARGS ?= $(env_args)
|
2022-11-18 15:01:16 +01:00
|
|
|
ENV_FILE ?= $(wildcard $(if $(filter-out myos,$(MYOS)),$(MONOREPO_DIR)/.env) $(CONFIG)/$(ENV)/$(APP)/.env .env)
|
2021-06-13 02:09:57 +02:00
|
|
|
ENV_LIST ?= $(shell ls .git/refs/heads/ 2>/dev/null)
|
2021-02-09 17:05:00 +01:00
|
|
|
ENV_RESET ?= false
|
2022-11-11 23:37:27 +01:00
|
|
|
ENV_VARS ?= APP BRANCH DOMAIN ENV HOME HOSTNAME GID GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME GROUP MONOREPO MONOREPO_DIR TAG UID USER VERSION
|
2021-05-17 15:28:24 +02:00
|
|
|
GID ?= $(shell id -g 2>/dev/null)
|
2022-07-06 23:27:41 +02:00
|
|
|
GIDS ?= $(shell id -G 2>/dev/null)
|
2021-06-13 02:09:57 +02:00
|
|
|
GIT_AUTHOR_EMAIL ?= $(or $(shell git config user.email 2>/dev/null),$(USER)@my.os)
|
|
|
|
GIT_AUTHOR_NAME ?= $(or $(shell git config user.name 2>/dev/null),$(USER))
|
2021-06-09 00:44:36 +02:00
|
|
|
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
|
|
|
|
GIT_COMMIT ?= $(shell git rev-parse $(BRANCH) 2>/dev/null)
|
2021-05-17 15:28:24 +02:00
|
|
|
GIT_REPOSITORY ?= $(if $(SUBREPO),$(shell awk -F ' = ' '$$1 ~ /^[[\s\t]]*remote$$/ {print $$2}' .gitrepo 2>/dev/null),$(shell git config --get remote.origin.url 2>/dev/null))
|
2021-06-14 14:32:14 +02:00
|
|
|
GIT_STATUS ?= $(shell git status -uno --porcelain 2>/dev/null |wc -l)
|
2021-06-09 00:44:36 +02:00
|
|
|
GIT_TAG ?= $(shell git tag -l --points-at $(BRANCH) 2>/dev/null)
|
2022-06-22 10:02:09 +02:00
|
|
|
GIT_UPSTREAM_REPOSITORY ?= $(if $(GIT_REPOSITORY),$(if $(findstring ://,$(GIT_REPOSITORY)),$(call pop,$(call pop,$(GIT_REPOSITORY)))/,$(call pop,$(GIT_REPOSITORY),:):)$(or $(GIT_UPSTREAM_USER),$(GIT_USER))/$(lastword $(subst /, ,$(GIT_REPOSITORY))))
|
2021-06-13 02:09:57 +02:00
|
|
|
GIT_UPSTREAM_USER ?= $(lastword $(subst /, ,$(call pop,$(MYOS_REPOSITORY))))
|
2022-06-22 10:02:09 +02:00
|
|
|
GIT_USER ?= $(USER)
|
2021-06-09 00:44:36 +02:00
|
|
|
GIT_VERSION ?= $(shell git describe --tags $(BRANCH) 2>/dev/null || git rev-parse $(BRANCH) 2>/dev/null)
|
2022-07-06 23:27:41 +02:00
|
|
|
GROUP ?= $(shell id -ng 2>/dev/null)
|
2021-05-17 15:28:24 +02:00
|
|
|
HOSTNAME ?= $(shell hostname 2>/dev/null |sed 's/\..*//')
|
2021-06-13 02:09:57 +02:00
|
|
|
IGNORE_DRYRUN ?= false
|
2021-06-14 12:52:48 +02:00
|
|
|
IGNORE_VERBOSE ?= false
|
2022-05-09 02:32:23 +02:00
|
|
|
INSTALL ?= $(RUN) $(SUDO) $(subst &&,&& $(RUN) $(SUDO),$(INSTALL_CMD))
|
2021-06-16 13:19:52 +02:00
|
|
|
INSTALL_CMDS ?= APK_INSTALL APT_INSTALL
|
|
|
|
$(foreach cmd,$(INSTALL_CMDS),$(if $(CMD_$(cmd)),$(eval INSTALL_CMD ?= $(CMD_$(cmd)))))
|
2021-06-12 03:13:01 +02:00
|
|
|
LOG_LEVEL ?= $(if $(DEBUG),debug,$(if $(VERBOSE),info,error))
|
2021-05-31 20:52:49 +02:00
|
|
|
MAKE_ARGS ?= $(foreach var,$(MAKE_VARS),$(if $($(var)),$(var)='$($(var))'))
|
2021-05-31 03:45:39 +02:00
|
|
|
MAKE_SUBDIRS ?= $(if $(filter myos,$(MYOS)),monorepo,$(if $(APP),apps $(foreach type,$(APP_TYPE),$(if $(wildcard $(MAKE_DIR)/apps/$(type)),apps/$(type)))))
|
2021-05-31 20:52:49 +02:00
|
|
|
MAKE_CMD_ARGS ?= $(foreach var,$(MAKE_CMD_VARS),$(var)='$($(var))')
|
|
|
|
MAKE_CMD_VARS ?= $(strip $(foreach var, $(filter-out .VARIABLES,$(.VARIABLES)), $(if $(filter command\ line,$(origin $(var))),$(var))))
|
|
|
|
MAKE_ENV_ARGS ?= $(foreach var,$(filter $(ENV_VARS),$(MAKE_ENV_VARS)),$(var)='$($(var))')
|
|
|
|
MAKE_ENV_VARS ?= $(strip $(foreach var, $(filter-out .VARIABLES,$(.VARIABLES)), $(if $(filter environment,$(origin $(var))),$(var))))
|
|
|
|
MAKE_FILE_ARGS ?= $(foreach var,$(filter $(ENV_VARS),$(MAKE_FILE_VARS)),$(var)='$($(var))')
|
|
|
|
MAKE_FILE_VARS ?= $(strip $(foreach var, $(filter-out .VARIABLES,$(.VARIABLES)), $(if $(filter file,$(origin $(var))),$(var))))
|
2021-06-06 16:29:54 +02:00
|
|
|
MAKE_OLDFILE ?= $@
|
2022-06-22 10:02:09 +02:00
|
|
|
MAKE_TARGETS ?= $(filter-out $(.VARIABLES),$(shell $(MAKE) -qp 2>/dev/null |awk -F':' '/^[a-zA-Z0-9][^$$\#\/\t=]*:([^=]|$$)/ {print $$1}' 2>/dev/null |sort -u))
|
2021-06-13 02:09:57 +02:00
|
|
|
MAKE_UNIXTIME_START := $(shell date -u +'%s' 2>/dev/null)
|
|
|
|
MAKE_UNIXTIME_CURRENT = $(shell date -u "+%s" 2>/dev/null)
|
2021-05-31 20:52:49 +02:00
|
|
|
MAKE_VARS ?= ENV
|
2021-05-22 02:34:24 +02:00
|
|
|
MONOREPO ?= $(if $(filter myos,$(MYOS)),$(notdir $(CURDIR)),$(if $(APP),$(notdir $(realpath $(CURDIR)/..))))
|
|
|
|
MONOREPO_DIR ?= $(if $(MONOREPO),$(if $(filter myos,$(MYOS)),$(realpath $(CURDIR)),$(if $(APP),$(realpath $(CURDIR)/..))))
|
2021-06-06 16:29:54 +02:00
|
|
|
MYOS ?= $(if $(filter $(MAKE_DIR),$(call pop,$(MAKE_DIR))),.,$(call pop,$(MAKE_DIR)))
|
2021-06-13 02:09:57 +02:00
|
|
|
MYOS_COMMIT ?= $(shell GIT_DIR=$(MYOS)/.git git rev-parse head 2>/dev/null)
|
|
|
|
MYOS_REPOSITORY ?= $(shell GIT_DIR=$(MYOS)/.git git config --get remote.origin.url 2>/dev/null)
|
2021-06-12 03:13:01 +02:00
|
|
|
QUIET ?= $(if $(VERBOSE),,--quiet)
|
2021-02-09 17:05:00 +01:00
|
|
|
RECURSIVE ?= true
|
2021-06-06 16:29:54 +02:00
|
|
|
RELATIVE ?= $(if $(filter myos,$(MYOS)),./,../)
|
2021-06-01 01:50:11 +02:00
|
|
|
SHARED ?= $(RELATIVE)shared
|
2021-05-17 15:28:24 +02:00
|
|
|
SSH_DIR ?= ${HOME}/.ssh
|
2021-06-14 14:32:14 +02:00
|
|
|
STATUS ?= $(GIT_STATUS)
|
2021-02-09 17:05:00 +01:00
|
|
|
SUBREPO ?= $(if $(wildcard .gitrepo),$(notdir $(CURDIR)))
|
2021-06-16 13:19:52 +02:00
|
|
|
SUDO ?= $(if $(filter-out 0,$(UID)),$(shell type -p sudo))
|
2021-06-09 00:44:36 +02:00
|
|
|
TAG ?= $(GIT_TAG)
|
2021-05-17 15:28:24 +02:00
|
|
|
UID ?= $(shell id -u 2>/dev/null)
|
|
|
|
USER ?= $(shell id -nu 2>/dev/null)
|
2021-06-12 03:13:01 +02:00
|
|
|
VERBOSE ?= $(if $(DEBUG),true)
|
2021-06-09 00:44:36 +02:00
|
|
|
VERSION ?= $(GIT_VERSION)
|
2021-02-09 17:05:00 +01:00
|
|
|
|
2021-06-12 03:13:01 +02:00
|
|
|
ifneq ($(DEBUG),)
|
2021-05-22 02:34:24 +02:00
|
|
|
CONTEXT += $(CONTEXT_DEBUG)
|
2021-06-12 03:13:01 +02:00
|
|
|
else
|
|
|
|
.SILENT:
|
2021-05-17 15:28:24 +02:00
|
|
|
endif
|
2021-06-12 03:13:01 +02:00
|
|
|
|
2021-06-16 13:19:52 +02:00
|
|
|
# Guess RUN
|
2021-05-31 20:52:49 +02:00
|
|
|
ifeq ($(DRYRUN),true)
|
2021-06-13 02:09:57 +02:00
|
|
|
RUN = $(if $(filter-out true,$(IGNORE_DRYRUN)),echo)
|
2021-05-17 15:28:24 +02:00
|
|
|
ifeq ($(RECURSIVE), true)
|
|
|
|
DRYRUN_RECURSIVE := true
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2021-02-09 17:05:00 +01:00
|
|
|
# Guess OS
|
|
|
|
ifeq ($(OSTYPE),cygwin)
|
2022-10-12 02:50:02 +02:00
|
|
|
SYSTEM := cygwin
|
2021-02-09 17:05:00 +01:00
|
|
|
else ifeq ($(OS),Windows_NT)
|
2022-10-12 02:50:02 +02:00
|
|
|
SYSTEM := Windows_NT
|
2021-02-09 17:05:00 +01:00
|
|
|
else
|
2022-10-12 02:50:02 +02:00
|
|
|
SYSTEM := $(shell uname -s 2>/dev/null)
|
2021-02-09 17:05:00 +01:00
|
|
|
endif
|
2022-10-12 02:50:02 +02:00
|
|
|
MACHINE := $(shell uname -m 2>/dev/null)
|
2021-02-09 17:05:00 +01:00
|
|
|
|
2022-10-12 02:50:02 +02:00
|
|
|
ifeq ($(SYSTEM),Darwin)
|
2021-06-14 14:32:14 +02:00
|
|
|
SED_SUFFIX := ''
|
2021-02-09 17:05:00 +01:00
|
|
|
endif
|
2021-06-16 13:19:52 +02:00
|
|
|
|
|
|
|
# include .env files
|
|
|
|
include $(wildcard $(ENV_FILE))
|
2021-02-09 17:05:00 +01:00
|
|
|
|
2021-07-11 09:56:03 +02:00
|
|
|
ERROR_FD := 2
|
|
|
|
# macro ERROR: print colorized warning
|
|
|
|
ERROR = \
|
|
|
|
printf '${COLOR_ERROR}ERROR:${COLOR_RESET} ${COLOR_INFO}$(APP)${COLOR_RESET}[${COLOR_VALUE}$(MAKELEVEL)${COLOR_RESET}]$(if $@, ${COLOR_VALUE}$@${COLOR_RESET}):${COLOR_RESET} ' >&$(ERROR_FD) \
|
|
|
|
$(if $(2), \
|
|
|
|
&& printf '$(1) ${COLOR_HIGHLIGHT}$(2)${COLOR_RESET}' >&$(ERROR_FD) \
|
|
|
|
$(if $(3),&& printf ' $(3)$(if $(4), ${COLOR_VALUE}$(4)${COLOR_RESET})' >&$(ERROR_FD)) \
|
|
|
|
, \
|
|
|
|
&& $(strip $(call PRINTF,$(1)) >&$(ERROR_FD)) \
|
|
|
|
) \
|
|
|
|
&& printf '\n' >&$(ERROR_FD) \
|
|
|
|
&& exit 2
|
|
|
|
|
2021-06-06 16:29:54 +02:00
|
|
|
# macro force: Run command 1 sine die
|
|
|
|
## it starts command 1 if it is not already running
|
|
|
|
## it returns never
|
2021-06-16 13:19:52 +02:00
|
|
|
force = $$(while true; do \
|
|
|
|
[ $$(ps x |awk '\
|
|
|
|
BEGIN {nargs=split("'"$$*"'",args)} \
|
|
|
|
$$field == args[1] { \
|
|
|
|
matched=1; \
|
|
|
|
for (i=1;i<=NF-field;i++) { \
|
|
|
|
if ($$(i+field) == args[i+1]) {matched++} \
|
|
|
|
} \
|
|
|
|
if (matched == nargs) {found++} \
|
|
|
|
} \
|
|
|
|
END {print found+0}' field=4) -eq 0 \
|
|
|
|
] \
|
|
|
|
&& $(RUN) $(1) || sleep 1; done \
|
|
|
|
)
|
2021-05-17 15:28:24 +02:00
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# macro gid: Return GID of group 1
|
2021-07-11 09:56:03 +02:00
|
|
|
gid = $(shell awk -F':' '$$1 == "$(1)" {print $$3}' /etc/group 2>/dev/null)
|
2021-02-09 17:05:00 +01:00
|
|
|
|
2021-06-14 12:52:48 +02:00
|
|
|
INFO_FD := 2
|
2021-07-11 09:56:03 +02:00
|
|
|
# macro INFO: print colorized info
|
|
|
|
INFO = $(if $(VERBOSE),$(if $(filter-out true,$(IGNORE_VERBOSE)), \
|
|
|
|
printf '${COLOR_INFO}$(APP)${COLOR_RESET}[${COLOR_VALUE}$(MAKELEVEL)${COLOR_RESET}]$(if $@, ${COLOR_VALUE}$@${COLOR_RESET}):${COLOR_RESET} ' >&$(INFO_FD) \
|
2021-06-14 12:52:48 +02:00
|
|
|
$(if $(2), \
|
2022-11-16 09:46:00 +01:00
|
|
|
&& printf 'Calling ${COLOR_HIGHLIGHT}$(1)${COLOR_RESET}$(lbracket)' >&$(INFO_FD) \
|
2021-06-14 12:52:48 +02:00
|
|
|
&& $(or $(strip $(call PRINTF,$(2))),printf '$(2)') >&$(INFO_FD) \
|
|
|
|
&& printf '$(rbracket)' >&$(INFO_FD) \
|
|
|
|
$(if $(3),&& printf ' ${COLOR_VALUE}in${COLOR_RESET} $(3)' >&$(INFO_FD)) \
|
|
|
|
, \
|
|
|
|
&& $(strip $(call PRINTF,$(1)) >&$(INFO_FD)) \
|
|
|
|
) \
|
2021-07-11 09:56:03 +02:00
|
|
|
&& printf '\n' >&$(INFO_FD) \
|
2021-06-14 12:52:48 +02:00
|
|
|
))
|
2021-06-12 03:13:01 +02:00
|
|
|
|
2021-07-12 21:04:06 +02:00
|
|
|
# macro pop: Return last word of string 1 according to separator 2
|
|
|
|
pop = $(patsubst %$(or $(2),/)$(lastword $(subst $(or $(2),/), ,$(1))),%,$(1))
|
|
|
|
|
|
|
|
# macro sed: Run sed script 1 on file 2
|
|
|
|
sed = $(RUN) sed -i $(SED_SUFFIX) '$(1)' $(2)
|
|
|
|
|
|
|
|
# macro TIME: Print time elapsed since unixtime 1
|
|
|
|
TIME = awk '{printf "%02d:%02d:%02d\n",int($$1/3600),int(($$1%3600)/60),int($$1%60)}' \
|
|
|
|
<<< $(shell awk 'BEGIN {current=$(or $(2),$(MAKE_UNIXTIME_CURRENT)); start=$(or $(1),$(MAKE_UNIXTIME_START)); print (current - start)}' 2>/dev/null)
|
|
|
|
|
|
|
|
WARNING_FD := 2
|
|
|
|
# macro WARNING: print colorized warning
|
|
|
|
WARNING = \
|
|
|
|
printf '${COLOR_WARNING}WARNING:${COLOR_RESET} ${COLOR_INFO}$(APP)${COLOR_RESET}[${COLOR_VALUE}$(MAKELEVEL)${COLOR_RESET}]$(if $@, ${COLOR_VALUE}$@${COLOR_RESET}):${COLOR_RESET} ' >&$(WARNING_FD) \
|
|
|
|
$(if $(2), \
|
|
|
|
&& printf '$(1) ${COLOR_HIGHLIGHT}$(2)${COLOR_RESET}' >&$(WARNING_FD) \
|
|
|
|
$(if $(3),&& printf ' $(3)$(if $(4), ${COLOR_VALUE}$(4)${COLOR_RESET})' >&$(WARNING_FD)) \
|
|
|
|
, \
|
|
|
|
&& $(strip $(call PRINTF,$(1)) >&$(WARNING_FD)) \
|
|
|
|
) \
|
|
|
|
&& printf '\n' >&$(WARNING_FD)
|
|
|
|
|
|
|
|
# function conf: Extract variable=value line from configuration files
|
|
|
|
## it prints the line with variable 3 definition from block 2 in file 1
|
|
|
|
define conf
|
|
|
|
$(call INFO,conf,$(1)$(comma) $(2)$(comma) $(3))
|
|
|
|
$(eval file := $(1))
|
|
|
|
$(eval block := $(2))
|
|
|
|
$(eval variable := $(3))
|
|
|
|
[ -r "$(file)" ] && while IFS='=' read -r key value; do \
|
|
|
|
case $${key} in \
|
|
|
|
\#*) \
|
|
|
|
continue; \
|
|
|
|
;; \
|
|
|
|
\[*\]) \
|
|
|
|
current_bloc="$${key##\[}"; \
|
|
|
|
current_bloc="$${current_bloc%%\]}"; \
|
|
|
|
[ -z "$(block)" ] && [ -z "$(variable)" ] && printf '%s\n' "$${current_bloc}" ||:; \
|
|
|
|
;; \
|
|
|
|
*) \
|
|
|
|
key=$${key%$${key##*[![:space:]]}}; \
|
|
|
|
value=$${value#$${value%%[![:space:]]*}}; \
|
|
|
|
if [ "$(block)" = "$${current_bloc}" ] && [ "$${key}" ]; then \
|
|
|
|
[ -z "$(variable)" ] && printf '%s=%s\n' "$${key}" "$${value}" ||:; \
|
|
|
|
[ "$(variable)" = "$${key}" ] && printf '%s\n' "$${value}" ||:; \
|
|
|
|
fi \
|
|
|
|
;; \
|
|
|
|
esac \
|
|
|
|
done < "$(file)"
|
|
|
|
endef
|
|
|
|
|
2022-11-19 02:03:37 +01:00
|
|
|
# function env-exec: Exec arg 1 with env
|
2021-07-12 21:04:06 +02:00
|
|
|
define env-exec
|
|
|
|
$(call INFO,env-exec,$(1))
|
|
|
|
IFS=$$'\n'; env $(env_reset) $(env_args) $(1)
|
|
|
|
endef
|
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# function make: Call make with predefined options and variables
|
2022-11-18 15:01:16 +01:00
|
|
|
# 1st arg: make command line (targets and arguments)
|
2021-02-09 17:05:00 +01:00
|
|
|
# 2nd arg: directory to call make from
|
|
|
|
# 3rd arg: list of variables to pass to make (ENV by default)
|
|
|
|
# 4th arg: path to .env file with additional arguments to call make with (file must exist when calling make)
|
2021-05-31 20:52:49 +02:00
|
|
|
# add list of VARIABLE=VALUE from vars to MAKE_ARGS
|
|
|
|
# add list of arguments from file to MAKE_ARGS
|
2021-02-09 17:05:00 +01:00
|
|
|
# eval MAKE_DIR option to -C $(2) if $(2) given
|
|
|
|
# add current target to MAKE_OLDFILE (list of already fired targets)
|
|
|
|
# print command that will be run if VERBOSE mode
|
|
|
|
# actually run make command
|
|
|
|
# if DRYRUN_RECURSIVE mode, run make command in DRYRUN mode
|
|
|
|
define make
|
|
|
|
$(eval cmd := $(1))
|
|
|
|
$(eval dir := $(2))
|
|
|
|
$(eval vars := $(3))
|
|
|
|
$(eval file := $(4))
|
2021-05-31 20:52:49 +02:00
|
|
|
$(if $(vars),$(eval MAKE_ARGS += $(foreach var,$(vars),$(if $($(var)),$(var)='$($(var))'))))
|
|
|
|
$(if $(wildcard $(file)),$(eval MAKE_ARGS += $(shell cat $(file) |sed '/^$$/d; /^#/d; /=/!d; s/^[[\s\t]]*//; s/[[\s\t]]*=[[\s\t]]*/=/;' |awk -F '=' '{print $$1"='\''"$$2"'\''"}')))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(eval MAKE_DIR := $(if $(dir),-C $(dir)))
|
|
|
|
$(eval MAKE_OLDFILE += $(filter-out $(MAKE_OLDFILE), $^))
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,make,$(MAKE_ARGS) $(cmd),$(dir))
|
|
|
|
$(RUN) $(MAKE) $(MAKE_DIR) $(patsubst %,-o %,$(MAKE_OLDFILE)) MAKE_OLDFILE="$(MAKE_OLDFILE)" $(MAKE_ARGS) $(cmd)
|
2021-06-13 02:09:57 +02:00
|
|
|
$(if $(filter true,$(DRYRUN_RECURSIVE)),$(MAKE) $(MAKE_DIR) $(patsubst %,-o %,$(MAKE_OLDFILE)) MAKE_OLDFILE="$(MAKE_OLDFILE)" DRYRUN=$(DRYRUN) RECURSIVE=$(RECURSIVE) $(MAKE_ARGS) $(cmd))
|
2021-02-09 17:05:00 +01:00
|
|
|
endef
|
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# function TARGET:ENV: Create a new target ending with :env
|
|
|
|
## it sets ENV, ENV_FILE and calls original target
|
2021-02-09 17:05:00 +01:00
|
|
|
define TARGET:ENV
|
|
|
|
.PHONY: $(TARGET)
|
|
|
|
$(TARGET): $(ASSIGN_ENV)
|
|
|
|
$(TARGET): $(ASSIGN_ENV_FILE)
|
|
|
|
$(TARGET):
|
|
|
|
$$(call make,$$*,,ENV_FILE)
|
|
|
|
endef
|
2021-05-17 15:28:24 +02:00
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# set ENV=env for targets ending with :env
|
|
|
|
## for each env in ENV_LIST
|
|
|
|
## it overrides value of ENV with env
|
2021-06-09 00:44:36 +02:00
|
|
|
## it adds $(CONFIG)/$(env)/$(APP)/.env file to ENV_FILE
|
2021-06-02 00:54:33 +02:00
|
|
|
## it evals TARGET:ENV
|
2021-06-09 00:44:36 +02:00
|
|
|
$(foreach env,$(ENV_LIST),$(eval TARGET := %\:$(env)) $(eval ASSIGN_ENV := ENV:=$(env)) $(eval ASSIGN_ENV_FILE := ENV_FILE+=$(wildcard $(CONFIG)/$(env)/$(APP)/.env)) $(eval $(TARGET:ENV)))
|
2021-02-09 17:05:00 +01:00
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# set ENV=env for targets ending with @env
|
2021-02-09 17:05:00 +01:00
|
|
|
$(foreach env,$(ENV_LIST),$(eval %@$(env): ENV:=$(env)))
|