Skip to content

Commit 685ab31

Browse files
author
jan
committed
a implementation for #1675 and #1670
Not sure they will work properly. Needs some testing
1 parent cbe2390 commit 685ab31

File tree

5 files changed

+83
-144
lines changed

5 files changed

+83
-144
lines changed

io.sloeber.core/src/io/sloeber/core/Activator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ private static void initializeImportantVariables() {
221221
InstancePreferences.setPrivateLibraryPaths(InstancePreferences.getPrivateLibraryPaths());
222222
InstancePreferences.setPrivateHardwarePaths(InstancePreferences.getPrivateHardwarePaths());
223223
InstancePreferences.setAutomaticallyImportLibraries(InstancePreferences.getAutomaticallyImportLibraries());
224-
BoardsManager.setJsonURLs(BoardsManager.getJsonURLs());
225224
}
226225

227226
private void runPluginCoreStartInstantiatorJob() {

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

Lines changed: 72 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.io.Reader;
1212
import java.net.MalformedURLException;
1313
import java.net.URL;
14+
import java.nio.charset.Charset;
15+
import java.nio.charset.StandardCharsets;
1416
import java.nio.file.Files;
1517
import java.nio.file.Paths;
1618
import java.nio.file.StandardCopyOption;
@@ -22,11 +24,9 @@
2224
import java.util.HashSet;
2325
import java.util.List;
2426
import java.util.Map;
25-
import java.util.Set;
2627
import java.util.TreeMap;
2728
import java.util.TreeSet;
2829

29-
import org.eclipse.cdt.core.parser.util.StringUtil;
3030
import org.eclipse.core.runtime.IPath;
3131
import org.eclipse.core.runtime.IProgressMonitor;
3232
import org.eclipse.core.runtime.IStatus;
@@ -62,16 +62,13 @@
6262
*
6363
*/
6464
public class BoardsManager {
65-
private static String stringSplitter = "\n";//$NON-NLS-1$
66-
private static final String KEY_MANAGER_JSON_URLS_V3 = "Arduino Manager board Urls"; //$NON-NLS-1$
67-
private static final String KEY_MANAGER_ARDUINO_LIBRARY_JSON_URL = "https://downloads.arduino.cc/libraries/library_index.json"; //$NON-NLS-1$
68-
private static final String KEY_MANAGER_JSON_URLS = "Manager jsons"; //$NON-NLS-1$
69-
private static final String DEFAULT_JSON_URLS = "https://downloads.arduino.cc/packages/package_index.json\n" //$NON-NLS-1$
70-
+ "https://raw.githubusercontent.com/jantje/hardware/master/package_jantje_index.json\n" //$NON-NLS-1$
71-
+ "https://raw.githubusercontent.com/jantje/ArduinoLibraries/master/library_jantje_index.json\n" //$NON-NLS-1$
72-
+ "https://arduino.esp8266.com/stable/package_esp8266com_index.json\n" //$NON-NLS-1$
73-
+ "https://www.pjrc.com/teensy/package_teensy_index.json\n" //$NON-NLS-1$
74-
+ KEY_MANAGER_ARDUINO_LIBRARY_JSON_URL;
65+
private static final String THIRD_PARTY_URL_FILE="sloeber_third_party_url.txt"; //$NON-NLS-1$
66+
private static final String[] DEFAULT_JSON_URLS = {"https://downloads.arduino.cc/packages/package_index.json", //$NON-NLS-1$
67+
"https://raw.githubusercontent.com/jantje/hardware/master/package_jantje_index.json", //$NON-NLS-1$
68+
"https://raw.githubusercontent.com/jantje/ArduinoLibraries/master/library_jantje_index.json", //$NON-NLS-1$
69+
"https://arduino.esp8266.com/stable/package_esp8266com_index.json", //$NON-NLS-1$
70+
"https://www.pjrc.com/teensy/package_teensy_index.json", //$NON-NLS-1$
71+
"https://downloads.arduino.cc/libraries/library_index.json"};//$NON-NLS-1$
7572

7673
protected static List<ArduinoPlatformPackageIndex> packageIndices;
7774
private static boolean myHasbeenLogged = false;
@@ -131,17 +128,21 @@ static private BoardDescription getNewestBoardIDFromBoardsManager(String jsonFil
131128
return boardid;
132129
}
133130

134-
public static void addPackageURLs(HashSet<String> packageUrlsToAdd, boolean forceDownload) {
131+
public static void addPackageURLs(Collection<String> packageUrlsToAdd, boolean forceDownload) {
132+
if (!isReady()) {
133+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
134+
return;
135+
}
135136
HashSet<String> originalJsonUrls = new HashSet<>(Arrays.asList(getJsonURLList()));
136137
packageUrlsToAdd.addAll(originalJsonUrls);
137138

138139
setJsonURLs(packageUrlsToAdd);
139140
loadJsons(forceDownload);
140141
}
141142

142-
public static void setPackageURLs(HashSet<String> packageUrls, boolean forceDownload) {
143+
public static void setPackageURLs(Collection<String> packageUrls, boolean forceDownload) {
143144
if (!isReady()) {
144-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
145+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
145146
return;
146147
}
147148
setJsonURLs(packageUrls);
@@ -152,7 +153,7 @@ public static void setPackageURLs(HashSet<String> packageUrls, boolean forceDown
152153
* installs a subset of the latest platforms It skips the first <fromIndex>
153154
* platforms And stops at <toIndex> platforms. To install the 5 first latest
154155
* platforms installsubsetOfLatestPlatforms(0,5)
155-
*
156+
*
156157
* @param fromIndex
157158
* the platforms at the start to skip
158159
* @param toIndex
@@ -161,7 +162,7 @@ public static void setPackageURLs(HashSet<String> packageUrls, boolean forceDown
161162
public static void installsubsetOfLatestPlatforms(int fromIndex, int toIndex) {
162163
String DEPRECATED = "DEPRECATED"; //$NON-NLS-1$
163164
if (!isReady()) {
164-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
165+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
165166
return;
166167
}
167168
envVarsNeedUpdating = true;
@@ -199,7 +200,7 @@ public static void installAllLatestPlatforms() {
199200

200201
public static void installLatestPlatform(String JasonName, String packageName, String architectureName) {
201202
if (!isReady()) {
202-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
203+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
203204
return;
204205
}
205206
envVarsNeedUpdating = true;
@@ -215,7 +216,7 @@ public static void installLatestPlatform(String JasonName, String packageName, S
215216
}
216217
}
217218
}
218-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID,
219+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
219220
"failed to find " + JasonName + " " + packageName + " " + architectureName)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
220221
}
221222

@@ -276,7 +277,7 @@ private static IStatus install(ArduinoPlatformVersion platformVersion, IProgress
276277

277278
WorkAround.applyKnownWorkArounds(platformVersion);
278279

279-
System.out.println("done installing platform " + platformVersion.toString()); //$NON-NLS-1$
280+
System.out.println("done installing platform " + platformVersion.toString()); //$NON-NLS-1$
280281
return mstatus;
281282
}
282283

@@ -308,10 +309,10 @@ public static File[] getAllBoardsFiles() {
308309

309310
TreeSet<File> boardFiles = new TreeSet<>();
310311
for (String CurFolder : hardwareFolders) {
311-
searchFiles(new File(CurFolder), boardFiles, Const.BOARDS_FILE_NAME, 6);
312+
searchFiles(new File(CurFolder), boardFiles, BOARDS_FILE_NAME, 6);
312313
}
313314
if (boardFiles.size() == 0) {
314-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID,
315+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID,
315316
Helpers_No_boards_txt_found.replace(FILE_TAG, String.join("\n", hardwareFolders)), null)); //$NON-NLS-1$
316317
return null;
317318
}
@@ -323,15 +324,18 @@ private static void searchFiles(File folder, TreeSet<File> Hardwarelists, String
323324
File[] a = folder.listFiles();
324325
if (a == null) {
325326
if (!myHasbeenLogged) {
326-
Common.log(new Status(IStatus.INFO, Const.CORE_PLUGIN_ID,
327+
Common.log(new Status(IStatus.INFO, CORE_PLUGIN_ID,
327328
Helpers_Error_The_folder_is_empty.replace(FOLDER_TAG, folder.toString()), null));
328329
myHasbeenLogged = true;
329330
}
330331
return;
331332
}
332333
for (File f : a) {
333334
if (f.isDirectory()) {
334-
searchFiles(f, Hardwarelists, Filename, depth - 1);
335+
//ignore folders named tools
336+
if(!f.getName().equals(TOOLS)) {
337+
searchFiles(f, Hardwarelists, Filename, depth - 1);
338+
}
335339
} else if (f.getName().equals(Filename)) {
336340
Hardwarelists.add(f);
337341
}
@@ -352,7 +356,7 @@ private static String[] getHardwarePaths() {
352356
public static IStatus updatePlatforms(List<ArduinoPlatformVersion> platformsToInstall,
353357
List<ArduinoPlatformVersion> platformsToRemove, IProgressMonitor monitor, MultiStatus status) {
354358
if (!isReady()) {
355-
status.add(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, null));
359+
status.add(new Status(IStatus.ERROR, CORE_PLUGIN_ID, BoardsManagerIsBussy, null));
356360
return status;
357361
}
358362
//TODO updating the jsons after selecting what to install seems dangerous to me; check to delete
@@ -405,7 +409,7 @@ public static TreeMap<String, String> getAllmenus() {
405409

406410
public static void setPrivateHardwarePaths(String[] hardWarePaths) {
407411
if (!isReady()) {
408-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
412+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
409413
return;
410414
}
411415
InstancePreferences.setPrivateHardwarePaths(hardWarePaths);
@@ -509,91 +513,55 @@ protected static File getLocalFileName(String url, boolean show_error) {
509513
return packagePath.toFile();
510514
}
511515

512-
public static String getDefaultJsonURLs() {
516+
public static String[] getDefaultJsonURLs() {
513517
return DEFAULT_JSON_URLS;
514518
}
515519

516-
public static String getJsonUrlsKey() {
517-
return KEY_MANAGER_JSON_URLS;
518-
}
519-
520-
public static void setJsonURLs(String urls) {
521-
setString(KEY_MANAGER_JSON_URLS, urls);
522-
}
523520

524-
private static void saveJsonURLs(String urls[]) {
525-
setString(KEY_MANAGER_JSON_URLS, StringUtil.join(urls, stringSplitter));
526-
}
527-
528-
public static void setJsonURLs(HashSet<String> urls) {
529-
setString(KEY_MANAGER_JSON_URLS, StringUtil.join(urls, stringSplitter));
521+
private static void setJsonURLs(Collection<String> urls) {
522+
IPath myThirdPartyURLStoragePath=getThirdPartyURLStoragePath();
523+
try {
524+
if(myThirdPartyURLStoragePath!=null ) {
525+
Files.write(myThirdPartyURLStoragePath.toPath(),urls, Charset.forName(StandardCharsets.UTF_8.name()));
526+
}
527+
} catch (IOException e) {
528+
// TODO Auto-generated catch block
529+
e.printStackTrace();
530+
}
530531
}
531532

532533
public static String[] getJsonURLList() {
533-
return getJsonURLs().replace("\r", new String()).split(stringSplitter); //$NON-NLS-1$
534-
}
535-
536-
public static String getJsonURLs() {
537-
// I added some code here to get easier from V3 to V4
538-
// the library json url is now managed as the boards url's so it also
539-
// needs to be added to the json url's
540-
// this is doen in the default but people who have installed other
541-
// boards or do not move to the default (which is by default)
542-
// wil not see libraries
543-
// to fix this I changed the storage name and if the new storage name is
544-
// empty I read the ol one and add the lib
545-
String ret = getString(KEY_MANAGER_JSON_URLS, DEFAULT_JSON_URLS);
546-
if (DEFAULT_JSON_URLS.equals(ret)) {
547-
ret = getString(KEY_MANAGER_JSON_URLS_V3, DEFAULT_JSON_URLS);
548-
if (!DEFAULT_JSON_URLS.equals(ret)) {
549-
ret += System.lineSeparator() + KEY_MANAGER_ARDUINO_LIBRARY_JSON_URL;
550-
setString(KEY_MANAGER_JSON_URLS, ret);
551-
removeKey(KEY_MANAGER_JSON_URLS_V3);
552-
}
553-
}
554-
return ret;
534+
IPath myThirdPartyURLStoragePath=getThirdPartyURLStoragePath();
535+
try {
536+
if(myThirdPartyURLStoragePath!=null && myThirdPartyURLStoragePath.toFile().exists()) {
537+
List<String>thirdPartyURLs = Files.readAllLines(myThirdPartyURLStoragePath.toPath(), Charset.forName(StandardCharsets.UTF_8.name()));
538+
if(thirdPartyURLs.size()>0) {
539+
return thirdPartyURLs.toArray(new String[thirdPartyURLs.size()]);
540+
}
541+
}
542+
} catch (IOException e) {
543+
// TODO Auto-generated catch block
544+
e.printStackTrace();
545+
}
546+
//The new way of storing the thirdparty urls's failed
547+
//try the Sloeber V3 way for downwards compatibility
548+
String[] sloeberV4Storage= getString("Manager jsons", EMPTY_STRING).replace("\r", new String()).split("\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
549+
if(sloeberV4Storage.length>3) {
550+
return sloeberV4Storage;
551+
}
552+
//Everything failed; This is probably a new install; return the defaults;
553+
return DEFAULT_JSON_URLS;
554+
555+
}
556+
557+
private static IPath getThirdPartyURLStoragePath() {
558+
return sloeberHomePath.append(SLOEBER_HOME_SUB_FOLDER).append(THIRD_PARTY_URL_FILE);
555559
}
556560

557-
/**
558-
* Completely replace the list with jsons with a new list
559-
*
560-
* @param newJsonUrls
561-
*/
562-
public static void setJsonURLs(String[] newJsonUrls) {
563-
if (!isReady()) {
564-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
565-
return;
566-
}
567-
568-
String curJsons[] = getJsonURLList();
569-
HashSet<String> origJsons = new HashSet<>(Arrays.asList(curJsons));
570-
HashSet<String> currentSelectedJsons = new HashSet<>(Arrays.asList(newJsonUrls));
571-
origJsons.removeAll(currentSelectedJsons);
572-
// remove the files from disk which were in the old lst but not in the
573-
// new one
574-
for (String curJson : origJsons) {
575-
try {
576-
File localFile = getLocalFileName(curJson, false);
577-
if (localFile.exists()) {
578-
localFile.delete();
579-
}
580-
} catch (@SuppressWarnings("unused") Exception e) {
581-
// ignore
582-
}
583-
}
584-
// save to configurationsettings before calling LoadIndices
585-
saveJsonURLs(newJsonUrls);
586-
// reload the indices (this will remove all potential remaining
587-
// references
588-
// existing files do not need to be refreshed as they have been
589-
// refreshed at startup
590-
// new files will be added
591-
loadJsons(false);
592-
}
593561

594562
public static void removeAllInstalledPlatforms() {
595563
if (!isReady()) {
596-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
564+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
597565
return;
598566
}
599567
try {
@@ -617,9 +585,9 @@ public static Map<String, String> getEnvironmentVariables() {
617585
return myWorkbenchEnvironmentVariables;
618586
}
619587
myWorkbenchEnvironmentVariables.clear();
620-
ArduinoPlatformVersion latestAvrPlatform = getNewestInstalledPlatform(Const.VENDOR_ARDUINO, Const.AVR);
621-
ArduinoPlatformVersion latestSamdPlatform = getNewestInstalledPlatform(Const.VENDOR_ARDUINO, Const.SAMD);
622-
ArduinoPlatformVersion latestSamPlatform = getNewestInstalledPlatform(Const.VENDOR_ARDUINO, Const.SAM);
588+
ArduinoPlatformVersion latestAvrPlatform = getNewestInstalledPlatform(VENDOR_ARDUINO, AVR);
589+
ArduinoPlatformVersion latestSamdPlatform = getNewestInstalledPlatform(VENDOR_ARDUINO, SAMD);
590+
ArduinoPlatformVersion latestSamPlatform = getNewestInstalledPlatform(VENDOR_ARDUINO, SAM);
623591

624592
if (latestSamdPlatform != null) {
625593
myWorkbenchEnvironmentVariables.putAll(getEnvVarPlatformFileTools(latestSamdPlatform));
@@ -651,7 +619,7 @@ private static Map<String, String> getEnvVarPlatformFileTools(ArduinoPlatformVer
651619
/**
652620
* given a vendor and a architecture provide the newest installed platform
653621
* version
654-
*
622+
*
655623
* @param vendor
656624
* @param architecture
657625
* @return the found platformVersion or null if none found
@@ -827,48 +795,13 @@ static public ArduinoPackage getPackage(String packageName) {
827795
return null;
828796
}
829797

830-
/**
831-
* This method removes the json files from disk and removes memory references to
832-
* these files or their content
833-
*
834-
* @param packageUrlsToRemove
835-
*/
836-
public static void removePackageURLs(Set<String> packageUrlsToRemove) {
837-
if (!isReady()) {
838-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
839-
return;
840-
}
841-
// remove the files from memory
842-
Set<String> activeUrls = new HashSet<>(Arrays.asList(getJsonURLList()));
843-
844-
activeUrls.removeAll(packageUrlsToRemove);
845-
846-
setJsonURLs(activeUrls.toArray((String[]) null));
847-
848-
// remove the files from disk
849-
for (String curJson : packageUrlsToRemove) {
850-
File localFile = getLocalFileName(curJson, true);
851-
if (localFile != null) {
852-
if (localFile.exists()) {
853-
localFile.delete();
854-
}
855-
}
856-
}
857-
858-
// reload the indices (this will remove all potential remaining
859-
// references
860-
// existing files do not need to be refreshed as they have been
861-
// refreshed at startup
862-
loadJsons(false);
863-
864-
}
865798

866799
/**
867800
* Remove all packages that have a more recent version
868801
*/
869802
public static void onlyKeepLatestPlatforms() {
870803
if (!isReady()) {
871-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
804+
Common.log(new Status(IStatus.ERROR, CORE_PLUGIN_ID, BoardsManagerIsBussy, new Exception()));
872805
return;
873806
}
874807
List<ArduinoPackage> allPackages = getPackages();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void setString(String key, String value) {
7171
}
7272

7373
public static IPath getInstallationPath() {
74-
return Common.sloeberHomePath.append("arduinoPlugin"); //$NON-NLS-1$
74+
return Common.sloeberHomePath.append(SLOEBER_HOME_SUB_FOLDER);
7575
}
7676

7777
public static IPath getInstallationPathLibraries() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class Const extends AutoBuildConstants {
7979
public static final String LOCAL = "local";
8080

8181
// Folder and file Information
82+
public static final String SLOEBER_HOME_SUB_FOLDER ="arduinoPlugin";
8283
public static final String ARDUINO_HARDWARE_FOLDER_NAME = HARDWARE;
8384
public static final String ARDUINO_CODE_FOLDER_NAME = CORE;
8485
public static final String ARDUINO_VARIANTS_FOLDER_NAME = VARIANTS;

0 commit comments

Comments
 (0)