1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-12 06:54:24 +01:00

Library manager through arduino-cli (WIP, part 1)

- library index is now fetched arduino-cli
- ContributedLibraries and derivatives classes have been adapted
  to arduino-cli structure
- install/update/remove are temporary disabled
- library index updated is now done trough arduino-cli
- added progress wrapper

Next steps:

- detect installed libraries using arduino-cli
- implement install/update/remove using arduino-cli
This commit is contained in:
Cristian Maglie 2019-11-29 00:28:33 +01:00
parent cc26e75e96
commit f6ca1f7b16
18 changed files with 339 additions and 564 deletions

View File

@ -29,15 +29,14 @@
package cc.arduino.contributions.libraries.filters;
import java.util.List;
import java.util.function.Predicate;
import cc.arduino.contributions.VersionComparator;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
import cc.arduino.contributions.libraries.LibrariesIndexer;
import processing.app.BaseNoGui;
public class UpdatableLibraryPredicate implements Predicate<ContributedLibraryRelease> {
public class UpdatableLibraryPredicate implements Predicate<ContributedLibrary> {
LibrariesIndexer librariesIndexer;
@ -50,13 +49,11 @@ public class UpdatableLibraryPredicate implements Predicate<ContributedLibraryRe
}
@Override
public boolean test(ContributedLibraryRelease lib) {
if (!lib.isLibraryInstalled()) {
public boolean test(ContributedLibrary lib) {
if (!lib.getInstalled().isPresent()) {
return false;
}
String libraryName = lib.getName();
List<ContributedLibraryRelease> libraries = librariesIndexer.getIndex().find(libraryName);
ContributedLibraryRelease latest = libraries.stream().reduce(VersionComparator::max).get();
ContributedLibraryRelease latest = lib.getLatest().get(); // it must be present
return !latest.isLibraryInstalled();
}
}

View File

@ -36,18 +36,18 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ContributedLibraryReleasesComparator implements Comparator<ContributedLibrary> {
public class ContributedLibraryComparatorWithTypePriority implements Comparator<ContributedLibrary> {
private final String firstType;
public ContributedLibraryReleasesComparator(String firstType) {
public ContributedLibraryComparatorWithTypePriority(String firstType) {
this.firstType = firstType;
}
@Override
public int compare(ContributedLibrary o1, ContributedLibrary o2) {
ContributedLibraryRelease lib1 = o1.getLatest();
ContributedLibraryRelease lib2 = o2.getLatest();
ContributedLibraryRelease lib1 = o1.getLatest().get();
ContributedLibraryRelease lib2 = o2.getLatest().get();
List<String> types1 = lib1.getTypes();
List<String> types2 = lib2.getTypes();

View File

@ -33,6 +33,8 @@ import static processing.app.I18n.tr;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@ -43,39 +45,39 @@ import javax.swing.JTable;
import cc.arduino.contributions.DownloadableContributionVersionComparator;
import cc.arduino.contributions.VersionComparator;
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
import cc.arduino.contributions.ui.InstallerTableCell;
import cc.arduino.utils.ReverseComparator;
@SuppressWarnings("serial")
public class ContributedLibraryTableCellEditor extends InstallerTableCell {
private ContributedLibrary editorValue;
private ContributedLibrary editorLibrary;
private ContributedLibraryTableCellJPanel editorCell;
@Override
public Object getCellEditorValue() {
return editorValue;
return editorLibrary;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row,
int column) {
editorValue = (ContributedLibrary) value;
editorLibrary = (ContributedLibrary) value;
editorCell = new ContributedLibraryTableCellJPanel(table, value, true);
editorCell.installButton
.addActionListener(e -> onInstall(editorValue.getSelected(),
editorValue.getInstalled()));
.addActionListener(e -> onInstall(editorLibrary.getSelected().get(),
editorLibrary.getInstalled()));
editorCell.downgradeButton.addActionListener(e -> {
JComboBox chooser = editorCell.downgradeChooser;
ContributedLibraryRelease lib = (ContributedLibraryRelease) chooser.getSelectedItem();
onInstall(lib, editorValue.getInstalled());
onInstall(lib, editorLibrary.getInstalled());
});
editorCell.versionToInstallChooser.addActionListener(e -> {
editorValue.select((ContributedLibraryRelease) editorCell.versionToInstallChooser.getSelectedItem());
editorLibrary.select((ContributedLibraryRelease) editorCell.versionToInstallChooser.getSelectedItem());
if (editorCell.versionToInstallChooser.getSelectedIndex() != 0) {
InstallerTableCell.dropdownSelected(true);
}
@ -83,12 +85,12 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
setEnabled(true);
final Optional<ContributedLibraryRelease> mayInstalled = editorValue.getInstalled();
final Optional<ContributedLibraryRelease> mayInstalled = editorLibrary.getInstalled();
List<ContributedLibraryRelease> releases = editorValue.getReleases();
List<ContributedLibraryRelease> notInstalled = new LinkedList<>(releases);
Collection<ContributedLibraryRelease> releases = editorLibrary.getReleases();
List<ContributedLibraryRelease> notInstalled = new ArrayList<>(releases);
if (mayInstalled.isPresent()) {
notInstalled.remove(editorValue.getInstalled().get());
notInstalled.remove(mayInstalled.get());
}
Collections.sort(notInstalled, new ReverseComparator<>(

View File

@ -125,7 +125,7 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
if (releases == null)
return;
ContributedLibraryRelease selected = releases.getSelected();
ContributedLibraryRelease selected = releases.getSelected().get();
titledBorder.setTitle(selected.getName());
Optional<ContributedLibraryRelease> mayInstalled = releases.getInstalled();

View File

@ -54,7 +54,7 @@ public class DropdownLibraryOfCategoryItem implements DropdownItem<ContributedLi
return new Predicate<ContributedLibrary>() {
@Override
public boolean test(ContributedLibrary rel) {
ContributedLibraryRelease lib = rel.getLatest();
ContributedLibraryRelease lib = rel.getLatest().get();
return category.equals(lib.getCategory());
}
};

View File

@ -54,7 +54,7 @@ public class DropdownLibraryOfTypeItem implements DropdownItem<ContributedLibrar
return new Predicate<ContributedLibrary>() {
@Override
public boolean test(ContributedLibrary lib) {
List<String> types = lib.getLatest().getTypes();
List<String> types = lib.getLatest().get().getTypes();
return types != null && types.contains(type);
}
};

View File

@ -36,6 +36,7 @@ import cc.arduino.contributions.ui.FilteredAbstractTableModel;
import processing.app.BaseNoGui;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
@ -131,7 +132,7 @@ public class LibrariesIndexTableModel
}
public ContributedLibraryRelease getSelectedRelease(int row) {
return contributions.get(row).getSelected();
return contributions.get(row).getSelected().get();
}
public void update() {
@ -144,7 +145,7 @@ public class LibrariesIndexTableModel
return false;
}
ContributedLibraryRelease latest = lib.getLatest();
ContributedLibraryRelease latest = lib.getLatest().get();
String compoundTargetSearchText = latest.getName() + " "
+ latest.getParagraph() + " "
+ latest.getSentence();
@ -158,55 +159,12 @@ public class LibrariesIndexTableModel
return true;
}
public void updateLibrary(ContributedLibraryRelease lib) {
// Find the row interested in the change
int row = -1;
for (ContributedLibrary releases : contributions) {
if (releases.shouldContain(lib))
row = contributions.indexOf(releases);
}
updateContributions();
// If the library is found in the list send update event
// or insert event on the specific row...
for (ContributedLibrary releases : contributions) {
if (releases.shouldContain(lib)) {
if (row == -1) {
row = contributions.indexOf(releases);
fireTableRowsInserted(row, row);
} else {
fireTableRowsUpdated(row, row);
}
return;
}
}
// ...otherwise send a row deleted event
fireTableRowsDeleted(row, row);
}
private List<ContributedLibrary> rebuildContributionsFromIndex() {
List<ContributedLibrary> res = new ArrayList<>();
BaseNoGui.librariesIndexer.getIndex().getLibraries(). //
forEach(lib -> {
for (ContributedLibrary contribution : res) {
if (!contribution.shouldContain(lib))
continue;
contribution.add(lib);
return;
}
res.add(new ContributedLibrary(lib));
});
return res;
}
private void updateContributions() {
List<ContributedLibrary> all = rebuildContributionsFromIndex();
Collection<ContributedLibrary> all = BaseNoGui.librariesIndexer.getIndex().getLibraries();
contributions.clear();
all.stream().filter(this::filterCondition).forEach(contributions::add);
Collections.sort(contributions,
new ContributedLibraryReleasesComparator("Arduino"));
new ContributedLibraryComparatorWithTypePriority("Arduino"));
}
}

View File

@ -200,7 +200,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
installerThread = new Thread(() -> {
try {
setProgressVisible(true, "");
installer.updateIndex(this::setProgress);
BaseNoGui.getArduinoCoreService().updateLibrariesIndex(this::setProgress);
onIndexesUpdated();
if (contribTable.getCellEditor() != null) {
contribTable.getCellEditor().stopCellEditing();
@ -218,7 +218,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
}
public void onInstallPressed(final ContributedLibraryRelease lib) {
List<ContributedLibraryRelease> deps = BaseNoGui.librariesIndexer.getIndex().resolveDependeciesOf(lib);
List<ContributedLibraryRelease> deps = BaseNoGui.getArduinoCoreService().libraryResolveDependecies(lib);
boolean depsInstalled = deps.stream().allMatch(l -> l.getInstalledLibrary().isPresent() || l.getName().equals(lib.getName()));
Result installDeps;
if (!depsInstalled) {

View File

@ -27,6 +27,7 @@ import cc.arduino.Constants;
import cc.arduino.UpdatableBoardsLibsFakeURLsHandler;
import cc.arduino.UploaderUtils;
import cc.arduino.contributions.*;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
import cc.arduino.contributions.libraries.LibrariesIndexer;
import cc.arduino.contributions.libraries.LibraryInstaller;
@ -298,7 +299,7 @@ public class Base {
final GPGDetachedSignatureVerifier gpgDetachedSignatureVerifier = new GPGDetachedSignatureVerifier();
contributionInstaller = new ContributionInstaller(BaseNoGui.getPlatform(), gpgDetachedSignatureVerifier);
libraryInstaller = new LibraryInstaller(BaseNoGui.getPlatform(), gpgDetachedSignatureVerifier);
libraryInstaller = new LibraryInstaller(BaseNoGui.getPlatform());
parser.parseArgumentsPhase2();
@ -358,15 +359,15 @@ public class Base {
BaseNoGui.onBoardOrPortChange();
ProgressListener progressListener = new ConsoleProgressListener();
libraryInstaller.updateIndex(progressListener);
BaseNoGui.getArduinoCoreService().updateLibrariesIndex(progressListener);
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder());
indexer.parseIndex();
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getArduinoCoreService());
indexer.regenerateIndex();
indexer.setLibrariesFolders(BaseNoGui.getLibrariesFolders());
indexer.rescanLibraries();
for (String library : parser.getLibraryToInstall().split(",")) {
String[] libraryToInstallParts = library.split(":");
for (String libraryArg : parser.getLibraryToInstall().split(",")) {
String[] libraryToInstallParts = libraryArg.split(":");
ContributedLibraryRelease selected = null;
if (libraryToInstallParts.length == 2) {
@ -375,12 +376,11 @@ public class Base {
System.out.println(format(tr("Invalid version {0}"), libraryToInstallParts[1]));
System.exit(1);
}
selected = indexer.getIndex().find(libraryToInstallParts[0], version.get().toString());
selected = indexer.getIndex().find(libraryToInstallParts[0], version.get().toString()).orElse(null);
} else if (libraryToInstallParts.length == 1) {
List<ContributedLibraryRelease> librariesByName = indexer.getIndex().find(libraryToInstallParts[0]);
Collections.sort(librariesByName, new DownloadableContributionVersionComparator());
if (!librariesByName.isEmpty()) {
selected = librariesByName.get(librariesByName.size() - 1);
ContributedLibrary library = indexer.getIndex().find(libraryToInstallParts[0]).orElse(null);
if (library != null) {
selected = library.getLatest().orElse(null);
}
}
if (selected == null) {
@ -392,7 +392,7 @@ public class Base {
if (mayInstalled.isPresent() && selected.isIDEBuiltIn()) {
System.out.println(tr(I18n
.format("Library {0} is available as built-in in the IDE.\nRemoving the other version {1} installed in the sketchbook...",
library, mayInstalled.get().getParsedVersion())));
libraryArg, mayInstalled.get().getParsedVersion())));
libraryInstaller.remove(mayInstalled.get(), progressListener);
} else {
libraryInstaller.install(selected, progressListener);

View File

@ -1,82 +0,0 @@
package cc.arduino.contributions;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
import cc.arduino.contributions.libraries.LibrariesIndexer;
import processing.app.BaseNoGui;
import processing.app.packages.UserLibraryFolder;
import processing.app.packages.UserLibraryFolder.Location;
public class UpdatableLibraryTest {
File testdata = new File(
UpdatableLibraryTest.class.getResource("/").getFile(),
"../testdata/libraries");
File index_SD_only = new File(testdata, "index_SD_only");
File SD111 = new File(testdata, "SD_1.1.1");
File SD121 = new File(testdata, "SD_1.2.1");
File index_Bridge_only = new File(testdata, "index_Bridge_only");
File Bridge163 = new File(testdata, "Bridge_1.6.3");
File Bridge170 = new File(testdata, "Bridge_1.7.0");
@Test
public void testUpdatableLibrary() throws Exception {
List<UserLibraryFolder> folders = new ArrayList<>();
folders.add(new UserLibraryFolder(SD111, Location.IDE_BUILTIN));
LibrariesIndexer indexer = new LibrariesIndexer(index_SD_only);
BaseNoGui.librariesIndexer = indexer;
indexer.parseIndex();
indexer.setLibrariesFoldersAndRescan(folders);
ContributedLibraryRelease sdLib = indexer.getIndex().getInstalled("SD").get();
assertTrue("SD lib is installed", sdLib.isLibraryInstalled());
assertEquals("SD installed version", "1.1.1", sdLib.getParsedVersion());
assertTrue(ContributionsSelfCheck.checkForUpdatableLibraries());
folders.add(new UserLibraryFolder(SD121, Location.SKETCHBOOK));
indexer.setLibrariesFoldersAndRescan(folders);
sdLib = indexer.getIndex().getInstalled("SD").get();
assertTrue("SD lib is installed", sdLib.isLibraryInstalled());
assertEquals("SD installed version", "1.2.1", sdLib.getParsedVersion());
assertFalse(ContributionsSelfCheck.checkForUpdatableLibraries());
}
@Test
public void testUpdatableLibraryWithBundled() throws Exception {
List<UserLibraryFolder> folders = new ArrayList<>();
folders.add(new UserLibraryFolder(Bridge163, Location.IDE_BUILTIN));
LibrariesIndexer indexer = new LibrariesIndexer(index_Bridge_only);
BaseNoGui.librariesIndexer = indexer;
indexer.parseIndex();
indexer.setLibrariesFoldersAndRescan(folders);
ContributedLibraryRelease l = indexer.getIndex().getInstalled("Bridge").get();
assertTrue("Bridge lib is installed", l.isLibraryInstalled());
assertEquals("Bridge installed version", "1.6.3", l.getParsedVersion());
assertTrue(ContributionsSelfCheck.checkForUpdatableLibraries());
folders.add(new UserLibraryFolder(Bridge170, Location.SKETCHBOOK));
indexer.setLibrariesFoldersAndRescan(folders);
l = indexer.getIndex().getInstalled("Bridge").get();
assertTrue("Bridge lib is installed", l.isLibraryInstalled());
assertEquals("Bridge installed version", "1.7.0", l.getParsedVersion());
assertFalse(ContributionsSelfCheck.checkForUpdatableLibraries());
}
}

View File

@ -151,4 +151,9 @@ public class ArduinoCoreInstance {
throw e.getStatus().asException();
}
}
public List<ContributedLibraryRelease> libraryResolveDependecies(ContributedLibraryRelease lib) {
return new ArrayList<>();
}
}

View File

@ -29,56 +29,52 @@
package cc.arduino.contributions.libraries;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import cc.arduino.contributions.VersionComparator;
import processing.app.packages.UserLibraryFolder.Location;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
public class ContributedLibrary {
private List<ContributedLibraryRelease> releases = new LinkedList<>();
private List<String> versions = new LinkedList<>();
private ContributedLibraryRelease latest = null;
private ContributedLibraryRelease selected = null;
private String name;
// version -> release map
private Map<String, ContributedLibraryRelease> releases = new HashMap<>();
private Optional<ContributedLibraryRelease> latest = Optional.empty();
private Optional<ContributedLibraryRelease> selected = Optional.empty();
public ContributedLibrary(ContributedLibraryRelease release) {
add(release);
public ContributedLibrary(String name) {
this.name = name;
}
public ContributedLibrary(List<ContributedLibraryRelease> releases) {
releases.forEach(this::add);
public String getName() {
return name;
}
public List<ContributedLibraryRelease> getReleases() {
return releases;
public Collection<ContributedLibraryRelease> getReleases() {
return releases.values();
}
public boolean shouldContain(ContributedLibraryRelease release) {
if (latest == null) {
return true;
public Optional<ContributedLibraryRelease> getVersion(String version) {
return Optional.ofNullable(releases.get(version));
}
public void addRelease(ContributedLibraryRelease release) {
if (!latest.isPresent()) {
latest = Optional.of(release);
}
return release.getName().equals(latest.getName());
}
public void add(ContributedLibraryRelease release) {
if (latest == null) {
latest = release;
}
releases.add(release);
String version = release.getParsedVersion();
if (version != null) {
versions.add(version);
}
if (VersionComparator.greaterThan(version, latest.getParsedVersion())) {
latest = release;
releases.put(version, release);
if (VersionComparator.greaterThan(version, latest.get().getParsedVersion())) {
latest = Optional.of(release);
}
selected = latest;
}
public Optional<ContributedLibraryRelease> getInstalled() {
return releases.stream() //
return releases.values().stream() //
.filter(ContributedLibraryRelease::isLibraryInstalled) //
.reduce((x, y) -> {
Location lx = x.getInstalledLibrary().get().getLocation();
@ -90,18 +86,18 @@ public class ContributedLibrary {
});
}
public ContributedLibraryRelease getLatest() {
public Optional<ContributedLibraryRelease> getLatest() {
return latest;
}
public ContributedLibraryRelease getSelected() {
public Optional<ContributedLibraryRelease> getSelected() {
return selected;
}
public void select(ContributedLibraryRelease lib) {
for (ContributedLibraryRelease r : releases) {
for (ContributedLibraryRelease r : releases.values()) {
if (r == lib) {
selected = r;
selected = Optional.of(r);
return;
}
}

View File

@ -35,7 +35,6 @@ import processing.app.packages.UserLibrary;
import static processing.app.I18n.tr;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@ -43,61 +42,136 @@ import cc.arduino.contributions.VersionHelper;
public class ContributedLibraryRelease extends DownloadableContribution {
private String url;
private String version;
private String checksum;
private long size;
private String archiveFileName;
private String name;
private String maintainer;
private String author;
private String website;
private String category;
private String licence;
private String paragraph;
private String sentence;
private ArrayList<String> architectures;
private ArrayList<String> types;
private ArrayList<ContributedLibraryDependency> dependencies;
private ArrayList<String> providesIncludes;
final private ContributedLibrary library;
final private String maintainer;
final private String author;
final private String website;
final private String category;
final private String license;
final private String paragraph;
final private String sentence;
final private List<String> architectures;
final private List<String> types;
final private List<ContributedLibraryDependency> dependencies;
final private List<String> providesIncludes;
public String getUrl() { return url; }
public ContributedLibraryRelease(ContributedLibrary library,
String maintainer, String author,
String website, String category,
String license, String paragraph,
String sentence, List<String> architectures,
List<String> types,
List<ContributedLibraryDependency> dependencies,
List<String> providesIncludes, //
//
String url, String version, String checksum,
long size, String archiveFileName) {
this.library = library;
this.maintainer = maintainer;
this.author = author;
this.website = website;
this.category = category;
this.license = license;
this.paragraph = paragraph;
this.sentence = sentence;
this.architectures = architectures;
this.types = types;
this.dependencies = dependencies;
this.providesIncludes = providesIncludes;
public String getVersion() { return version; }
this.url = url;
this.version = version;
this.checksum = checksum;
this.size = size;
this.archiveFileName = archiveFileName;
}
public String getChecksum() { return checksum; }
public ContributedLibrary getLibrary() {
return library;
}
public long getSize() { return size; }
public String getName() {
return library.getName();
}
public String getArchiveFileName() { return archiveFileName; }
public String getMaintainer() {
return maintainer;
}
public String getName() { return name; }
public String getAuthor() {
return author;
}
public String getMaintainer() { return maintainer; }
public String getWebsite() {
return website;
}
public String getAuthor() { return author; }
public String getCategory() {
return category;
}
public String getWebsite() { return website; }
public String getLicense() {
return license;
}
public String getCategory() { return category; }
public String getParagraph() {
return paragraph;
}
public void setCategory(String category) { this.category = category; }
public String getSentence() {
return sentence;
}
public String getLicense() { return licence; }
public List<String> getArchitectures() {
return architectures;
}
public String getParagraph() { return paragraph; }
public List<String> getTypes() {
return types;
}
public String getSentence() { return sentence; }
public List<ContributedLibraryDependency> getDependencies() {
return dependencies;
}
public List<String> getArchitectures() { return architectures; }
public List<String> getProvidesIncludes() {
return providesIncludes;
}
public List<String> getTypes() { return types; }
// Implementation of DownloadableResource
final private String url;
final private String version;
final private String checksum;
final private long size;
final private String archiveFileName;
public List<ContributedLibraryDependency> getDependencies() { return dependencies; }
@Override
public String getUrl() {
return url;
}
public List<String> getProvidesIncludes() { return providesIncludes; }
@Override
public String getVersion() {
return version;
}
public static final Comparator<ContributedLibraryRelease> CASE_INSENSITIVE_ORDER = (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName());
@Override
public String getChecksum() {
return checksum;
}
@Override
public long getSize() {
return size;
}
@Override
public String getArchiveFileName() {
return archiveFileName;
}
public static final Comparator<ContributedLibraryRelease> //
CASE_INSENSITIVE_ORDER = (o1, o2) -> o1.getName()
.compareToIgnoreCase(o2.getName());
private Optional<UserLibrary> installedLib = Optional.empty();
@ -132,14 +206,16 @@ public class ContributedLibraryRelease extends DownloadableContribution {
* @return
*/
public boolean supportsArchitecture(String reqArch) {
return getArchitectures().contains(reqArch) || getArchitectures().contains("*");
return getArchitectures().contains(reqArch)
|| getArchitectures().contains("*");
}
/**
* Returns <b>true</b> if the library declares to support at least one of the
* specified architectures.
*
* @param reqArchs A List of architectures to check
* @param reqArchs
* A List of architectures to check
* @return
*/
public boolean supportsArchitecture(List<String> reqArchs) {

View File

@ -32,151 +32,60 @@ package cc.arduino.contributions.libraries;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import cc.arduino.contributions.VersionComparator;
import java.util.Set;
public class LibrariesIndex {
private ArrayList<ContributedLibraryRelease> list = new ArrayList<>();
// name -> library map
private Map<String, ContributedLibrary> libraries = new HashMap<>();
private Set<String> categories = new HashSet<>();
private Set<String> types = new HashSet<>();
public List<ContributedLibraryRelease> getLibraries() {
return list;
public Collection<ContributedLibrary> getLibraries() {
return libraries.values();
}
public List<ContributedLibraryRelease> find(final String name) {
return getLibraries().stream() //
.filter(l -> name.equals(l.getName())) //
.collect(Collectors.toList());
public void add(ContributedLibrary library) {
libraries.put(library.getName(), library);
library.getReleases().forEach(rel -> {
categories.add(rel.getCategory());
types.addAll(rel.getTypes());
});
}
public ContributedLibraryRelease find(String name, String version) {
if (name == null || version == null) {
return null;
}
for (ContributedLibraryRelease lib : find(name)) {
if (version.equals(lib.getParsedVersion())) {
return lib;
}
}
return null;
public Optional<ContributedLibrary> find(String name) {
return Optional.ofNullable(libraries.get(name));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (ContributedLibraryRelease library : getLibraries()) {
sb.append(library.toString());
public Optional<ContributedLibraryRelease> find(String name, String version) {
if (libraries.containsKey(name)) {
return libraries.get(name).getVersion(version);
}
return sb.toString();
return Optional.empty();
}
public List<String> getCategories() {
List<String> categories = new LinkedList<>();
for (ContributedLibraryRelease lib : getLibraries()) {
if (lib.getCategory() != null && !categories.contains(lib.getCategory())) {
categories.add(lib.getCategory());
}
}
Collections.sort(categories);
return categories;
List<String> res = new ArrayList<>(categories);
Collections.sort(res);
return res;
}
public List<String> getTypes() {
Collection<String> typesAccumulator = new HashSet<>();
for (ContributedLibraryRelease lib : getLibraries()) {
if (lib.getTypes() != null) {
typesAccumulator.addAll(lib.getTypes());
}
}
List<String> types = new LinkedList<>(typesAccumulator);
Collections.sort(types);
return types;
List<String> res = new ArrayList<>(types);
Collections.sort(res);
return res;
}
public Optional<ContributedLibraryRelease> getInstalled(String name) {
ContributedLibrary rel = new ContributedLibrary(find(name));
return rel.getInstalled();
}
public List<ContributedLibraryRelease> resolveDependeciesOf(ContributedLibraryRelease library) {
List<ContributedLibraryRelease> solution = new ArrayList<>();
solution.add(library);
if (resolveDependeciesOf(solution, library)) {
return solution;
} else {
return null;
if (libraries.containsKey(name)) {
return libraries.get(name).getInstalled();
}
return Optional.empty();
}
public boolean resolveDependeciesOf(List<ContributedLibraryRelease> solution,
ContributedLibraryRelease library) {
List<ContributedLibraryDependency> requirements = library.getDependencies();
if (requirements == null) {
// No deps for this library, great!
return true;
}
for (ContributedLibraryDependency dep : requirements) {
// If the current solution already contains this dependency, skip over
boolean alreadyInSolution = solution.stream()
.anyMatch(l -> l.getName().equals(dep.getName()));
if (alreadyInSolution)
continue;
// Generate possible matching dependencies
List<ContributedLibraryRelease> possibleDeps = findMatchingDependencies(dep);
// If there are no dependencies available add as "missing" lib
if (possibleDeps.isEmpty()) {
solution.add(new UnavailableContributedLibrary(dep));
continue;
}
// Pick the installed version if available
ContributedLibraryRelease selected;
Optional<ContributedLibraryRelease> installed = possibleDeps.stream()
.filter(l -> l.getInstalledLibrary().isPresent()).findAny();
if (installed.isPresent()) {
selected = installed.get();
} else {
// otherwise pick the latest version
selected = possibleDeps.stream().reduce(VersionComparator::max).get();
}
// Add dependency to the solution and process recursively
solution.add(selected);
if (!resolveDependeciesOf(solution, selected)) {
return false;
}
}
return true;
}
private List<ContributedLibraryRelease> findMatchingDependencies(ContributedLibraryDependency dep) {
List<ContributedLibraryRelease> available = find(dep.getName());
if (dep.getVersion() == null || dep.getVersion().isEmpty())
return available;
// XXX: The following part is actually never reached. The use of version
// constraints requires a much complex backtracking algorithm, the following
// is just a draft placeholder.
// List<ContributedLibrary> match = available.stream()
// // TODO: add more complex version comparators (> >= < <= ~ 1.0.* 1.*...)
// .filter(candidate -> candidate.getParsedVersion()
// .equals(dep.getVersionRequired()))
// .collect(Collectors.toList());
// return match;
return available;
}
}

View File

@ -29,14 +29,19 @@
package cc.arduino.contributions.libraries;
import cc.arduino.Constants;
import cc.arduino.contributions.packages.ContributedPlatform;
import static processing.app.I18n.format;
import static processing.app.I18n.tr;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.compress.utils.IOUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import cc.arduino.cli.ArduinoCoreInstance;
import cc.arduino.contributions.packages.ContributedPlatform;
import io.grpc.StatusException;
import processing.app.BaseNoGui;
import processing.app.I18n;
import processing.app.helpers.filefilters.OnlyDirs;
@ -47,70 +52,54 @@ import processing.app.packages.UserLibraryFolder;
import processing.app.packages.UserLibraryFolder.Location;
import processing.app.packages.UserLibraryPriorityComparator;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static processing.app.I18n.format;
import static processing.app.I18n.tr;
public class LibrariesIndexer {
private LibrariesIndex index;
private final LibraryList installedLibraries = new LibraryList();
private List<UserLibraryFolder> librariesFolders;
private final File indexFile;
private final File stagingFolder;
private final List<String> badLibNotified = new ArrayList<>();
private ArduinoCoreInstance core;
public LibrariesIndexer(File preferencesFolder) {
indexFile = new File(preferencesFolder, "library_index.json");
stagingFolder = new File(new File(preferencesFolder, "staging"), "libraries");
public LibrariesIndexer(ArduinoCoreInstance core) {
this.core = core;
}
public void parseIndex() throws IOException {
index = new LibrariesIndex(); // Fallback
if (!indexFile.exists()) {
return;
}
parseIndex(indexFile);
// TODO: resolve libraries inner references
}
private void parseIndex(File file) throws IOException {
InputStream indexIn = null;
public void regenerateIndex() {
index = new LibrariesIndex();
try {
indexIn = new FileInputStream(file);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
LibrariesIndex newIndex = mapper.readValue(indexIn, LibrariesIndex.class);
newIndex.getLibraries()
.stream()
.filter(library -> library.getCategory() == null || "".equals(library.getCategory()) || !Constants.LIBRARY_CATEGORIES.contains(library.getCategory()))
.forEach(library -> library.setCategory("Uncategorized"));
index = newIndex;
} catch (JsonParseException | JsonMappingException e) {
System.err.println(
format(tr("Error parsing libraries index: {0}\nTry to open the Library Manager to update the libraries index."),
e.getMessage()));
} catch (Exception e) {
System.err.println(format(tr("Error reading libraries index: {0}"), e.getMessage()));
} finally {
IOUtils.closeQuietly(indexIn);
core.searchLibrary("").forEach(inLib -> {
ContributedLibrary library = new ContributedLibrary(inLib.getName());
inLib.getReleasesMap().forEach((ver, rel) -> {
ContributedLibraryRelease release = new ContributedLibraryRelease(
library, //
rel.getMaintainer(), //
rel.getAuthor(), //
rel.getWebsite(), //
rel.getCategory(), //
"", // TODO: license
rel.getParagraph(), //
rel.getSentence(), //
rel.getArchitecturesList(), //
rel.getTypesList(), //
null, // TODO: dependencies - List<ContributedLibraryDependency>
null, // TODO: providesIncludes - List<String>
//
rel.getResources().getUrl(), //
rel.getVersion(), //
rel.getResources().getChecksum(), //
rel.getResources().getSize(), //
rel.getResources().getArchivefilename());
library.addRelease(release);
});
index.add(library);
});
} catch (StatusException e) {
e.printStackTrace();
}
// format(tr("Error parsing libraries index: {0}\nTry to open the Library Manager to update the libraries index."),
// System.err.println(format(tr("Error reading libraries index: {0}"),
}
public void setLibrariesFolders(List<UserLibraryFolder> folders) {
@ -126,7 +115,8 @@ public class LibrariesIndexer {
return librariesFolders;
}
private UserLibraryPriorityComparator priorityComparator = new UserLibraryPriorityComparator(null);
private UserLibraryPriorityComparator priorityComparator = new UserLibraryPriorityComparator(
null);
public void addToInstalledLibraries(UserLibrary lib) {
UserLibrary toReplace = installedLibraries.getByName(lib.getName());
@ -154,8 +144,10 @@ public class LibrariesIndexer {
return;
}
for (ContributedLibraryRelease lib : index.getLibraries()) {
lib.unsetInstalledUserLibrary();
for (ContributedLibrary lib : index.getLibraries()) {
for (ContributedLibraryRelease libRelease : lib.getReleases()) {
libRelease.unsetInstalledUserLibrary();
}
}
// Rescan libraries
@ -165,10 +157,12 @@ public class LibrariesIndexer {
installedLibraries.stream() //
.filter(l -> l.getTypes().contains("Contributed")) //
.filter(l -> l.getLocation() == Location.CORE || l.getLocation() == Location.REFERENCED_CORE) //
.filter(l -> l.getLocation() == Location.CORE
|| l.getLocation() == Location.REFERENCED_CORE) //
.forEach(l -> {
File libFolder = l.getInstalledFolder();
Optional<ContributedPlatform> platform = BaseNoGui.indexer.getPlatformByFolder(libFolder);
Optional<ContributedPlatform> platform = BaseNoGui.indexer
.getPlatformByFolder(libFolder);
if (platform.isPresent()) {
l.setTypes(Collections.singletonList(platform.get().getCategory()));
}
@ -185,15 +179,17 @@ public class LibrariesIndexer {
String subfolderName = subfolder.getName();
if (!BaseNoGui.isSanitaryName(subfolderName)) {
// Detect whether the current folder name has already had a notification.
// Detect whether the current folder name has already had a
// notification.
if (!badLibNotified.contains(subfolderName)) {
badLibNotified.add(subfolderName);
String mess = I18n.format(tr("The library \"{0}\" cannot be used.\n"
+ "Library folder names must start with a letter or number, followed by letters,\n"
+ "numbers, dashes, dots and underscores. Maximum length is 63 characters."),
subfolderName);
String mess = I18n.format(
tr("The library \"{0}\" cannot be used.\n"
+ "Library folder names must start with a letter or number, followed by letters,\n"
+ "numbers, dashes, dots and underscores. Maximum length is 63 characters."),
subfolderName);
BaseNoGui.showMessage(tr("Ignoring library with bad name"), mess);
}
continue;
@ -202,7 +198,8 @@ public class LibrariesIndexer {
try {
scanLibrary(new UserLibraryFolder(subfolder, folderDesc.location));
} catch (IOException e) {
System.out.println(I18n.format(tr("Invalid library found in {0}: {1}"), subfolder, e.getMessage()));
System.out.println(I18n.format(tr("Invalid library found in {0}: {1}"),
subfolder, e.getMessage()));
}
}
}
@ -214,9 +211,11 @@ public class LibrariesIndexer {
if (!check.exists() || !check.isFile()) {
// Create a legacy library and exit
LegacyUserLibrary lib = LegacyUserLibrary.create(folderDesc);
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
String[] headers = BaseNoGui
.headerListFromIncludePath(lib.getSrcFolder());
if (headers.length == 0) {
throw new IOException(format(tr("no headers files (.h) found in {0}"), lib.getSrcFolder()));
throw new IOException(format(tr("no headers files (.h) found in {0}"),
lib.getSrcFolder()));
}
addToInstalledLibraries(lib);
return;
@ -226,7 +225,8 @@ public class LibrariesIndexer {
UserLibrary lib = UserLibrary.create(folderDesc);
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
if (headers.length == 0) {
throw new IOException(format(tr("no headers files (.h) found in {0}"), lib.getSrcFolder()));
throw new IOException(
format(tr("no headers files (.h) found in {0}"), lib.getSrcFolder()));
}
addToInstalledLibraries(lib);
@ -234,11 +234,10 @@ public class LibrariesIndexer {
if (loc != Location.CORE && loc != Location.REFERENCED_CORE) {
// Check if we can find the same library in the index
// and mark it as installed
ContributedLibraryRelease foundLib = index.find(lib.getName(), lib.getVersion());
if (foundLib != null) {
index.find(lib.getName(), lib.getVersion()).ifPresent(foundLib -> {
foundLib.setInstalledUserLibrary(lib);
lib.setTypes(foundLib.getTypes());
}
});
}
if (lib.getTypes().isEmpty() && loc == Location.SKETCHBOOK) {
@ -257,12 +256,4 @@ public class LibrariesIndexer {
public LibraryList getInstalledLibraries() {
return new LibraryList(installedLibraries);
}
public File getStagingFolder() {
return stagingFolder;
}
public File getIndexFile() {
return indexFile;
}
}

View File

@ -60,16 +60,14 @@ public class LibraryInstaller {
private static Logger log = LogManager.getLogger(LibraryInstaller.class);
private final Platform platform;
private final GPGDetachedSignatureVerifier signatureVerifier;
public LibraryInstaller(Platform platform, GPGDetachedSignatureVerifier signatureVerifier) {
public LibraryInstaller(Platform platform) {
this.platform = platform;
this.signatureVerifier = signatureVerifier;
}
public synchronized void updateIndex(ProgressListener progressListener) throws Exception {
/*
final MultiStepProgress progress = new MultiStepProgress(3);
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.librariesIndexer.getStagingFolder());
// Step 1: Download index
File outputFile = BaseNoGui.librariesIndexer.getIndexFile();
@ -104,11 +102,11 @@ public class LibraryInstaller {
}
// Step 2: Parse index
BaseNoGui.librariesIndexer.parseIndex();
BaseNoGui.librariesIndexer.regenerateIndex();
// Step 3: Rescan index
rescanLibraryIndex(progress, progressListener);
*/
}
public void install(ContributedLibraryRelease lib, ProgressListener progressListener) throws Exception {
@ -118,6 +116,7 @@ public class LibraryInstaller {
}
public synchronized void install(List<ContributedLibraryRelease> libs, ProgressListener progressListener) throws Exception {
/*
MultiStepProgress progress = new MultiStepProgress(3 * libs.size() + 1);
for (ContributedLibraryRelease lib : libs) {
@ -127,9 +126,11 @@ public class LibraryInstaller {
// Rescan index (1 step)
rescanLibraryIndex(progress, progressListener);
*/
}
private void performInstall(ContributedLibraryRelease lib, ProgressListener progressListener, MultiStepProgress progress) throws Exception {
/*
if (lib.isLibraryInstalled()) {
System.out.println(I18n.format(tr("Library is already installed: {0}:{1}"), lib.getName(), lib.getParsedVersion()));
return;
@ -140,12 +141,28 @@ public class LibraryInstaller {
// Check if we are replacing an already installed lib
LibrariesIndex index = BaseNoGui.librariesIndexer.getIndex();
Optional<ContributedLibraryRelease> replacedLib = index.find(lib.getName()).stream() //
.filter(l -> l.getInstalledLibrary().isPresent()) //
.filter(l -> l.getInstalledLibrary().get().getInstalledFolder().equals(destFolder)) //
.findAny();
Optional<ContributedLibraryRelease> mayReplacedLib = lib.getLibrary()
.getInstalled();
if (mayReplacedLib.isPresent()) {
if (mayReplacedLib.get().getInstalledLibrary().get().getInstalledFolder()
.equals(destFolder) && destFolder.exists()) {
System.out
.println(I18n.format(tr("Library {0} is already installed in: {1}"),
lib.getName(), destFolder));
return;
}
}
// TODO: Check correctness
// Optional<ContributedLibraryRelease> mayReplacedLib =
// index.find(lib.getName());
// .filter(l ->
// l.getInstalledLibrary().get().getInstalledFolder().equals(destFolder)) //
// .findAny();
if (!replacedLib.isPresent() && destFolder.exists()) {
System.out.println(I18n.format(tr("Library {0} is already installed in: {1}"), lib.getName(), destFolder));
System.out
.println(I18n.format(tr("Library {0} is already installed in: {1}"),
lib.getName(), destFolder));
return;
}
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.librariesIndexer.getStagingFolder());
@ -182,9 +199,11 @@ public class LibraryInstaller {
}
tmpFolder.renameTo(destFolder);
progress.stepDone();
*/
}
public synchronized void remove(ContributedLibraryRelease lib, ProgressListener progressListener) throws IOException {
/*
if (lib.isIDEBuiltIn()) {
return;
}
@ -199,6 +218,7 @@ public class LibraryInstaller {
// Step 2: Rescan index
rescanLibraryIndex(progress, progressListener);
*/
}
private void rescanLibraryIndex(MultiStepProgress progress, ProgressListener progressListener) {

View File

@ -30,114 +30,22 @@
package cc.arduino.contributions.libraries;
import java.util.ArrayList;
import java.util.List;
public class UnavailableContributedLibrary extends ContributedLibraryRelease {
private String name;
private String version;
public UnavailableContributedLibrary(ContributedLibraryDependency dependency) {
this(dependency.getName(), dependency.getVersion());
}
public UnavailableContributedLibrary(String _name, String _version) {
name = _name;
version = _version;
}
@Override
public String getName() {
return name;
}
@Override
public String getMaintainer() {
return "Unknown";
}
@Override
public String getAuthor() {
return "Unknown";
}
@Override
public String getWebsite() {
return "Unknown";
}
@Override
public String getCategory() {
return "Uncategorized";
}
@Override
public void setCategory(String category) {
// Empty
}
@Override
public String getLicense() {
return "Unknown";
}
@Override
public String getParagraph() {
return "";
}
@Override
public String getSentence() {
return "";
}
@Override
public List<String> getArchitectures() {
return new ArrayList<>();
}
@Override
public List<String> getTypes() {
return new ArrayList<>();
}
@Override
public List<ContributedLibraryDependency> getDependencies() {
return new ArrayList<>();
}
@Override
public String getUrl() {
return "";
}
@Override
public String getVersion() {
return version;
}
@Override
public String getChecksum() {
return "";
}
@Override
public long getSize() {
return 0;
}
@Override
public String getArchiveFileName() {
return "";
public UnavailableContributedLibrary(String name, String version) {
super(new ContributedLibrary(name), "Unknown", "Unknown", "Unknown",
"Uncategorized", "Unknown", "", "", new ArrayList<>(),
new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), "", version,
"", 0, "");
}
@Override
public String toString() {
return "!" + super.toString();
}
@Override
public List<String> getProvidesIncludes() {
return new ArrayList<>();
}
}

View File

@ -520,13 +520,8 @@ public class BaseNoGui {
loadHardware(getSketchbookHardwareFolder());
createToolPreferences(indexer.getInstalledTools(), true);
librariesIndexer = new LibrariesIndexer(getSettingsFolder());
try {
librariesIndexer.parseIndex();
} catch (JsonProcessingException e) {
File librariesIndexFile = librariesIndexer.getIndexFile();
librariesIndexFile.delete();
}
librariesIndexer = new LibrariesIndexer(BaseNoGui.getArduinoCoreService());
librariesIndexer.regenerateIndex();
if (discoveryManager == null) {
discoveryManager = new DiscoveryManager(packages);