Skip to content

Commit 42b07f0

Browse files
author
Federico Fissore
committed
Libraries bundled with cores will take the type of that core, instead of relying on an undocumented 'types' property. Fixes #2875
1 parent 65e0d8b commit 42b07f0

File tree

14 files changed

+84
-22
lines changed

14 files changed

+84
-22
lines changed

app/src/processing/app/Base.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ protected void onProgress(Progress progress) {
397397
System.exit(0);
398398

399399
} else if (parser.isInstallLibrary()) {
400-
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder());
400+
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder(), new ContributionsIndexer(BaseNoGui.getSettingsFolder()));
401401
LibraryInstaller installer = new LibraryInstaller(indexer) {
402402
private String lastStatus = "";
403403

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

+24-6
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@
2828
*/
2929
package cc.arduino.contributions.libraries;
3030

31+
import cc.arduino.contributions.libraries.filters.LibraryInstalledInsideCore;
32+
import cc.arduino.contributions.libraries.filters.TypePredicate;
33+
import cc.arduino.contributions.packages.ContributedPlatform;
34+
import cc.arduino.contributions.packages.ContributionsIndexer;
3135
import com.fasterxml.jackson.databind.DeserializationFeature;
3236
import com.fasterxml.jackson.databind.ObjectMapper;
3337
import com.fasterxml.jackson.module.mrbean.MrBeanModule;
38+
import com.google.common.base.Function;
39+
import com.google.common.collect.FluentIterable;
3440
import processing.app.BaseNoGui;
3541
import processing.app.I18n;
3642
import processing.app.helpers.FileUtils;
@@ -50,6 +56,7 @@
5056

5157
public class LibrariesIndexer {
5258

59+
private final ContributionsIndexer contributionsIndexer;
5360
private LibrariesIndex index;
5461
private final LibraryList installedLibraries = new LibraryList();
5562
private final LibraryList installedLibrariesWithDuplicates = new LibraryList();
@@ -58,10 +65,10 @@ public class LibrariesIndexer {
5865
private final File stagingFolder;
5966
private File sketchbookLibrariesFolder;
6067

61-
public LibrariesIndexer(File preferencesFolder) {
62-
indexFile = new File(preferencesFolder, "library_index.json");
63-
stagingFolder = new File(preferencesFolder, "staging" + File.separator +
64-
"libraries");
68+
public LibrariesIndexer(File preferencesFolder, ContributionsIndexer contributionsIndexer) {
69+
this.contributionsIndexer = contributionsIndexer;
70+
this.indexFile = new File(preferencesFolder, "library_index.json");
71+
this.stagingFolder = new File(new File(preferencesFolder, "staging"), "libraries");
6572
}
6673

6774
public void parseIndex() throws IOException {
@@ -101,12 +108,23 @@ public void rescanLibraries() {
101108
// Clear all installed flags
102109
installedLibraries.clear();
103110
installedLibrariesWithDuplicates.clear();
104-
for (ContributedLibrary lib : index.getLibraries())
111+
for (ContributedLibrary lib : index.getLibraries()) {
105112
lib.setInstalled(false);
113+
}
106114

107115
// Rescan libraries
108-
for (File folder : librariesFolders)
116+
for (File folder : librariesFolders) {
109117
scanInstalledLibraries(folder, folder.equals(sketchbookLibrariesFolder));
118+
}
119+
120+
FluentIterable.from(installedLibraries).filter(new TypePredicate("Contributed")).filter(new LibraryInstalledInsideCore(contributionsIndexer)).transform(new Function<UserLibrary, Object>() {
121+
@Override
122+
public Object apply(UserLibrary userLibrary) {
123+
ContributedPlatform platform = contributionsIndexer.getPlatformByFolder(userLibrary.getInstalledFolder());
124+
userLibrary.setTypes(Arrays.asList(platform.getCategory()));
125+
return userLibrary;
126+
}
127+
}).toList();
110128
}
111129

112130
private void scanInstalledLibraries(File folder, boolean isSketchbook) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cc.arduino.contributions.libraries.filters;
2+
3+
import cc.arduino.contributions.libraries.ContributedLibrary;
4+
import cc.arduino.contributions.packages.ContributionsIndexer;
5+
import com.google.common.base.Predicate;
6+
7+
public class LibraryInstalledInsideCore implements Predicate<ContributedLibrary> {
8+
9+
private final ContributionsIndexer indexer;
10+
11+
public LibraryInstalledInsideCore(ContributionsIndexer indexer) {
12+
this.indexer = indexer;
13+
}
14+
15+
@Override
16+
public boolean apply(ContributedLibrary contributedLibrary) {
17+
return indexer.isFolderInsidePlatform(contributedLibrary.getInstalledFolder());
18+
}
19+
20+
}

arduino-core/src/cc/arduino/contributions/packages/ContributionsIndex.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ public ContributedPlatform findPlatform(String packageName, final String platfor
8383
return platforms.iterator().next();
8484
}
8585

86-
public ContributedPlatform getInstalled(String packageName, String platformArch) {
86+
public List<ContributedPlatform> getInstalledPlatforms() {
87+
return Lists.newLinkedList(Collections2.filter(getPlatforms(), new InstalledPredicate()));
88+
}
89+
90+
public ContributedPlatform getInstalledPlatform(String packageName, String platformArch) {
8791
List<ContributedPlatform> installedPlatforms = new LinkedList<ContributedPlatform>(Collections2.filter(findPlatforms(packageName, platformArch), new InstalledPredicate()));
8892
Collections.sort(installedPlatforms, new DownloadableContributionBuiltInAtTheBottomComparator());
8993

arduino-core/src/cc/arduino/contributions/packages/ContributionsIndexer.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@
3737
import com.fasterxml.jackson.databind.ObjectMapper;
3838
import com.fasterxml.jackson.module.mrbean.MrBeanModule;
3939
import com.google.common.base.Function;
40+
import com.google.common.base.Predicate;
4041
import com.google.common.base.Predicates;
4142
import com.google.common.collect.Collections2;
4243
import com.google.common.collect.ImmutableListMultimap;
44+
import com.google.common.collect.Iterables;
4345
import com.google.common.collect.Multimaps;
4446
import processing.app.BaseNoGui;
4547
import processing.app.debug.TargetPackage;
4648
import processing.app.debug.TargetPlatform;
4749
import processing.app.debug.TargetPlatformException;
50+
import processing.app.helpers.FileUtils;
4851
import processing.app.helpers.PreferencesMap;
4952

5053
import java.io.File;
@@ -398,6 +401,29 @@ public ContributedPlatform getInstalled(String packageName, String platformArch)
398401
if (index == null) {
399402
return null;
400403
}
401-
return index.getInstalled(packageName, platformArch);
404+
return index.getInstalledPlatform(packageName, platformArch);
405+
}
406+
407+
public List<ContributedPlatform> getInstalledPlatforms() {
408+
if (index == null) {
409+
return new LinkedList<ContributedPlatform>();
410+
}
411+
return index.getInstalledPlatforms();
412+
}
413+
414+
public boolean isFolderInsidePlatform(final File folder) {
415+
return getPlatformByFolder(folder) != null;
416+
}
417+
418+
public ContributedPlatform getPlatformByFolder(final File folder) {
419+
com.google.common.base.Optional<ContributedPlatform> platformOptional = Iterables.tryFind(getInstalledPlatforms(), new Predicate<ContributedPlatform>() {
420+
@Override
421+
public boolean apply(ContributedPlatform contributedPlatform) {
422+
assert contributedPlatform.getInstalledFolder() != null;
423+
return FileUtils.isSubDirectory(contributedPlatform.getInstalledFolder(), folder);
424+
}
425+
});
426+
427+
return platformOptional.orNull();
402428
}
403429
}

arduino-core/src/processing/app/BaseNoGui.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ static public void initPackages() throws Exception {
630630
loadContributedHardware(indexer);
631631
createToolPreferences(indexer);
632632

633-
librariesIndexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder());
633+
librariesIndexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder(), indexer);
634634
File librariesIndexFile = librariesIndexer.getIndexFile();
635635
if (!librariesIndexFile.isFile()) {
636636
File defaultLibraryJsonFile = new File(getContentFile("dist"), "library_index.json");

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public class UserLibrary extends ContributedLibrary {
5656
private List<String> declaredTypes;
5757

5858
private static final List<String> MANDATORY_PROPERTIES = Arrays
59-
.asList(new String[]{"name", "version", "author", "maintainer",
60-
"sentence", "paragraph", "url"});
59+
.asList("name", "version", "author", "maintainer",
60+
"sentence", "paragraph", "url");
6161

62-
private static final List<String> CATEGORIES = Arrays.asList(new String[]{
63-
"Display", "Communication", "Signal Input/Output", "Sensors",
64-
"Device Control", "Timing", "Data Storage", "Data Processing", "Other",
65-
"Uncategorized"});
62+
private static final List<String> CATEGORIES = Arrays.asList(
63+
"Display", "Communication", "Signal Input/Output", "Sensors",
64+
"Device Control", "Timing", "Data Storage", "Data Processing", "Other",
65+
"Uncategorized");
6666

6767
public static UserLibrary create(File libFolder) throws IOException {
6868
// Parse metadata

hardware/arduino/avr/libraries/EEPROM/library.properties

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ sentence=Enables reading and writing to the permanent board storage. For all Ard
66
paragraph=
77
url=http://arduino.cc/en/Reference/EEPROM
88
architectures=avr
9-
types=Arduino
109

hardware/arduino/avr/libraries/SPI/library.properties

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ sentence=Enables the communication with devices that use the Serial Peripheral I
66
paragraph=
77
url=http://arduino.cc/en/Reference/SPI
88
architectures=avr
9-
types=Arduino
109

hardware/arduino/avr/libraries/SoftwareSerial/library.properties

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ sentence=Enables serial communication on digital pins. For all Arduino boards, B
66
paragraph=
77
url=http://arduino.cc/en/Reference/SoftwareSerial
88
architectures=avr
9-
types=Arduino
109

hardware/arduino/avr/libraries/Wire/library.properties

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ sentence=Allows the communication between devices or sensors connected via Two W
66
paragraph=
77
url=http://arduino.cc/en/Reference/Wire
88
architectures=avr
9-
types=Arduino
109

hardware/arduino/sam/libraries/SPI/library.properties

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ sentence=Enables the communication with devices that use the Serial Peripheral I
66
paragraph=
77
url=http://arduino.cc/en/Reference/SPI
88
architectures=sam
9-
types=Arduino
109

hardware/arduino/sam/libraries/Wire/library.properties

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ sentence=Allows the communication between devices or sensors connected via Two W
66
paragraph=
77
url=http://arduino.cc/en/Reference/Wire
88
architectures=sam
9-
types=Arduino
109

0 commit comments

Comments
 (0)