Makefile 如何让make自动对同一源数据的其他模板作出反应?
我有一个数据文件Makefile 如何让make自动对同一源数据的其他模板作出反应?,makefile,Makefile,我有一个数据文件data.json,一个处理数据文件render的脚本,以及3个模板文件accounts.template,locals.template,和header.template 目标被称为账户和本地人,并与相应模板的扩展相关,该模板的数据保存在data.json中。没什么特别复杂的 目标和data.json位于项目的根目录中。 渲染脚本位于/scripts中。 模板位于/scripts/templates中 当前我的Makefile看起来像这样 render: data.json s
data.json
,一个处理数据文件render
的脚本,以及3个模板文件accounts.template
,locals.template
,和header.template
目标被称为账户
和本地人
,并与相应模板的扩展相关,该模板的数据保存在data.json
中。没什么特别复杂的
目标和data.json
位于项目的根目录中。
渲染
脚本位于/scripts
中。
模板位于/scripts/templates
中
当前我的Makefile
看起来像这样
render: data.json scripts/render locals accounts
@echo Completed
locals: data.json scripts/render scripts/templates/locals.template scripts/templates/header.template
@echo "Building locals"
@echo "------------------"
@php scripts/render locals
@echo ""
accounts: data.json scripts/render scripts/templates/accounts.template scripts/templates/header.template
@echo "Building accounts"
@echo "--------------------"
@php scripts/render accounts
@echo ""
这实际上是不可扩展的。我还有大约20个不同的模板要从data.json
文件生成(如果需要,每个模板可以将数据拆分为1个json文件)
是否有一种更智能、更理想的自动扩展方式来实现这一点
基本上,如果我有一个模板没有被称为标题(标题只是生成内容的一个“请勿编辑”横幅-将其移动到脚本/模板/common
,我认为在这里是明智的),然后使用模板名称作为参数运行呈现脚本(不带“.template”部分)
这可行吗
此外,如果目标已经被编辑(因为有一个巨大的横幅,所以不应该被编辑),是否有任何方法使其无效,以便在再次运行make时强制重建
编辑:根据下面的注释更新Makefile
TEMPLATES := $(patsubst scripts/templates/%.template.php,%.tf,$(wildcard scripts/templates/*.template.php))
render: $(TEMPLATES)
@echo Completed
%.tf: scripts/templates/%.template.php scripts/templates/common/header.template.php locals.json scripts/render.php
@php scripts/render.php $(subst .tf,,$@)
这是一个简单的应用程序:
对于编辑目标的人来说,没有好办法,因为make只在修改时间上工作。没有办法知道目标是被修改的,因为没有什么可以比较它来告诉你,即使你添加了一个临时文件来检测它,也不会做任何事情,因为目标会比临时文件更新,所以让它考虑它。如果我触摸数据或任何模板,则不会生成任何内容。仅获取“已完成”的输出。没有生成。那么模板变量的内容肯定是错误的。。。哦,显然是我忘了一步。。。我来修理。好的,试试看,谢谢。我不得不为我做的一些额外的小改动而调整。
TEMPLATES := $(filter-out header,$(patsubst scripts/templates/%.template,%,$(wildcard scripts/templates/*.template)))
render: $(TEMPLATES)
@echo Completed
%: scripts/templates/%.template scripts/templates/header.template data.json scripts/render
@echo "Building $@"
@echo "------------------"
@php scripts/render $@
@echo ""