Skip to content

Commit 34c3798

Browse files
author
jan
committed
Use KeyValueTree for serialisation; add json info to serialisation
1 parent 1fb3cd7 commit 34c3798

9 files changed

+265
-165
lines changed

io.sloeber.core/src/io/sloeber/arduinoFramework/api/BoardDescription.java

+183-55
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static io.sloeber.core.api.Common.*;
55
import static io.sloeber.core.api.Const.*;
66
import static io.sloeber.autoBuild.api.AutoBuildCommon.*;
7+
import static io.sloeber.autoBuild.helpers.api.AutoBuildConstants.DOT;
78

89
import java.io.BufferedReader;
910
import java.io.File;
@@ -45,7 +46,7 @@
4546
import io.sloeber.core.txt.TxtFile;
4647

4748
public class BoardDescription {
48-
private static final String FIRST_SLOEBER_LINE = "#Sloeber created file please do not modify V1.00.test 02 "; //$NON-NLS-1$
49+
private static final String FIRST_SLOEBER_LINE = "#Sloeber created file please do not modify V1.00.test 05 "; //$NON-NLS-1$
4950
private static final IEclipsePreferences myStorageNode = InstanceScope.INSTANCE.getNode(NODE_ARDUINO);
5051

5152
/*
@@ -65,8 +66,10 @@ public class BoardDescription {
6566
private IArduinoPlatformVersion myReferencedPlatformVariant = null;
6667
private IArduinoPlatformVersion myReferencedPlatformCore = null;
6768
private IArduinoPlatformVersion myReferencedPlatformUpload = null;
69+
private String myJsonFileName=null;
70+
private String myJsonURL=null;
6871

69-
private boolean isDirty = true;
72+
private boolean myIsDirty = true;
7073

7174
@Override
7275
public String toString() {
@@ -290,16 +293,23 @@ public static List<BoardDescription> makeBoardDescriptors(File boardFile) {
290293
* if null default options are taken
291294
*/
292295
public BoardDescription(File boardsFile, String boardID, Map<String, String> options) {
296+
File expandedBoardsFile=resolvePathEnvironmentString(boardsFile);
297+
if(!expandedBoardsFile.exists()) {
298+
Activator.log(new Status(IStatus.ERROR,Activator.getId(),"BoardsFile " +boardsFile+" does not exist")); //$NON-NLS-1$//$NON-NLS-2$
299+
return;
300+
}
293301
myBoardID = boardID;
294302
myUserSelectedBoardsTxtFile = boardsFile;
295-
mySloeberBoardTxtFile = new BoardTxtFile(resolvePathEnvironmentString(myUserSelectedBoardsTxtFile));
303+
mySloeberBoardTxtFile = new BoardTxtFile(expandedBoardsFile);
304+
getJSonInfo();
296305
setDefaultOptions();
297306
if (options != null) {
298307
myOptions.putAll(options);
299308
}
300309
}
301310

302-
public BoardDescription() {
311+
312+
public BoardDescription() {
303313
myUserSelectedBoardsTxtFile = new File(myStorageNode.get(KEY_LAST_USED_BOARDS_FILE, EMPTY));
304314
if (!myUserSelectedBoardsTxtFile.exists()) {
305315

@@ -311,25 +321,55 @@ public BoardDescription() {
311321
}
312322
myUserSelectedBoardsTxtFile = platform.getBoardsFile();
313323
mySloeberBoardTxtFile = new BoardTxtFile(myUserSelectedBoardsTxtFile);
324+
getJSonInfo(platform);
314325

315326
if (mySloeberBoardTxtFile.getAllBoardIDs().contains(UNO)) {
316327
myBoardID = UNO;
317328
} else {
318329
myBoardID = mySloeberBoardTxtFile.getAllBoardIDs().get(0);
319330
}
331+
setDefaultOptions();
320332
} else {
321333
mySloeberBoardTxtFile = new BoardTxtFile(myUserSelectedBoardsTxtFile);
322334
myBoardID = myStorageNode.get(KEY_LAST_USED_BOARD, EMPTY);
323335
myUploadPort = myStorageNode.get(KEY_LAST_USED_UPLOAD_PORT, EMPTY);
324336
myProgrammer = myStorageNode.get(KEY_LAST_USED_UPLOAD_PROTOCOL, EMPTY);
337+
myJsonFileName=myStorageNode.get(KEY_LAST_USED_JSON_FILENAME, EMPTY);
338+
myJsonURL=myStorageNode.get(KEY_LAST_USED_JSON_URL, EMPTY);
325339
myOptions = KeyValue.makeMap(myStorageNode.get(KEY_LAST_USED_BOARD_MENU_OPTIONS, EMPTY));
326340
}
327341

328342
}
329343

330-
public BoardDescription(BoardDescription srcObject) {
344+
private void getJSonInfo(IArduinoPlatformVersion platform) {
345+
IArduinoPlatformPackageIndex packageIndex=platform.getParent().getParent().getPackageIndex();
346+
myJsonFileName=packageIndex.getJsonFile().getName();
347+
myJsonURL=packageIndex.getJsonURL();
348+
}
349+
350+
351+
private void getJSonInfo() {
352+
IArduinoPlatformVersion platform = BoardsManager.getNewestInstalledPlatform(getVendor(), getArchitecture());
353+
if (platform == null) {
354+
platform = BoardsManager.getNewestInstalledPlatform(VENDOR_ARDUINO, AVR);
355+
}
356+
if (platform == null) {
357+
List<IArduinoPlatformVersion> platforms = BoardsManager.getInstalledPlatforms();
358+
// If you crash on the next line no platform have been installed
359+
platform = platforms.get(0);
360+
}
361+
if (platform != null) {
362+
myUserSelectedBoardsTxtFile = platform.getBoardsFile();
363+
mySloeberBoardTxtFile = new BoardTxtFile(myUserSelectedBoardsTxtFile);
364+
getJSonInfo(platform);
365+
}
366+
}
367+
368+
public BoardDescription(BoardDescription srcObject) {
331369
myUserSelectedBoardsTxtFile = srcObject.myUserSelectedBoardsTxtFile;
332370
mySloeberBoardTxtFile = srcObject.mySloeberBoardTxtFile;
371+
myJsonFileName=srcObject.myJsonFileName;
372+
myJsonURL=srcObject.myJsonURL;
333373
myBoardID = srcObject.myBoardID;
334374
myUploadPort = srcObject.myUploadPort;
335375
myProgrammer = srcObject.myProgrammer;
@@ -382,20 +422,64 @@ private void removeInvalidMenuIDs() {
382422
* project
383423
*/
384424
public void saveUserSelection() {
385-
myStorageNode.put(KEY_LAST_USED_BOARDS_FILE, getReferencingBoardsFile().toString());
386-
myStorageNode.put(KEY_LAST_USED_BOARD, this.myBoardID);
387-
myStorageNode.put(KEY_LAST_USED_UPLOAD_PORT, this.myUploadPort);
388-
myStorageNode.put(KEY_LAST_USED_UPLOAD_PROTOCOL, this.myProgrammer);
389-
myStorageNode.put(KEY_LAST_USED_BOARD_MENU_OPTIONS, KeyValue.makeString(this.myOptions));
425+
myStorageNode.put(KEY_LAST_USED_BOARDS_FILE, myUserSelectedBoardsTxtFile.toString());
426+
myStorageNode.put(KEY_LAST_USED_BOARD, myBoardID);
427+
myStorageNode.put(KEY_LAST_USED_UPLOAD_PORT, myUploadPort);
428+
myStorageNode.put(KEY_LAST_USED_UPLOAD_PROTOCOL, myProgrammer);
429+
myStorageNode.put(KEY_LAST_USED_JSON_FILENAME, myJsonFileName);
430+
myStorageNode.put(KEY_LAST_USED_JSON_URL, myJsonURL);
431+
myStorageNode.put(KEY_LAST_USED_BOARD_MENU_OPTIONS, KeyValue.makeString(myOptions));
390432
}
391433

392-
public String getArchitecture() {
393-
return mySloeberBoardTxtFile.getArchitecture();
394-
}
434+
/*
435+
* Returns the architecture based on the myUserSelectedBoardsTxtFile file name
436+
* Caters for the packages (with version number and for the old way if the boards
437+
* file does not exists returns avr
438+
*/
439+
public String getArchitecture() {
440+
if (myUserSelectedBoardsTxtFile == null) {
441+
return AVR;
442+
}
443+
IPath platformFile = new Path(myUserSelectedBoardsTxtFile.toString().trim());
444+
int index=hardwareSegmentIndex(platformFile);
445+
if(index<0){
446+
return AVR;
447+
}
448+
return platformFile.segment(index+3);
449+
}
395450

396-
public String getVendor() {
397-
return mySloeberBoardTxtFile.getVendor();
398-
}
451+
/*
452+
* Returns the vendor based on the myUserSelectedBoardsTxtFile file name
453+
* Caters for the packages (with version number and for the old way if the boards
454+
* file does not exists returns VENDOR_ARDUINO
455+
*/
456+
public String getVendor() {
457+
if (myUserSelectedBoardsTxtFile == null) {
458+
return VENDOR_ARDUINO;
459+
}
460+
IPath platformFile = new Path(myUserSelectedBoardsTxtFile.toString().trim());
461+
int index=hardwareSegmentIndex(platformFile);
462+
if(index<1){
463+
return VENDOR_ARDUINO;
464+
}
465+
return platformFile.segment(index+1);
466+
}
467+
468+
/**
469+
* return the segment that contains the name packages
470+
* or -1 if no such segment is found
471+
* @param platformFile
472+
* @return
473+
*/
474+
private static int hardwareSegmentIndex(IPath platformFile) {
475+
for(int i=0;i<platformFile.segmentCount();i++) {
476+
if(PACKAGES_FOLDER_NAME.equals( platformFile.segment(i))){
477+
return i;
478+
}
479+
}
480+
return -1;
481+
482+
}
399483

400484
public File getReferencingBoardsFile() {
401485
return myUserSelectedBoardsTxtFile;
@@ -446,7 +530,7 @@ public void setBoardID(String boardID) {
446530
}
447531

448532
private void setDirty() {
449-
isDirty = true;
533+
myIsDirty = true;
450534

451535
}
452536

@@ -501,9 +585,9 @@ public Map<String, String> getOptions() {
501585
}
502586

503587
private void updateWhenDirty() {
504-
if (isDirty) {
588+
if (myIsDirty) {
505589
calculateDerivedFields();
506-
isDirty = false;
590+
myIsDirty = false;
507591
}
508592
}
509593

@@ -690,8 +774,8 @@ protected BoardDescription(File boardsFile, String boardID) {
690774
myBoardID = boardID;
691775
myUserSelectedBoardsTxtFile = boardsFile;
692776
mySloeberBoardTxtFile = new BoardTxtFile(myUserSelectedBoardsTxtFile);
693-
setDefaultOptions();
694777
calculateDerivedFields();
778+
getJSonInfo();
695779
}
696780

697781
BoardDescription(TxtFile configFile, String prefix) {
@@ -705,6 +789,10 @@ protected BoardDescription(File boardsFile, String boardID) {
705789
KeyValueTree optionsTree = section.getChild(KEY_SLOEBER_MENU_SELECTION);
706790
Map<String, String> options = optionsTree.toKeyValues(EMPTY);
707791

792+
KeyValueTree boardSection = tree.getChild(BOARD);
793+
myJsonFileName = boardSection.getValue(JSON_NAME);
794+
myJsonURL = boardSection.getValue(JSON_URL);
795+
708796
myUserSelectedBoardsTxtFile = resolvePathEnvironmentString(new File(board_txt));
709797
mySloeberBoardTxtFile = new BoardTxtFile(myUserSelectedBoardsTxtFile);
710798
setDefaultOptions();
@@ -719,32 +807,52 @@ protected BoardDescription(File boardsFile, String boardID) {
719807
*
720808
* @param envVars
721809
*/
722-
public BoardDescription(Map<String, String> envVars) {
723-
int menuKeyLength = KEY_SLOEBER_MENU_SELECTION.length() + DOT.length();
724-
for (Entry<String, String> curEnvVar : envVars.entrySet()) {
725-
String key = curEnvVar.getKey();
726-
String value = curEnvVar.getValue();
727-
switch (key) {
728-
case KEY_SLOEBER_PROGRAMMER:
729-
myProgrammer = value;
730-
break;
731-
case KEY_SLOEBER_BOARD_ID:
732-
myBoardID = value;
733-
break;
734-
case KEY_SLOEBER_BOARD_TXT:
735-
myUserSelectedBoardsTxtFile = resolvePathEnvironmentString(new File(value));
736-
mySloeberBoardTxtFile = new BoardTxtFile(myUserSelectedBoardsTxtFile);
737-
break;
738-
case KEY_SLOEBER_UPLOAD_PORT:
739-
myUploadPort = value;
740-
break;
741-
default:
742-
if (key.startsWith(KEY_SLOEBER_MENU_SELECTION + DOT)) {
743-
String cleanKey = key.substring(menuKeyLength);
744-
myOptions.put(cleanKey, value);
745-
}
746-
}
810+
public BoardDescription(KeyValueTree keyValues) {
811+
myProgrammer=keyValues.getValue(KEY_SLOEBER_PROGRAMMER);
812+
myUploadPort=keyValues.getValue(KEY_SLOEBER_UPLOAD_PORT);
813+
814+
KeyValueTree boardvalueTree=keyValues.getChild(KEY_BOARD);
815+
myBoardID=boardvalueTree.getValue(KEY_SLOEBER_BOARD_ID);
816+
String txtFile=boardvalueTree.getValue(KEY_SLOEBER_BOARD_TXT);
817+
818+
KeyValueTree jSonvalueTree=keyValues.getChild(KEY_JSON);
819+
myJsonFileName=jSonvalueTree.getValue(KEY_JSON_FILENAME);
820+
myJsonURL=jSonvalueTree.getValue(KEY_JSON_URL);
821+
822+
823+
myUserSelectedBoardsTxtFile = resolvePathEnvironmentString(new File(txtFile));
824+
mySloeberBoardTxtFile = new BoardTxtFile(myUserSelectedBoardsTxtFile);
825+
KeyValueTree options=keyValues.getChild(KEY_SLOEBER_MENU_SELECTION);
826+
for(KeyValueTree curOption: options.getChildren().values()) {
827+
myOptions.put(curOption.getKey(), curOption.getValue());
747828
}
829+
830+
831+
// int menuKeyLength = KEY_SLOEBER_MENU_SELECTION.length() + DOT.length();
832+
// for (Entry<String, String> curEnvVar : envVars.entrySet()) {
833+
// String key = curEnvVar.getKey();
834+
// String value = curEnvVar.getValue();
835+
// switch (key) {
836+
// case KEY_SLOEBER_PROGRAMMER:
837+
// myProgrammer = value;
838+
// break;
839+
// case KEY_SLOEBER_BOARD_ID:
840+
// myBoardID = value;
841+
// break;
842+
// case KEY_SLOEBER_BOARD_TXT:
843+
// myUserSelectedBoardsTxtFile = resolvePathEnvironmentString(new File(value));
844+
// mySloeberBoardTxtFile = new BoardTxtFile(myUserSelectedBoardsTxtFile);
845+
// break;
846+
// case KEY_SLOEBER_UPLOAD_PORT:
847+
// myUploadPort = value;
848+
// break;
849+
// default:
850+
// if (key.startsWith(KEY_SLOEBER_MENU_SELECTION + DOT)) {
851+
// String cleanKey = key.substring(menuKeyLength);
852+
// myOptions.put(cleanKey, value);
853+
// }
854+
// }
855+
// }
748856
}
749857

750858
/**
@@ -755,19 +863,37 @@ public BoardDescription(Map<String, String> envVars) {
755863
*
756864
* @return the minimum list of environment variables to recreate the project
757865
*/
758-
public Map<String, String> getEnvVarsConfig() {
759-
Map<String, String> allVars = new TreeMap<>();
866+
public void serialize(KeyValueTree keyValueTree) {
760867
String board_txt = makePathVersionString(getReferencingBoardsFile());
868+
keyValueTree.addChild(KEY_SLOEBER_PROGRAMMER, myProgrammer);
869+
keyValueTree.addChild(KEY_SLOEBER_UPLOAD_PORT, myUploadPort);
761870

762-
allVars.put(KEY_SLOEBER_PROGRAMMER, myProgrammer);
763-
allVars.put(KEY_SLOEBER_BOARD_ID, myBoardID);
764-
allVars.put(KEY_SLOEBER_BOARD_TXT, board_txt);
765-
allVars.put(KEY_SLOEBER_UPLOAD_PORT, myUploadPort);
871+
KeyValueTree boardvalueTree=keyValueTree.addChild(KEY_BOARD);
872+
boardvalueTree.addChild(KEY_SLOEBER_BOARD_ID, myBoardID);
873+
boardvalueTree.addChild(KEY_SLOEBER_BOARD_TXT, board_txt);
766874

875+
KeyValueTree jSonvalueTree=keyValueTree.addChild(KEY_JSON);
876+
jSonvalueTree.addChild(KEY_JSON_FILENAME, myJsonFileName);
877+
jSonvalueTree.addChild(KEY_JSON_URL, myJsonURL);
878+
879+
880+
KeyValueTree menuvalueTree=keyValueTree.addChild(KEY_SLOEBER_MENU_SELECTION);
767881
for (Entry<String, String> curOption : myOptions.entrySet()) {
768-
allVars.put(KEY_SLOEBER_MENU_SELECTION + DOT + curOption.getKey(), curOption.getValue());
882+
menuvalueTree.addValue( curOption.getKey(), curOption.getValue());
769883
}
770-
return allVars;
884+
885+
// allVars.put(KEY_SLOEBER_PROGRAMMER, myProgrammer);
886+
// allVars.put(KEY_SLOEBER_BOARD_ID, myBoardID);
887+
// allVars.put(KEY_SLOEBER_BOARD_TXT, board_txt);
888+
// allVars.put(KEY_SLOEBER_UPLOAD_PORT, myUploadPort);
889+
//
890+
// allVars.put(KEY_JSON_FILENAME, myJsonFileName);
891+
// allVars.put(KEY_JSON_URL, myJsonURL);
892+
//
893+
// for (Entry<String, String> curOption : myOptions.entrySet()) {
894+
// allVars.put(KEY_SLOEBER_MENU_SELECTION + DOT + curOption.getKey(), curOption.getValue());
895+
// }
896+
// return allVars;
771897
}
772898

773899
private Map<String, String> onlyKeepValidOptions(Map<String, String> options) {
@@ -982,9 +1108,11 @@ private Map<String, String> getEnVarPlatformInfo() throws IOException {
9821108
* @return environment variables pointing to the tools used by the platform
9831109
* @throws IOException
9841110
*/
985-
private static Map<String, String> getEnvVarPlatformFileTools(IArduinoPlatformVersion platformVersion) throws IOException {
1111+
private Map<String, String> getEnvVarPlatformFileTools(IArduinoPlatformVersion platformVersion) throws IOException {
9861112
if(platformVersion==null) {
987-
return new HashMap<>();
1113+
Path path=new Path(myUserSelectedBoardsTxtFile.toString());
1114+
File sloeberTxtFile= path.removeLastSegments(1).append(SLOEBER_TXT_FILE_NAME).toFile();
1115+
return getEnvVarPlatformFileTools(sloeberTxtFile);
9881116
}
9891117
File sloeberTxtFile = platformVersion.getInstallPath().append(SLOEBER_TXT_FILE_NAME).toFile();
9901118
deleteIfOutdated (sloeberTxtFile);

io.sloeber.core/src/io/sloeber/arduinoFramework/api/BoardsManager.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ static private void loadJson(String url, boolean forceDownload) {
458458
}
459459
if (jsonFile.exists()) {
460460
if (jsonFile.getName().toLowerCase().startsWith("package_")) { //$NON-NLS-1$
461-
loadPackage(jsonFile);
461+
loadPackage(url,jsonFile);
462462
} else if (jsonFile.getName().toLowerCase().startsWith("library_")) { //$NON-NLS-1$
463463
LibraryManager.loadJson(jsonFile);
464464
} else {
@@ -468,10 +468,11 @@ static private void loadJson(String url, boolean forceDownload) {
468468
}
469469
}
470470

471-
static private void loadPackage(File jsonFile) {
471+
static private void loadPackage(String url, File jsonFile) {
472472
try (Reader reader = new FileReader(jsonFile)) {
473473
ArduinoPlatformPackageIndex index = new Gson().fromJson(reader, ArduinoPlatformPackageIndex.class);
474474
index.setPackageFile(jsonFile);
475+
index.setURL(url);
475476
packageIndices.add(index);
476477
} catch (Exception e) {
477478
Activator.log(new Status(IStatus.ERROR, Activator.getId(),

io.sloeber.core/src/io/sloeber/arduinoFramework/api/IArduinoPlatformPackageIndex.java

+2
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ public interface IArduinoPlatformPackageIndex {
1818

1919
String getName();
2020

21+
String getJsonURL();
22+
2123
}

0 commit comments

Comments
 (0)