4
4
import static io .sloeber .core .api .Common .*;
5
5
import static io .sloeber .core .api .Const .*;
6
6
import static io .sloeber .autoBuild .api .AutoBuildCommon .*;
7
+ import static io .sloeber .autoBuild .helpers .api .AutoBuildConstants .DOT ;
7
8
8
9
import java .io .BufferedReader ;
9
10
import java .io .File ;
45
46
import io .sloeber .core .txt .TxtFile ;
46
47
47
48
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$
49
50
private static final IEclipsePreferences myStorageNode = InstanceScope .INSTANCE .getNode (NODE_ARDUINO );
50
51
51
52
/*
@@ -65,8 +66,10 @@ public class BoardDescription {
65
66
private IArduinoPlatformVersion myReferencedPlatformVariant = null ;
66
67
private IArduinoPlatformVersion myReferencedPlatformCore = null ;
67
68
private IArduinoPlatformVersion myReferencedPlatformUpload = null ;
69
+ private String myJsonFileName =null ;
70
+ private String myJsonURL =null ;
68
71
69
- private boolean isDirty = true ;
72
+ private boolean myIsDirty = true ;
70
73
71
74
@ Override
72
75
public String toString () {
@@ -290,16 +293,23 @@ public static List<BoardDescription> makeBoardDescriptors(File boardFile) {
290
293
* if null default options are taken
291
294
*/
292
295
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
+ }
293
301
myBoardID = boardID ;
294
302
myUserSelectedBoardsTxtFile = boardsFile ;
295
- mySloeberBoardTxtFile = new BoardTxtFile (resolvePathEnvironmentString (myUserSelectedBoardsTxtFile ));
303
+ mySloeberBoardTxtFile = new BoardTxtFile (expandedBoardsFile );
304
+ getJSonInfo ();
296
305
setDefaultOptions ();
297
306
if (options != null ) {
298
307
myOptions .putAll (options );
299
308
}
300
309
}
301
310
302
- public BoardDescription () {
311
+
312
+ public BoardDescription () {
303
313
myUserSelectedBoardsTxtFile = new File (myStorageNode .get (KEY_LAST_USED_BOARDS_FILE , EMPTY ));
304
314
if (!myUserSelectedBoardsTxtFile .exists ()) {
305
315
@@ -311,25 +321,55 @@ public BoardDescription() {
311
321
}
312
322
myUserSelectedBoardsTxtFile = platform .getBoardsFile ();
313
323
mySloeberBoardTxtFile = new BoardTxtFile (myUserSelectedBoardsTxtFile );
324
+ getJSonInfo (platform );
314
325
315
326
if (mySloeberBoardTxtFile .getAllBoardIDs ().contains (UNO )) {
316
327
myBoardID = UNO ;
317
328
} else {
318
329
myBoardID = mySloeberBoardTxtFile .getAllBoardIDs ().get (0 );
319
330
}
331
+ setDefaultOptions ();
320
332
} else {
321
333
mySloeberBoardTxtFile = new BoardTxtFile (myUserSelectedBoardsTxtFile );
322
334
myBoardID = myStorageNode .get (KEY_LAST_USED_BOARD , EMPTY );
323
335
myUploadPort = myStorageNode .get (KEY_LAST_USED_UPLOAD_PORT , EMPTY );
324
336
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 );
325
339
myOptions = KeyValue .makeMap (myStorageNode .get (KEY_LAST_USED_BOARD_MENU_OPTIONS , EMPTY ));
326
340
}
327
341
328
342
}
329
343
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 ) {
331
369
myUserSelectedBoardsTxtFile = srcObject .myUserSelectedBoardsTxtFile ;
332
370
mySloeberBoardTxtFile = srcObject .mySloeberBoardTxtFile ;
371
+ myJsonFileName =srcObject .myJsonFileName ;
372
+ myJsonURL =srcObject .myJsonURL ;
333
373
myBoardID = srcObject .myBoardID ;
334
374
myUploadPort = srcObject .myUploadPort ;
335
375
myProgrammer = srcObject .myProgrammer ;
@@ -382,20 +422,64 @@ private void removeInvalidMenuIDs() {
382
422
* project
383
423
*/
384
424
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 ));
390
432
}
391
433
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
+ }
395
450
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
+ }
399
483
400
484
public File getReferencingBoardsFile () {
401
485
return myUserSelectedBoardsTxtFile ;
@@ -446,7 +530,7 @@ public void setBoardID(String boardID) {
446
530
}
447
531
448
532
private void setDirty () {
449
- isDirty = true ;
533
+ myIsDirty = true ;
450
534
451
535
}
452
536
@@ -501,9 +585,9 @@ public Map<String, String> getOptions() {
501
585
}
502
586
503
587
private void updateWhenDirty () {
504
- if (isDirty ) {
588
+ if (myIsDirty ) {
505
589
calculateDerivedFields ();
506
- isDirty = false ;
590
+ myIsDirty = false ;
507
591
}
508
592
}
509
593
@@ -690,8 +774,8 @@ protected BoardDescription(File boardsFile, String boardID) {
690
774
myBoardID = boardID ;
691
775
myUserSelectedBoardsTxtFile = boardsFile ;
692
776
mySloeberBoardTxtFile = new BoardTxtFile (myUserSelectedBoardsTxtFile );
693
- setDefaultOptions ();
694
777
calculateDerivedFields ();
778
+ getJSonInfo ();
695
779
}
696
780
697
781
BoardDescription (TxtFile configFile , String prefix ) {
@@ -705,6 +789,10 @@ protected BoardDescription(File boardsFile, String boardID) {
705
789
KeyValueTree optionsTree = section .getChild (KEY_SLOEBER_MENU_SELECTION );
706
790
Map <String , String > options = optionsTree .toKeyValues (EMPTY );
707
791
792
+ KeyValueTree boardSection = tree .getChild (BOARD );
793
+ myJsonFileName = boardSection .getValue (JSON_NAME );
794
+ myJsonURL = boardSection .getValue (JSON_URL );
795
+
708
796
myUserSelectedBoardsTxtFile = resolvePathEnvironmentString (new File (board_txt ));
709
797
mySloeberBoardTxtFile = new BoardTxtFile (myUserSelectedBoardsTxtFile );
710
798
setDefaultOptions ();
@@ -719,32 +807,52 @@ protected BoardDescription(File boardsFile, String boardID) {
719
807
*
720
808
* @param envVars
721
809
*/
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 ());
747
828
}
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
+ // }
748
856
}
749
857
750
858
/**
@@ -755,19 +863,37 @@ public BoardDescription(Map<String, String> envVars) {
755
863
*
756
864
* @return the minimum list of environment variables to recreate the project
757
865
*/
758
- public Map <String , String > getEnvVarsConfig () {
759
- Map <String , String > allVars = new TreeMap <>();
866
+ public void serialize (KeyValueTree keyValueTree ) {
760
867
String board_txt = makePathVersionString (getReferencingBoardsFile ());
868
+ keyValueTree .addChild (KEY_SLOEBER_PROGRAMMER , myProgrammer );
869
+ keyValueTree .addChild (KEY_SLOEBER_UPLOAD_PORT , myUploadPort );
761
870
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 );
766
874
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 );
767
881
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 ());
769
883
}
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;
771
897
}
772
898
773
899
private Map <String , String > onlyKeepValidOptions (Map <String , String > options ) {
@@ -982,9 +1108,11 @@ private Map<String, String> getEnVarPlatformInfo() throws IOException {
982
1108
* @return environment variables pointing to the tools used by the platform
983
1109
* @throws IOException
984
1110
*/
985
- private static Map <String , String > getEnvVarPlatformFileTools (IArduinoPlatformVersion platformVersion ) throws IOException {
1111
+ private Map <String , String > getEnvVarPlatformFileTools (IArduinoPlatformVersion platformVersion ) throws IOException {
986
1112
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 );
988
1116
}
989
1117
File sloeberTxtFile = platformVersion .getInstallPath ().append (SLOEBER_TXT_FILE_NAME ).toFile ();
990
1118
deleteIfOutdated (sloeberTxtFile );
0 commit comments