Skip to content

Commit

Permalink
#1339 moved libraryTree class to gui
Browse files Browse the repository at this point in the history
still need to do the install part
removed the ([index name]) from the library name and added
the indexname to the tooltip
Added a (can update) tag when a newer version is available
  • Loading branch information
jantje committed Nov 9, 2021
1 parent 206dd61 commit 0f1493d
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 278 deletions.
1 change: 1 addition & 0 deletions io.sloeber.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +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.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 @@ -20,7 +20,6 @@
import com.google.gson.annotations.JsonAdapter;

import io.sloeber.core.api.Defaults;
import io.sloeber.core.api.LibraryDescriptor;
import io.sloeber.core.api.VersionNumber;

/**
Expand Down Expand Up @@ -123,11 +122,11 @@ public Map<String, LibraryJson> getLatestLibraries() {
*
* @return
*/
public Map<String, LibraryDescriptor> getLatestInstallableLibraries() {
Map<String, LibraryDescriptor> ret = new HashMap<>();
public Map<String, LibraryJson> getLatestInstallableLibraries() {
Map<String, LibraryJson> ret = new HashMap<>();
for (Entry<String, LibraryJson> curLibrary : this.latestLibs.entrySet()) {
if (!curLibrary.getValue().isAVersionInstalled()) {
ret.put(curLibrary.getKey(), new LibraryDescriptor(curLibrary.getValue()));
ret.put(curLibrary.getKey(), curLibrary.getValue());
}
}
return ret;
Expand Down Expand Up @@ -159,15 +158,15 @@ public String getName() {
*
* @return
*/
public Map<String, LibraryDescriptor> getLatestInstallableLibraries(Set<String> libNames) {
Map<String, LibraryDescriptor> ret = new HashMap<>();
public Map<String, LibraryJson> getLatestInstallableLibraries(Set<String> libNames) {
Map<String, LibraryJson> ret = new HashMap<>();
if (libNames.isEmpty()) {
return ret;
}
for (Entry<String, LibraryJson> curLibrary : this.latestLibs.entrySet()) {
if (libNames.contains(curLibrary.getKey())) {
if (!curLibrary.getValue().isAVersionInstalled()) {
ret.put(curLibrary.getKey(), new LibraryDescriptor(curLibrary.getValue()));
ret.put(curLibrary.getKey(), curLibrary.getValue());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ public class LibraryJson implements Comparable<LibraryJson> {
private String archiveFileName;
private int size;
private String checksum;
private LibraryIndexJson myParent;

public static final String LIBRARY_SOURCE_FODER = "src"; //$NON-NLS-1$

@SuppressWarnings("nls")
public LibraryJson(JsonElement json, LibraryIndexJson libraryIndexJson) {
JsonObject jsonObject = json.getAsJsonObject();
try {
myParent = libraryIndexJson;
name = getSafeString(jsonObject, "name");
version = getSafeVersion(jsonObject, "version");
author = getSafeString(jsonObject, "author");
Expand Down Expand Up @@ -221,7 +224,11 @@ public Collection<IPath> getSources(IProject project) {

@Override
public int compareTo(LibraryJson other) {
return this.name.compareTo(other.name);
int ret = this.name.compareTo(other.name);
if (ret == 0) {
ret = this.version.compareTo(other.version);
}
return ret;
}

/**
Expand All @@ -247,4 +254,7 @@ public IStatus remove(IProgressMonitor monitor) {
return Status.OK_STATUS;
}

public LibraryIndexJson getParent() {
return myParent;
}
}
207 changes: 13 additions & 194 deletions io.sloeber.core/src/io/sloeber/core/api/LibraryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
Expand Down Expand Up @@ -62,194 +60,15 @@ static public List<LibraryIndexJson> getLibraryIndices() {
return libraryIndices;
}

public static LibraryTree getLibraryTree() {
return new LibraryTree();

}

public static class LibraryTree {

private TreeMap<String, Category> categories = new TreeMap<>();

public class Category implements Comparable<Category>, Node {
private String name;
protected TreeMap<String, Library> libraries = new TreeMap<>();

public Category(String name) {
this.name = name;
}

@Override
public String getName() {
return this.name;
}

public Collection<Library> getLibraries() {
return this.libraries.values();
}

@Override
public int compareTo(Category other) {
return this.name.compareTo(other.name);
}

@Override
public boolean hasChildren() {
return !this.libraries.isEmpty();
}

@Override
public Object[] getChildren() {
return this.libraries.values().toArray();
}

@Override
public Object getParent() {
return LibraryTree.this;
}
}

public class Library implements Comparable<Library>, Node {
private String name;
private String indexName;
private Category category;
protected TreeSet<VersionNumber> versions = new TreeSet<>();
protected VersionNumber version;
private String tooltip;

public Library(Category category, String name, String indexName, String tooltip) {
this.category = category;
this.name = name;
this.tooltip = tooltip;
this.indexName = indexName;
}

public Collection<VersionNumber> getVersions() {
return this.versions;
}

@Override
public String getName() {
return name;
}

public String getTooltip() {
return tooltip;
}

public VersionNumber getLatest() {
return versions.last();
}

public VersionNumber getVersion() {
return version;
}

public String getIndexName() {
return indexName;
}

public void setVersion(VersionNumber version) {
this.version = version;
}

@Override
public int compareTo(Library other) {
return this.name.compareTo(other.name);
}

@Override
public boolean hasChildren() {
return false;
}

@Override
public Object[] getChildren() {
return null;
}

@Override
public Object getParent() {
return this.category;
}
}

public LibraryTree() {
for (LibraryIndexJson libraryIndex : getLibraryIndices()) {
for (String categoryName : libraryIndex.getCategories()) {
Category category = this.categories.get(categoryName);
if (category == null) {
category = new Category(categoryName);
this.categories.put(category.getName(), category);
}
for (io.sloeber.core.api.Json.library.LibraryJson library : libraryIndex
.getLibraries(categoryName)) {
Library lib = category.libraries.get(library.getName() + " (" + libraryIndex.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
if (lib == null) {
String builder = "Architectures:" + library.getArchitectures().toString() + "\n\n" //$NON-NLS-1$ //$NON-NLS-2$
+ library.getSentence();
lib = new Library(category, library.getName(), libraryIndex.getName(), builder);
category.libraries.put(library.getName() + " (" + libraryIndex.getName() + ")", lib); //$NON-NLS-1$//$NON-NLS-2$
}
lib.versions.add(library.getVersion());
if (library.isInstalled()) {
lib.version = library.getVersion();
}
}
}
}
}

public Collection<Category> getCategories() {
return this.categories.values();
}

public Collection<Library> getAllLibraries() {
Set<Library> all = new TreeSet<>();
for (Category category : this.categories.values()) {
all.addAll(category.getLibraries());
}
return all;
}

private static LibraryIndexJson findLibraryIndex(String name) {
for (LibraryIndexJson libraryIndex : getLibraryIndices()) {
if (libraryIndex.getName().equals(name))
return libraryIndex;
}
return null;
}

public void reset() {
for (Library library : this.getAllLibraries()) {
LibraryIndexJson libraryIndex = findLibraryIndex(library.getIndexName());

if (libraryIndex != null) {
io.sloeber.core.api.Json.library.LibraryJson installed = libraryIndex
.getInstalledLibrary(library.getName());
library.setVersion(installed != null ? installed.getVersion() : null);
}
}
public static IStatus setLibraryTree(List<LibraryJson> removeLibs, List<LibraryJson> addLibs,
IProgressMonitor monitor, MultiStatus status) {
for (LibraryJson lib : removeLibs) {
status.add(lib.remove(monitor));
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
}

}

public static IStatus setLibraryTree(LibraryTree libs, IProgressMonitor monitor, MultiStatus status) {
for (LibraryTree.Library lib : libs.getAllLibraries()) {
LibraryIndexJson libraryIndex = getLibraryIndex(lib.getIndexName());

if (libraryIndex != null) {
io.sloeber.core.api.Json.library.LibraryJson toRemove = libraryIndex.getInstalledLibrary(lib.getName());
if (toRemove != null && !toRemove.getVersion().equals(lib.getVersion())) {
status.add(toRemove.remove(monitor));
}
io.sloeber.core.api.Json.library.LibraryJson toInstall = libraryIndex.getLibrary(lib.getName(),
lib.getVersion());
if (toInstall != null && !toInstall.isInstalled()) {
status.add(toInstall.install(monitor));
}
}

for (LibraryJson lib : addLibs) {
status.add(lib.install(monitor));
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
}
Expand Down Expand Up @@ -307,10 +126,10 @@ static public void loadJson(File jsonFile) {
public static void installLibrary(String libName) {
Set<String> libNamesToInstall = new TreeSet<>();
libNamesToInstall.add(libName);
Map<String, LibraryDescriptor> libsToInstall = LibraryManager.getLatestInstallableLibraries(libNamesToInstall);
Map<String, LibraryJson> libsToInstall = LibraryManager.getLatestInstallableLibraries(libNamesToInstall);
if (!libsToInstall.isEmpty()) {
for (Entry<String, LibraryDescriptor> curLib : libsToInstall.entrySet()) {
curLib.getValue().toLibrary().install(new NullProgressMonitor());
for (LibraryJson curLib : libsToInstall.values()) {
curLib.install(new NullProgressMonitor());
}
}
}
Expand Down Expand Up @@ -371,9 +190,9 @@ public static void removeAllLibs() {
}
}

public static Map<String, LibraryDescriptor> getLatestInstallableLibraries(Set<String> libnames) {
public static Map<String, LibraryJson> getLatestInstallableLibraries(Set<String> libnames) {
Set<String> remainingLibNames = new TreeSet<>(libnames);
Map<String, LibraryDescriptor> ret = new HashMap<>();
Map<String, LibraryJson> ret = new HashMap<>();
for (LibraryIndexJson libraryIndex : libraryIndices) {
ret.putAll(libraryIndex.getLatestInstallableLibraries(remainingLibNames));
remainingLibNames.removeAll(ret.keySet());
Expand Down
11 changes: 0 additions & 11 deletions io.sloeber.core/src/io/sloeber/core/api/Node.java

This file was deleted.

7 changes: 3 additions & 4 deletions io.sloeber.core/src/io/sloeber/core/tools/Libraries.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import io.sloeber.core.api.BoardDescription;
import io.sloeber.core.api.IInstallLibraryHandler;
import io.sloeber.core.api.LibraryDescriptor;
import io.sloeber.core.api.LibraryManager;
import io.sloeber.core.api.SloeberProject;
import io.sloeber.core.api.VersionNumber;
Expand Down Expand Up @@ -423,7 +422,7 @@ public static void checkLibraries(IProject affectedProject) {
if (!uninstalledIncludedHeaders.isEmpty()) {
// some libraries may need to be installed

Map<String, LibraryDescriptor> availableLibs = LibraryManager
Map<String, LibraryJson> availableLibs = LibraryManager
.getLatestInstallableLibraries(uninstalledIncludedHeaders);

if (!availableLibs.isEmpty()) {
Expand All @@ -432,8 +431,8 @@ public static void checkLibraries(IProject affectedProject) {
// be some user
// interaction
availableLibs = installHandler.selectLibrariesToInstall(availableLibs);
for (Entry<String, LibraryDescriptor> curLib : availableLibs.entrySet()) {
curLib.getValue().toLibrary().install(new NullProgressMonitor());
for (Entry<String, LibraryJson> curLib : availableLibs.entrySet()) {
curLib.getValue().install(new NullProgressMonitor());
}
}
}
Expand Down
Loading

0 comments on commit 0f1493d

Please sign in to comment.