1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-03-13 10:29:35 +01:00

UserLibrary doesn't extend ContributedLibrary anymore

ContributedLibrary is used to decode library_index.json and it's
intended to keep data coming only from the index.

Now, when the library_index is synced with the filesystem, the
metadata about installed libraries are kept in a separate list
to not mess up with the main index.
This commit is contained in:
Cristian Maglie 2017-12-17 20:02:58 +01:00
parent 3ec6748dc4
commit e896595214
20 changed files with 83 additions and 94 deletions

View File

@ -31,7 +31,9 @@ package cc.arduino.contributions.libraries;
import java.util.Comparator;
public class LibraryByTypeComparator implements Comparator<ContributedLibrary> {
import processing.app.packages.UserLibrary;
public class LibraryByTypeComparator implements Comparator<UserLibrary> {
private final LibraryTypeComparator libraryTypeComparator;
@ -44,7 +46,7 @@ public class LibraryByTypeComparator implements Comparator<ContributedLibrary> {
}
@Override
public int compare(ContributedLibrary o1, ContributedLibrary o2) {
public int compare(UserLibrary o1, UserLibrary o2) {
if (o1.getTypes() == null) {
return 1;
}

View File

@ -31,10 +31,12 @@ package cc.arduino.contributions.libraries;
import java.util.Comparator;
public class LibraryOfSameTypeComparator implements Comparator<ContributedLibrary> {
import processing.app.packages.UserLibrary;
public class LibraryOfSameTypeComparator implements Comparator<UserLibrary> {
@Override
public int compare(ContributedLibrary o1, ContributedLibrary o2) {
public int compare(UserLibrary o1, UserLibrary o2) {
if (o1.getTypes() == null) {
return 1;
}

View File

@ -29,7 +29,6 @@
package cc.arduino.contributions.libraries.filters;
import cc.arduino.contributions.filters.InstalledPredicate;
import cc.arduino.contributions.libraries.ContributedLibrary;
import processing.app.BaseNoGui;
@ -40,14 +39,14 @@ public class InstalledLibraryPredicate implements Predicate<ContributedLibrary>
@Override
public boolean test(ContributedLibrary input) {
if (input.isInstalled()) {
if (input.isLibraryInstalled()) {
return true;
}
List<ContributedLibrary> libraries = BaseNoGui.librariesIndexer.getIndex().find(input.getName());
return libraries.stream()
.filter(new InstalledPredicate())
.filter(l -> l.isLibraryInstalled())
.count() > 0;
}

View File

@ -29,15 +29,14 @@
package cc.arduino.contributions.libraries.filters;
import cc.arduino.contributions.libraries.ContributedLibrary;
import processing.app.packages.UserLibrary;
import java.util.function.Predicate;
public class OnlyUpstreamReleasePredicate implements Predicate<ContributedLibrary> {
public class OnlyUpstreamReleasePredicate implements Predicate<Object> {
@Override
public boolean test(ContributedLibrary input) {
public boolean test(Object input) {
return !(input instanceof UserLibrary);
}

View File

@ -35,20 +35,21 @@ import processing.app.BaseNoGui;
import processing.app.packages.UserLibrary;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
public class UpdatableLibraryPredicate implements Predicate<ContributedLibrary> {
@Override
public boolean test(ContributedLibrary contributedLibrary) {
String libraryName = contributedLibrary.getName();
UserLibrary installed = BaseNoGui.librariesIndexer.getInstalledLibraries().getByName(libraryName);
if (installed == null) {
public boolean test(ContributedLibrary lib) {
Optional<UserLibrary> installed = lib.getInstalledLibrary();
if (!installed.isPresent()) {
return false;
}
String installedVersion = installed.get().getVersion();
String libraryName = lib.getName();
List<ContributedLibrary> libraries = BaseNoGui.librariesIndexer.getIndex().find(libraryName);
return libraries.stream()
.filter(library -> VersionComparator.greaterThan(library.getParsedVersion(), installed.getParsedVersion()))
.count() > 0;
.anyMatch(library -> VersionComparator.greaterThan(library.getParsedVersion(), installedVersion));
}
}

View File

@ -30,7 +30,6 @@
package cc.arduino.contributions.libraries.ui;
import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
import cc.arduino.contributions.filters.InstalledPredicate;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.ui.FilteredAbstractTableModel;
@ -78,13 +77,11 @@ public class ContributedLibraryReleases {
}
public Optional<ContributedLibrary> getInstalled() {
List<ContributedLibrary> installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList());
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
List<ContributedLibrary> installedReleases = releases.stream().filter(l -> l.isLibraryInstalled()).collect(Collectors.toList());
if (installedReleases.isEmpty()) {
return Optional.empty();
}
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
return Optional.of(installedReleases.get(0));
}

View File

@ -45,7 +45,6 @@ import javax.swing.JTable;
import cc.arduino.contributions.DownloadableContributionVersionComparator;
import cc.arduino.contributions.VersionComparator;
import cc.arduino.contributions.filters.BuiltInPredicate;
import cc.arduino.contributions.filters.InstalledPredicate;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate;
import cc.arduino.contributions.ui.InstallerTableCell;
@ -92,10 +91,10 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
.filter(new OnlyUpstreamReleasePredicate())
.collect(Collectors.toList());
List<ContributedLibrary> uninstalledReleases = releases.stream()
.filter(new InstalledPredicate().negate()).collect(Collectors.toList());
.filter(l -> !l.isLibraryInstalled()).collect(Collectors.toList());
List<ContributedLibrary> installedBuiltIn = releases.stream()
.filter(new InstalledPredicate()).filter(new BuiltInPredicate())
.filter(l -> !l.isLibraryInstalled()).filter(new BuiltInPredicate())
.collect(Collectors.toList());
if (mayInstalled.isPresent() && !installedBuiltIn.contains(mayInstalled.get())) {

View File

@ -38,6 +38,7 @@ import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
@ -134,7 +135,6 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibraryRelease
categoryChooser.removeActionListener(categoryChooserActionListener);
typeChooser.removeActionListener(typeChooserActionListener);
// Load categories
categoryFilter = x -> true;
categoryChooser.removeAllItems();
@ -158,7 +158,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibraryRelease
typeChooser.addItem(new DropdownAllLibraries());
typeChooser.addItem(new DropdownUpdatableLibrariesItem());
typeChooser.addItem(new DropdownInstalledLibraryItem());
java.util.List<String> types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes());
List<String> types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes());
Collections.sort(types, new LibraryTypeComparator());
for (String type : types) {
typeChooser.addItem(new DropdownLibraryOfTypeItem(type));

View File

@ -1059,8 +1059,8 @@ public class Base {
}
}
private List<ContributedLibrary> getSortedLibraries() {
List<ContributedLibrary> installedLibraries = new LinkedList<>(BaseNoGui.librariesIndexer.getInstalledLibraries());
private LibraryList getSortedLibraries() {
LibraryList installedLibraries = BaseNoGui.librariesIndexer.getInstalledLibraries();
Collections.sort(installedLibraries, new LibraryByTypeComparator());
Collections.sort(installedLibraries, new LibraryOfSameTypeComparator());
return installedLibraries;
@ -1097,9 +1097,9 @@ public class Base {
TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform();
if (targetPlatform != null) {
List<ContributedLibrary> libs = getSortedLibraries();
LibraryList libs = getSortedLibraries();
String lastLibType = null;
for (ContributedLibrary lib : libs) {
for (UserLibrary lib : libs) {
String libType = lib.getTypes().get(0);
if (!libType.equals(lastLibType)) {
if (lastLibType != null) {

View File

@ -24,12 +24,12 @@
package processing.app.syntax;
import cc.arduino.contributions.libraries.ContributedLibrary;
import org.apache.commons.compress.utils.IOUtils;
import org.fife.ui.rsyntaxtextarea.TokenMap;
import org.fife.ui.rsyntaxtextarea.TokenTypes;
import processing.app.Base;
import processing.app.BaseNoGui;
import processing.app.packages.UserLibrary;
import processing.app.debug.TargetPlatform;
import java.io.BufferedReader;
@ -89,7 +89,7 @@ public class PdeKeywords {
File platformKeywords = new File(tp.getFolder(), "keywords.txt");
if (platformKeywords.exists()) parseKeywordsTxt(platformKeywords);
}
for (ContributedLibrary lib : BaseNoGui.librariesIndexer.getInstalledLibraries()) {
for (UserLibrary lib : BaseNoGui.librariesIndexer.getInstalledLibraries()) {
File keywords = new File(lib.getInstalledFolder(), "keywords.txt");
if (keywords.exists()) {
parseKeywordsTxt(keywords);

View File

@ -35,6 +35,7 @@ import java.io.File;
public abstract class DownloadableContribution {
// XXX: maybe installed fields should not be here but in UserLibrary and ContributedPlatform?
private boolean installed;
private File installedFolder;

View File

@ -31,9 +31,11 @@ package cc.arduino.contributions.libraries;
import cc.arduino.contributions.DownloadableContribution;
import processing.app.I18n;
import processing.app.packages.UserLibrary;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import static processing.app.I18n.tr;
@ -65,6 +67,24 @@ public abstract class ContributedLibrary extends DownloadableContribution {
public static final Comparator<ContributedLibrary> CASE_INSENSITIVE_ORDER = (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName());
private Optional<UserLibrary> installedLib = Optional.empty();
public Optional<UserLibrary> getInstalledLibrary() {
return installedLib;
}
public boolean isLibraryInstalled() {
return installedLib.isPresent();
}
public void setInstalledUserLibrary(UserLibrary installed) {
this.installedLib = Optional.of(installed);
}
public void unsetInstalledUserLibrary() {
installedLib = Optional.empty();
}
/**
* Returns <b>true</b> if the library declares to support the specified
* architecture (through the "architectures" property field).

View File

@ -30,7 +30,6 @@
package cc.arduino.contributions.libraries;
import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
import cc.arduino.contributions.filters.InstalledPredicate;
import cc.arduino.contributions.libraries.filters.LibraryWithNamePredicate;
import java.util.*;
@ -92,7 +91,7 @@ public abstract class LibrariesIndex {
}
public Optional<ContributedLibrary> getInstalled(String name) {
List<ContributedLibrary> installedReleases = find(name).stream().filter(new InstalledPredicate()).collect(Collectors.toList());
List<ContributedLibrary> installedReleases = find(name).stream().filter(l -> l.isLibraryInstalled()).collect(Collectors.toList());
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
if (installedReleases.isEmpty()) {

View File

@ -122,7 +122,7 @@ public class LibrariesIndexer {
}
for (ContributedLibrary lib : index.getLibraries()) {
lib.setInstalled(false);
lib.unsetInstalledUserLibrary();
}
// Rescan libraries
@ -176,7 +176,6 @@ public class LibrariesIndexer {
if (!check.exists() || !check.isFile()) {
// Create a legacy library and exit
LegacyUserLibrary lib = LegacyUserLibrary.create(folder);
lib.setReadOnly(readOnly);
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
if (headers.length == 0) {
throw new IOException(lib.getSrcFolder().getAbsolutePath());
@ -187,7 +186,6 @@ public class LibrariesIndexer {
// Create a regular library
UserLibrary lib = UserLibrary.create(folder);
lib.setReadOnly(readOnly);
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
if (headers.length == 0) {
throw new IOException(lib.getSrcFolder().getAbsolutePath());
@ -196,15 +194,13 @@ public class LibrariesIndexer {
// Check if we can find the same library in the index
// and mark it as installed
ContributedLibrary foundLib = index.find(lib.getName(), lib.getParsedVersion());
ContributedLibrary foundLib = index.find(lib.getName(), lib.getVersion());
if (foundLib != null) {
foundLib.setInstalled(true);
foundLib.setInstalledFolder(folder);
foundLib.setReadOnly(readOnly);
foundLib.setInstalledUserLibrary(lib);
lib.setTypes(foundLib.getTypes());
}
if (lib.isReadOnly() && lib.getTypes() == null && !lib.getDeclaredTypes().isEmpty()) {
if (readOnly && lib.getTypes() == null && !lib.getDeclaredTypes().isEmpty()) {
lib.setTypes(lib.getDeclaredTypes());
}

View File

@ -84,7 +84,7 @@ public class LibraryInstaller {
}
public synchronized void install(ContributedLibrary lib, Optional<ContributedLibrary> mayReplacedLib, ProgressListener progressListener) throws Exception {
if (lib.isInstalled()) {
if (lib.isLibraryInstalled()) {
System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion()));
return;
}
@ -141,7 +141,7 @@ public class LibraryInstaller {
// Step 1: Remove library
progress.setStatus(I18n.format(tr("Removing library: {0}"), lib.getName()));
progressListener.onProgress(progress);
FileUtils.recursiveDelete(lib.getInstalledFolder());
FileUtils.recursiveDelete(lib.getInstalledLibrary().get().getInstalledFolder());
progress.stepDone();
// Step 2: Rescan index

View File

@ -29,15 +29,15 @@
package cc.arduino.contributions.libraries.filters;
import cc.arduino.contributions.libraries.ContributedLibrary;
import processing.app.BaseNoGui;
import processing.app.packages.UserLibrary;
import java.util.function.Predicate;
public class LibraryInstalledInsideCore implements Predicate<ContributedLibrary> {
public class LibraryInstalledInsideCore implements Predicate<UserLibrary> {
@Override
public boolean test(ContributedLibrary contributedLibrary) {
public boolean test(UserLibrary contributedLibrary) {
return BaseNoGui.indexer.isFolderInsidePlatform(contributedLibrary.getInstalledFolder());
}

View File

@ -29,11 +29,11 @@
package cc.arduino.contributions.libraries.filters;
import cc.arduino.contributions.libraries.ContributedLibrary;
import processing.app.packages.UserLibrary;
import java.util.function.Predicate;
public class TypePredicate implements Predicate<ContributedLibrary> {
public class TypePredicate implements Predicate<UserLibrary> {
private final String type;
@ -42,7 +42,7 @@ public class TypePredicate implements Predicate<ContributedLibrary> {
}
@Override
public boolean test(ContributedLibrary input) {
public boolean test(UserLibrary input) {
return input.getTypes() != null && input.getTypes().contains(type);
}

View File

@ -39,8 +39,7 @@ public class LegacyUserLibrary extends UserLibrary {
public static LegacyUserLibrary create(File libFolder) {
// construct an old style library
LegacyUserLibrary res = new LegacyUserLibrary();
res.setInstalledFolder(libFolder);
res.setInstalled(true);
res.installedFolder = libFolder;
res.layout = LibraryLayout.FLAT;
res.name = libFolder.getName();
res.setTypes(Arrays.asList("Contributed"));

View File

@ -33,7 +33,6 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import cc.arduino.contributions.libraries.ContributedLibrary;
import processing.app.helpers.FileUtils;
@SuppressWarnings("serial")
@ -80,7 +79,7 @@ public class LibraryList extends LinkedList<UserLibrary> {
}
public synchronized void sort() {
Collections.sort(this, ContributedLibrary.CASE_INSENSITIVE_ORDER);
Collections.sort(this, (x, y) -> x.getName().compareToIgnoreCase(y.getName()));
}
public synchronized LibraryList filterLibrariesInSubfolder(File subFolder) {

View File

@ -29,7 +29,7 @@
package processing.app.packages;
import cc.arduino.Constants;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.VersionHelper;
import cc.arduino.contributions.libraries.ContributedLibraryReference;
import processing.app.helpers.PreferencesMap;
@ -41,7 +41,9 @@ import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class UserLibrary extends ContributedLibrary {
import com.github.zafarkhaja.semver.Version;
public class UserLibrary {
private String name;
private String version;
@ -57,6 +59,7 @@ public class UserLibrary extends ContributedLibrary {
private List<String> declaredTypes;
private boolean onGoingDevelopment;
private List<String> includes;
protected File installedFolder;
public static UserLibrary create(File libFolder) throws IOException {
// Parse metadata
@ -139,11 +142,13 @@ public class UserLibrary extends ContributedLibrary {
includes.add(i.trim());
}
String declaredVersion = properties.get("version").trim();
Version version = VersionHelper.valueOf(declaredVersion);
UserLibrary res = new UserLibrary();
res.setInstalledFolder(libFolder);
res.setInstalled(true);
res.installedFolder = libFolder;
res.name = properties.get("name").trim();
res.version = properties.get("version").trim();
res.version = version.toString();
res.author = properties.get("author").trim();
res.maintainer = properties.get("maintainer").trim();
res.sentence = properties.get("sentence").trim();
@ -159,42 +164,34 @@ public class UserLibrary extends ContributedLibrary {
return res;
}
@Override
public String getName() {
return name;
}
@Override
public List<String> getArchitectures() {
return architectures;
}
@Override
public String getAuthor() {
return author;
}
@Override
public String getParagraph() {
return paragraph;
}
@Override
public String getSentence() {
return sentence;
}
@Override
public String getWebsite() {
return website;
}
@Override
public String getCategory() {
return category;
}
@Override
public List<String> getTypes() {
return types;
}
@ -203,47 +200,22 @@ public class UserLibrary extends ContributedLibrary {
this.types = types;
}
@Override
public String getLicense() {
return license;
}
@Override
public void setCategory(String category) {
this.category = category;
}
@Override
public String getVersion() {
return version;
}
@Override
public String getMaintainer() {
return maintainer;
}
@Override
public String getChecksum() {
return null;
}
@Override
public long getSize() {
return 0;
}
@Override
public String getUrl() {
return null;
}
@Override
public String getArchiveFileName() {
return null;
}
@Override
public List<ContributedLibraryReference> getRequires() {
return null;
}
@ -269,9 +241,9 @@ public class UserLibrary extends ContributedLibrary {
public File getSrcFolder() {
switch (layout) {
case FLAT:
return getInstalledFolder();
return installedFolder;
case RECURSIVE:
return new File(getInstalledFolder(), "src");
return new File(installedFolder, "src");
default:
return null; // Keep compiler happy :-(
}
@ -286,4 +258,8 @@ public class UserLibrary extends ContributedLibrary {
return name + ":" + version + " " + architectures + " " + installedFolder.getAbsolutePath();
}
public File getInstalledFolder() {
return installedFolder;
}
}