如何自动生成Makefile帮助命令
How to automatically generate a Makefile help command
我最近发现 this article online,它解释了如何设置一个 make help
目标,该目标将自动解析 Makefile 以获取注释并显示格式良好的帮助命令。
它解析:
install: ## Install npm dependencies for the api, admin, and frontend apps
@echo "Installing Node dependencies"
@npm install
install-dev: install ## Install dependencies and prepared development configuration
@./node_modules/.bin/selenium-standalone install
@cp -n ./config/development.js-dist ./config/development.js | true
run-frontend-dev: webpack.PID ## Run the frontend and admin apps in dev (using webpack-dev-server)
进入:
install Install npm dependencies for the api, admin, and frontend apps
install-dev Install dependencies and prepared development configuration
run-frontend-dev Run the frontend and admin apps in dev (using webpack-dev-server)
但出于某种原因我无法让它工作(至少在 OSX 上)。有了这个目标:
help: ## Show the help prompt.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "3[36m%-30s3[0m %s\n", $, $}'
当我运行:
make help
我刚刚得到:
$ make help
make: Nothing to be done for `help'.
我也尝试了文章中评论中的所有不同解决方案,但似乎没有任何效果。我做错了什么?
此外,脚本的变体是什么,它允许将注释放在目标 之前 的行上,而不是在目标之后。像这样:
# Build the source files with Babel.
build: $(shell find lib)
@rm -rf build
@mkdir -p build
@$(babel) lib $(babel_options)
# Seed the database with fake data.
db-fake:
@$(run) node bin/run-sql fake
这是有问题的完整 Makefile:
# Options.
DEBUG ?=
SOURCEMAPS ?= true
WATCH ?=
# Variables.
bin = ./node_modules/.bin
tests = $(shell find test/routes -depth 1)
shorthand-tests = $(patsubst test/routes/%.js,test-%,$(tests))
# Binaries.
babel = $(bin)/babel
mocha = $(bin)/mocha
node = node
nodemon = $(bin)/nodemon
run = heroku local:run
# Babel options.
babel_options = \
--out-dir build \
--copy-files
# Sourcemaps?
ifneq ($(SOURCEMAPS),false)
babel_options += --source-maps
endif
# Watch?
ifdef WATCH
babel_options += --watch
endif
# Development?
ifneq ($(NODE_ENV),production)
node = $(nodemon)
endif
# Debug?
ifdef DEBUG
node += debug
mocha += debug
endif
# Default.
default: help ## Default.
# Build the source files with Babel.
build: $(shell find lib) ## Build the source files with Babel.
@rm -rf build
@mkdir -p build
@$(babel) lib $(babel_options)
# Seed the database with fake data.
db-fake: ## Seed the database with fake data.
@$(run) node bin/run-sql fake
# Reset the database, dropping everything and the creating again.
db-reset: ## Reset the database, dropping everything and the creating again.
@$(run) node bin/run-sql drop
@$(run) node bin/run-sql create
# Seed the database with test data.
db-seed: ## Seed the database with test data.
@$(run) node bin/run-sql seed
# Show the help prompt.
help: ## Show the help prompt.
@awk '/^#/{c=substr([=16=],3);next}c&&/^[[:alpha:]][[:alnum:]_-]+:/{print substr(,1,index(,":")),c}1{c=0}' mm.mk | column -s: -t
# Open the PSQL interface.
psql: ## Open the PSQL interface.
@psql contentshq
# Run the development server.
server: ## Run the development server.
@$(run) $(node) bin/api
# Start the local postgres server.
start: ## Start the local postgres server.
@pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
# Stop the local postgres server.
stop: ## Stop the local postgres server.
@pg_ctl -D /usr/local/var/postgres stop -s -m fast
# Run all of the tests.
test: ## Run all of the tests.
@$(run) $(mocha) $(tests)
# Run specific route tests.
$(shorthand-tests): ## Run specific route tests.
@$(run) $(mocha) test/routes/$(subst test-,,$@).js
# Watch the source files with Babel.
watch: ## Watch the source files with Babel.
@WATCH=true $(MAKE) build
# Phony targets.
.PHONY: help
.PHONY: test
.PHONY: watch
您需要一个 help
规则来实际提取和打印消息。假设您正在谈论的文章是 http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html,这就是您要查找的内容。
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "3[36m%-30s3[0m %s\n", $, $}'
您在命令方法之前的文档可能是可行的,但需要更多的工作。大多数 UNIX 命令都是逐行运行的,因此使用文章中指定的注释更容易。
这里有一个小的 awk 脚本,可以处理注释出现在目标名称之前的情况。我使用 column
将文本放入列中,但您可以使用 printf 在 awk 中完成(如果需要,还可以添加控制台代码来为输出着色);使用 column
的优点是它会自动计算出列宽。
您可以用同样的方式将它插入到 Makefile 中:
.PHONY: help
# Show this help.
help:
@awk '/^#/{c=substr($[=10=],3);next}c&&/^[[:alpha:]][[:alnum:]_-]+:/{print substr($,1,index($,":")),c}1{c=0}' $(MAKEFILE_LIST) | column -s: -t
我最近发现 this article online,它解释了如何设置一个 make help
目标,该目标将自动解析 Makefile 以获取注释并显示格式良好的帮助命令。
它解析:
install: ## Install npm dependencies for the api, admin, and frontend apps
@echo "Installing Node dependencies"
@npm install
install-dev: install ## Install dependencies and prepared development configuration
@./node_modules/.bin/selenium-standalone install
@cp -n ./config/development.js-dist ./config/development.js | true
run-frontend-dev: webpack.PID ## Run the frontend and admin apps in dev (using webpack-dev-server)
进入:
install Install npm dependencies for the api, admin, and frontend apps
install-dev Install dependencies and prepared development configuration
run-frontend-dev Run the frontend and admin apps in dev (using webpack-dev-server)
但出于某种原因我无法让它工作(至少在 OSX 上)。有了这个目标:
help: ## Show the help prompt.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "3[36m%-30s3[0m %s\n", $, $}'
当我运行:
make help
我刚刚得到:
$ make help
make: Nothing to be done for `help'.
我也尝试了文章中评论中的所有不同解决方案,但似乎没有任何效果。我做错了什么?
此外,脚本的变体是什么,它允许将注释放在目标 之前 的行上,而不是在目标之后。像这样:
# Build the source files with Babel.
build: $(shell find lib)
@rm -rf build
@mkdir -p build
@$(babel) lib $(babel_options)
# Seed the database with fake data.
db-fake:
@$(run) node bin/run-sql fake
这是有问题的完整 Makefile:
# Options.
DEBUG ?=
SOURCEMAPS ?= true
WATCH ?=
# Variables.
bin = ./node_modules/.bin
tests = $(shell find test/routes -depth 1)
shorthand-tests = $(patsubst test/routes/%.js,test-%,$(tests))
# Binaries.
babel = $(bin)/babel
mocha = $(bin)/mocha
node = node
nodemon = $(bin)/nodemon
run = heroku local:run
# Babel options.
babel_options = \
--out-dir build \
--copy-files
# Sourcemaps?
ifneq ($(SOURCEMAPS),false)
babel_options += --source-maps
endif
# Watch?
ifdef WATCH
babel_options += --watch
endif
# Development?
ifneq ($(NODE_ENV),production)
node = $(nodemon)
endif
# Debug?
ifdef DEBUG
node += debug
mocha += debug
endif
# Default.
default: help ## Default.
# Build the source files with Babel.
build: $(shell find lib) ## Build the source files with Babel.
@rm -rf build
@mkdir -p build
@$(babel) lib $(babel_options)
# Seed the database with fake data.
db-fake: ## Seed the database with fake data.
@$(run) node bin/run-sql fake
# Reset the database, dropping everything and the creating again.
db-reset: ## Reset the database, dropping everything and the creating again.
@$(run) node bin/run-sql drop
@$(run) node bin/run-sql create
# Seed the database with test data.
db-seed: ## Seed the database with test data.
@$(run) node bin/run-sql seed
# Show the help prompt.
help: ## Show the help prompt.
@awk '/^#/{c=substr([=16=],3);next}c&&/^[[:alpha:]][[:alnum:]_-]+:/{print substr(,1,index(,":")),c}1{c=0}' mm.mk | column -s: -t
# Open the PSQL interface.
psql: ## Open the PSQL interface.
@psql contentshq
# Run the development server.
server: ## Run the development server.
@$(run) $(node) bin/api
# Start the local postgres server.
start: ## Start the local postgres server.
@pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
# Stop the local postgres server.
stop: ## Stop the local postgres server.
@pg_ctl -D /usr/local/var/postgres stop -s -m fast
# Run all of the tests.
test: ## Run all of the tests.
@$(run) $(mocha) $(tests)
# Run specific route tests.
$(shorthand-tests): ## Run specific route tests.
@$(run) $(mocha) test/routes/$(subst test-,,$@).js
# Watch the source files with Babel.
watch: ## Watch the source files with Babel.
@WATCH=true $(MAKE) build
# Phony targets.
.PHONY: help
.PHONY: test
.PHONY: watch
您需要一个 help
规则来实际提取和打印消息。假设您正在谈论的文章是 http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html,这就是您要查找的内容。
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "3[36m%-30s3[0m %s\n", $, $}'
您在命令方法之前的文档可能是可行的,但需要更多的工作。大多数 UNIX 命令都是逐行运行的,因此使用文章中指定的注释更容易。
这里有一个小的 awk 脚本,可以处理注释出现在目标名称之前的情况。我使用 column
将文本放入列中,但您可以使用 printf 在 awk 中完成(如果需要,还可以添加控制台代码来为输出着色);使用 column
的优点是它会自动计算出列宽。
您可以用同样的方式将它插入到 Makefile 中:
.PHONY: help
# Show this help.
help:
@awk '/^#/{c=substr($[=10=],3);next}c&&/^[[:alpha:]][[:alnum:]_-]+:/{print substr($,1,index($,":")),c}1{c=0}' $(MAKEFILE_LIST) | column -s: -t