This commit is contained in:
aynic.os
2021-06-09 00:44:36 +02:00
parent 11916a1bc3
commit 15227453ec
12 changed files with 76 additions and 137 deletions
+20 -14
View File
@@ -7,9 +7,11 @@ APP_NAME ?= $(APP)
APP_TYPE ?= $(if $(SUBREPO),subrepo) $(if $(filter .,$(MYOS)),myos)
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)))
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
BRANCH ?= $(GIT_BRANCH)
CMDS ?= exec exec:% exec@% install-app install-apps run run:% run@%
COMMIT ?= $(shell git rev-parse $(BRANCH) 2>/dev/null)
COMMIT ?= $(or $(SUBREPO_COMMIT),$(GIT_COMMIT))
CONFIG ?= $(RELATIVE)config
CONFIG_REPOSITORY ?= $(call pop,$(or $(APP_UPSTREAM_REPOSITORY),$(GIT_UPSTREAM_REPOSITORY)))/$(notdir $(CONFIG))
CONTEXT ?= $(if $(APP),APP BRANCH VERSION) $(shell awk 'BEGIN {FS="="}; $$1 !~ /^(\#|$$)/ {print $$1}' .env.dist 2>/dev/null)
CONTEXT_DEBUG ?= MAKEFILE_LIST env APPS GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME LOG_LEVEL MAKE_DIR MAKE_SUBDIRS MAKE_CMD_ARGS MAKE_ENV_ARGS MONOREPO_DIR UID USER
DEBUG ?= false
@@ -20,16 +22,20 @@ DRYRUN ?= false
DRYRUN_IGNORE ?= false
DRYRUN_RECURSIVE ?= false
ENV ?= dist
ENV_FILE ?= $(wildcard $(PARAMETERS)/$(ENV)/$(APP)/.env) .env
ENV_FILE ?= $(wildcard $(CONFIG)/$(ENV)/$(APP)/.env) .env
ENV_LIST ?= debug local tests release master #TODO: staging develop
ENV_RESET ?= false
ENV_VARS ?= APP BRANCH ENV HOSTNAME GID GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME MONOREPO MONOREPO_DIR TAG UID USER VERSION
GID ?= $(shell id -g 2>/dev/null)
GIT_AUTHOR_EMAIL ?= $(shell git config user.email 2>/dev/null)
GIT_AUTHOR_NAME ?= $(shell git config user.name 2>/dev/null)
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
GIT_COMMIT ?= $(shell git rev-parse $(BRANCH) 2>/dev/null)
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))
GIT_TAG ?= $(shell git tag -l --points-at $(BRANCH) 2>/dev/null)
GIT_UPSTREAM_REPOSITORY ?= $(if $(findstring ://,$(GIT_REPOSITORY)),$(call pop,$(call pop,$(GIT_REPOSITORY)))/,$(call pop,$(GIT_REPOSITORY),:):)$(GIT_UPSTREAM_USER)/$(lastword $(subst /, ,$(GIT_REPOSITORY)))
GIT_UPSTREAM_USER ?= $(or $(MONOREPO),$(USER))
GIT_VERSION ?= $(shell git describe --tags $(BRANCH) 2>/dev/null || git rev-parse $(BRANCH) 2>/dev/null)
HOSTNAME ?= $(shell hostname 2>/dev/null |sed 's/\..*//')
LOG_LEVEL ?= $(if $(filter false,$(VERBOSE)),error,$(if $(filter true,$(DEBUG)),debug))
MAKE_ARGS ?= $(foreach var,$(MAKE_VARS),$(if $($(var)),$(var)='$($(var))'))
@@ -42,23 +48,22 @@ MAKE_FILE_ARGS ?= $(foreach var,$(filter $(ENV_VARS),$(MAKE_FIL
MAKE_FILE_VARS ?= $(strip $(foreach var, $(filter-out .VARIABLES,$(.VARIABLES)), $(if $(filter file,$(origin $(var))),$(var))))
MAKE_OLDFILE ?= $@
MAKE_TARGETS ?= $(filter-out $(.VARIABLES),$(shell $(MAKE) -qp 2>/dev/null |awk -F':' '/^[a-zA-Z0-9][^$$\#\/\t=]*:([^=]|$$)/ {print $$1}' |sort -u))
MAKE_UNIXTIME ?= $(shell date +%s 2>/dev/null)
MAKE_VARS ?= ENV
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)/..))))
MYOS ?= $(if $(filter $(MAKE_DIR),$(call pop,$(MAKE_DIR))),.,$(call pop,$(MAKE_DIR)))
PARAMETERS ?= $(RELATIVE)parameters
PARAMETERS_REPOSITORY ?= $(call pop,$(GIT_UPSTREAM_REPOSITORY))/$(notdir $(PARAMETERS))
QUIET ?= $(if $(filter false,$(VERBOSE)),--quiet)
RECURSIVE ?= true
RELATIVE ?= $(if $(filter myos,$(MYOS)),./,../)
SHARED ?= $(RELATIVE)shared
SSH_DIR ?= ${HOME}/.ssh
SUBREPO ?= $(if $(wildcard .gitrepo),$(notdir $(CURDIR)))
TAG ?= $(shell git tag -l --points-at $(BRANCH) 2>/dev/null)
TAG ?= $(GIT_TAG)
UID ?= $(shell id -u 2>/dev/null)
USER ?= $(shell id -nu 2>/dev/null)
VERBOSE ?= false
VERSION ?= $(shell git describe --tags $(BRANCH) 2>/dev/null || git rev-parse $(BRANCH) 2>/dev/null)
VERSION ?= $(GIT_VERSION)
ifeq ($(DOCKER), true)
ENV_ARGS = $(env.docker.args) $(env.docker.dist)
@@ -170,21 +175,22 @@ pop = $(patsubst %$(or $(2),/)$(lastword $(subst $(or $(2),/), ,$(1))),%,$(1))
# macro sed: Exec sed script 1 on file 2
sed = $(call exec,sed -i $(SED_SUFFIX) '\''$(1)'\'' $(2))
# function install-app: Exec 'git clone url 1 dir 2'
# function install-app: Exec 'git clone url 1 dir 2' or Call update-app
## it installs application source files
define install-app
$(eval url := $(or $(1), $(APP_REPOSITORY)))
$(eval dir := $(or $(2), $(RELATIVE)$(lastword $(subst /, ,$(url)))))
[ -d $(dir) ] && $(call update-app,$(url),$(dir))
[ -d $(dir) ] || $(call exec,$(ECHO) git clone $(QUIET) $(url) $(dir))
endef
# function update-app: Exec 'cd dir 1 && git pull'
# function update-app: Exec 'cd dir 1 && git pull' or Call install-app
## it updates application source files
define update-app
$(eval dir := $(or $(1), $(APP_DIR)))
$(eval url := $(or $(2), $(APP_REPOSITORY)))
$(call install-app,$(url),$(dir))
$(eval url := $(or $(1), $(APP_REPOSITORY)))
$(eval dir := $(or $(2), $(APP_DIR)))
[ -d $(dir) ] && $(call exec,cd $(dir) && $(ECHO) git pull $(QUIET))
[ -d $(dir) ] || $(call install-app,$(url),$(dir))
endef
# function TARGET:ENV: Create a new target ending with :env
@@ -200,9 +206,9 @@ endef
# set ENV=env for targets ending with :env
## for each env in ENV_LIST
## it overrides value of ENV with env
## it adds $(PARAMETERS)/$(env)/$(APP)/.env file to ENV_FILE
## it adds $(CONFIG)/$(env)/$(APP)/.env file to ENV_FILE
## it evals TARGET:ENV
$(foreach env,$(ENV_LIST),$(eval TARGET := %\:$(env)) $(eval ASSIGN_ENV := ENV:=$(env)) $(eval ASSIGN_ENV_FILE := ENV_FILE+=$(wildcard $(PARAMETERS)/$(env)/$(APP)/.env)) $(eval $(TARGET:ENV)))
$(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)))
# set ENV=env for targets ending with @env
$(foreach env,$(ENV_LIST),$(eval %@$(env): ENV:=$(env)))