Skip to content

Commit b4d14b5

Browse files
authored
Merge pull request #733 from Sloeber/#732_Multi_Managed_Lib_issue
#732 multi managed lib issue
2 parents 28ecda7 + 7a74c7f commit b4d14b5

File tree

9 files changed

+131
-76
lines changed

9 files changed

+131
-76
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public LibraryTree() {
142142
.append(library.getArchitectures().toString()).append("\n\n") //$NON-NLS-1$
143143
.append(library.getSentence());
144144
lib = new Library(category, library.getName(), libraryIndex.getName(), builder.toString());
145-
category.libraries.put(library.getName() + " (" + libraryIndex.getName() + ")", lib); //$NON-NLS-1$//$NON-NLS-2$
145+
category.libraries.put(library.getName() + " (" + libraryIndex.getName() + ")", lib); //$NON-NLS-1$//$NON-NLS-2$
146146
}
147147
lib.versions.add(new VersionNumber(library.getVersion()));
148148
if (library.isInstalled()) {
@@ -219,18 +219,21 @@ public static String getPrivateLibraryPathsString() {
219219
return InstancePreferences.getPrivateLibraryPathsString();
220220
}
221221

222-
public static void installAllLatestLibraries(String category) {
223-
Manager.installAllLatestLibraries(category);
224-
}
225-
222+
/**
223+
* Wrapper method for Manager. installAllLatestLibraries()
224+
*
225+
* @param category
226+
*/
226227
public static void installAllLatestLibraries() {
227-
Set<String> allcategories = getAllCategories();
228-
for (String categorieName : allcategories) {
229-
Manager.installAllLatestLibraries(categorieName);
230-
}
231-
228+
Manager.installAllLatestLibraries();
232229
}
233230

231+
/**
232+
* get all the categories for all libraries (installed or not)
233+
*
234+
* @return a list of all the categories that exist in any library json file
235+
* known by sloeber
236+
*/
234237
public static Set<String> getAllCategories() {
235238

236239
Set<String> ret = new TreeSet<>();

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.regex.Pattern;
1212

1313
import io.sloeber.core.api.Defaults;
14+
import io.sloeber.core.tools.Version;
1415

1516
public class LibraryIndex {
1617
private String indexName;
@@ -40,7 +41,7 @@ public void resolve() {
4041

4142
Library current = this.latestLibs.get(name);
4243
if (current != null) {
43-
if (Manager.compareVersions(library.getVersion(), current.getVersion()) > 0) {
44+
if (Version.compare(library.getVersion(), current.getVersion()) > 0) {
4445
this.latestLibs.put(name, library);
4546
}
4647
} else {
@@ -88,6 +89,11 @@ public Collection<Library> getLatestLibraries(String category) {
8889
return libs;
8990
}
9091

92+
public Collection<Library> getLatestLibraries() {
93+
94+
return this.latestLibs.values();
95+
}
96+
9197
public Collection<Library> getLibraries(String category) {
9298
Set<String> categoryLibs = this.categories.get(category);
9399
if (categoryLibs == null) {

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

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static public List<LibraryIndex> getLibraryIndices() {
281281
}
282282
return libraryIndices;
283283
}
284-
284+
285285
static public LibraryIndex getLibraryIndex(String name) {
286286
for (LibraryIndex index : getLibraryIndices()) {
287287
if (index.getName().equals(name)) {
@@ -745,60 +745,6 @@ private static void copyStreamToFile(InputStream in, long size, File outputFile)
745745
}
746746
}
747747

748-
/**
749-
* compares 2 strings as if they are version numbers if version1<version2
750-
* returns -1 if version1==version2(also if both are null) returns 0 else
751-
* return 1 This method caters for the null case
752-
*
753-
* @param version1
754-
* @param version2
755-
* @return
756-
*/
757-
public static int compareVersions(String version1, String version2) {
758-
if (version1 == null) {
759-
return version2 == null ? 0 : -1;
760-
}
761-
762-
if (version2 == null) {
763-
return 1;
764-
}
765-
766-
String[] v1 = version1.split("\\."); //$NON-NLS-1$
767-
String[] v2 = version2.split("\\."); //$NON-NLS-1$
768-
for (int i = 0; i < Math.max(v1.length, v2.length); ++i) {
769-
if (v1.length <= i) {
770-
return v2.length < i ? 0 : -1;
771-
}
772-
773-
if (v2.length <= i) {
774-
return 1;
775-
}
776-
777-
try {
778-
int vi1 = Integer.parseInt(v1[i]);
779-
int vi2 = Integer.parseInt(v2[i]);
780-
if (vi1 < vi2) {
781-
return -1;
782-
}
783-
784-
if (vi1 > vi2) {
785-
return 1;
786-
}
787-
} catch (@SuppressWarnings("unused") NumberFormatException e) {
788-
// not numbers, do string compares
789-
int c = v1[i].compareTo(v2[i]);
790-
if (c < 0) {
791-
return -1;
792-
}
793-
if (c > 0) {
794-
return 1;
795-
}
796-
}
797-
}
798-
799-
return 0;
800-
}
801-
802748
/**
803749
* This method removes the json files from disk and removes memory
804750
* references to these files or their content
@@ -913,11 +859,22 @@ public static void onlyKeepLatestPlatforms() {
913859
}
914860
}
915861

916-
public static void installAllLatestLibraries(String category) {
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() {
917870
List<LibraryIndex> libraryIndices1 = getLibraryIndices();
918871
for (LibraryIndex libraryIndex : libraryIndices1) {
919-
Collection<Library> libraries = libraryIndex.getLatestLibraries(category);
872+
Collection<Library> libraries = libraryIndex.getLatestLibraries();
920873
for (Library library : libraries) {
874+
Library previousVersion = libraryIndex.getInstalledLibrary(library.getName());
875+
if ((previousVersion != null) && (previousVersion != library)) {
876+
previousVersion.remove(new NullProgressMonitor());
877+
}
921878
if (!library.isInstalled()) {
922879
library.install(new NullProgressMonitor());
923880
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.List;
1414
import java.util.Map;
1515

16+
import io.sloeber.core.tools.Version;
17+
1618
public class Package implements Comparable<Package> {
1719

1820
private String name;
@@ -51,7 +53,7 @@ public String getWebsiteURL() {
5153
public String getEmail() {
5254
return this.email;
5355
}
54-
56+
5557
public Help getHelp() {
5658
return this.help;
5759
}
@@ -69,7 +71,7 @@ public Collection<ArduinoPlatform> getLatestPlatforms() {
6971
Map<String, ArduinoPlatform> platformMap = new HashMap<>();
7072
for (ArduinoPlatform platform : this.platforms) {
7173
ArduinoPlatform p = platformMap.get(platform.getName());
72-
if (p == null || Manager.compareVersions(platform.getVersion(), p.getVersion()) > 0) {
74+
if (p == null || Version.compare(platform.getVersion(), p.getVersion()) > 0) {
7375
platformMap.put(platform.getName(), platform);
7476
}
7577
}
@@ -89,7 +91,7 @@ public Collection<ArduinoPlatform> getInstalledPlatforms() {
8991
for (ArduinoPlatform platform : this.platforms) {
9092
if (platform.isInstalled()) {
9193
ArduinoPlatform p = platformMap.get(platform.getName());
92-
if (p == null || Manager.compareVersions(platform.getVersion(), p.getVersion()) > 0) {
94+
if (p == null || Version.compare(platform.getVersion(), p.getVersion()) > 0) {
9395
platformMap.put(platform.getName(), platform);
9496
}
9597
}
@@ -104,7 +106,7 @@ public ArduinoPlatform getLatestPlatform(String platformName) {
104106
if (foundPlatform == null) {
105107
foundPlatform = platform;
106108
} else {
107-
if (Manager.compareVersions(platform.getVersion(), foundPlatform.getVersion()) > 0) {
109+
if (Version.compare(platform.getVersion(), foundPlatform.getVersion()) > 0) {
108110
foundPlatform = platform;
109111
}
110112
}
@@ -117,7 +119,7 @@ public ArduinoPlatform getPlatform(String platformName, String version) {
117119

118120
for (ArduinoPlatform platform : this.platforms) {
119121
if (platform.getName().equals(platformName)) {
120-
if (Manager.compareVersions(platform.getVersion(), version) == 0) {
122+
if (Version.compare(platform.getVersion(), version) == 0) {
121123
return platform;
122124
}
123125
}
@@ -142,7 +144,7 @@ public Tool getLatestTool(String toolName) {
142144
Tool latestTool = null;
143145
for (Tool tool : this.tools) {
144146
if (tool.getName().equals(toolName)) {
145-
if (latestTool == null || Manager.compareVersions(tool.getVersion(), latestTool.getVersion()) > 0) {
147+
if (latestTool == null || Version.compare(tool.getVersion(), latestTool.getVersion()) > 0) {
146148
latestTool = tool;
147149
}
148150
}

io.sloeber.core/src/io/sloeber/core/tools/Helpers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ private static void setTheEnvironmentVariablesAddThePlatformInfo(IContributedEnv
706706
addPlatformFileTools(curPlatform, contribEnv, confDesc);
707707
if (curPlatform.isInstalled() && "avr".equalsIgnoreCase(curPlatform.getArchitecture())
708708
&& "arduino".equalsIgnoreCase(curPlatform.getPackage().getMaintainer())) {
709-
if (Manager.compareVersions(curPlatform.getVersion(), curversion) > 0) {
709+
if (Version.compare(curPlatform.getVersion(), curversion) > 0) {
710710
curversion = curPlatform.getVersion();
711711
platform = curPlatform;
712712
}

io.sloeber.core/src/io/sloeber/core/tools/Libraries.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,15 @@ public static Map<String, IPath> findAllArduinoManagerLibraries() {
108108

109109
String[] versions = Lib_root.toFile().list();
110110
if (versions != null) {
111-
if (versions.length == 1) {// There can only be 1
111+
if (versions.length == 1) {// There should only be 1
112112
// version of a lib
113113
ret.put(curLib, Lib_root.append(versions[0]));
114+
} else {// If there is more than 1 take the latest and
115+
// drop a warning
116+
int highestVersion = Version.getHighestVersionn(versions);
117+
ret.put(curLib, Lib_root.append(versions[highestVersion]));
118+
Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID,
119+
Messages.MultipleVersionsOfLib.replace("${LIB}", curLib))); //$NON-NLS-1$
114120
}
115121
}
116122
}

io.sloeber.core/src/io/sloeber/core/tools/Messages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class Messages extends NLS {
66
private static final String BUNDLE_NAME = "io.sloeber.core.tools.messages"; //$NON-NLS-1$
7+
public static String MultipleVersionsOfLib;
78
public static String Boards_Failed_to_read_boards;
89
public static String Boards_Get_menu_item_name_from_id_did_not_find;
910
public static String Boards_name;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.sloeber.core.tools;
2+
3+
public class Version {
4+
/**
5+
* compares 2 strings as if they are version numbers if version1<version2
6+
* returns -1 if version1==version2(also if both are null) returns 0 else
7+
* return 1 This method caters for the null case
8+
*
9+
* @param version1
10+
* @param version2
11+
* @return
12+
*/
13+
public static int compare(String version1, String version2) {
14+
if (version1 == null) {
15+
return version2 == null ? 0 : -1;
16+
}
17+
18+
if (version2 == null) {
19+
return 1;
20+
}
21+
22+
String[] v1 = version1.split("\\."); //$NON-NLS-1$
23+
String[] v2 = version2.split("\\."); //$NON-NLS-1$
24+
for (int i = 0; i < Math.max(v1.length, v2.length); ++i) {
25+
if (v1.length <= i) {
26+
return v2.length < i ? 0 : -1;
27+
}
28+
29+
if (v2.length <= i) {
30+
return 1;
31+
}
32+
33+
try {
34+
int vi1 = Integer.parseInt(v1[i]);
35+
int vi2 = Integer.parseInt(v2[i]);
36+
if (vi1 < vi2) {
37+
return -1;
38+
}
39+
40+
if (vi1 > vi2) {
41+
return 1;
42+
}
43+
} catch (NumberFormatException e) {
44+
// not numbers, do string compares
45+
int c = v1[i].compareTo(v2[i]);
46+
if (c < 0) {
47+
return -1;
48+
}
49+
if (c > 0) {
50+
return 1;
51+
}
52+
}
53+
}
54+
55+
return 0;
56+
}
57+
58+
/**
59+
* Given a list of version strings returns the index of the highest version
60+
* If the highest version is multiple times in the list the result will
61+
* point to one of those but the result may be different for each call
62+
*
63+
* @param versions
64+
* a string list of version numbers
65+
*
66+
* @return the index to the highest version
67+
*/
68+
public static int getHighestVersionn(String[] versions) {
69+
int returnIndex = 0;
70+
for (int curVersion = 1; curVersion < versions.length; curVersion++) {
71+
if (compare(versions[returnIndex], versions[curVersion]) == -1) {
72+
returnIndex = curVersion;
73+
}
74+
75+
}
76+
return returnIndex;
77+
}
78+
79+
}

io.sloeber.core/src/io/sloeber/core/tools/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ import_lib_failed=Failed to import library
3030
security_login=login
3131
security_password=password
3232
getMenuItemIDFromMenuItemName=getMenuItemIDFromMenuItemName did not find menu item with name "${MENUITEMNAME}" for menu ID "${MENUID}" and for boardID "${BOARDID}"!
33+
MultipleVersionsOfLib=Multiple versions of Library ${LIB} exist on disk. This is not supported.

0 commit comments

Comments
 (0)