我想在生成一个文件之前测试它是否不存在。
是否可以使用类似isFileExist的东西?
[template public generate(conf : Configuration) ? (isFileExist('/path/to/file.txt'))]
[file ('/path/to/file.txt', false, 'UTF-8')]
file content
[/file]
[/template]
我刚刚遇到了与您相同的问题,我最终通过在外部Java类中"外部化"该函数使其工作。这样,您就可以定义如下方法:
public boolean existsFile(String filepath) {
Path p = Paths.get(filepath);
p.normalize();
File f = new File(filepath);
return f.exists();
}
,然后通过定义如下查询从Acceleo调用:
[query public existsFile(filePath : String):
Boolean = invoke('utils.AcceleoUtils', 'existsFile(java.lang.String)', Sequence{filePath})
/]
这样,在你的例子中,你可以做
[template public generate(conf : Configuration)]
[if existsFile('/path/to/file.txt')/]
[file ('/path/to/file.txt', false, 'UTF-8')]
file content
[/file]
[/if]
[/template]
p。d:小心路径,因为默认情况下' file '标签会在目标路径中输出文件,所以如果您没有提供绝对路径,则需要在调用existsFile函数时或在existsFile函数中包含它。