我想解析几个json文件,获取它们的内容,前置/附加一些文本并将新字符串保存在另一个文件中。目前我有这个适用于单个文件:
#set variables for shared libs
SHAREDFILE=$(SERVERPATH)/xx/mod/supervisor/cfg/prod/config.json
JSONCONF = `cat "$(SHAREDFILE)"`
share:
@echo "Sharing node-client code in ${PROJECT}.- ENV $(ENV)..."
@echo "parseConfig($(JSONCONF));" > $(CLIENTPATH)/public/js/shared/config.json
由于我所理解的变量只能在规则之外设置,我将如何为文件列表执行此操作?
更新:要复制的文件可以在不同的目录中,但应将它们全部复制到同一目标目录。
答案 0 :(得分:2)
基于对您的文件列表的猜测:
SHAREDFILES = foo.json bar.json baz.json
TARGETS = $(addprefix $(CLIENTPATH)/public/js/shared/, $(SHAREDFILES))
share: $(TARGETS)
$(TARGETS) : $(CLIENTPATH)/public/js/shared/%.json : $(SERVERPATH)/xx/mod/supervisor/cfg/prod/%.json
@echo "Sharing node-client code in ${PROJECT}.- ENV $(ENV)..."
@echo "parseConfig(`cat $<`);" > $@
修改:
如果源文件在几个目录中,但目标都进入同一目录......有几种方法可以做到这一点。这可能是最好的:
SHAREDFILES = some/path/foo.json another/path/bar.json yet/another/path/baz.json
TARGETDIR = $(CLIENTPATH)/public/js/shared
TARGETS = $(addprefix $(TARGETDIR)/, $(notdir $(SHAREDFILES)))
vpath %.json $(dir $(SHAREDFILES))
share: $(TARGETS)
$(TARGETS) : $(TARGETDIR)/%.json : %.json
@echo "Sharing node-client code in ${PROJECT}.- ENV $(ENV)..."
@echo "parseConfig(`cat $<`);" > $@