Reputation: 607
I would like to test if a file does not exist before generate it.
Is it possible to use something like isFileExist ?
[template public generate(conf : Configuration) ? (isFileExist('/path/to/file.txt'))]
[file ('/path/to/file.txt', false, 'UTF-8')]
file content
[/file]
[/template]
Thanks in advance.
Upvotes: 1
Views: 363
Reputation: 379
I just had the same problem as you have, and I finally got it working by "externalizing" this function in an external Java class. That way, you can just define a method such as:
public boolean existsFile(String filepath) {
Path p = Paths.get(filepath);
p.normalize();
File f = new File(filepath);
return f.exists();
}
and then call it from Acceleo by defining a query like:
[query public existsFile(filePath : String):
Boolean = invoke('utils.AcceleoUtils', 'existsFile(java.lang.String)', Sequence{filePath})
/]
This way, in your example, you could do
[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: Be carefull with the paths, because the ´file´ tag outputs your files in your target path by default, so in case you´re not providing an absolute Path you will need to include it either when calling or inside the existsFile function.
Upvotes: 1