我正在尝试使用 makefile
和 perl
oneliner
自动创建 php 类。
我有这个文件document.txt
[ENTITY]
Document
MyDefaultBundle
number:string(200)
issueDate:datetime
expiryDate:datetime
file_path:string(200)
filename:string(200)
description:text
document_type:string(200)
基本上第一行是classname
,第二行是Bundlename
,第三行是fields
我在 make 文件中使用它来执行 symfony 命令来生成类
MYFILE = Entity/document.txt
READ_ENTITY=eval `perl -l -00pe 'y/\n\r[]/ /d; s/([A-Z]+) (\w+) (\w+) (.*)/$$1=$$3:$$2\nFIELDS="$$4"\n/g' $1`
mytest:
$(call READ_ENTITY,$(MYFILE)) && $(SYMFONY_COMMAND) --entity=$$ENTITY --fields="$$FIELDS"
这对于一个实体来说效果很好。 (我已经用 $$ 转义了 $)
现在我的条件是我有多个实体在循环中
文档.txt
[ENTITY]
Document
MyDefaultBundle
number:string(200)
issueDate:datetime
expiryDate:datetime
file_path:string(200)
filename:string(200)
description:text
document_type:string(200)
[ENTITY]
Document2
MyDefaultBundle
number:string(200)
issueDate:datetime
expiryDate:datetime
file_path:string(200)
filename:string(200)
description:text
document_type:string(200)
[ENTITY]
Document3
MyDefaultBundle
number:string(200)
issueDate:datetime
expiryDate:datetime
file_path:string(200)
filename:string(200)
description:text
document_type:string(200)
所以我想在循环中执行该语句。我不知道如何在 make 文件中执行此操作。
基本上使用eval
,我制作shell变量ENTITY
和FIELDS
,然后在symfony命令行中使用它
有什么想法吗?
编辑:
对于perl命令,上面显示的document.txt,即输入
perl coomand 的输出是
ENTITY=MyDefaultBundle:Document
FIELDS="number:string(200) issueDate:datetime expiryDate:datetime file_path:string(200) filename:string(200) description:text document_type:string(200)"
这基本上使它们成为 shell 变量,以便我可以使用 $ENTITY
插入它们
问题是,当我有多个由空格分隔的 [ENTITY] 时,我无法将其用作最后一个条目并覆盖实体变量。
我想要一些类似的东西
for [ENTITY] IN document.txt
EXECUTE $(call READ_ENTITY,$(MYFILE)) && $(SYMFONY_COMMAND) --entity=$$ENTITY --fields="$$FIELDS"
这样在每个循环中我都有不同的ENTITY
和FIELDS
变量
最佳答案
如果您使用 makefile 模式规则,则此处可能不需要循环。
- 为每个输出 php 文档创建一个输入文件。
创建一个模式规则,该规则采用一个输入文件并生成一个 php 文档。确保该命令生成的文件是目标名称:
# Take input files from Entity/, produce outputs in Documents/ Documents/Document%.php : Entity/document%.txt EXECUTE $(call READ_ENTITY,$<) && $(SYMFONY_COMMAND) --entity=$$ENTITY --fields="$$FIELDS" --output-filename=$@
创建取决于需要构建的文档的规则:
all : Documents/Document1.php Documents/Document2.php Documents/Document3.php .PHONY: all
调用
make all
。如果目标all
是 makefile 中的第一个目标(这是一种约定),那么只需make
。
关于linux - 如何在 perl 中为 makefile 进行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797591/