Skip to content

Commit 2374d9d

Browse files
committed
Introducing UserLibraryFolder
This class allows to attach a Location property to a folder path, this way we directly know if a library is in the sketchbook, core, referenced-core or bundled in the IDE. This simplify a lot of logic in the IDE.
1 parent 040caad commit 2374d9d

File tree

11 files changed

+121
-71
lines changed

11 files changed

+121
-71
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
152152

153153
// Library name...
154154
desc += format("<b>{0}</b>", name);
155-
if (mayInstalled.isPresent() && mayInstalled.get().isReadOnly()) {
155+
if (mayInstalled.isPresent() && mayInstalled.get().isIDEBuiltIn()) {
156156
desc += " Built-In ";
157157
}
158158

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected InstallerTableCell createCellEditor() {
8181
return new ContributedLibraryTableCellEditor() {
8282
@Override
8383
protected void onInstall(ContributedLibrary selectedLibrary, Optional<ContributedLibrary> mayInstalledLibrary) {
84-
if (selectedLibrary.isReadOnly() && mayInstalledLibrary.isPresent()) {
84+
if (mayInstalledLibrary.isPresent() && selectedLibrary.isIDEBuiltIn()) {
8585
onRemovePressed(mayInstalledLibrary.get());
8686
} else {
8787
onInstallPressed(selectedLibrary, mayInstalledLibrary);

app/src/processing/app/Base.java

+9-15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import processing.app.macosx.ThinkDifferent;
5353
import processing.app.packages.LibraryList;
5454
import processing.app.packages.UserLibrary;
55+
import processing.app.packages.UserLibraryFolder.Location;
5556
import processing.app.syntax.PdeKeywords;
5657
import processing.app.syntax.SketchTextAreaDefaultInputMap;
5758
import processing.app.tools.MenuScroller;
@@ -337,8 +338,7 @@ public Base(String[] args) throws Exception {
337338

338339
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder());
339340
indexer.parseIndex();
340-
indexer.setSketchbookLibrariesFolder(BaseNoGui.getSketchbookLibrariesFolder());
341-
indexer.setLibrariesFolders(BaseNoGui.getLibrariesPath());
341+
indexer.setLibrariesFolders(BaseNoGui.getLibrariesFolders());
342342

343343
for (String library : parser.getLibraryToInstall().split(",")) {
344344
String[] libraryToInstallParts = library.split(":");
@@ -359,7 +359,7 @@ public Base(String[] args) throws Exception {
359359
}
360360

361361
Optional<ContributedLibrary> mayInstalled = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
362-
if (selected.isReadOnly() && mayInstalled.isPresent()) {
362+
if (mayInstalled.isPresent() && mayInstalled.get().isIDEBuiltIn()) {
363363
libraryInstaller.remove(mayInstalled.get(), progressListener);
364364
} else {
365365
libraryInstaller.install(selected, mayInstalled, progressListener);
@@ -1148,25 +1148,19 @@ public void rebuildExamplesMenu(JMenu menu) {
11481148
}
11491149

11501150
// Libraries can come from 4 locations: collect info about all four
1151-
File ideLibraryPath = BaseNoGui.getContentFile("libraries");
1152-
File sketchbookLibraryPath = BaseNoGui.getSketchbookLibrariesFolder();
1153-
File platformLibraryPath = null;
1154-
File referencedPlatformLibraryPath = null;
11551151
String boardId = null;
11561152
String referencedPlatformName = null;
11571153
String myArch = null;
11581154
TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform();
11591155
if (targetPlatform != null) {
11601156
myArch = targetPlatform.getId();
11611157
boardId = BaseNoGui.getTargetBoard().getName();
1162-
platformLibraryPath = new File(targetPlatform.getFolder(), "libraries");
11631158
String core = BaseNoGui.getBoardPreferences().get("build.core", "arduino");
11641159
if (core.contains(":")) {
11651160
String refcore = core.split(":")[0];
11661161
TargetPlatform referencedPlatform = BaseNoGui.getTargetPlatform(refcore, myArch);
11671162
if (referencedPlatform != null) {
11681163
referencedPlatformName = referencedPlatform.getPreferences().get("name");
1169-
referencedPlatformLibraryPath = new File(referencedPlatform.getFolder(), "libraries");
11701164
}
11711165
}
11721166
}
@@ -1186,7 +1180,7 @@ public void rebuildExamplesMenu(JMenu menu) {
11861180
LibraryList otherLibs = new LibraryList();
11871181
for (UserLibrary lib : allLibraries) {
11881182
// Get the library's location - used for sorting into categories
1189-
File libraryLocation = lib.getInstalledFolder().getParentFile();
1183+
Location location = lib.getLocation();
11901184
// Is this library compatible?
11911185
List<String> arch = lib.getArchitectures();
11921186
boolean compatible;
@@ -1196,7 +1190,7 @@ public void rebuildExamplesMenu(JMenu menu) {
11961190
compatible = arch.contains(myArch);
11971191
}
11981192
// IDE Libaries (including retired)
1199-
if (libraryLocation.equals(ideLibraryPath)) {
1193+
if (location == Location.IDE_BUILTIN) {
12001194
if (compatible) {
12011195
// only compatible IDE libs are shown
12021196
boolean retired = false;
@@ -1209,15 +1203,15 @@ public void rebuildExamplesMenu(JMenu menu) {
12091203
}
12101204
}
12111205
// Platform Libraries
1212-
} else if (libraryLocation.equals(platformLibraryPath)) {
1206+
} else if (location == Location.CORE) {
12131207
// all platform libs are assumed to be compatible
12141208
platformLibs.add(lib);
12151209
// Referenced Platform Libraries
1216-
} else if (libraryLocation.equals(referencedPlatformLibraryPath)) {
1210+
} else if (location == Location.REFERENCED_CORE) {
12171211
// all referenced platform libs are assumed to be compatible
12181212
referencedPlatformLibs.add(lib);
12191213
// Sketchbook Libraries (including incompatible)
1220-
} else if (libraryLocation.equals(sketchbookLibraryPath)) {
1214+
} else if (location == Location.SKETCHBOOK) {
12211215
if (compatible) {
12221216
// libraries promoted from sketchbook (behave as builtin)
12231217
if (lib.getTypes() != null && lib.getTypes().contains("Arduino")
@@ -2321,7 +2315,7 @@ public void handleAddLibrary() {
23212315
}
23222316

23232317
// copy folder
2324-
File destinationFolder = new File(BaseNoGui.getSketchbookLibrariesFolder(), sourceFile.getName());
2318+
File destinationFolder = new File(BaseNoGui.getSketchbookLibrariesFolder().folder, sourceFile.getName());
23252319
if (!destinationFolder.mkdir()) {
23262320
activeEditor.statusError(I18n.format(tr("A library named {0} already exists"), sourceFile.getName()));
23272321
return;

arduino-core/src/cc/arduino/Compiler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private void callArduinoBuilder(TargetBoard board, TargetPlatform platform, Targ
247247
addPathFlagIfPathExists(cmd, "-tools", installedPackagesFolder);
248248

249249
addPathFlagIfPathExists(cmd, "-built-in-libraries", BaseNoGui.getContentFile("libraries"));
250-
addPathFlagIfPathExists(cmd, "-libraries", BaseNoGui.getSketchbookLibrariesFolder());
250+
addPathFlagIfPathExists(cmd, "-libraries", BaseNoGui.getSketchbookLibrariesFolder().folder);
251251

252252
String fqbn = Stream.of(aPackage.getId(), platform.getId(), board.getId(), boardOptions(board)).filter(s -> !s.isEmpty()).collect(Collectors.joining(":"));
253253
cmd.add("-fqbn=" + fqbn);

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

+7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ public void unsetInstalledUserLibrary() {
8585
installedLib = Optional.empty();
8686
}
8787

88+
public boolean isIDEBuiltIn() {
89+
if (!installedLib.isPresent()) {
90+
return false;
91+
}
92+
return installedLib.get().isIDEBuiltIn();
93+
}
94+
8895
/**
8996
* Returns <b>true</b> if the library declares to support the specified
9097
* architecture (through the "architectures" property field).

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

+21-33
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
import org.apache.commons.compress.utils.IOUtils;
4040
import processing.app.BaseNoGui;
4141
import processing.app.I18n;
42-
import processing.app.helpers.FileUtils;
4342
import processing.app.helpers.filefilters.OnlyDirs;
4443
import processing.app.packages.LegacyUserLibrary;
4544
import processing.app.packages.LibraryList;
4645
import processing.app.packages.UserLibrary;
46+
import processing.app.packages.UserLibraryFolder;
47+
import processing.app.packages.UserLibraryFolder.Location;
4748

4849
import java.io.File;
4950
import java.io.FileInputStream;
@@ -59,10 +60,9 @@ public class LibrariesIndexer {
5960

6061
private LibrariesIndex index;
6162
private final LibraryList installedLibraries = new LibraryList();
62-
private List<File> librariesFolders;
63+
private List<UserLibraryFolder> librariesFolders;
6364
private final File indexFile;
6465
private final File stagingFolder;
65-
private File sketchbookLibrariesFolder;
6666

6767
private final List<String> badLibNotified = new ArrayList<>();
6868

@@ -104,12 +104,12 @@ private void parseIndex(File file) throws IOException {
104104
}
105105
}
106106

107-
public void setLibrariesFolders(List<File> _librariesFolders) {
108-
librariesFolders = _librariesFolders;
107+
public void setLibrariesFolders(List<UserLibraryFolder> folders) {
108+
librariesFolders = folders;
109109
rescanLibraries();
110110
}
111111

112-
public List<File> getLibrariesFolders() {
112+
public List<UserLibraryFolder> getLibrariesFolders() {
113113
return librariesFolders;
114114
}
115115

@@ -126,8 +126,8 @@ public void rescanLibraries() {
126126
}
127127

128128
// Rescan libraries
129-
for (File folder : librariesFolders) {
130-
scanInstalledLibraries(folder, folder.equals(sketchbookLibrariesFolder));
129+
for (UserLibraryFolder folderDesc : librariesFolders) {
130+
scanInstalledLibraries(folderDesc);
131131
}
132132

133133
installedLibraries.stream().filter(new TypePredicate("Contributed")).filter(new LibraryInstalledInsideCore()).forEach(userLibrary -> {
@@ -136,46 +136,47 @@ public void rescanLibraries() {
136136
});
137137
}
138138

139-
private void scanInstalledLibraries(File folder, boolean isSketchbook) {
140-
File list[] = folder.listFiles(OnlyDirs.ONLY_DIRS);
139+
private void scanInstalledLibraries(UserLibraryFolder folderDesc) {
140+
File list[] = folderDesc.folder.listFiles(OnlyDirs.ONLY_DIRS);
141141
// if a bad folder or something like that, this might come back null
142142
if (list == null)
143143
return;
144144

145145
for (File subfolder : list) {
146-
if (!BaseNoGui.isSanitaryName(subfolder.getName())) {
146+
String subfolderName = subfolder.getName();
147+
if (!BaseNoGui.isSanitaryName(subfolderName)) {
147148

148149
// Detect whether the current folder name has already had a notification.
149-
if (!badLibNotified.contains(subfolder.getName())) {
150+
if (!badLibNotified.contains(subfolderName)) {
150151

151-
badLibNotified.add(subfolder.getName());
152+
badLibNotified.add(subfolderName);
152153

153154
String mess = I18n.format(tr("The library \"{0}\" cannot be used.\n"
154155
+ "Library names must contain only basic letters and numbers.\n"
155156
+ "(ASCII only and no spaces, and it cannot start with a number)"),
156-
subfolder.getName());
157+
subfolderName);
157158
BaseNoGui.showMessage(tr("Ignoring bad library name"), mess);
158159
}
159160
continue;
160161
}
161162

162163
try {
163-
scanLibrary(subfolder, isSketchbook);
164+
scanLibrary(new UserLibraryFolder(subfolder, folderDesc.location));
164165
} catch (IOException e) {
165166
System.out.println(I18n.format(tr("Invalid library found in {0}: {1}"), subfolder, e.getMessage()));
166167
}
167168
}
168169
}
169170

170-
private void scanLibrary(File folder, boolean isSketchbook) throws IOException {
171-
boolean readOnly = !FileUtils.isSubDirectory(sketchbookLibrariesFolder, folder);
171+
private void scanLibrary(UserLibraryFolder folderDesc) throws IOException {
172+
boolean readOnly = (folderDesc.location == Location.SKETCHBOOK);
172173

173174
// A library is considered "legacy" if it doesn't contains
174175
// a file called "library.properties"
175-
File check = new File(folder, "library.properties");
176+
File check = new File(folderDesc.folder, "library.properties");
176177
if (!check.exists() || !check.isFile()) {
177178
// Create a legacy library and exit
178-
LegacyUserLibrary lib = LegacyUserLibrary.create(folder);
179+
LegacyUserLibrary lib = LegacyUserLibrary.create(folderDesc);
179180
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
180181
if (headers.length == 0) {
181182
throw new IOException(lib.getSrcFolder().getAbsolutePath());
@@ -185,7 +186,7 @@ private void scanLibrary(File folder, boolean isSketchbook) throws IOException {
185186
}
186187

187188
// Create a regular library
188-
UserLibrary lib = UserLibrary.create(folder);
189+
UserLibrary lib = UserLibrary.create(folderDesc);
189190
String[] headers = BaseNoGui.headerListFromIncludePath(lib.getSrcFolder());
190191
if (headers.length == 0) {
191192
throw new IOException(lib.getSrcFolder().getAbsolutePath());
@@ -221,19 +222,6 @@ public File getStagingFolder() {
221222
return stagingFolder;
222223
}
223224

224-
/**
225-
* Set the sketchbook library folder. <br />
226-
* New libraries will be installed here. <br />
227-
* Libraries not found on this folder will be marked as read-only.
228-
*/
229-
public void setSketchbookLibrariesFolder(File folder) {
230-
this.sketchbookLibrariesFolder = folder;
231-
}
232-
233-
public File getSketchbookLibrariesFolder() {
234-
return sketchbookLibrariesFolder;
235-
}
236-
237225
public File getIndexFile() {
238226
return indexFile;
239227
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public synchronized void install(ContributedLibrary lib, Optional<ContributedLib
108108
// Step 2: Unpack library on the correct location
109109
progress.setStatus(I18n.format(tr("Installing library: {0}"), lib.getName()));
110110
progressListener.onProgress(progress);
111-
File libsFolder = BaseNoGui.librariesIndexer.getSketchbookLibrariesFolder();
111+
File libsFolder = BaseNoGui.getSketchbookLibrariesFolder().folder;
112112
File tmpFolder = FileUtils.createTempFolder(libsFolder);
113113
try {
114114
new ArchiveExtractor(platform).extract(lib.getDownloadedFile(), tmpFolder, 1);
@@ -132,7 +132,7 @@ public synchronized void install(ContributedLibrary lib, Optional<ContributedLib
132132
}
133133

134134
public synchronized void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException {
135-
if (lib.isReadOnly()) {
135+
if (lib.isIDEBuiltIn()) {
136136
return;
137137
}
138138

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

+10-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import processing.app.legacy.PApplet;
2121
import processing.app.packages.LibraryList;
2222
import processing.app.packages.UserLibrary;
23-
23+
import processing.app.packages.UserLibraryFolder;
24+
import processing.app.packages.UserLibraryFolder.Location;
2425
import cc.arduino.files.DeleteFilesOnShutdown;
2526
import processing.app.helpers.FileUtils;
2627

@@ -87,7 +88,7 @@ public class BaseNoGui {
8788
public static Map<String, LibraryList> importToLibraryTable;
8889

8990
// XXX: Remove this field
90-
static private List<File> librariesFolders;
91+
static private List<UserLibraryFolder> librariesFolders;
9192

9293
static UserNotifier notifier = new BasicUserNotifier();
9394

@@ -245,7 +246,7 @@ static public String getHardwarePath() {
245246
return getHardwareFolder().getAbsolutePath();
246247
}
247248

248-
static public List<File> getLibrariesPath() {
249+
static public List<UserLibraryFolder> getLibrariesFolders() {
249250
return librariesFolders;
250251
}
251252

@@ -326,7 +327,7 @@ static public File getSketchbookHardwareFolder() {
326327
return new File(getSketchbookFolder(), "hardware");
327328
}
328329

329-
static public File getSketchbookLibrariesFolder() {
330+
static public UserLibraryFolder getSketchbookLibrariesFolder() {
330331
File libdir = new File(getSketchbookFolder(), "libraries");
331332
if (!libdir.exists()) {
332333
FileWriter freadme = null;
@@ -340,7 +341,7 @@ static public File getSketchbookLibrariesFolder() {
340341
IOUtils.closeQuietly(freadme);
341342
}
342343
}
343-
return libdir;
344+
return new UserLibraryFolder(libdir, Location.SKETCHBOOK);
344345
}
345346

346347
static public String getSketchbookPath() {
@@ -651,7 +652,7 @@ static public void onBoardOrPortChange() {
651652
librariesFolders = new ArrayList<>();
652653

653654
// Add IDE libraries folder
654-
librariesFolders.add(getContentFile("libraries"));
655+
librariesFolders.add(new UserLibraryFolder(getContentFile("libraries"), Location.IDE_BUILTIN));
655656

656657
TargetPlatform targetPlatform = getTargetPlatform();
657658
if (targetPlatform != null) {
@@ -663,13 +664,13 @@ static public void onBoardOrPortChange() {
663664
File referencedPlatformFolder = referencedPlatform.getFolder();
664665
// Add libraries folder for the referenced platform
665666
File folder = new File(referencedPlatformFolder, "libraries");
666-
librariesFolders.add(folder);
667+
librariesFolders.add(new UserLibraryFolder(folder, Location.REFERENCED_CORE));
667668
}
668669
}
669670
File platformFolder = targetPlatform.getFolder();
670671
// Add libraries folder for the selected platform
671672
File folder = new File(platformFolder, "libraries");
672-
librariesFolders.add(folder);
673+
librariesFolders.add(new UserLibraryFolder(folder, Location.CORE));
673674
}
674675

675676
// Add libraries folder for the sketchbook
@@ -678,10 +679,7 @@ static public void onBoardOrPortChange() {
678679
// Scan for libraries in each library folder.
679680
// Libraries located in the latest folders on the list can override
680681
// other libraries with the same name.
681-
librariesIndexer.setSketchbookLibrariesFolder(getSketchbookLibrariesFolder());
682-
if (librariesIndexer.getLibrariesFolders() == null || !librariesIndexer.getLibrariesFolders().equals(librariesFolders)) {
683-
librariesIndexer.setLibrariesFolders(librariesFolders);
684-
}
682+
librariesIndexer.setLibrariesFolders(librariesFolders);
685683

686684
populateImportToLibraryTable();
687685
}

0 commit comments

Comments
 (0)