Skip to content

Commit

Permalink
#1339 introduced library and LibraryVersion library manager now works
Browse files Browse the repository at this point in the history
Still lots of work to do
  • Loading branch information
jantje committed Nov 10, 2021
1 parent 0a880a1 commit 517418f
Show file tree
Hide file tree
Showing 17 changed files with 715 additions and 634 deletions.
2 changes: 1 addition & 1 deletion io.sloeber.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Export-Package: cc.arduino.packages;x-internal:=true,
cc.arduino.packages.ssh;x-internal:=true,
io.sloeber.core;x-friends:="io.sloeber.tests",
io.sloeber.core.api,
io.sloeber.core.api.Json.library,
io.sloeber.core.api.Json,
io.sloeber.core.api.Json.packages,
io.sloeber.core.builder;x-internal:=true,
io.sloeber.core.common;x-friends:="io.sloeber.tests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Map;

import io.sloeber.core.api.Json.library.LibraryJson;
import io.sloeber.core.api.Json.ArduinoLibraryVersion;

/**
* this interface is to allow the ui to handle the automatic installation
Expand Down Expand Up @@ -34,5 +34,5 @@ public interface IInstallLibraryHandler {
*
* @return The libraries the user wants to install
*/
abstract Map<String, LibraryJson> selectLibrariesToInstall(Map<String, LibraryJson> proposedLibsToInstall);
abstract Map<String, ArduinoLibraryVersion> selectLibrariesToInstall(Map<String, ArduinoLibraryVersion> proposedLibsToInstall);
}
161 changes: 161 additions & 0 deletions io.sloeber.core/src/io/sloeber/core/api/Json/ArduinoLibrary.java
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 io.sloeber.core/src/io/sloeber/core/api/Json/ArduinoLibraryIndex.java
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();
}

}
Loading

0 comments on commit 517418f

Please sign in to comment.