Skip to content

Commit 4c315c2

Browse files
author
jan
committed
create sloeber.txt file based on json if not present #1676
1 parent 4c3b6b0 commit 4c315c2

File tree

2 files changed

+86
-44
lines changed

2 files changed

+86
-44
lines changed

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

+85-44
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
import static io.sloeber.core.api.Const.*;
66
import static io.sloeber.autoBuild.api.AutoBuildCommon.*;
77

8+
import java.io.BufferedReader;
89
import java.io.File;
10+
import java.io.FileReader;
911
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.nio.file.StandardOpenOption;
1014
import java.util.ArrayList;
1115
import java.util.Collections;
1216
import java.util.HashMap;
@@ -29,7 +33,7 @@
2933
import io.sloeber.arduinoFramework.internal.ArduinoPlatformTooldDependency;
3034
import io.sloeber.autoBuild.api.IAutoBuildConfigurationDescription;
3135
import io.sloeber.autoBuild.helpers.api.KeyValueTree;
32-
import io.sloeber.core.api.Common;
36+
import io.sloeber.core.Activator;
3337
import io.sloeber.core.api.ConfigurationPreferences;
3438
import io.sloeber.core.api.Const;
3539
import io.sloeber.core.api.Preferences;
@@ -41,6 +45,7 @@
4145
import io.sloeber.core.txt.TxtFile;
4246

4347
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$
4449
private static final IEclipsePreferences myStorageNode = InstanceScope.INSTANCE.getNode(NODE_ARDUINO);
4550

4651
/*
@@ -165,7 +170,7 @@ private void ParseSection() {
165170
myBoardsCore = valueSplit[1];
166171
myReferencedPlatformCore = BoardsManager.getNewestInstalledPlatform(refVendor, architecture);
167172
if (myReferencedPlatformCore == null) {
168-
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
173+
Activator.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
169174
Helpers_tool_reference_missing.replace(TOOL_TAG, core)
170175
.replace(FILE_TAG, getReferencingBoardsFile().toString())
171176
.replace(BOARD_TAG, getBoardID())));
@@ -178,7 +183,7 @@ private void ParseSection() {
178183
myBoardsCore = valueSplit[3];
179184
myReferencedPlatformCore = BoardsManager.getPlatform(refVendor, refArchitecture, refVersion);
180185
if (myReferencedPlatformCore == null) {
181-
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
186+
Activator.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
182187
Helpers_tool_reference_missing.replace(TOOL_TAG, core)
183188
.replace(FILE_TAG, getReferencingBoardsFile().toString())
184189
.replace(BOARD_TAG, getBoardID())));
@@ -195,7 +200,7 @@ private void ParseSection() {
195200
myBoardsVariant = valueSplit[1];
196201
myReferencedPlatformVariant = BoardsManager.getNewestInstalledPlatform(refVendor, architecture);
197202
if (myReferencedPlatformVariant == null) {
198-
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
203+
Activator.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
199204
Helpers_tool_reference_missing.replace(TOOL_TAG, variant)
200205
.replace(FILE_TAG, getReferencingBoardsFile().toString())
201206
.replace(BOARD_TAG, getBoardID())));
@@ -212,7 +217,7 @@ private void ParseSection() {
212217
myReferencedPlatformVariant = BoardsManager.getPlatform(refVendor, refArchitecture, refVersion);
213218
}
214219
if (myReferencedPlatformVariant == null) {
215-
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
220+
Activator.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
216221
Helpers_tool_reference_missing.replace(TOOL_TAG, variant)
217222
.replace(FILE_TAG, getReferencingBoardsFile().toString())
218223
.replace(BOARD_TAG, getBoardID())));
@@ -229,7 +234,7 @@ private void ParseSection() {
229234
myUploadTool = valueSplit[1];
230235
myReferencedPlatformUpload = BoardsManager.getNewestInstalledPlatform(refVendor, architecture);
231236
if (myReferencedPlatformUpload == null) {
232-
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
237+
Activator.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
233238
Helpers_tool_reference_missing.replace(TOOL_TAG, upload)
234239
.replace(FILE_TAG, getReferencingBoardsFile().toString())
235240
.replace(BOARD_TAG, getBoardID())));
@@ -242,7 +247,7 @@ private void ParseSection() {
242247
myUploadTool = valueSplit[3];
243248
myReferencedPlatformUpload = BoardsManager.getPlatform(refVendor, refArchitecture, refVersion);
244249
if (this.myReferencedPlatformUpload == null) {
245-
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
250+
Activator.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
246251
Helpers_tool_reference_missing.replace(TOOL_TAG, upload)
247252
.replace(FILE_TAG, getReferencingBoardsFile().toString())
248253
.replace(BOARD_TAG, getBoardID())));
@@ -879,7 +884,12 @@ public Map<String, String> getEnvVars() {
879884
}
880885

881886
// 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+
}
883893

884894
// boards settings not coming from menu selections
885895
allVars.putAll(mySloeberBoardTxtFile.getBoardEnvironVars(getBoardID()));
@@ -926,37 +936,28 @@ private String getBoardFQBN() {
926936
return fqbn+options;
927937
}
928938

929-
private Map<String, String> getEnVarPlatformInfo() {
939+
private Map<String, String> getEnVarPlatformInfo() throws IOException {
930940
Map<String, String> ret = new HashMap<>();
931941

932-
if (myReferencedPlatformUpload != null) {
933942
ret.putAll(getEnvVarPlatformFileTools(myReferencedPlatformUpload));
934-
}
935-
if (myReferencedPlatformVariant != null) {
936943
ret.putAll(getEnvVarPlatformFileTools(myReferencedPlatformVariant));
937-
}
938-
939-
if (myReferencedPlatformCore != null) {
940944
ret.putAll(getEnvVarPlatformFileTools(myReferencedPlatformCore));
941-
}
945+
946+
IArduinoPlatformVersion latestArduinoPlatform = BoardsManager.getNewestInstalledPlatform(Const.VENDOR_ARDUINO,
947+
getArchitecture());
948+
ret.putAll(getEnvVarPlatformFileTools(latestArduinoPlatform));
942949

943950
IPath referencingPlatformPath = getreferencingPlatformPath();
944951
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));
955956
}
956957

958+
957959
if (myReferencedPlatformCore == null) {
958960
//there is no referenced core so no need to do smart stuff
959-
ret.putAll(getEnvVarPlatformFileTools(referencingPlatform));
960961
return ret;
961962
}
962963

@@ -967,7 +968,6 @@ private Map<String, String> getEnVarPlatformInfo() {
967968
return ret;
968969
}
969970
// standard arduino IDE way
970-
ret.putAll(getEnvVarPlatformFileTools(referencingPlatform));
971971
ret.putAll(getEnvVarPlatformFileTools(myReferencedPlatformCore));
972972
return ret;
973973

@@ -980,23 +980,64 @@ private Map<String, String> getEnVarPlatformInfo() {
980980
*
981981
* @param platformVersion
982982
* @return environment variables pointing to the tools used by the platform
983+
* @throws IOException
983984
*/
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+
}
10001041

10011042
/**
10021043
* Following post processing is done

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

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public class Const extends AutoBuildConstants {
9696
public static final String SLOEBER_PROJECT = ".sproject";
9797
public static final String LIBRARY_PROPERTIES = "library.properties";
9898
public static final String LIBRARY_DOT_A_LINKAGE = "dot_a_linkage";
99+
public static final String SLOEBER_TXT_FILE_NAME="sloeber.txt";
99100

100101
// Environment variable stuff
101102
public static final String ENV_KEY_SLOEBER_START = "sloeber" + DOT;

0 commit comments

Comments
 (0)