5
5
import static io .sloeber .core .api .Const .*;
6
6
import static io .sloeber .autoBuild .api .AutoBuildCommon .*;
7
7
8
+ import java .io .BufferedReader ;
8
9
import java .io .File ;
10
+ import java .io .FileReader ;
9
11
import java .io .IOException ;
12
+ import java .nio .file .Files ;
13
+ import java .nio .file .StandardOpenOption ;
10
14
import java .util .ArrayList ;
11
15
import java .util .Collections ;
12
16
import java .util .HashMap ;
29
33
import io .sloeber .arduinoFramework .internal .ArduinoPlatformTooldDependency ;
30
34
import io .sloeber .autoBuild .api .IAutoBuildConfigurationDescription ;
31
35
import io .sloeber .autoBuild .helpers .api .KeyValueTree ;
32
- import io .sloeber .core .api . Common ;
36
+ import io .sloeber .core .Activator ;
33
37
import io .sloeber .core .api .ConfigurationPreferences ;
34
38
import io .sloeber .core .api .Const ;
35
39
import io .sloeber .core .api .Preferences ;
41
45
import io .sloeber .core .txt .TxtFile ;
42
46
43
47
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$
44
49
private static final IEclipsePreferences myStorageNode = InstanceScope .INSTANCE .getNode (NODE_ARDUINO );
45
50
46
51
/*
@@ -165,7 +170,7 @@ private void ParseSection() {
165
170
myBoardsCore = valueSplit [1 ];
166
171
myReferencedPlatformCore = BoardsManager .getNewestInstalledPlatform (refVendor , architecture );
167
172
if (myReferencedPlatformCore == null ) {
168
- Common .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
173
+ Activator .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
169
174
Helpers_tool_reference_missing .replace (TOOL_TAG , core )
170
175
.replace (FILE_TAG , getReferencingBoardsFile ().toString ())
171
176
.replace (BOARD_TAG , getBoardID ())));
@@ -178,7 +183,7 @@ private void ParseSection() {
178
183
myBoardsCore = valueSplit [3 ];
179
184
myReferencedPlatformCore = BoardsManager .getPlatform (refVendor , refArchitecture , refVersion );
180
185
if (myReferencedPlatformCore == null ) {
181
- Common .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
186
+ Activator .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
182
187
Helpers_tool_reference_missing .replace (TOOL_TAG , core )
183
188
.replace (FILE_TAG , getReferencingBoardsFile ().toString ())
184
189
.replace (BOARD_TAG , getBoardID ())));
@@ -195,7 +200,7 @@ private void ParseSection() {
195
200
myBoardsVariant = valueSplit [1 ];
196
201
myReferencedPlatformVariant = BoardsManager .getNewestInstalledPlatform (refVendor , architecture );
197
202
if (myReferencedPlatformVariant == null ) {
198
- Common .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
203
+ Activator .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
199
204
Helpers_tool_reference_missing .replace (TOOL_TAG , variant )
200
205
.replace (FILE_TAG , getReferencingBoardsFile ().toString ())
201
206
.replace (BOARD_TAG , getBoardID ())));
@@ -212,7 +217,7 @@ private void ParseSection() {
212
217
myReferencedPlatformVariant = BoardsManager .getPlatform (refVendor , refArchitecture , refVersion );
213
218
}
214
219
if (myReferencedPlatformVariant == null ) {
215
- Common .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
220
+ Activator .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
216
221
Helpers_tool_reference_missing .replace (TOOL_TAG , variant )
217
222
.replace (FILE_TAG , getReferencingBoardsFile ().toString ())
218
223
.replace (BOARD_TAG , getBoardID ())));
@@ -229,7 +234,7 @@ private void ParseSection() {
229
234
myUploadTool = valueSplit [1 ];
230
235
myReferencedPlatformUpload = BoardsManager .getNewestInstalledPlatform (refVendor , architecture );
231
236
if (myReferencedPlatformUpload == null ) {
232
- Common .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
237
+ Activator .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
233
238
Helpers_tool_reference_missing .replace (TOOL_TAG , upload )
234
239
.replace (FILE_TAG , getReferencingBoardsFile ().toString ())
235
240
.replace (BOARD_TAG , getBoardID ())));
@@ -242,7 +247,7 @@ private void ParseSection() {
242
247
myUploadTool = valueSplit [3 ];
243
248
myReferencedPlatformUpload = BoardsManager .getPlatform (refVendor , refArchitecture , refVersion );
244
249
if (this .myReferencedPlatformUpload == null ) {
245
- Common .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
250
+ Activator .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,
246
251
Helpers_tool_reference_missing .replace (TOOL_TAG , upload )
247
252
.replace (FILE_TAG , getReferencingBoardsFile ().toString ())
248
253
.replace (BOARD_TAG , getBoardID ())));
@@ -879,7 +884,12 @@ public Map<String, String> getEnvVars() {
879
884
}
880
885
881
886
// put in the installed tools info
882
- allVars .putAll (getEnVarPlatformInfo ());
887
+ try {
888
+ allVars .putAll (getEnVarPlatformInfo ());
889
+ } catch (IOException e ) {
890
+ // TODO Auto-generated catch block
891
+ Activator .log (new Status (IStatus .ERROR , CORE_PLUGIN_ID ,"failed to get platform paths" ,e ));
892
+ }
883
893
884
894
// boards settings not coming from menu selections
885
895
allVars .putAll (mySloeberBoardTxtFile .getBoardEnvironVars (getBoardID ()));
@@ -926,37 +936,28 @@ private String getBoardFQBN() {
926
936
return fqbn +options ;
927
937
}
928
938
929
- private Map <String , String > getEnVarPlatformInfo () {
939
+ private Map <String , String > getEnVarPlatformInfo () throws IOException {
930
940
Map <String , String > ret = new HashMap <>();
931
941
932
- if (myReferencedPlatformUpload != null ) {
933
942
ret .putAll (getEnvVarPlatformFileTools (myReferencedPlatformUpload ));
934
- }
935
- if (myReferencedPlatformVariant != null ) {
936
943
ret .putAll (getEnvVarPlatformFileTools (myReferencedPlatformVariant ));
937
- }
938
-
939
- if (myReferencedPlatformCore != null ) {
940
944
ret .putAll (getEnvVarPlatformFileTools (myReferencedPlatformCore ));
941
- }
945
+
946
+ IArduinoPlatformVersion latestArduinoPlatform = BoardsManager .getNewestInstalledPlatform (Const .VENDOR_ARDUINO ,
947
+ getArchitecture ());
948
+ ret .putAll (getEnvVarPlatformFileTools (latestArduinoPlatform ));
942
949
943
950
IPath referencingPlatformPath = getreferencingPlatformPath ();
944
951
IArduinoPlatformVersion referencingPlatform = BoardsManager .getPlatform (referencingPlatformPath );
945
-
946
- if (referencingPlatform == null ) {
947
- // This is the case for private hardware
948
- //there is no need to specify tool path as they do not use them
949
- return ret ;
950
- }
951
- IArduinoPlatformVersion latestArduinoPlatform = BoardsManager .getNewestInstalledPlatform (Const .VENDOR_ARDUINO ,
952
- referencingPlatform .getArchitecture ());
953
- if (latestArduinoPlatform != null ) {
954
- ret .putAll (getEnvVarPlatformFileTools (latestArduinoPlatform ));
952
+ if (referencingPlatform ==null ) {
953
+ ret .putAll (getEnvVarPlatformFileTools (referencingPlatformPath .toFile ()));
954
+ }else {
955
+ ret .putAll (getEnvVarPlatformFileTools (referencingPlatform ));
955
956
}
956
957
958
+
957
959
if (myReferencedPlatformCore == null ) {
958
960
//there is no referenced core so no need to do smart stuff
959
- ret .putAll (getEnvVarPlatformFileTools (referencingPlatform ));
960
961
return ret ;
961
962
}
962
963
@@ -967,7 +968,6 @@ private Map<String, String> getEnVarPlatformInfo() {
967
968
return ret ;
968
969
}
969
970
// standard arduino IDE way
970
- ret .putAll (getEnvVarPlatformFileTools (referencingPlatform ));
971
971
ret .putAll (getEnvVarPlatformFileTools (myReferencedPlatformCore ));
972
972
return ret ;
973
973
@@ -980,23 +980,64 @@ private Map<String, String> getEnVarPlatformInfo() {
980
980
*
981
981
* @param platformVersion
982
982
* @return environment variables pointing to the tools used by the platform
983
+ * @throws IOException
983
984
*/
984
- private static Map <String , String > getEnvVarPlatformFileTools (IArduinoPlatformVersion platformVersion ) {
985
- HashMap <String , String > vars = new HashMap <>();
986
- for (ArduinoPlatformTooldDependency tool : platformVersion .getToolsDependencies ()) {
987
- IPath installPath = tool .getInstallPath ();
988
- if (installPath .toFile ().exists ()) {
989
- String value = installPath .toOSString ();
990
- String keyString = ENV_KEY_RUNTIME_TOOLS + tool .getName () + tool .getVersion () + DOT_PATH ;
991
- vars .put (keyString , value );
992
- keyString = ENV_KEY_RUNTIME_TOOLS + tool .getName () + '-' + tool .getVersion () + DOT_PATH ;
993
- vars .put (keyString , value );
994
- keyString = ENV_KEY_RUNTIME_TOOLS + tool .getName () + DOT_PATH ;
995
- vars .put (keyString , value );
996
- }
997
- }
998
- return vars ;
999
- }
985
+ private static Map <String , String > getEnvVarPlatformFileTools (IArduinoPlatformVersion platformVersion ) throws IOException {
986
+ if (platformVersion ==null ) {
987
+ return new HashMap <>();
988
+ }
989
+ File sloeberTxtFile = platformVersion .getInstallPath ().append (SLOEBER_TXT_FILE_NAME ).toFile ();
990
+ deleteIfOutdated (sloeberTxtFile );
991
+
992
+ if (!sloeberTxtFile .exists ()) {
993
+
994
+ String vars = FIRST_SLOEBER_LINE +NEWLINE ;
995
+ for (ArduinoPlatformTooldDependency tool : platformVersion .getToolsDependencies ()) {
996
+ IPath installPath = tool .getInstallPath ();
997
+ if (installPath .toFile ().exists ()) {
998
+ String value = installPath .toOSString ();
999
+ String keyString = ENV_KEY_RUNTIME_TOOLS + tool .getName () + tool .getVersion () + DOT_PATH ;
1000
+ vars =vars +NEWLINE +keyString +EQUAL + value ;
1001
+ keyString = ENV_KEY_RUNTIME_TOOLS + tool .getName () + '-' + tool .getVersion () + DOT_PATH ;
1002
+ vars =vars +NEWLINE +keyString +EQUAL + value ;
1003
+ keyString = ENV_KEY_RUNTIME_TOOLS + tool .getName () + DOT_PATH ;
1004
+ vars =vars +NEWLINE +keyString +EQUAL + value ;
1005
+ }
1006
+ }
1007
+ Files .write (sloeberTxtFile .toPath (), vars .getBytes (), StandardOpenOption .TRUNCATE_EXISTING ,
1008
+ StandardOpenOption .CREATE );
1009
+ }
1010
+
1011
+ return getEnvVarPlatformFileTools (sloeberTxtFile );
1012
+ }
1013
+
1014
+ private static Map <String , String > getEnvVarPlatformFileTools (File sloeberTxtFile ) {
1015
+ if (sloeberTxtFile ==null || (!sloeberTxtFile .exists ())) {
1016
+ return new HashMap <>();
1017
+ }
1018
+ TxtFile sloeberTxt = new TxtFile (sloeberTxtFile );
1019
+ return sloeberTxt .getAllEnvironVars (EMPTY );
1020
+ }
1021
+
1022
+ /**
1023
+ * If the sloeber.txt variant exists delete it if it is outdated
1024
+ *
1025
+ * @param tmpFile
1026
+ */
1027
+ private static void deleteIfOutdated (File tmpFile ) {
1028
+ if (tmpFile .exists ()) {
1029
+ // delete if outdated
1030
+ String firstLine = null ;
1031
+ try (BufferedReader Buff = new BufferedReader (new FileReader (tmpFile ));) {
1032
+ firstLine = Buff .readLine ().trim ();
1033
+ } catch (@ SuppressWarnings ("unused" ) Exception e ) {
1034
+ // ignore and delete the file
1035
+ }
1036
+ if (!FIRST_SLOEBER_LINE .trim ().equals (firstLine )) {
1037
+ tmpFile .delete ();
1038
+ }
1039
+ }
1040
+ }
1000
1041
1001
1042
/**
1002
1043
* Following post processing is done
0 commit comments