keep the dynamism of make in pure shell
Two mechanisms, matching what the make engine actually did:
Lazy defaults. A stack setting is a function myos_default_<VAR>, called only
when the variable has no value, and called again at every reference. That is
exactly a recursive ?=: an explicit value wins, and the default follows a
DOMAIN that a .env changes later. The prefix is what makes it safe; the first
version used a bare function named after the variable, and the test suite
caught it running /usr/bin/host for a stack group called host.
Templates. myos env-update fills a .env from the .env.dist files, expanding
${VAR} against the current values and running $(command), forward references
included.
Also fixed: the project .env now wins over /etc/conf.d/myos, which is what the
documentation claimed and the code did not.
share/make/shim.mk lets a project keep make as a front end: every myos command
becomes a target that shells out to bin/myos, and the project keeps its own
targets and its stack .mk files. It sits outside make/ because the legacy
engine globs every .mk in there.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
##
|
||||
# myos, from make.
|
||||
#
|
||||
# This file gives a project the myos commands as make targets, without the make
|
||||
# engine: every target shells out to bin/myos, which is pure POSIX sh. What the
|
||||
# project keeps from make is what make is actually good at, and the CLI is not:
|
||||
# its own targets, its own dependencies, and the .mk files of its stacks.
|
||||
#
|
||||
# MYOS ?= /usr/local/lib/myos
|
||||
# include $(MYOS)/share/make/shim.mk
|
||||
#
|
||||
# It lives outside make/ on purpose: the legacy engine includes every .mk of
|
||||
# that directory, and would pull this one in too.
|
||||
#
|
||||
# Then `make up STACK=host` and `myos up host` do the same thing, through the
|
||||
# same code. Variables given on the command line are forwarded, so
|
||||
# `make up STACK=host DOMAIN=example.org` behaves as expected.
|
||||
|
||||
MYOS ?= $(patsubst %/share/make/shim.mk,%,$(lastword $(MAKEFILE_LIST)))
|
||||
MYOS_BIN ?= $(MYOS)/bin/myos
|
||||
STACK_DIR_NAME ?= stack
|
||||
STACK_DIR ?= $(wildcard $(CURDIR)/$(STACK_DIR_NAME))
|
||||
|
||||
# variable MYOS_ARGS: variables set on the make command line, forwarded to myos
|
||||
MYOS_ARGS ?= $(foreach v,$(MAKEOVERRIDES),$(v))
|
||||
|
||||
.DEFAULT_GOAL := help
|
||||
|
||||
## the stack files of the project may add their own targets and variables
|
||||
include $(wildcard $(STACK_DIR)/*.mk $(STACK_DIR)/*/*.mk)
|
||||
|
||||
# function make: run a myos command, for the stack .mk files that call it
|
||||
define make
|
||||
$(MYOS_BIN) $(MYOS_ARGS) $(1)
|
||||
endef
|
||||
|
||||
# target help: List the myos commands
|
||||
.PHONY: help
|
||||
help:
|
||||
@$(MYOS_BIN) help
|
||||
|
||||
# target myos: Run an arbitrary myos command, as in `make myos ARGS="up host"`
|
||||
.PHONY: myos
|
||||
myos:
|
||||
@$(MYOS_BIN) $(MYOS_ARGS) $(ARGS)
|
||||
|
||||
# make tries to remake every makefile it read, and the catch-all below would
|
||||
# hand each of them to myos as a command. An empty rule stops that.
|
||||
$(MAKEFILE_LIST): ;
|
||||
|
||||
# target %: Hand anything else to myos
|
||||
## a target the project defines itself keeps precedence over this rule
|
||||
%: FORCE
|
||||
@$(MYOS_BIN) $(MYOS_ARGS) $@ $(ARGS)
|
||||
|
||||
.PHONY: FORCE
|
||||
FORCE: ;
|
||||
Reference in New Issue
Block a user