Skip to content

Commit 2769961

Browse files
authored
Merge pull request #735 from Sloeber/#732_Multi_Managed_Lib_issue
Only keypad and base 64 remain having 2 versions
2 parents b4d14b5 + bec98d2 commit 2769961

File tree

5 files changed

+126
-102
lines changed

5 files changed

+126
-102
lines changed

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

Lines changed: 110 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
package io.sloeber.core.api;
22

3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.Reader;
6+
import java.util.ArrayList;
37
import java.util.Collection;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
411
import java.util.Set;
512
import java.util.TreeMap;
613
import java.util.TreeSet;
714

815
import org.eclipse.core.runtime.IProgressMonitor;
916
import org.eclipse.core.runtime.IStatus;
1017
import org.eclipse.core.runtime.MultiStatus;
18+
import org.eclipse.core.runtime.NullProgressMonitor;
1119
import org.eclipse.core.runtime.Status;
1220

21+
import com.google.gson.Gson;
22+
23+
import io.sloeber.core.Activator;
24+
import io.sloeber.core.common.Common;
1325
import io.sloeber.core.common.InstancePreferences;
26+
import io.sloeber.core.managers.Library;
1427
import io.sloeber.core.managers.LibraryIndex;
1528
import io.sloeber.core.managers.Manager;
29+
import io.sloeber.core.managers.Messages;
30+
import io.sloeber.core.tools.Version;
1631

1732
public class LibraryManager {
33+
static private List<LibraryIndex> libraryIndices;
34+
35+
static public List<LibraryIndex> getLibraryIndices() {
36+
if (libraryIndices == null) {
37+
Manager.getPackageIndices();
38+
}
39+
return libraryIndices;
40+
}
41+
1842
public static LibraryTree getLibraryTree() {
1943
return new LibraryTree();
2044

@@ -128,7 +152,7 @@ public Object getParent() {
128152
}
129153

130154
public LibraryTree() {
131-
for (LibraryIndex libraryIndex : Manager.getLibraryIndices()) {
155+
for (LibraryIndex libraryIndex : getLibraryIndices()) {
132156
for (String categoryName : libraryIndex.getCategories()) {
133157
Category category = this.categories.get(categoryName);
134158
if (category == null) {
@@ -166,7 +190,7 @@ public Collection<Library> getAllLibraries() {
166190
}
167191

168192
private static LibraryIndex findLibraryIndex(String name) {
169-
for (LibraryIndex libraryIndex : Manager.getLibraryIndices()) {
193+
for (LibraryIndex libraryIndex : getLibraryIndices()) {
170194
if (libraryIndex.getName().equals(name))
171195
return libraryIndex;
172196
}
@@ -188,7 +212,7 @@ public void reset() {
188212

189213
public static IStatus setLibraryTree(LibraryTree libs, IProgressMonitor monitor, MultiStatus status) {
190214
for (LibraryTree.Library lib : libs.getAllLibraries()) {
191-
LibraryIndex libraryIndex = findLibraryIndex(lib.getIndexName());
215+
LibraryIndex libraryIndex = getLibraryIndex(lib.getIndexName());
192216

193217
if (libraryIndex != null) {
194218
io.sloeber.core.managers.Library toRemove = libraryIndex.getInstalledLibrary(lib.getName());
@@ -207,27 +231,10 @@ public static IStatus setLibraryTree(LibraryTree libs, IProgressMonitor monitor,
207231
return status;
208232
}
209233

210-
private static LibraryIndex findLibraryIndex(String indexName) {
211-
for (LibraryIndex libraryIndex : Manager.getLibraryIndices()) {
212-
if (libraryIndex.getName().equals(indexName))
213-
return libraryIndex;
214-
}
215-
return null;
216-
}
217-
218234
public static String getPrivateLibraryPathsString() {
219235
return InstancePreferences.getPrivateLibraryPathsString();
220236
}
221237

222-
/**
223-
* Wrapper method for Manager. installAllLatestLibraries()
224-
*
225-
* @param category
226-
*/
227-
public static void installAllLatestLibraries() {
228-
Manager.installAllLatestLibraries();
229-
}
230-
231238
/**
232239
* get all the categories for all libraries (installed or not)
233240
*
@@ -238,11 +245,93 @@ public static Set<String> getAllCategories() {
238245

239246
Set<String> ret = new TreeSet<>();
240247

241-
for (LibraryIndex libraryIndex : Manager.getLibraryIndices()) {
248+
for (LibraryIndex libraryIndex : getLibraryIndices()) {
242249
for (String categoryName : libraryIndex.getCategories()) {
243250
ret.add(categoryName);
244251
}
245252
}
246253
return ret;
247254
}
255+
256+
public static void InstallDefaultLibraries(IProgressMonitor monitor) {
257+
LibraryIndex libindex = getLibraryIndex(Defaults.DEFAULT);
258+
if (libindex == null)
259+
return;
260+
261+
for (String library : Defaults.INSTALLED_LIBRARIES) {
262+
Library toInstalLib = libindex.getLatestLibrary(library);
263+
if (toInstalLib != null) {
264+
toInstalLib.install(monitor);
265+
}
266+
}
267+
}
268+
269+
static private LibraryIndex getLibraryIndex(String name) {
270+
for (LibraryIndex index : getLibraryIndices()) {
271+
if (index.getName().equals(name)) {
272+
return index;
273+
}
274+
}
275+
return null;
276+
}
277+
278+
static public void loadJson(File jsonFile) {
279+
try (Reader reader = new FileReader(jsonFile)) {
280+
LibraryIndex index = new Gson().fromJson(reader, LibraryIndex.class);
281+
index.resolve();
282+
index.setJsonFile(jsonFile);
283+
libraryIndices.add(index);
284+
} catch (Exception e) {
285+
Common.log(new Status(IStatus.ERROR, Activator.getId(),
286+
Messages.Manager_Failed_to_parse.replace("${FILE}", jsonFile.getAbsolutePath()), e)); //$NON-NLS-1$
287+
jsonFile.delete();// Delete the file so it stops damaging
288+
}
289+
}
290+
291+
/**
292+
* Install the latest version of all the libraries belonging to this
293+
* category If a earlier version is installed this version will be removed
294+
* before installation of the newer version
295+
*
296+
* @param category
297+
*/
298+
public static void installAllLatestLibraries() {
299+
List<LibraryIndex> libraryIndices1 = getLibraryIndices();
300+
Map<String, Library> latestLibs = new HashMap<>();
301+
for (LibraryIndex libraryIndex : libraryIndices1) {
302+
Map<String, Library> libraries = libraryIndex.getLatestLibraries();
303+
for (Map.Entry<String, Library> entry : libraries.entrySet()) {
304+
String curLibName = entry.getKey();
305+
Library curLibrary = entry.getValue();
306+
Library current = latestLibs.get(curLibName);
307+
if (current != null) {
308+
if (Version.compare(curLibrary.getVersion(), current.getVersion()) > 0) {
309+
latestLibs.put(curLibName, curLibrary);
310+
}
311+
} else {
312+
latestLibs.put(curLibName, curLibrary);
313+
}
314+
}
315+
}
316+
for (Map.Entry<String, Library> entry : latestLibs.entrySet()) {
317+
String curLibName = entry.getKey();
318+
Library curLibrary = entry.getValue();
319+
for (LibraryIndex libraryIndex : libraryIndices1) {
320+
321+
Library previousVersion = libraryIndex.getInstalledLibrary(curLibName);
322+
if ((previousVersion != null) && (previousVersion != curLibrary)) {
323+
previousVersion.remove(new NullProgressMonitor());
324+
}
325+
}
326+
if (!curLibrary.isInstalled()) {
327+
curLibrary.install(new NullProgressMonitor());
328+
}
329+
}
330+
331+
}
332+
333+
public static void flushIndices() {
334+
libraryIndices = new ArrayList<>();
335+
}
336+
248337
}

io.sloeber.core/src/io/sloeber/core/managers/LibraryIndex.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import io.sloeber.core.tools.Version;
1515

1616
public class LibraryIndex {
17-
private String indexName;
17+
private String jsonFileName;
1818
private List<Library> libraries;
1919

2020
// category name to library name
@@ -89,9 +89,8 @@ public Collection<Library> getLatestLibraries(String category) {
8989
return libs;
9090
}
9191

92-
public Collection<Library> getLatestLibraries() {
93-
94-
return this.latestLibs.values();
92+
public Map<String, Library> getLatestLibraries() {
93+
return this.latestLibs;
9594
}
9695

9796
public Collection<Library> getLibraries(String category) {
@@ -112,14 +111,14 @@ public Collection<Library> getLibraries(String category) {
112111
public void setJsonFile(File packageFile) {
113112
String fileName = packageFile.getName().toLowerCase();
114113
if (fileName.matches("(?i)library_index.json")) { //$NON-NLS-1$
115-
this.indexName = Defaults.DEFAULT;
114+
this.jsonFileName = Defaults.DEFAULT;
116115
} else {
117-
this.indexName = fileName.replaceAll("(?i)" + Pattern.quote("library_"), "") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
116+
this.jsonFileName = fileName.replaceAll("(?i)" + Pattern.quote("library_"), "") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
118117
.replaceAll("(?i)" + Pattern.quote("_index.json"), ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
119118
}
120119
}
121120

122121
public String getName() {
123-
return this.indexName;
122+
return this.jsonFileName;
124123
}
125124
}

io.sloeber.core/src/io/sloeber/core/managers/Manager.java

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.nio.file.Paths;
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
31-
import java.util.Collection;
3231
import java.util.HashMap;
3332
import java.util.HashSet;
3433
import java.util.List;
@@ -45,7 +44,6 @@
4544
import org.eclipse.core.runtime.IPath;
4645
import org.eclipse.core.runtime.IProgressMonitor;
4746
import org.eclipse.core.runtime.IStatus;
48-
import org.eclipse.core.runtime.NullProgressMonitor;
4947
import org.eclipse.core.runtime.Platform;
5048
import org.eclipse.core.runtime.Status;
5149
import org.eclipse.ui.statushandlers.StatusManager;
@@ -54,14 +52,15 @@
5452

5553
import io.sloeber.core.Activator;
5654
import io.sloeber.core.api.Defaults;
55+
import io.sloeber.core.api.LibraryManager;
5756
import io.sloeber.core.common.Common;
5857
import io.sloeber.core.common.ConfigurationPreferences;
5958
import io.sloeber.core.tools.MyMultiStatus;
6059

6160
public class Manager {
6261

6362
static private List<PackageIndex> packageIndices;
64-
static private List<LibraryIndex> libraryIndices;
63+
6564
private static boolean myIsReady = false;
6665

6766
public static boolean isReady() {
@@ -90,10 +89,10 @@ public static void startup_Pluging(IProgressMonitor monitor) {
9089
loadJsons(ConfigurationPreferences.getUpdateJasonFilesFlag());
9190
List<Board> allBoards = getInstalledBoards();
9291
if (allBoards.isEmpty()) { // If boards are installed do nothing
93-
InstallDefaultLibraries(monitor);
92+
LibraryManager.InstallDefaultLibraries(monitor);
9493
MyMultiStatus mstatus = new MyMultiStatus("Failed to configer Sloeber"); //$NON-NLS-1$
9594

96-
// Downnload sample programs
95+
// Download sample programs
9796
mstatus.addErrors(downloadAndInstall(Defaults.EXAMPLES_URL, Defaults.EXAMPLE_PACKAGE,
9897
Paths.get(ConfigurationPreferences.getInstallationPathExamples().toString()), false, monitor));
9998

@@ -123,19 +122,6 @@ public static void startup_Pluging(IProgressMonitor monitor) {
123122

124123
}
125124

126-
private static void InstallDefaultLibraries(IProgressMonitor monitor) {
127-
LibraryIndex libindex = getLibraryIndex(Defaults.DEFAULT);
128-
if (libindex == null)
129-
return;
130-
131-
for (String library : Defaults.INSTALLED_LIBRARIES) {
132-
Library toInstalLib = libindex.getLatestLibrary(library);
133-
if (toInstalLib != null) {
134-
toInstalLib.install(monitor);
135-
}
136-
}
137-
}
138-
139125
/**
140126
* Given a platform description in a json file download and install all
141127
* needed stuff. All stuff is including all tools and core files and
@@ -179,7 +165,7 @@ static public IStatus downloadAndInstall(ArduinoPlatform platform, boolean force
179165

180166
static private void loadJsons(boolean forceDownload) {
181167
packageIndices = new ArrayList<>();
182-
libraryIndices = new ArrayList<>();
168+
LibraryManager.flushIndices();
183169

184170
String[] jsonUrls = ConfigurationPreferences.getJsonURLList();
185171
for (String jsonUrl : jsonUrls) {
@@ -237,7 +223,7 @@ static private void loadJson(String url, boolean forceDownload) {
237223
if (jsonFile.getName().toLowerCase().startsWith("package_")) { //$NON-NLS-1$
238224
loadPackage(jsonFile);
239225
} else if (jsonFile.getName().toLowerCase().startsWith("library_")) { //$NON-NLS-1$
240-
loadLibrary(jsonFile);
226+
LibraryManager.loadJson(jsonFile);
241227
}
242228
}
243229
}
@@ -249,21 +235,8 @@ static private void loadPackage(File jsonFile) {
249235
index.setJsonFile(jsonFile);
250236
packageIndices.add(index);
251237
} catch (Exception e) {
252-
Common.log(
253-
new Status(IStatus.ERROR, Activator.getId(), "Unable to parse " + jsonFile.getAbsolutePath(), e)); //$NON-NLS-1$
254-
jsonFile.delete();// Delete the file so it stops damaging
255-
}
256-
}
257-
258-
static private void loadLibrary(File jsonFile) {
259-
try (Reader reader = new FileReader(jsonFile)) {
260-
LibraryIndex index = new Gson().fromJson(reader, LibraryIndex.class);
261-
index.resolve();
262-
index.setJsonFile(jsonFile);
263-
libraryIndices.add(index);
264-
} catch (Exception e) {
265-
Common.log(
266-
new Status(IStatus.ERROR, Activator.getId(), "Unable to parse " + jsonFile.getAbsolutePath(), e)); //$NON-NLS-1$
238+
Common.log(new Status(IStatus.ERROR, Activator.getId(),
239+
Messages.Manager_Failed_to_parse.replace("${FILE}", jsonFile.getAbsolutePath()), e)); //$NON-NLS-1$
267240
jsonFile.delete();// Delete the file so it stops damaging
268241
}
269242
}
@@ -275,22 +248,6 @@ static public List<PackageIndex> getPackageIndices() {
275248
return packageIndices;
276249
}
277250

278-
static public List<LibraryIndex> getLibraryIndices() {
279-
if (libraryIndices == null) {
280-
loadJsons(false);
281-
}
282-
return libraryIndices;
283-
}
284-
285-
static public LibraryIndex getLibraryIndex(String name) {
286-
for (LibraryIndex index : getLibraryIndices()) {
287-
if (index.getName().equals(name)) {
288-
return index;
289-
}
290-
}
291-
return null;
292-
}
293-
294251
static public Board getBoard(String boardName, String platformName, String packageName) {
295252
for (PackageIndex index : getPackageIndices()) {
296253
Package pkg = index.getPackage(packageName);
@@ -859,27 +816,4 @@ public static void onlyKeepLatestPlatforms() {
859816
}
860817
}
861818

862-
/**
863-
* Install the latest version of all the libraries belonging to this
864-
* category If a earlier version is installed this version will be removed
865-
* before installation of the newer version
866-
*
867-
* @param category
868-
*/
869-
public static void installAllLatestLibraries() {
870-
List<LibraryIndex> libraryIndices1 = getLibraryIndices();
871-
for (LibraryIndex libraryIndex : libraryIndices1) {
872-
Collection<Library> libraries = libraryIndex.getLatestLibraries();
873-
for (Library library : libraries) {
874-
Library previousVersion = libraryIndex.getInstalledLibrary(library.getName());
875-
if ((previousVersion != null) && (previousVersion != library)) {
876-
previousVersion.remove(new NullProgressMonitor());
877-
}
878-
if (!library.isInstalled()) {
879-
library.install(new NullProgressMonitor());
880-
}
881-
}
882-
}
883-
}
884-
885819
}

0 commit comments

Comments
 (0)