Use file templates inside a project template

IƱigo Huguet <[email protected]> Wed, 28 Jun 2017 10:31:39 +0200
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <[email protected]>
Hi!

I wan't to create a project template with some files that are also 
template files. This template files contains some variables like ${user} 
which must be replaced with the "user" variable value.

I have followed this tutorial to create the project template: 
https://platform.netbeans.org/tutorials/nbm-projectsamples.html

However, I don't know how to get the template files to be processed with 
freemarker template engine, despite being trying for 3 days. I haven't 
found any information about how to do what I want (only info about 
project templates and file templates, but no one of both together).

This is what I have tried:

First, I modified the 'instantiate' method of the wizard iterator to 
process the template files:
[code]
     public Set/*<FileObject>*/ instantiate(/*ProgressHandle handle*/) 
throws IOException {
         Set<FileObject> resultSet = new LinkedHashSet<FileObject>();
         File dirF = FileUtil.normalizeFile((File) 
wiz.getProperty("projdir"));
         dirF.mkdirs();

         FileObject template = Templates.getTemplate(wiz);
         FileObject dir = FileUtil.toFileObject(dirF);
         unZipFile(template.getInputStream(), dir);

         ///// ADDED CODE
         FileObject templ = 
FileUtil.getSystemConfigFile("ADDITIONALSAMPLE_INIGO/templates/main.c");
         Map<String, String> attrs = new HashMap<>();
         attrs.put("javax.script.ScriptEngine", "freemarker");
         attrs.put("user", "myName");
         FileBuilder.createFromTemplate(templ, dir, "main", attrs, 
FileBuilder.Mode.FORMAT);
         ///// END OF ADDED CODE


         // Always open top dir as a project:
         resultSet.add(dir);
         // Look for nested projects to open as well:
         Enumeration<? extends FileObject> e = dir.getFolders(true);
         while (e.hasMoreElements()) {
             FileObject subfolder = e.nextElement();
             if (ProjectManager.getDefault().isProject(subfolder)) {
                 resultSet.add(subfolder);
             }
         }

         File parent = dirF.getParentFile();
         if (parent != null && parent.exists()) {
             ProjectChooser.setProjectsFolder(parent);
         }

         return resultSet;
     }
[/code]


I modified the templateregistration annotation of the top of the same 
file and let it like this:
[code]
@TemplateRegistrations(value = {
     @TemplateRegistration(folder = "Project/My custom project", 
displayName = "#customproj_displayName", description = 
"customproj.html", iconBase = 
"org/myplugins/additionalsample/MyCustomProject.png", content = 
"MyCustomProject.zip", scriptEngine = "freemarker"),
     @TemplateRegistration(folder = "Others", content = 
"templates/main.c", scriptEngine = "freemarker")
})
[/code]


And finally I created this layer.xml file:
[code]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" 
"http://www.netbeans.org/dtds/filesystem-1_2.dtd">
<filesystem>
     <folder name="ADDITIONALSAMPLE_INIGO">
         <folder name="templates">
             <file name="main.c" url="templates/main.c" />
         </folder>
     </folder>
</filesystem>
[/code]

However, main.c file is copied as is, the text '${user}' in the file is 
copied literally, not processed by freemarker.

And also, the project is created in disk but it's not added to the open 
projects.

Hope anyone can help me.

Thanks!