Skip to content

Commit 7471b83

Browse files
author
jantje
committed
#682 triggering on workspace change eclipse runtinme change and os
change Also: Use board descriptor for upload instead of com port
1 parent a1d38fc commit 7471b83

File tree

5 files changed

+225
-85
lines changed

5 files changed

+225
-85
lines changed

io.sloeber.core/src/io/sloeber/core/InternalBoardDescriptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public TxtFile getTxtFile() {
4040

4141
@Override
4242
public void saveConfiguration() {
43-
saveConfiguration(this.mConfdesc);
43+
saveConfiguration(this.mConfdesc, null);
4444
}
4545

4646
}

io.sloeber.core/src/io/sloeber/core/api/BoardDescriptor.java

+186-49
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.net.URI;
55
import java.util.ArrayList;
6+
import java.util.List;
67
import java.util.Map;
78
import java.util.Map.Entry;
89
import java.util.Set;
@@ -34,44 +35,82 @@
3435
import org.eclipse.core.runtime.NullProgressMonitor;
3536
import org.eclipse.core.runtime.OperationCanceledException;
3637
import org.eclipse.core.runtime.Path;
38+
import org.eclipse.core.runtime.Platform;
3739
import org.eclipse.core.runtime.Status;
3840
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
3941
import org.eclipse.core.runtime.preferences.InstanceScope;
4042

4143
import io.sloeber.core.Activator;
4244
import io.sloeber.core.InternalBoardDescriptor;
4345
import io.sloeber.core.common.Common;
46+
import io.sloeber.core.common.ConfigurationPreferences;
4447
import io.sloeber.core.common.Const;
4548
import io.sloeber.core.tools.Helpers;
4649
import io.sloeber.core.tools.Programmers;
4750
import io.sloeber.core.tools.ShouldHaveBeenInCDT;
4851
import io.sloeber.core.tools.TxtFile;
4952

53+
@SuppressWarnings("nls")
5054
public class BoardDescriptor {
5155

56+
@Override
57+
public String toString() {
58+
return getBoardsFile() + " \"" + getBoardName() + "\" " + getUploadPort(); //$NON-NLS-2$
59+
}
60+
5261
// preference nodes
5362
public static final String NODE_ARDUINO = Activator.NODE_ARDUINO;
54-
/**
55-
*
56-
*/
5763

64+
/*
65+
* This is the basic info contained in the descriptor
66+
*/
5867
private String myUploadPort;
5968
private String myUploadProtocol;
6069
private String myBoardID;
61-
private String myProjectName = new String();
6270
private Map<String, String> myOptions;
71+
72+
/*
73+
* the following data is stored to detect changes that will make the equal
74+
* fail so os changes, workspace changes, eclipse install changes will force
75+
* a update on the stored data
76+
*/
77+
private String myProjectName = new String();
78+
private String myOSName = Platform.getOS();
79+
private String myWorkSpaceLocation = Common.getWorkspaceRoot().toString();
80+
private String myWorkEclipseLocation = ConfigurationPreferences.getEclipseHome().toString();
81+
82+
/*
83+
* Stuff to make things work
84+
*/
6385
private File myBoardsFile;
6486
protected TxtFile myTxtFile;
6587
private ChangeListener myChangeListeners = null;
6688
private static final IEclipsePreferences myStorageNode = InstanceScope.INSTANCE.getNode(NODE_ARDUINO);
6789

68-
private static final String KEY_LAST_USED_BOARD = "Last used Board"; //$NON-NLS-1$
69-
private static final String KEY_LAST_USED_UPLOAD_PORT = "Last Used Upload port"; //$NON-NLS-1$
70-
private static final String KEY_LAST_USED_UPLOAD_PROTOCOL = "last Used upload Protocol"; //$NON-NLS-1$
71-
private static final String KEY_LAST_USED_BOARDS_FILE = "Last used Boards file"; //$NON-NLS-1$
72-
private static final String KEY_LAST_USED_BOARD_MENU_OPTIONS = "last used Board custom option selections"; //$NON-NLS-1$
73-
private static final String MENUSELECTION = Const.ENV_KEY_JANTJE_START + "MENU."; //$NON-NLS-1$
90+
/*
91+
* Some constants
92+
*/
93+
private static final String KEY_LAST_USED_BOARD = "Last used Board";
94+
private static final String KEY_LAST_USED_UPLOAD_PORT = "Last Used Upload port";
95+
private static final String KEY_LAST_USED_UPLOAD_PROTOCOL = "last Used upload Protocol";
96+
private static final String KEY_LAST_USED_BOARDS_FILE = "Last used Boards file";
97+
private static final String KEY_LAST_USED_BOARD_MENU_OPTIONS = "last used Board custom option selections";
98+
private static final String MENUSELECTION = Const.ENV_KEY_JANTJE_START + "MENU.";
99+
private static final String ENV_KEY_JANTJE_UPLOAD_PORT = Const.ENV_KEY_JANTJE_START + "COM_PORT";
100+
private static final String ENV_KEY_JANTJE_BOARD_NAME = Const.ENV_KEY_JANTJE_START + "BOARD_NAME";
101+
private static final String ENV_KEY_JANTJE_PROJECT_NAME = Const.ENV_KEY_JANTJE_START + "PROJECT_NAME";
102+
private static final String ENV_KEY_JANTJE_OS = Const.ENV_KEY_JANTJE_START + "OS_NAME";
103+
private static final String ENV_KEY_JANTJE_WORKSPACE_LOCATION = Const.ENV_KEY_JANTJE_START + "WORKSPACE_LOCATION";
104+
private static final String ENV_KEY_JANTJE_ECLIPSE_LOCATION = Const.ENV_KEY_JANTJE_START + "ECLIPSE_LOCATION";
74105

106+
/**
107+
* Compare 2 descriptors and return true is they are equal. This method
108+
* detects - OS changes - project name changes - moves of workspace -
109+
* changed runtine eclipse install
110+
*
111+
* @param obj
112+
* @return true if equal otherwise false
113+
*/
75114
public boolean equals(BoardDescriptor obj) {
76115
if (!this.getUploadPort().equals(obj.getUploadPort())) {
77116
return false;
@@ -91,13 +130,18 @@ public boolean equals(BoardDescriptor obj) {
91130
if (!this.getProjectName().equals(obj.getProjectName())) {
92131
return false;
93132
}
133+
if (!this.getMyOSName().equals(obj.getMyOSName())) {
134+
return false;
135+
}
136+
if (!this.getMyWorkEclipseLocation().equals(obj.getMyWorkEclipseLocation())) {
137+
return false;
138+
}
139+
if (!this.getMyWorkSpaceLocation().equals(obj.getMyWorkSpaceLocation())) {
140+
return false;
141+
}
94142
return true;
95143
}
96144

97-
public String getProjectName() {
98-
return this.myProjectName;
99-
}
100-
101145
/*
102146
* Create a sketchProject. This class does not really create a sketch
103147
* object. Nor does it look for existing (mapping) sketch projects This
@@ -115,7 +159,6 @@ protected BoardDescriptor() {
115159

116160
}
117161

118-
@SuppressWarnings("nls")
119162
protected BoardDescriptor(ICConfigurationDescription confdesc) {
120163
if (confdesc == null) {
121164
this.myBoardsFile = new File(myStorageNode.get(KEY_LAST_USED_BOARDS_FILE, ""));
@@ -127,14 +170,19 @@ protected BoardDescriptor(ICConfigurationDescription confdesc) {
127170
menuOptionsFromString(myStorageNode.get(KEY_LAST_USED_BOARD_MENU_OPTIONS, new String()));
128171

129172
} else {
130-
this.myUploadPort = Common.getBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_UPLOAD_PORT, "");
173+
this.myUploadPort = Common.getBuildEnvironmentVariable(confdesc, ENV_KEY_JANTJE_UPLOAD_PORT, "");
131174
this.myUploadProtocol = Common.getBuildEnvironmentVariable(confdesc,
132175
Common.get_Jantje_KEY_PROTOCOL(Const.ACTION_UPLOAD), "");
133176
this.myBoardsFile = new File(
134177
Common.getBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_BOARDS_FILE, ""));
135178
this.myBoardID = Common.getBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_BOARD_ID, "");
136-
this.myProjectName = Common.getBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_PROJECT_NAME, "");
179+
this.myProjectName = Common.getBuildEnvironmentVariable(confdesc, ENV_KEY_JANTJE_PROJECT_NAME, "");
137180
this.myTxtFile = new TxtFile(this.myBoardsFile);
181+
this.myOSName = Common.getBuildEnvironmentVariable(confdesc, ENV_KEY_JANTJE_OS, "");
182+
this.myWorkSpaceLocation = Common.getBuildEnvironmentVariable(confdesc, ENV_KEY_JANTJE_WORKSPACE_LOCATION,
183+
"");
184+
this.myWorkEclipseLocation = Common.getBuildEnvironmentVariable(confdesc, ENV_KEY_JANTJE_ECLIPSE_LOCATION,
185+
"");
138186

139187
this.myOptions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
140188
IEnvironmentVariableManager envManager = CCorePlugin.getDefault().getBuildEnvironmentManager();
@@ -152,14 +200,53 @@ public static BoardDescriptor makeBoardDescriptor(File boardsFile, String boardI
152200
return new InternalBoardDescriptor(boardsFile, boardID, options);
153201
}
154202

203+
/**
204+
* make a board descriptor for each board in the board.txt file with the
205+
* default options
206+
*
207+
* @param boardFile
208+
* @return a list of board descriptors
209+
*/
210+
public static List<BoardDescriptor> makeBoardDescriptors(File boardFile) {
211+
TxtFile txtFile = new TxtFile(boardFile);
212+
List<BoardDescriptor> boards = new ArrayList<>();
213+
for (String curboardName : txtFile.getAllNames()) {
214+
Map<String, String> boardSection = txtFile.getSection(curboardName);
215+
if (!"true".equalsIgnoreCase(boardSection.get("hide"))) {
216+
boards.add(makeBoardDescriptor(boardFile, txtFile.getBoardIDFromBoardName(curboardName), null));
217+
}
218+
}
219+
return boards;
220+
}
221+
222+
/**
223+
* create a board descriptor
224+
*
225+
* @param boardsFile
226+
* @param boardID
227+
* @param options
228+
* if null default options are taken
229+
*/
155230
protected BoardDescriptor(File boardsFile, String boardID, Map<String, String> options) {
156-
this.myUploadPort = Const.EMPTY_STRING;
231+
this.myUploadPort = new String();
157232
this.myUploadProtocol = Defaults.getDefaultUploadProtocol();
158233
this.myBoardID = boardID;
159234
this.myOptions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
160-
this.myOptions.putAll(options);
161235
this.myBoardsFile = boardsFile;
162236
this.myTxtFile = new TxtFile(this.myBoardsFile);
237+
if (options != null) {
238+
this.myOptions.putAll(options);
239+
} else {
240+
TreeMap<String, String> allOptions = this.myTxtFile.getMenus();
241+
for (Map.Entry<String, String> curoption : allOptions.entrySet()) {
242+
if (!this.myOptions.containsKey(curoption.getKey())) {
243+
String[] menuOptions = this.myTxtFile.getMenuItemIDsFromMenuID(curoption.getKey(), boardID);
244+
if (menuOptions.length > 0) {
245+
this.myOptions.put(curoption.getKey(), menuOptions[0]);
246+
}
247+
}
248+
}
249+
}
163250
}
164251

165252
/**
@@ -177,9 +264,8 @@ public boolean configureProject(IProject project, IProgressMonitor monitor) {
177264
// prjCDesc.setActiveConfiguration(configurationDescription);
178265
CoreModel.getDefault().getProjectDescriptionManager().setProjectDescription(project, prjCDesc, true, null);
179266
} catch (Exception e) {
180-
// TODO Auto-generated catch block
181267
e.printStackTrace();
182-
Common.log(new Status(IStatus.ERROR, io.sloeber.core.Activator.getId(), "failed to save the board settings", //$NON-NLS-1$
268+
Common.log(new Status(IStatus.ERROR, io.sloeber.core.Activator.getId(), "failed to save the board settings",
183269
e));
184270
return false;
185271
}
@@ -235,19 +321,18 @@ public IProject createProject(String projectName, URI projectURI,
235321
ICConfigurationDescription defaultConfigDescription = prjCDesc
236322
.getConfigurationByName(cfgNamesAndTCIds.get(0).configName);
237323

238-
ICResourceDescription cfgd = defaultConfigDescription.getResourceDescription(new Path(Const.EMPTY_STRING),
239-
true);
324+
ICResourceDescription cfgd = defaultConfigDescription.getResourceDescription(new Path(new String()), true);
240325
ICExclusionPatternPathEntry[] entries = cfgd.getConfiguration().getSourceEntries();
241326
if (entries.length == 1) {
242327
Path exclusionPath[] = new Path[8];
243-
exclusionPath[0] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/**/?xamples/**"); //$NON-NLS-1$
244-
exclusionPath[1] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/**/?xtras/**"); //$NON-NLS-1$
245-
exclusionPath[2] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/**/test*/**"); //$NON-NLS-1$
246-
exclusionPath[3] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/**/third-party/**"); //$NON-NLS-1$
247-
exclusionPath[4] = new Path(Const.LIBRARY_PATH_SUFFIX + "**/._*"); //$NON-NLS-1$
248-
exclusionPath[5] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/c*/?*"); //$NON-NLS-1$
249-
exclusionPath[6] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/d*/?*"); //$NON-NLS-1$
250-
exclusionPath[7] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/D*/?*"); //$NON-NLS-1$
328+
exclusionPath[0] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/**/?xamples/**");
329+
exclusionPath[1] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/**/?xtras/**");
330+
exclusionPath[2] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/**/test*/**");
331+
exclusionPath[3] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/**/third-party/**");
332+
exclusionPath[4] = new Path(Const.LIBRARY_PATH_SUFFIX + "**/._*");
333+
exclusionPath[5] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/c*/?*");
334+
exclusionPath[6] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/d*/?*");
335+
exclusionPath[7] = new Path(Const.LIBRARY_PATH_SUFFIX + "/?*/D*/?*");
251336

252337
ICExclusionPatternPathEntry newSourceEntry = new CSourceEntry(entries[0].getFullPath(), exclusionPath,
253338
ICSettingEntry.VALUE_WORKSPACE_PATH);
@@ -275,7 +360,7 @@ public IProject createProject(String projectName, URI projectURI,
275360
}
276361

277362
public void save(ICConfigurationDescription confdesc) throws Exception {
278-
saveConfiguration(confdesc);
363+
saveConfiguration(confdesc, null);
279364
if (confdesc != null) {
280365
IProject project = confdesc.getProjectDescription().getProject();
281366

@@ -289,25 +374,38 @@ public void save(ICConfigurationDescription confdesc) throws Exception {
289374
}
290375

291376
public void saveConfiguration() {
292-
saveConfiguration(null);
377+
saveConfiguration(null, null);
293378
}
294379

295-
public void saveConfiguration(ICConfigurationDescription confdesc) {
296-
if (confdesc != null) {
297-
298-
Common.setBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_PLATFORM_FILE, getPlatformFile());
299-
Common.setBuildEnvironmentVariable(confdesc, "JANTJE.SELECTED.PLATFORM", getPlatformPath().toString()); //$NON-NLS-1$
300-
Common.setBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_BOARD_NAME, getBoardName());
301-
Common.setBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_BOARDS_FILE, getBoardsFile());
302-
Common.setBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_BOARD_ID, this.myBoardID);
303-
Common.setBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_ARCITECTURE_ID, getArchitecture());
304-
Common.setBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_PACKAGE_ID, getPackage());
305-
Common.setBuildEnvironmentVariable(confdesc, Const.ENV_KEY_JANTJE_UPLOAD_PORT, this.myUploadPort);
306-
Common.setBuildEnvironmentVariable(confdesc, Common.get_Jantje_KEY_PROTOCOL(Const.ACTION_UPLOAD),
380+
public void saveConfiguration(ICConfigurationDescription confDesc, IContributedEnvironment contribEnvIn) {
381+
if (confDesc != null) {
382+
IContributedEnvironment contribEnv = contribEnvIn;
383+
if (contribEnv == null) {
384+
IEnvironmentVariableManager envManager = CCorePlugin.getDefault().getBuildEnvironmentManager();
385+
contribEnv = envManager.getContributedEnvironment();
386+
}
387+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, Const.ENV_KEY_JANTJE_PLATFORM_FILE,
388+
getPlatformFile());
389+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, "JANTJE.SELECTED.PLATFORM",
390+
getPlatformPath().toString());
391+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, ENV_KEY_JANTJE_BOARD_NAME, getBoardName());
392+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, Const.ENV_KEY_JANTJE_BOARDS_FILE, getBoardsFile());
393+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, Const.ENV_KEY_JANTJE_BOARD_ID, this.myBoardID);
394+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, Const.ENV_KEY_JANTJE_ARCITECTURE_ID,
395+
getArchitecture());
396+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, Const.ENV_KEY_JANTJE_PACKAGE_ID, getPackage());
397+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, ENV_KEY_JANTJE_UPLOAD_PORT, this.myUploadPort);
398+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, ENV_KEY_JANTJE_PROJECT_NAME, getProjectName());
399+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, ENV_KEY_JANTJE_OS, this.myOSName);
400+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, ENV_KEY_JANTJE_WORKSPACE_LOCATION,
401+
this.myWorkSpaceLocation);
402+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, ENV_KEY_JANTJE_ECLIPSE_LOCATION,
403+
this.myWorkEclipseLocation);
404+
Common.setBuildEnvironmentVariable(confDesc, Common.get_Jantje_KEY_PROTOCOL(Const.ACTION_UPLOAD),
307405
this.myUploadProtocol);
308406
if (this.myOptions != null) {
309407
for (Map.Entry<String, String> curoption : this.myOptions.entrySet()) {
310-
Common.setBuildEnvironmentVariable(confdesc, MENUSELECTION + curoption.getKey(),
408+
Common.setBuildEnvironmentVariable(contribEnv, confDesc, MENUSELECTION + curoption.getKey(),
311409
curoption.getValue());
312410
}
313411
}
@@ -468,28 +566,43 @@ public String getMenuIdFromMenuName(String menuName) {
468566
return this.myTxtFile.getMenuIDFromMenuName(menuName);
469567
}
470568

569+
public String getMenuNameFromMenuID(String id) {
570+
return this.myTxtFile.getMenuNameFromID(id);
571+
}
572+
471573
public String getMenuItemNamedFromMenuItemID(String menuItemID, String menuID) {
472574
return this.myTxtFile.getMenuItemNameFromMenuItemID(this.myBoardID, menuID, menuItemID);
473575
}
474576

577+
/**
578+
* convert the options to a string so it can be stored
579+
*
580+
* @return a string representation of the options
581+
*/
475582
private String menuOptionsToString() {
476583
String ret = new String();
477584
String concat = new String();
478585
if (this.myOptions != null) {
479586
for (Entry<String, String> curOption : this.myOptions.entrySet()) {
480587
ret += concat + curOption.getKey() + '=' + curOption.getValue();
481-
concat = "\n"; //$NON-NLS-1$
588+
concat = "\n";
482589
}
483590
}
484591
return ret;
485592
}
486593

594+
/**
595+
* convert a string to a options so it can be read from a string based
596+
* storage
597+
*
598+
* @param options
599+
*/
487600
private void menuOptionsFromString(String options) {
488601
this.myOptions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
489602
if (options != null) {
490-
String[] lines = options.split("\n"); //$NON-NLS-1$
603+
String[] lines = options.split("\n");
491604
for (String curLine : lines) {
492-
String[] values = curLine.split("=", 2); //$NON-NLS-1$
605+
String[] values = curLine.split("=", 2);
493606
if (values.length == 2) {
494607
this.myOptions.put(values[0], values[1]);
495608
}
@@ -501,4 +614,28 @@ public String getMenuItemIDFromMenuItemName(String menuItemName, String menuID)
501614
return this.myTxtFile.getMenuItemIDFromMenuItemName(this.myBoardID, menuID, menuItemName);
502615
}
503616

617+
public static String getUploadPort(IProject project) {
618+
return Common.getBuildEnvironmentVariable(project, ENV_KEY_JANTJE_UPLOAD_PORT, new String());
619+
}
620+
621+
public static void storeUploadPort(IProject project, String uploadPort) {
622+
Common.setBuildEnvironmentVariable(project, ENV_KEY_JANTJE_UPLOAD_PORT, uploadPort);
623+
}
624+
625+
public String getMyOSName() {
626+
return this.myOSName;
627+
}
628+
629+
public String getMyWorkSpaceLocation() {
630+
return this.myWorkSpaceLocation;
631+
}
632+
633+
public String getMyWorkEclipseLocation() {
634+
return this.myWorkEclipseLocation;
635+
}
636+
637+
public String getProjectName() {
638+
return this.myProjectName;
639+
}
640+
504641
}

0 commit comments

Comments
 (0)