diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/ArduinoProjectGenerator.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/ArduinoProjectGenerator.java index 6964fa9da1c..05c24cc48be 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/ArduinoProjectGenerator.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/ArduinoProjectGenerator.java @@ -24,7 +24,6 @@ import org.eclipse.cdt.core.model.IPathEntry; import org.eclipse.core.resources.IBuildConfiguration; import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IncrementalProjectBuilder; @@ -72,14 +71,10 @@ public class ArduinoProjectGenerator { Map fmModel = new HashMap<>(); fmModel.put("projectName", project.getName()); //$NON-NLS-1$ - IFolder sourceFolder = project.getFolder("src"); //$NON-NLS-1$ - if (!sourceFolder.exists()) { - sourceFolder.create(true, true, monitor); - } - IPathEntry[] entries = new IPathEntry[] { CoreModel.newOutputEntry(sourceFolder.getFullPath()) }; + IPathEntry[] entries = new IPathEntry[] { CoreModel.newSourceEntry(project.getFullPath()) }; CoreModel.getDefault().create(project).setRawPathEntries(entries, monitor); - sourceFile = sourceFolder.getFile(project.getName() + ".cpp"); //$NON-NLS-1$ + sourceFile = project.getFile(project.getName() + ".cpp"); //$NON-NLS-1$ templateGen.generateFile(fmModel, "arduino.cpp", sourceFile, monitor); //$NON-NLS-1$ } diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/ArduinoTemplateGenerator.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/ArduinoTemplateGenerator.java index 56fc5b9aa44..28b664c7f02 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/ArduinoTemplateGenerator.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/ArduinoTemplateGenerator.java @@ -3,6 +3,8 @@ package org.eclipse.cdt.arduino.core.internal; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.io.StringWriter; import java.net.URISyntaxException; import java.net.URL; @@ -16,22 +18,19 @@ import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; +import freemarker.cache.TemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; -public class ArduinoTemplateGenerator { +public class ArduinoTemplateGenerator implements TemplateLoader { private final Configuration config; + private Path templateRoot = new Path("/templates"); //$NON-NLS-1$ public ArduinoTemplateGenerator() throws CoreException { config = new Configuration(Configuration.VERSION_2_3_22); - URL templateDirURL = FileLocator.find(Activator.getContext().getBundle(), new Path("/templates"), null); //$NON-NLS-1$ - try { - config.setDirectoryForTemplateLoading(new File(FileLocator.toFileURL(templateDirURL).toURI())); - } catch (IOException | URISyntaxException e) { - throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Template configuration", e)); - } + config.setTemplateLoader(this); } public void generateFile(final Object model, String templateFile, final IFile outputFile, IProgressMonitor monitor) @@ -51,8 +50,39 @@ public class ArduinoTemplateGenerator { } } catch (IOException | TemplateException e) { throw new CoreException( - new Status(IStatus.ERROR, Activator.getId(), "Processing template " + templateFile, e)); + new Status(IStatus.ERROR, Activator.getId(), "Processing template " + templateFile, e)); //$NON-NLS-1$ } } + @Override + public Object findTemplateSource(String name) throws IOException { + return FileLocator.find(Activator.getContext().getBundle(), templateRoot.append(name), null); + } + + @Override + public long getLastModified(Object source) { + try { + URL url = (URL) source; + if (url.getProtocol().equals("file")) { //$NON-NLS-1$ + File file = new File(url.toURI()); + return file.lastModified(); + } else { + return 0; + } + } catch (URISyntaxException e) { + return 0; + } + } + + @Override + public Reader getReader(Object source, String encoding) throws IOException { + URL url = (URL) source; + return new InputStreamReader(url.openStream()); + } + + @Override + public void closeTemplateSource(Object arg0) throws IOException { + // Nothing to do + } + }