Skip to content

Commit 0c03c2d

Browse files
committed
Merge pull request #93 from nicoverduin/master
Enable custom templates for sketch.cpp and sketch.h
2 parents 31ec1b1 + 5b0fdf6 commit 0c03c2d

File tree

6 files changed

+612
-25
lines changed

6 files changed

+612
-25
lines changed

it.baeyens.arduino.common/src/it/baeyens/arduino/common/ArduinoConst.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ public class ArduinoConst {
108108
public static final String ENV_KEY_JANTJE_ADDITIONAL_COMPILE_OPTIONS = ENV_KEY_JANTJE_START + "EXTRA.COMPILE";
109109
public static final String ENV_KEY_JANTJE_ADDITIONAL_C_COMPILE_OPTIONS = ENV_KEY_JANTJE_START + "EXTRA.C.COMPILE";
110110
public static final String ENV_KEY_JANTJE_ADDITIONAL_CPP_COMPILE_OPTIONS = ENV_KEY_JANTJE_START + "EXTRA.CPP.COMPILE";
111+
//
112+
// template Sketch information
111113

114+
public static final String ENV_KEY_SKETCH_TEMPLATE_FOLDER = ENV_KEY_JANTJE_START + "TEMPLATE_FOLDER";
115+
public static final String ENV_KEY_SKETCH_TEMPLATE_USE_DEFAULT = ENV_KEY_JANTJE_START + "TEMPLATE_USE_DEFAULT";
116+
112117
public static final String JANTJE_SIZE_COMMAND = "\"${A.COMPILER.PATH}${A.COMPILER.SIZE.CMD}\" --format=avr --mcu=${A.BUILD.MCU} \"${A.BUILD.PATH}/${A.BUILD.PROJECT_NAME}.elf\"";
113118

114119
public static final String ENV_KEY_WARNING_LEVEL_OFF = "";

it.baeyens.arduino.common/src/it/baeyens/arduino/common/ArduinoInstancePreferences.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,24 @@ public static void setLastUsedScopeFilter(boolean newFilter) {
224224
setGlobalBoolean(KEY_LAST_USED_SCOPE_FILTER_MENU_OPTION, newFilter);
225225

226226
}
227+
//
228+
// get/set last used "use default sketch location"
229+
//
230+
public static boolean getLastUsedDefaultSketchSelection(){
231+
return getGlobalBoolean(ENV_KEY_SKETCH_TEMPLATE_USE_DEFAULT);
232+
}
233+
234+
public static void setLastUsedDefaultSketchSelection(boolean newFilter) {
235+
setGlobalBoolean(ENV_KEY_SKETCH_TEMPLATE_USE_DEFAULT, newFilter);
236+
}
237+
//
238+
// get/set last used sketch template folder parameters
239+
//
240+
public static String getLastTemplateFolderName() {
241+
return getGlobalValue(ENV_KEY_SKETCH_TEMPLATE_FOLDER);
242+
}
243+
public static void setLastTemplateFolderName(String folderName) {
244+
setGlobalValue(ENV_KEY_SKETCH_TEMPLATE_FOLDER, folderName);
245+
246+
}
227247
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package it.baeyens.arduino.tools;
2+
3+
import it.baeyens.arduino.common.Common;
4+
import it.baeyens.arduino.ui.NewArduinoSketchWizard;
5+
6+
import java.io.BufferedReader;
7+
import java.io.ByteArrayInputStream;
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.InputStreamReader;
12+
13+
import org.eclipse.core.runtime.CoreException;
14+
import org.eclipse.core.runtime.IStatus;
15+
import org.eclipse.core.runtime.Status;
16+
17+
public class DiskStream {
18+
/**
19+
* Initialize the file contents to contents of the given file.
20+
*/
21+
public static InputStream openContentStream(String title, String Include, String Resource) throws CoreException {
22+
/* We want to be truly OS-agnostic */
23+
final String newline = System.getProperty("line.separator");
24+
String line;
25+
StringBuffer stringBuffer = new StringBuffer();
26+
try {
27+
FileInputStream input = new FileInputStream(Resource);
28+
// "templates/index-xhtml-template.resource");
29+
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
30+
try {
31+
while ((line = reader.readLine()) != null) {
32+
line = line.replaceAll("\\{title\\}", title).replaceAll("\\{Include\\}", Include);
33+
stringBuffer.append(line);
34+
stringBuffer.append(newline);
35+
}
36+
} finally {
37+
reader.close();
38+
}
39+
} catch (IOException ioe) {
40+
IStatus status = new Status(IStatus.ERROR, "NewFileWizard", IStatus.OK, ioe.getLocalizedMessage(), null);
41+
Common.log(status);
42+
throw new CoreException(status);
43+
}
44+
return new ByteArrayInputStream(stringBuffer.toString().getBytes());
45+
}
46+
47+
}

it.baeyens.arduino.core/src/it/baeyens/arduino/ui/NewArduinoSketchWizard.java

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import it.baeyens.arduino.tools.ArduinoHelpers;
77
import it.baeyens.arduino.tools.ShouldHaveBeenInCDT;
88
import it.baeyens.arduino.tools.Stream;
9+
import it.baeyens.arduino.tools.DiskStream;
910
import it.baeyens.arduino.ui.BuildConfigurationsPage.ConfigurationDescriptor;
1011

1112
import java.io.File;
13+
import java.io.InputStream;
1214
import java.lang.reflect.InvocationTargetException;
1315
import java.net.URI;
1416
import java.util.ArrayList;
@@ -57,9 +59,10 @@
5759
*/
5860
public class NewArduinoSketchWizard extends Wizard implements INewWizard, IExecutableExtension {
5961

60-
private WizardNewProjectCreationPage mWizardPage;
61-
private ArduinoSettingsPage mArduinoPage;
62-
private BuildConfigurationsPage mBuildCfgPage;
62+
private WizardNewProjectCreationPage mWizardPage; // first page of the dialog
63+
private SketchTemplatePage mSketchTemplatePage; // add the folder for the templates
64+
private ArduinoSettingsPage mArduinoPage; // add Arduino board and comp port
65+
private BuildConfigurationsPage mBuildCfgPage; // build the configuration
6366
private IConfigurationElement mConfig;
6467
private IProject mProject;
6568

@@ -68,42 +71,79 @@ public NewArduinoSketchWizard() {
6871
}
6972

7073
@Override
74+
/**
75+
* adds pages to the wizard. We are using the standard project wizard of Eclipse
76+
*/
7177
public void addPages() {
78+
//
7279
// We assume everything is OK as it is tested in the handler
80+
// create each page and fill in the title and description
81+
// first page to fill in the project name
82+
//
7383
mWizardPage = new WizardNewProjectCreationPage("New Arduino sketch");
7484
mWizardPage.setDescription("Create a new Arduino sketch.");
7585
mWizardPage.setTitle("New Arduino sketch");
76-
86+
//
87+
// settings for template file location
88+
//
89+
mSketchTemplatePage = new SketchTemplatePage("Sketch Template location");
90+
mSketchTemplatePage.setTitle("Provide the sketch template folder");
91+
mSketchTemplatePage.setDescription("The folder must contain a sketch.cpp and sketch.h");
92+
//
93+
// settings for Arduino board etc
94+
//
7795
mArduinoPage = new ArduinoSettingsPage("Arduino information");
7896
mArduinoPage.setTitle("Provide the Arduino information.");
7997
mArduinoPage.setDescription("These settings can be changed later.");
80-
98+
//
99+
// configuration page but I haven't seen it
100+
//
81101
mBuildCfgPage = new BuildConfigurationsPage("Build configurations");
82102
mBuildCfgPage.setTitle("Select additional build configurations for this project.");
83103
mBuildCfgPage.setDescription("If you are using additional tools you may want one or more of these extra configurations.");
84-
104+
//
105+
// actually add the pages to the wizard
106+
///
85107
addPage(mWizardPage);
108+
addPage(mSketchTemplatePage);
86109
addPage(mArduinoPage);
87110
addPage(mBuildCfgPage);
88-
89111
}
90-
112+
/**
113+
* this method is required by IWizard otherwise nothing will actually happen
114+
*/
91115
@Override
92116
public boolean performFinish() {
93-
117+
//
118+
// if the project is filled in then we are done
119+
//
94120
if (mProject != null) {
95121
return true;
96122
}
97-
123+
//
124+
// get an IProject handle to our project
125+
//
98126
final IProject projectHandle = ResourcesPlugin.getWorkspace().getRoot().getProject(Common.MakeNameCompileSafe(mWizardPage.getProjectName()));
127+
//
128+
// let's validate it
129+
//
99130
try {
100-
131+
//
132+
// get the URL if it is filled in. This depends on the check box "use defaults" is checked
133+
// or not
134+
//
101135
URI projectURI = (!mWizardPage.useDefaults()) ? mWizardPage.getLocationURI() : null;
102-
136+
//
137+
// get the workspace name
138+
//
103139
IWorkspace workspace = ResourcesPlugin.getWorkspace();
104-
140+
//
141+
// the project descriptions is set equal to the name of the project
142+
//
105143
final IProjectDescription desc = workspace.newProjectDescription(projectHandle.getName());
106-
144+
//
145+
// get our workspace location
146+
//
107147
desc.setLocationURI(projectURI);
108148

109149
/*
@@ -112,6 +152,9 @@ public boolean performFinish() {
112152
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
113153
@Override
114154
protected void execute(IProgressMonitor monitor) throws CoreException {
155+
//
156+
// actually create the project
157+
//
115158
createProject(desc, projectHandle, monitor);
116159
}
117160
};
@@ -127,13 +170,18 @@ protected void execute(IProgressMonitor monitor) throws CoreException {
127170
MessageDialog.openError(getShell(), "Error", realException.getMessage());
128171
return false;
129172
}
130-
173+
//
174+
// so the project is created we can start
175+
//
131176
mProject = projectHandle;
132177

133178
if (mProject == null) {
134179
return false;
135180
}
136-
181+
//
182+
// so now we set Eclipse to the right perspective and switch to our just created
183+
// project
184+
//
137185
BasicNewProjectResourceWizard.updatePerspective(mConfig);
138186
IWorkbenchWindow TheWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
139187
BasicNewResourceWizard.selectAndReveal(mProject, TheWindow);
@@ -179,6 +227,7 @@ void createProject(IProjectDescription description, IProject project, IProgressM
179227

180228
for (int i = 0; i < cfgNamesAndTCIds.size(); i++) {
181229
ICConfigurationDescription configurationDescription = prjDesc.getConfigurationByName(cfgNamesAndTCIds.get(i).Name);
230+
mSketchTemplatePage.saveAllSelections(configurationDescription);
182231
mArduinoPage.saveAllSelections(configurationDescription);
183232
ArduinoHelpers.setTheEnvironmentVariables(project, configurationDescription, cfgNamesAndTCIds.get(i).DebugCompilerSettings);
184233
}
@@ -196,20 +245,45 @@ void createProject(IProjectDescription description, IProject project, IProgressM
196245
// Insert The Arduino Code
197246
// NOTE: Not duplicated for debug (the release reference is just to get at some environment variables)
198247
ArduinoHelpers.addArduinoCodeToProject(project, defaultConfigDescription);
199-
200-
/* Add the sketch source code file */
201-
ArduinoHelpers.addFileToProject(container, new Path(project.getName() + ".cpp"),
202-
Stream.openContentStream(project.getName(), "", "templates/sketch.cpp"), monitor);
203-
204-
/* Add the sketch header file */
248+
//
249+
// depending on the the Use Default selection we will either select the default
250+
// templates or the ones specified in the Template Sketch folder
251+
//
252+
// first determine type of include due to Arduino version. Since version 1.0 we use Arduino.h
253+
//
205254
String Include = "WProgram.h";
206255
if (ArduinoInstancePreferences.isArduinoIdeOne()) // Arduino v1.0+
207256
{
208257
Include = "Arduino.h";
209258
}
210-
ArduinoHelpers.addFileToProject(container, new Path(project.getName() + ".h"),
211-
Stream.openContentStream(project.getName(), Include, "templates/sketch.h"), monitor);
212-
259+
//
260+
// Create the source files (sketch.cpp and sketch.h)
261+
//
262+
InputStream cppTemplateFile = null;
263+
InputStream hTemplateFile = null;
264+
265+
if (ArduinoInstancePreferences.getLastUsedDefaultSketchSelection() == true) {
266+
//
267+
// we will use the default sketch.cpp and sketch.h
268+
//
269+
cppTemplateFile = Stream.openContentStream(project.getName(), "", "templates/sketch.cpp");
270+
hTemplateFile = Stream.openContentStream(project.getName(), Include, "templates/sketch.h");
271+
} else {
272+
//
273+
// we are using our own created sketch.cpp and sketch.h. We use a different streaming mechanism
274+
// here. Standard used relative paths (Stream class). I created a different Class (DiskStream) to
275+
// handle absolute paths
276+
//
277+
String folderName = ArduinoInstancePreferences.getLastTemplateFolderName();
278+
cppTemplateFile = DiskStream.openContentStream(project.getName(), "", folderName + "\\sketch.cpp");
279+
hTemplateFile = DiskStream.openContentStream(project.getName(), Include, folderName + "\\sketch.h");
280+
}
281+
//
282+
// add both files to the project
283+
//
284+
ArduinoHelpers.addFileToProject(container, new Path(project.getName() + ".cpp"), cppTemplateFile, monitor);
285+
ArduinoHelpers.addFileToProject(container, new Path(project.getName() + ".h"), hTemplateFile, monitor);
286+
213287
// exclude "Librarie/*/?xample from the build
214288
// ICSourceEntry[] folder;
215289
// folder = configurationDescription.getSourceEntries();

0 commit comments

Comments
 (0)