mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-05 07:15:39 +02:00
Fix template loading for packaged bundles. Remove src folder.
Change-Id: Ie0a00e36e6ffd4dc8fa920f6823c04cfd538cbeb
This commit is contained in:
parent
919698379f
commit
4ba5681024
2 changed files with 40 additions and 15 deletions
|
@ -24,7 +24,6 @@ import org.eclipse.cdt.core.model.IPathEntry;
|
||||||
import org.eclipse.core.resources.IBuildConfiguration;
|
import org.eclipse.core.resources.IBuildConfiguration;
|
||||||
import org.eclipse.core.resources.ICommand;
|
import org.eclipse.core.resources.ICommand;
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.resources.IFolder;
|
|
||||||
import org.eclipse.core.resources.IProject;
|
import org.eclipse.core.resources.IProject;
|
||||||
import org.eclipse.core.resources.IProjectDescription;
|
import org.eclipse.core.resources.IProjectDescription;
|
||||||
import org.eclipse.core.resources.IncrementalProjectBuilder;
|
import org.eclipse.core.resources.IncrementalProjectBuilder;
|
||||||
|
@ -72,14 +71,10 @@ public class ArduinoProjectGenerator {
|
||||||
Map<String, Object> fmModel = new HashMap<>();
|
Map<String, Object> fmModel = new HashMap<>();
|
||||||
fmModel.put("projectName", project.getName()); //$NON-NLS-1$
|
fmModel.put("projectName", project.getName()); //$NON-NLS-1$
|
||||||
|
|
||||||
IFolder sourceFolder = project.getFolder("src"); //$NON-NLS-1$
|
IPathEntry[] entries = new IPathEntry[] { CoreModel.newSourceEntry(project.getFullPath()) };
|
||||||
if (!sourceFolder.exists()) {
|
|
||||||
sourceFolder.create(true, true, monitor);
|
|
||||||
}
|
|
||||||
IPathEntry[] entries = new IPathEntry[] { CoreModel.newOutputEntry(sourceFolder.getFullPath()) };
|
|
||||||
CoreModel.getDefault().create(project).setRawPathEntries(entries, monitor);
|
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$
|
templateGen.generateFile(fmModel, "arduino.cpp", sourceFile, monitor); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ package org.eclipse.cdt.arduino.core.internal;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
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.Path;
|
||||||
import org.eclipse.core.runtime.Status;
|
import org.eclipse.core.runtime.Status;
|
||||||
|
|
||||||
|
import freemarker.cache.TemplateLoader;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.Template;
|
import freemarker.template.Template;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
||||||
|
|
||||||
public class ArduinoTemplateGenerator {
|
public class ArduinoTemplateGenerator implements TemplateLoader {
|
||||||
|
|
||||||
private final Configuration config;
|
private final Configuration config;
|
||||||
|
private Path templateRoot = new Path("/templates"); //$NON-NLS-1$
|
||||||
|
|
||||||
public ArduinoTemplateGenerator() throws CoreException {
|
public ArduinoTemplateGenerator() throws CoreException {
|
||||||
config = new Configuration(Configuration.VERSION_2_3_22);
|
config = new Configuration(Configuration.VERSION_2_3_22);
|
||||||
URL templateDirURL = FileLocator.find(Activator.getContext().getBundle(), new Path("/templates"), null); //$NON-NLS-1$
|
config.setTemplateLoader(this);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateFile(final Object model, String templateFile, final IFile outputFile, IProgressMonitor monitor)
|
public void generateFile(final Object model, String templateFile, final IFile outputFile, IProgressMonitor monitor)
|
||||||
|
@ -51,8 +50,39 @@ public class ArduinoTemplateGenerator {
|
||||||
}
|
}
|
||||||
} catch (IOException | TemplateException e) {
|
} catch (IOException | TemplateException e) {
|
||||||
throw new CoreException(
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue