Skip to content

Commit 1fd4470

Browse files
author
jantje
committed
#1339 introduced library and LibraryVersion library manager now works
Still lots of work to do
1 parent b201237 commit 1fd4470

17 files changed

+715
-634
lines changed

io.sloeber.core/META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Export-Package: cc.arduino.packages;x-internal:=true,
3232
cc.arduino.packages.ssh;x-internal:=true,
3333
io.sloeber.core;x-friends:="io.sloeber.tests",
3434
io.sloeber.core.api,
35-
io.sloeber.core.api.Json.library,
35+
io.sloeber.core.api.Json,
3636
io.sloeber.core.api.Json.packages,
3737
io.sloeber.core.builder;x-internal:=true,
3838
io.sloeber.core.common;x-friends:="io.sloeber.tests",

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.Map;
44

5-
import io.sloeber.core.api.Json.library.LibraryJson;
5+
import io.sloeber.core.api.Json.ArduinoLibraryVersion;
66

77
/**
88
* this interface is to allow the ui to handle the automatic installation
@@ -34,5 +34,5 @@ public interface IInstallLibraryHandler {
3434
*
3535
* @return The libraries the user wants to install
3636
*/
37-
abstract Map<String, LibraryJson> selectLibrariesToInstall(Map<String, LibraryJson> proposedLibsToInstall);
37+
abstract Map<String, ArduinoLibraryVersion> selectLibrariesToInstall(Map<String, ArduinoLibraryVersion> proposedLibsToInstall);
3838
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package io.sloeber.core.api.Json;
2+
3+
import static io.sloeber.core.Gson.GsonConverter.*;
4+
5+
import java.util.Collection;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.TreeMap;
9+
10+
import org.eclipse.core.runtime.IPath;
11+
12+
import com.google.gson.JsonElement;
13+
import com.google.gson.JsonObject;
14+
import com.google.gson.JsonParseException;
15+
16+
import io.sloeber.core.api.VersionNumber;
17+
import io.sloeber.core.common.ConfigurationPreferences;
18+
19+
/**
20+
* This class represents an entry ina a library json file
21+
*
22+
* @author jan
23+
*
24+
*/
25+
26+
public class ArduinoLibrary extends Node implements Comparable<ArduinoLibrary> {
27+
28+
private String name;
29+
private TreeMap<VersionNumber, ArduinoLibraryVersion> versions = new TreeMap<>(Collections.reverseOrder());
30+
private ArduinoLibraryIndex myParent;
31+
32+
private static final String VERSION_KEY = "version"; //$NON-NLS-1$
33+
34+
@SuppressWarnings("nls")
35+
public ArduinoLibrary(JsonElement json, ArduinoLibraryIndex libraryIndexJson) {
36+
JsonObject jsonObject = json.getAsJsonObject();
37+
try {
38+
myParent = libraryIndexJson;
39+
name = getSafeString(jsonObject, "name");
40+
addVersion(json);
41+
} catch (Exception e) {
42+
throw new JsonParseException("failed to parse json " + e.getMessage());
43+
}
44+
45+
}
46+
47+
protected void addVersion(JsonElement json) {
48+
JsonObject jsonObject = json.getAsJsonObject();
49+
VersionNumber versionNumber = getSafeVersion(jsonObject, VERSION_KEY);
50+
versions.put(versionNumber, new ArduinoLibraryVersion(jsonObject, this));
51+
}
52+
53+
public Collection<ArduinoLibraryVersion> getVersions() {
54+
return versions.values();
55+
}
56+
57+
public String getAuthor() {
58+
return getNewestVersion().getAuthor();
59+
}
60+
61+
public String getMaintainer() {
62+
return getNewestVersion().getMaintainer();
63+
}
64+
65+
public String getSentence() {
66+
return getNewestVersion().getSentence();
67+
}
68+
69+
public String getParagraph() {
70+
return getNewestVersion().getParagraph();
71+
}
72+
73+
public String getWebsite() {
74+
return getNewestVersion().getWebsite();
75+
}
76+
77+
public String getCategory() {
78+
return getNewestVersion().getCategory();
79+
}
80+
81+
public List<String> getArchitectures() {
82+
return getNewestVersion().getArchitectures();
83+
}
84+
85+
public List<String> getTypes() {
86+
return getNewestVersion().getTypes();
87+
}
88+
89+
public String getUrl() {
90+
return getNewestVersion().getUrl();
91+
}
92+
93+
/**
94+
* Get the newest version of this library
95+
*
96+
* @return the newest version of this library
97+
*/
98+
public ArduinoLibraryVersion getNewestVersion() {
99+
return versions.firstEntry().getValue();
100+
}
101+
102+
/**
103+
* Get the version that is installed
104+
* If no version is installed return NULL
105+
*
106+
* @return
107+
*/
108+
public ArduinoLibraryVersion getInstalledVersion() {
109+
for (ArduinoLibraryVersion curVersion : versions.values()) {
110+
if (curVersion.isInstalled()) {
111+
return curVersion;
112+
}
113+
}
114+
return null;
115+
}
116+
117+
/**
118+
* checks if a version of this library is installed.
119+
*
120+
* @return true if a version is installed. false in case no version is installed
121+
*/
122+
public boolean isInstalled() {
123+
return getInstalledVersion() != null;
124+
}
125+
126+
@Override
127+
public int compareTo(ArduinoLibrary other) {
128+
return getID().compareTo(other.getID());
129+
}
130+
131+
//Below are the Node overrides
132+
@Override
133+
public String getName() {
134+
return name;
135+
}
136+
137+
@Override
138+
public Node getParent() {
139+
return myParent;
140+
}
141+
142+
@Override
143+
public Node[] getChildren() {
144+
return versions.values().toArray(new Node[versions.size()]);
145+
}
146+
147+
@Override
148+
public String getID() {
149+
return name;
150+
}
151+
152+
@Override
153+
public IPath getInstallPath() {
154+
return ConfigurationPreferences.getInstallationPathLibraries().append(this.name.replace(' ', '_'));
155+
}
156+
157+
public ArduinoLibraryVersion getVersion(VersionNumber versionNumber) {
158+
return versions.get(versionNumber);
159+
}
160+
161+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package io.sloeber.core.api.Json;
2+
3+
import static io.sloeber.core.Gson.GsonConverter.*;
4+
5+
import java.io.File;
6+
import java.lang.reflect.Type;
7+
import java.util.Collection;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.Set;
11+
import java.util.TreeMap;
12+
13+
import org.eclipse.core.runtime.IPath;
14+
15+
import com.google.gson.JsonDeserializationContext;
16+
import com.google.gson.JsonDeserializer;
17+
import com.google.gson.JsonElement;
18+
import com.google.gson.JsonObject;
19+
import com.google.gson.JsonParseException;
20+
import com.google.gson.annotations.JsonAdapter;
21+
22+
/**
23+
* This class represents a json file that references libraries
24+
*
25+
* @author jan
26+
*
27+
*/
28+
@JsonAdapter(ArduinoLibraryIndex.class)
29+
public class ArduinoLibraryIndex extends Node
30+
implements Comparable<ArduinoLibraryIndex>, JsonDeserializer<ArduinoLibraryIndex> {
31+
private TreeMap<String, ArduinoLibrary> libraries = new TreeMap<>();
32+
private transient File jsonFile;
33+
34+
public void setJsonFile(File packageFile) {
35+
jsonFile = packageFile;
36+
}
37+
38+
/**
39+
* given a library name provide the library
40+
*
41+
* @param libraryName
42+
* @return the library or null if not found
43+
*/
44+
public ArduinoLibrary getLibrary(String libraryName) {
45+
return libraries.get(libraryName);
46+
}
47+
48+
/**
49+
* get all the latest versions of all the libraries provided that can be
50+
* installed but are not yet installed To do so I find all latest libraries and
51+
* I remove the once that are installed.
52+
*
53+
* @return
54+
*/
55+
public Map<String, ArduinoLibraryVersion> getLatestInstallableLibraries(Set<String> libNames) {
56+
Map<String, ArduinoLibraryVersion> ret = new HashMap<>();
57+
if (libNames.isEmpty()) {
58+
return ret;
59+
}
60+
for (ArduinoLibrary curLibrary : libraries.values()) {
61+
if (libNames.contains(curLibrary.getName())) {
62+
if (!curLibrary.isInstalled()) {
63+
ret.put(curLibrary.getName(), curLibrary.getNewestVersion());
64+
}
65+
}
66+
}
67+
return ret;
68+
}
69+
70+
@SuppressWarnings("nls")
71+
@Override
72+
public ArduinoLibraryIndex deserialize(JsonElement json, Type arg1, JsonDeserializationContext arg2)
73+
throws JsonParseException {
74+
JsonObject jsonObject = json.getAsJsonObject();
75+
76+
try {
77+
for (JsonElement curElement : jsonObject.get("libraries").getAsJsonArray()) {
78+
JsonObject jsonObject2 = curElement.getAsJsonObject();
79+
String libName = getSafeString(jsonObject2, "name");
80+
ArduinoLibrary library = libraries.get(libName);
81+
if (library == null) {
82+
libraries.put(libName, new ArduinoLibrary(curElement, this));
83+
} else {
84+
library.addVersion(curElement);
85+
}
86+
}
87+
} catch (Exception e) {
88+
throw new JsonParseException("failed to parse LibraryIndexJson json " + e.getMessage(), e);
89+
}
90+
91+
return this;
92+
93+
}
94+
95+
@Override
96+
public String getName() {
97+
return jsonFile.getName();
98+
}
99+
100+
@Override
101+
public Node[] getChildren() {
102+
return libraries.values().toArray(new Node[libraries.size()]);
103+
}
104+
105+
@Override
106+
public Node getParent() {
107+
return null;
108+
}
109+
110+
@Override
111+
public String getID() {
112+
return getName();
113+
}
114+
115+
@Override
116+
public IPath getInstallPath() {
117+
// TODO Auto-generated method stub
118+
return null;
119+
}
120+
121+
@Override
122+
public int compareTo(ArduinoLibraryIndex o) {
123+
return getID().compareTo(o.getID());
124+
}
125+
126+
public Collection<ArduinoLibrary> getLibraries() {
127+
return libraries.values();
128+
}
129+
130+
}

0 commit comments

Comments
 (0)