Skip to content

Commit e896595

Browse files
committed
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.
1 parent 3ec6748 commit e896595

20 files changed

+83
-94
lines changed

app/src/cc/arduino/contributions/libraries/LibraryByTypeComparator.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131

3232
import java.util.Comparator;
3333

34-
public class LibraryByTypeComparator implements Comparator<ContributedLibrary> {
34+
import processing.app.packages.UserLibrary;
35+
36+
public class LibraryByTypeComparator implements Comparator<UserLibrary> {
3537

3638
private final LibraryTypeComparator libraryTypeComparator;
3739

@@ -44,7 +46,7 @@ public LibraryByTypeComparator(LibraryTypeComparator libraryTypeComparator) {
4446
}
4547

4648
@Override
47-
public int compare(ContributedLibrary o1, ContributedLibrary o2) {
49+
public int compare(UserLibrary o1, UserLibrary o2) {
4850
if (o1.getTypes() == null) {
4951
return 1;
5052
}

app/src/cc/arduino/contributions/libraries/LibraryOfSameTypeComparator.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131

3232
import java.util.Comparator;
3333

34-
public class LibraryOfSameTypeComparator implements Comparator<ContributedLibrary> {
34+
import processing.app.packages.UserLibrary;
35+
36+
public class LibraryOfSameTypeComparator implements Comparator<UserLibrary> {
3537

3638
@Override
37-
public int compare(ContributedLibrary o1, ContributedLibrary o2) {
39+
public int compare(UserLibrary o1, UserLibrary o2) {
3840
if (o1.getTypes() == null) {
3941
return 1;
4042
}

app/src/cc/arduino/contributions/libraries/filters/InstalledLibraryPredicate.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
package cc.arduino.contributions.libraries.filters;
3131

32-
import cc.arduino.contributions.filters.InstalledPredicate;
3332
import cc.arduino.contributions.libraries.ContributedLibrary;
3433
import processing.app.BaseNoGui;
3534

@@ -40,14 +39,14 @@ public class InstalledLibraryPredicate implements Predicate<ContributedLibrary>
4039

4140
@Override
4241
public boolean test(ContributedLibrary input) {
43-
if (input.isInstalled()) {
42+
if (input.isLibraryInstalled()) {
4443
return true;
4544
}
4645

4746
List<ContributedLibrary> libraries = BaseNoGui.librariesIndexer.getIndex().find(input.getName());
4847

4948
return libraries.stream()
50-
.filter(new InstalledPredicate())
49+
.filter(l -> l.isLibraryInstalled())
5150
.count() > 0;
5251
}
5352

app/src/cc/arduino/contributions/libraries/filters/OnlyUpstreamReleasePredicate.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@
2929

3030
package cc.arduino.contributions.libraries.filters;
3131

32-
import cc.arduino.contributions.libraries.ContributedLibrary;
3332
import processing.app.packages.UserLibrary;
3433

3534
import java.util.function.Predicate;
3635

37-
public class OnlyUpstreamReleasePredicate implements Predicate<ContributedLibrary> {
36+
public class OnlyUpstreamReleasePredicate implements Predicate<Object> {
3837

3938
@Override
40-
public boolean test(ContributedLibrary input) {
39+
public boolean test(Object input) {
4140
return !(input instanceof UserLibrary);
4241
}
4342

app/src/cc/arduino/contributions/libraries/filters/UpdatableLibraryPredicate.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@
3535
import processing.app.packages.UserLibrary;
3636

3737
import java.util.List;
38+
import java.util.Optional;
3839
import java.util.function.Predicate;
3940

4041
public class UpdatableLibraryPredicate implements Predicate<ContributedLibrary> {
4142

4243
@Override
43-
public boolean test(ContributedLibrary contributedLibrary) {
44-
String libraryName = contributedLibrary.getName();
45-
UserLibrary installed = BaseNoGui.librariesIndexer.getInstalledLibraries().getByName(libraryName);
46-
if (installed == null) {
44+
public boolean test(ContributedLibrary lib) {
45+
Optional<UserLibrary> installed = lib.getInstalledLibrary();
46+
if (!installed.isPresent()) {
4747
return false;
4848
}
49+
String installedVersion = installed.get().getVersion();
50+
String libraryName = lib.getName();
4951
List<ContributedLibrary> libraries = BaseNoGui.librariesIndexer.getIndex().find(libraryName);
5052
return libraries.stream()
51-
.filter(library -> VersionComparator.greaterThan(library.getParsedVersion(), installed.getParsedVersion()))
52-
.count() > 0;
53+
.anyMatch(library -> VersionComparator.greaterThan(library.getParsedVersion(), installedVersion));
5354
}
5455
}

app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
package cc.arduino.contributions.libraries.ui;
3131

3232
import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
33-
import cc.arduino.contributions.filters.InstalledPredicate;
3433
import cc.arduino.contributions.libraries.ContributedLibrary;
3534
import cc.arduino.contributions.ui.FilteredAbstractTableModel;
3635

@@ -78,13 +77,11 @@ public void add(ContributedLibrary library) {
7877
}
7978

8079
public Optional<ContributedLibrary> getInstalled() {
81-
List<ContributedLibrary> installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList());
82-
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
83-
80+
List<ContributedLibrary> installedReleases = releases.stream().filter(l -> l.isLibraryInstalled()).collect(Collectors.toList());
8481
if (installedReleases.isEmpty()) {
8582
return Optional.empty();
8683
}
87-
84+
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
8885
return Optional.of(installedReleases.get(0));
8986
}
9087

app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import cc.arduino.contributions.DownloadableContributionVersionComparator;
4646
import cc.arduino.contributions.VersionComparator;
4747
import cc.arduino.contributions.filters.BuiltInPredicate;
48-
import cc.arduino.contributions.filters.InstalledPredicate;
4948
import cc.arduino.contributions.libraries.ContributedLibrary;
5049
import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate;
5150
import cc.arduino.contributions.ui.InstallerTableCell;
@@ -92,10 +91,10 @@ public Component getTableCellEditorComponent(JTable table, Object value,
9291
.filter(new OnlyUpstreamReleasePredicate())
9392
.collect(Collectors.toList());
9493
List<ContributedLibrary> uninstalledReleases = releases.stream()
95-
.filter(new InstalledPredicate().negate()).collect(Collectors.toList());
94+
.filter(l -> !l.isLibraryInstalled()).collect(Collectors.toList());
9695

9796
List<ContributedLibrary> installedBuiltIn = releases.stream()
98-
.filter(new InstalledPredicate()).filter(new BuiltInPredicate())
97+
.filter(l -> !l.isLibraryInstalled()).filter(new BuiltInPredicate())
9998
.collect(Collectors.toList());
10099

101100
if (mayInstalled.isPresent() && !installedBuiltIn.contains(mayInstalled.get())) {

app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Collection;
3939
import java.util.Collections;
4040
import java.util.LinkedList;
41+
import java.util.List;
4142
import java.util.Optional;
4243
import java.util.function.Predicate;
4344

@@ -134,7 +135,6 @@ public void updateUI() {
134135
categoryChooser.removeActionListener(categoryChooserActionListener);
135136
typeChooser.removeActionListener(typeChooserActionListener);
136137

137-
138138
// Load categories
139139
categoryFilter = x -> true;
140140
categoryChooser.removeAllItems();
@@ -158,7 +158,7 @@ public void updateUI() {
158158
typeChooser.addItem(new DropdownAllLibraries());
159159
typeChooser.addItem(new DropdownUpdatableLibrariesItem());
160160
typeChooser.addItem(new DropdownInstalledLibraryItem());
161-
java.util.List<String> types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes());
161+
List<String> types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes());
162162
Collections.sort(types, new LibraryTypeComparator());
163163
for (String type : types) {
164164
typeChooser.addItem(new DropdownLibraryOfTypeItem(type));

app/src/processing/app/Base.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1059,8 +1059,8 @@ protected void rebuildSketchbookMenu(JMenu menu) {
10591059
}
10601060
}
10611061

1062-
private List<ContributedLibrary> getSortedLibraries() {
1063-
List<ContributedLibrary> installedLibraries = new LinkedList<>(BaseNoGui.librariesIndexer.getInstalledLibraries());
1062+
private LibraryList getSortedLibraries() {
1063+
LibraryList installedLibraries = BaseNoGui.librariesIndexer.getInstalledLibraries();
10641064
Collections.sort(installedLibraries, new LibraryByTypeComparator());
10651065
Collections.sort(installedLibraries, new LibraryOfSameTypeComparator());
10661066
return installedLibraries;
@@ -1097,9 +1097,9 @@ public void actionPerformed(ActionEvent e) {
10971097
TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform();
10981098

10991099
if (targetPlatform != null) {
1100-
List<ContributedLibrary> libs = getSortedLibraries();
1100+
LibraryList libs = getSortedLibraries();
11011101
String lastLibType = null;
1102-
for (ContributedLibrary lib : libs) {
1102+
for (UserLibrary lib : libs) {
11031103
String libType = lib.getTypes().get(0);
11041104
if (!libType.equals(lastLibType)) {
11051105
if (lastLibType != null) {

app/src/processing/app/syntax/PdeKeywords.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424

2525
package processing.app.syntax;
2626

27-
import cc.arduino.contributions.libraries.ContributedLibrary;
2827
import org.apache.commons.compress.utils.IOUtils;
2928
import org.fife.ui.rsyntaxtextarea.TokenMap;
3029
import org.fife.ui.rsyntaxtextarea.TokenTypes;
3130
import processing.app.Base;
3231
import processing.app.BaseNoGui;
32+
import processing.app.packages.UserLibrary;
3333
import processing.app.debug.TargetPlatform;
3434

3535
import java.io.BufferedReader;
@@ -89,7 +89,7 @@ public void reload() {
8989
File platformKeywords = new File(tp.getFolder(), "keywords.txt");
9090
if (platformKeywords.exists()) parseKeywordsTxt(platformKeywords);
9191
}
92-
for (ContributedLibrary lib : BaseNoGui.librariesIndexer.getInstalledLibraries()) {
92+
for (UserLibrary lib : BaseNoGui.librariesIndexer.getInstalledLibraries()) {
9393
File keywords = new File(lib.getInstalledFolder(), "keywords.txt");
9494
if (keywords.exists()) {
9595
parseKeywordsTxt(keywords);

arduino-core/src/cc/arduino/contributions/DownloadableContribution.java

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
public abstract class DownloadableContribution {
3737

38+
// XXX: maybe installed fields should not be here but in UserLibrary and ContributedPlatform?
3839
private boolean installed;
3940
private File installedFolder;
4041

arduino-core/src/cc/arduino/contributions/libraries/ContributedLibrary.java

+20
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131

3232
import cc.arduino.contributions.DownloadableContribution;
3333
import processing.app.I18n;
34+
import processing.app.packages.UserLibrary;
3435

3536
import java.util.Comparator;
3637
import java.util.List;
38+
import java.util.Optional;
3739

3840
import static processing.app.I18n.tr;
3941

@@ -65,6 +67,24 @@ public abstract class ContributedLibrary extends DownloadableContribution {
6567

6668
public static final Comparator<ContributedLibrary> CASE_INSENSITIVE_ORDER = (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName());
6769

70+
private Optional<UserLibrary> installedLib = Optional.empty();
71+
72+
public Optional<UserLibrary> getInstalledLibrary() {
73+
return installedLib;
74+
}
75+
76+
public boolean isLibraryInstalled() {
77+
return installedLib.isPresent();
78+
}
79+
80+
public void setInstalledUserLibrary(UserLibrary installed) {
81+
this.installedLib = Optional.of(installed);
82+
}
83+
84+
public void unsetInstalledUserLibrary() {
85+
installedLib = Optional.empty();
86+
}
87+
6888
/**
6989
* Returns <b>true</b> if the library declares to support the specified
7090
* architecture (through the "architectures" property field).

arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
package cc.arduino.contributions.libraries;
3131

3232
import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
33-
import cc.arduino.contributions.filters.InstalledPredicate;
3433
import cc.arduino.contributions.libraries.filters.LibraryWithNamePredicate;
3534

3635
import java.util.*;
@@ -92,7 +91,7 @@ public List<String> getTypes() {
9291
}
9392

9493
public Optional<ContributedLibrary> getInstalled(String name) {
95-
List<ContributedLibrary> installedReleases = find(name).stream().filter(new InstalledPredicate()).collect(Collectors.toList());
94+
List<ContributedLibrary> installedReleases = find(name).stream().filter(l -> l.isLibraryInstalled()).collect(Collectors.toList());
9695
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
9796

9897
if (installedReleases.isEmpty()) {

arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void rescanLibraries() {
122122
}
123123

124124
for (ContributedLibrary lib : index.getLibraries()) {
125-
lib.setInstalled(false);
125+
lib.unsetInstalledUserLibrary();
126126
}
127127

128128
// Rescan libraries
@@ -176,7 +176,6 @@ private void scanLibrary(File folder, boolean isSketchbook) throws IOException {
176176
if (!check.exists() || !check.isFile()) {
177177
// Create a legacy library and exit
178178
LegacyUserLibrary lib = LegacyUserLibrary.create(folder);
179-
lib.setReadOnly(readOnly);
180179
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
181180
if (headers.length == 0) {
182181
throw new IOException(lib.getSrcFolder().getAbsolutePath());
@@ -187,7 +186,6 @@ private void scanLibrary(File folder, boolean isSketchbook) throws IOException {
187186

188187
// Create a regular library
189188
UserLibrary lib = UserLibrary.create(folder);
190-
lib.setReadOnly(readOnly);
191189
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
192190
if (headers.length == 0) {
193191
throw new IOException(lib.getSrcFolder().getAbsolutePath());
@@ -196,15 +194,13 @@ private void scanLibrary(File folder, boolean isSketchbook) throws IOException {
196194

197195
// Check if we can find the same library in the index
198196
// and mark it as installed
199-
ContributedLibrary foundLib = index.find(lib.getName(), lib.getParsedVersion());
197+
ContributedLibrary foundLib = index.find(lib.getName(), lib.getVersion());
200198
if (foundLib != null) {
201-
foundLib.setInstalled(true);
202-
foundLib.setInstalledFolder(folder);
203-
foundLib.setReadOnly(readOnly);
199+
foundLib.setInstalledUserLibrary(lib);
204200
lib.setTypes(foundLib.getTypes());
205201
}
206202

207-
if (lib.isReadOnly() && lib.getTypes() == null && !lib.getDeclaredTypes().isEmpty()) {
203+
if (readOnly && lib.getTypes() == null && !lib.getDeclaredTypes().isEmpty()) {
208204
lib.setTypes(lib.getDeclaredTypes());
209205
}
210206

arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public synchronized void updateIndex(ProgressListener progressListener) throws E
8484
}
8585

8686
public synchronized void install(ContributedLibrary lib, Optional<ContributedLibrary> mayReplacedLib, ProgressListener progressListener) throws Exception {
87-
if (lib.isInstalled()) {
87+
if (lib.isLibraryInstalled()) {
8888
System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion()));
8989
return;
9090
}
@@ -141,7 +141,7 @@ public synchronized void remove(ContributedLibrary lib, ProgressListener progres
141141
// Step 1: Remove library
142142
progress.setStatus(I18n.format(tr("Removing library: {0}"), lib.getName()));
143143
progressListener.onProgress(progress);
144-
FileUtils.recursiveDelete(lib.getInstalledFolder());
144+
FileUtils.recursiveDelete(lib.getInstalledLibrary().get().getInstalledFolder());
145145
progress.stepDone();
146146

147147
// Step 2: Rescan index

arduino-core/src/cc/arduino/contributions/libraries/filters/LibraryInstalledInsideCore.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929

3030
package cc.arduino.contributions.libraries.filters;
3131

32-
import cc.arduino.contributions.libraries.ContributedLibrary;
3332
import processing.app.BaseNoGui;
33+
import processing.app.packages.UserLibrary;
3434

3535
import java.util.function.Predicate;
3636

37-
public class LibraryInstalledInsideCore implements Predicate<ContributedLibrary> {
37+
public class LibraryInstalledInsideCore implements Predicate<UserLibrary> {
3838

3939
@Override
40-
public boolean test(ContributedLibrary contributedLibrary) {
40+
public boolean test(UserLibrary contributedLibrary) {
4141
return BaseNoGui.indexer.isFolderInsidePlatform(contributedLibrary.getInstalledFolder());
4242
}
4343

arduino-core/src/cc/arduino/contributions/libraries/filters/TypePredicate.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929

3030
package cc.arduino.contributions.libraries.filters;
3131

32-
import cc.arduino.contributions.libraries.ContributedLibrary;
32+
import processing.app.packages.UserLibrary;
3333

3434
import java.util.function.Predicate;
3535

36-
public class TypePredicate implements Predicate<ContributedLibrary> {
36+
public class TypePredicate implements Predicate<UserLibrary> {
3737

3838
private final String type;
3939

@@ -42,7 +42,7 @@ public TypePredicate(String type) {
4242
}
4343

4444
@Override
45-
public boolean test(ContributedLibrary input) {
45+
public boolean test(UserLibrary input) {
4646
return input.getTypes() != null && input.getTypes().contains(type);
4747
}
4848

arduino-core/src/processing/app/packages/LegacyUserLibrary.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public class LegacyUserLibrary extends UserLibrary {
3939
public static LegacyUserLibrary create(File libFolder) {
4040
// construct an old style library
4141
LegacyUserLibrary res = new LegacyUserLibrary();
42-
res.setInstalledFolder(libFolder);
43-
res.setInstalled(true);
42+
res.installedFolder = libFolder;
4443
res.layout = LibraryLayout.FLAT;
4544
res.name = libFolder.getName();
4645
res.setTypes(Arrays.asList("Contributed"));

0 commit comments

Comments
 (0)