Skip to content

Commit

Permalink
Unified method for selection of IDE_BUNDLED libraries
Browse files Browse the repository at this point in the history
Libraries installed in the sketchbook have priority over the
bundled.
  • Loading branch information
cmaglie committed May 17, 2018
1 parent b03a9af commit 926b73b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@

package cc.arduino.contributions.libraries;

import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
import cc.arduino.contributions.VersionComparator;
import processing.app.packages.UserLibraryFolder.Location;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class ContributedLibraryReleases {

Expand All @@ -49,6 +47,10 @@ public ContributedLibraryReleases(ContributedLibrary library) {
add(library);
}

public ContributedLibraryReleases(List<ContributedLibrary> libraries) {
libraries.forEach(this::add);
}

public List<ContributedLibrary> getReleases() {
return releases;
}
Expand Down Expand Up @@ -76,12 +78,16 @@ public void add(ContributedLibrary library) {
}

public Optional<ContributedLibrary> getInstalled() {
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));
return releases.stream() //
.filter(ContributedLibrary::isLibraryInstalled) //
.reduce((x, y) -> {
Location lx = x.getInstalledLibrary().get().getLocation();
Location ly = y.getInstalledLibrary().get().getLocation();
if (lx == ly) {
return VersionComparator.max(x, y);
}
return lx == Location.SKETCHBOOK ? x : y;
});
}

public ContributedLibrary getLatest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@

package cc.arduino.contributions.libraries;

import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
import cc.arduino.contributions.VersionComparator;
import cc.arduino.contributions.libraries.filters.LibraryWithNamePredicate;

import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import cc.arduino.contributions.libraries.filters.LibraryWithNamePredicate;

public abstract class LibrariesIndex {

public abstract List<ContributedLibrary> getLibraries();
Expand Down Expand Up @@ -92,14 +95,7 @@ public List<String> getTypes() {
}

public Optional<ContributedLibrary> getInstalled(String name) {
return find(name).stream() //
.filter(l -> l.isLibraryInstalled()) //
.reduce((x, y) -> {
if (x.isIDEBuiltIn() == y.isIDEBuiltIn()) {
return VersionComparator.max(x, y);
} else {
return x.isIDEBuiltIn() ? y : x;
}
});
ContributedLibraryReleases rel = new ContributedLibraryReleases(find(name));
return rel.getInstalled();
}
}

0 comments on commit 926b73b

Please sign in to comment.