From 5e3782c944aff8a2b4f7170530b05a94cb962272 Mon Sep 17 00:00:00 2001 From: Yann Autissier Date: Sat, 25 Nov 2023 23:20:04 +0100 Subject: [PATCH] update .env when older than .env.dist --- make/def.app.mk | 4 +--- make/def.mk | 6 ++++++ make/env.mk | 15 +++++++++------ make/utils.mk | 18 +++++++++++++++--- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/make/def.app.mk b/make/def.app.mk index 2737d5a..cedcf18 100644 --- a/make/def.app.mk +++ b/make/def.app.mk @@ -22,9 +22,7 @@ define app-bootstrap $(eval COMPOSE_PROJECT_NAME := $(if $(DOCKER_COMPOSE_PROJECT_NAME),$(DOCKER_COMPOSE_PROJECT_NAME),$(subst .,,$(call LOWERCASE,$(USER)-$(APP_NAME)-$(ENV)$(addprefix -,$(subst /,,$(subst -,,$(APP_PATH)))))))) $(eval COMPOSE_SERVICE_NAME := $(if $(DOCKER_COMPOSE_SERVICE_NAME),$(DOCKER_COMPOSE_SERVICE_NAME),$(subst _,-,$(COMPOSE_PROJECT_NAME)))) $(eval DOCKER_BUILD_DIR := $(APP_DIR)) - $(if $(wildcard $(APP_DIR)/.env.dist), $(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.dist)) - $(if $(wildcard $(APP_DIR)/.env.example), $(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.example)) - $(if $(wildcard $(APP_DIR)/.env.sample), $(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.sample)) + $(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.dist $(APP_DIR)/.env.example $(APP_DIR)/.env.sample) endef # function app-build: Call docker-build for each Dockerfile in dir 1 diff --git a/make/def.mk b/make/def.mk index 407eb53..1db68ac 100644 --- a/make/def.mk +++ b/make/def.mk @@ -225,6 +225,12 @@ force = $$(while true; do \ # macro gid: Return GID of group 1 gid = $(shell awk -F':' '$$1 == "$(1)" {print $$3}' /etc/group 2>/dev/null) +# macro newer: Return the newest file +newer = $(shell stat -c '%Y %n' $(1) $(2) $(if $(DEBUG),,2>/dev/null) |sort -n |tail -n1 |awk '{print $$2}') + +# older newer: Return the oldest file +older = $(shell stat -c '%Y %n' $(1) $(2) $(if $(DEBUG),,2>/dev/null) |sort -n |head -n1 |awk '{print $$2}') + # macro pop: Return last word of string 1 according to separator 2 pop = $(patsubst %$(or $(2),/)$(lastword $(subst $(or $(2),/), ,$(1))),%,$(1)) diff --git a/make/env.mk b/make/env.mk index 18ff7f2..864ece8 100644 --- a/make/env.mk +++ b/make/env.mk @@ -2,8 +2,9 @@ # ENV # target .env: Update file .env -## it updates file .env when file .env.dist is newer -.env: .env.dist +## it updates file .env +.PHONY: .env +.env: $(call .env,,,$(wildcard $(CONFIG)/$(ENV)/$(APP)/.env .env.$(ENV))) # target .env-clean: Remove file .env @@ -40,16 +41,18 @@ SHELL:=/bin/bash # function .env: Call .env_update function ## it sets .env, .env.dist and .env.ENV files paths -## it calls .env_update function if .env.dist file exists +## it calls .env_update function if .env.dist exists and is newer than .env +## of when file .env does not exist # 1st arg: path to .env file to update, default to .env # 2nd arg: path to .env.dist file, default to .env.dist # 3rd arg: path to .env override files, default to .env.$(ENV) define .env $(call INFO,.env,$(1)$(comma) $(2)$(comma) $(3)) $(eval env_file:=$(or $(1),.env)) - $(eval env_dist:=$(or $(2),$(env_file).dist)) - $(eval env_over:=$(or $(wildcard $(3)),$(wildcard $(env_file).$(ENV)))) - $(if $(wildcard $(env_dist)), $(call .env_update)) + $(eval env_dists:=$(wildcard $(or $(2),$(env_file).dist))) + $(eval env_over:=$(wildcard $(or $(3),$(env_file).$(ENV)))) + $(if $(FORCE)$(filter $(env_file),$(call newer,$(env_file) $(env_dists) $(env_over))), + ,$(foreach env_dist,$(env_dists),$(call .env_update))) endef # function .env_update: Update .env file with values from .env.dist diff --git a/make/utils.mk b/make/utils.mk index 0f88b9e..124d5c6 100644 --- a/make/utils.mk +++ b/make/utils.mk @@ -5,6 +5,7 @@ # ################################################################################ # source : https://raw.githubusercontent.com/buildroot/buildroot/master/support/misc/utils.mk +# customized for myos # Strip quotes and then whitespaces qstrip = $(strip $(subst ",,$(1))) @@ -14,6 +15,8 @@ qstrip = $(strip $(subst ",,$(1))) comma := , empty := space := $(empty) $(empty) +tab := $(empty) $(empty) +escape := $(shell printf '\x1b') # make 4.3: # https://lwn.net/Articles/810071/ @@ -69,6 +72,7 @@ reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1))) $(firstword # and in rules. Particularly useful for VCS version strings, that can contain # slashes, colons (OK in filenames but not in rules), and spaces. sanitize = $(subst $(space),_,$(subst :,_,$(subst /,_,$(strip $(1))))) +# customized for myos: slugify macro cleans up strings so it can be used as url slugify = $(subst $(space),,$(subst :,,$(subst /,,$(subst .,,$(1))))) # MESSAGE Macro -- display a message in bold type @@ -86,9 +90,17 @@ finddirclauses = $(call notfirstword,$(patsubst %,-o -path '$(1)/%',$(2))) # notfirstword(wordlist): returns all but the first word in wordlist notfirstword = $(wordlist 2,$(words $(1)),$(1)) -# build a comma-separated list of quoted items, from a space-separated +# build a comma-separated list of items, from a space-separated +# list of items: a b c d --> a, b, c, d +make-comma-list = $(subst $(space),$(comma)$(space),$(strip $(1))) + +# build a comma-separated list of double-quoted items, from a space-separated # list of unquoted items: a b c d --> "a", "b", "c", "d" -make-comma-list = $(subst $(space),$(comma)$(space),$(patsubst %,"%",$(strip $(1)))) +make-dq-comma-list = $(call make-comma-list,$(patsubst %,"%",$(strip $(1)))) + +# build a comma-separated list of single-quoted items, from a space-separated +# list of unquoted items: a b c d --> 'a', 'b', 'c', 'd' +make-sq-comma-list = $(call make-comma-list,$(patsubst %,'%',$(strip $(1)))) # Needed for the foreach loops to loop over the list of hooks, so that # each hook call is properly separated by a newline. @@ -124,10 +136,10 @@ QUOTE = ' # (that's why the single-quotes are escaped they way they are, above, # and why the dollar sign is not escaped) to printf(1). A trailing # newline is apended, too. -# Note: Removing the apended trailing newline for custom needs # # Note: leading or trailing spaces are *not* stripped. # +# customized for myos: Removing the trailing newline define PRINTF printf '$(subst $(sep),\n,\ $(subst $(PERCENT),$(PERCENT)$(PERCENT),\