-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1339 introduced library and LibraryVersion library manager now works
Still lots of work to do
- Loading branch information
jantje
committed
Nov 10, 2021
1 parent
0a880a1
commit 517418f
Showing
17 changed files
with
715 additions
and
634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
io.sloeber.core/src/io/sloeber/core/api/Json/ArduinoLibrary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package io.sloeber.core.api.Json; | ||
|
||
import static io.sloeber.core.Gson.GsonConverter.*; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.TreeMap; | ||
|
||
import org.eclipse.core.runtime.IPath; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
|
||
import io.sloeber.core.api.VersionNumber; | ||
import io.sloeber.core.common.ConfigurationPreferences; | ||
|
||
/** | ||
* This class represents an entry ina a library json file | ||
* | ||
* @author jan | ||
* | ||
*/ | ||
|
||
public class ArduinoLibrary extends Node implements Comparable<ArduinoLibrary> { | ||
|
||
private String name; | ||
private TreeMap<VersionNumber, ArduinoLibraryVersion> versions = new TreeMap<>(Collections.reverseOrder()); | ||
private ArduinoLibraryIndex myParent; | ||
|
||
private static final String VERSION_KEY = "version"; //$NON-NLS-1$ | ||
|
||
@SuppressWarnings("nls") | ||
public ArduinoLibrary(JsonElement json, ArduinoLibraryIndex libraryIndexJson) { | ||
JsonObject jsonObject = json.getAsJsonObject(); | ||
try { | ||
myParent = libraryIndexJson; | ||
name = getSafeString(jsonObject, "name"); | ||
addVersion(json); | ||
} catch (Exception e) { | ||
throw new JsonParseException("failed to parse json " + e.getMessage()); | ||
} | ||
|
||
} | ||
|
||
protected void addVersion(JsonElement json) { | ||
JsonObject jsonObject = json.getAsJsonObject(); | ||
VersionNumber versionNumber = getSafeVersion(jsonObject, VERSION_KEY); | ||
versions.put(versionNumber, new ArduinoLibraryVersion(jsonObject, this)); | ||
} | ||
|
||
public Collection<ArduinoLibraryVersion> getVersions() { | ||
return versions.values(); | ||
} | ||
|
||
public String getAuthor() { | ||
return getNewestVersion().getAuthor(); | ||
} | ||
|
||
public String getMaintainer() { | ||
return getNewestVersion().getMaintainer(); | ||
} | ||
|
||
public String getSentence() { | ||
return getNewestVersion().getSentence(); | ||
} | ||
|
||
public String getParagraph() { | ||
return getNewestVersion().getParagraph(); | ||
} | ||
|
||
public String getWebsite() { | ||
return getNewestVersion().getWebsite(); | ||
} | ||
|
||
public String getCategory() { | ||
return getNewestVersion().getCategory(); | ||
} | ||
|
||
public List<String> getArchitectures() { | ||
return getNewestVersion().getArchitectures(); | ||
} | ||
|
||
public List<String> getTypes() { | ||
return getNewestVersion().getTypes(); | ||
} | ||
|
||
public String getUrl() { | ||
return getNewestVersion().getUrl(); | ||
} | ||
|
||
/** | ||
* Get the newest version of this library | ||
* | ||
* @return the newest version of this library | ||
*/ | ||
public ArduinoLibraryVersion getNewestVersion() { | ||
return versions.firstEntry().getValue(); | ||
} | ||
|
||
/** | ||
* Get the version that is installed | ||
* If no version is installed return NULL | ||
* | ||
* @return | ||
*/ | ||
public ArduinoLibraryVersion getInstalledVersion() { | ||
for (ArduinoLibraryVersion curVersion : versions.values()) { | ||
if (curVersion.isInstalled()) { | ||
return curVersion; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* checks if a version of this library is installed. | ||
* | ||
* @return true if a version is installed. false in case no version is installed | ||
*/ | ||
public boolean isInstalled() { | ||
return getInstalledVersion() != null; | ||
} | ||
|
||
@Override | ||
public int compareTo(ArduinoLibrary other) { | ||
return getID().compareTo(other.getID()); | ||
} | ||
|
||
//Below are the Node overrides | ||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Node getParent() { | ||
return myParent; | ||
} | ||
|
||
@Override | ||
public Node[] getChildren() { | ||
return versions.values().toArray(new Node[versions.size()]); | ||
} | ||
|
||
@Override | ||
public String getID() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public IPath getInstallPath() { | ||
return ConfigurationPreferences.getInstallationPathLibraries().append(this.name.replace(' ', '_')); | ||
} | ||
|
||
public ArduinoLibraryVersion getVersion(VersionNumber versionNumber) { | ||
return versions.get(versionNumber); | ||
} | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
io.sloeber.core/src/io/sloeber/core/api/Json/ArduinoLibraryIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package io.sloeber.core.api.Json; | ||
|
||
import static io.sloeber.core.Gson.GsonConverter.*; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Type; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
|
||
import org.eclipse.core.runtime.IPath; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.annotations.JsonAdapter; | ||
|
||
/** | ||
* This class represents a json file that references libraries | ||
* | ||
* @author jan | ||
* | ||
*/ | ||
@JsonAdapter(ArduinoLibraryIndex.class) | ||
public class ArduinoLibraryIndex extends Node | ||
implements Comparable<ArduinoLibraryIndex>, JsonDeserializer<ArduinoLibraryIndex> { | ||
private TreeMap<String, ArduinoLibrary> libraries = new TreeMap<>(); | ||
private transient File jsonFile; | ||
|
||
public void setJsonFile(File packageFile) { | ||
jsonFile = packageFile; | ||
} | ||
|
||
/** | ||
* given a library name provide the library | ||
* | ||
* @param libraryName | ||
* @return the library or null if not found | ||
*/ | ||
public ArduinoLibrary getLibrary(String libraryName) { | ||
return libraries.get(libraryName); | ||
} | ||
|
||
/** | ||
* get all the latest versions of all the libraries provided that can be | ||
* installed but are not yet installed To do so I find all latest libraries and | ||
* I remove the once that are installed. | ||
* | ||
* @return | ||
*/ | ||
public Map<String, ArduinoLibraryVersion> getLatestInstallableLibraries(Set<String> libNames) { | ||
Map<String, ArduinoLibraryVersion> ret = new HashMap<>(); | ||
if (libNames.isEmpty()) { | ||
return ret; | ||
} | ||
for (ArduinoLibrary curLibrary : libraries.values()) { | ||
if (libNames.contains(curLibrary.getName())) { | ||
if (!curLibrary.isInstalled()) { | ||
ret.put(curLibrary.getName(), curLibrary.getNewestVersion()); | ||
} | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
@SuppressWarnings("nls") | ||
@Override | ||
public ArduinoLibraryIndex deserialize(JsonElement json, Type arg1, JsonDeserializationContext arg2) | ||
throws JsonParseException { | ||
JsonObject jsonObject = json.getAsJsonObject(); | ||
|
||
try { | ||
for (JsonElement curElement : jsonObject.get("libraries").getAsJsonArray()) { | ||
JsonObject jsonObject2 = curElement.getAsJsonObject(); | ||
String libName = getSafeString(jsonObject2, "name"); | ||
ArduinoLibrary library = libraries.get(libName); | ||
if (library == null) { | ||
libraries.put(libName, new ArduinoLibrary(curElement, this)); | ||
} else { | ||
library.addVersion(curElement); | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new JsonParseException("failed to parse LibraryIndexJson json " + e.getMessage(), e); | ||
} | ||
|
||
return this; | ||
|
||
} | ||
|
||
@Override | ||
public String getName() { | ||
return jsonFile.getName(); | ||
} | ||
|
||
@Override | ||
public Node[] getChildren() { | ||
return libraries.values().toArray(new Node[libraries.size()]); | ||
} | ||
|
||
@Override | ||
public Node getParent() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getID() { | ||
return getName(); | ||
} | ||
|
||
@Override | ||
public IPath getInstallPath() { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public int compareTo(ArduinoLibraryIndex o) { | ||
return getID().compareTo(o.getID()); | ||
} | ||
|
||
public Collection<ArduinoLibrary> getLibraries() { | ||
return libraries.values(); | ||
} | ||
|
||
} |
Oops, something went wrong.