我正在尝试创建一个漂亮且动态的帮助,但我正在努力解释项目的 .env 文件中的变量名称。我需要 .env 来解释这些名称。我将提供我能够达到的最接近的结果,但我无法取得任何进一步的进展。
在上图中,我标记了需要由.env解释的文本
这是代码:
SHELL := /bin/bash
include .env
ENV_FILE = .env
help: ## Show help.
@awk 'BEGIN { \
FS = ":.*##"; \
printf "\nUsage:\n make \033[0;34m<target>\033[0m\n\nTargets:\n" \
} \
/^[a-zA-Z_\/\-\$${}]+:.*?##/ { \
targetName = $$1; \
description = $$2; \
if (match(targetName, "\\$\\{([^}]+)\\}")) { \
varEnvName = substr(targetName, RSTART+1, RLENGTH-2); \
gsub("\\$$\\{[a-zA-Z_]+\\}", varEnvName, targetName); \
}; \
printf " \033[0;34m%-10s\033[0m %s\n", targetName, description \
}' $(MAKEFILE_LIST)
call/${DOCKER_PHP_CONTAINER_NAME}/a: ## PHP // Help dynamic.
@echo "Dynamic target running for ${DOCKER_PHP_CONTAINER_NAME}"
call/${DOCKER_NGINX_CONTAINER_NAME}/a: ## NGINX // Help dynamic.
@echo "Dynamic target running for ${DOCKER_NGINX_CONTAINER_NAME}"
call/bpro_php/b: ## Help fixed.
@echo "Fixed target running..."
我做到了:)
我不确定这是否是最佳解决方案,但它对我有用。
help: ## Show help.
@set -a; source .env &>/dev/null; set +a; \
awk -v ENV_FILE=".env" ' \
BEGIN { \
FS = ":.*##"; \
printf "\nUsage:\n make \033[0;34m<target>\033[0m\n\nTargets:\n" \
} \
/^[a-zA-Z_\/\-\$${}]+:.*?##/ { \
targetName = $$1; \
description = $$2; \
while ((getline < ENV_FILE) > 0) { \
if ($$0 ~ /^[^#[:space:]]/) { \
gsub(/=.*/, "", $$0); \
varName = $$0; \
varValue = ENVIRON[varName]; \
gsub("\\$\\{" varName "\\}", varValue, targetName); \
gsub(/\$$/, "", targetName); \
} \
} \
printf " \033[0;34m%-10s\033[0m %s\n", targetName, description; \
close(ENV_FILE); \
}' $(MAKEFILE_LIST)