Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing Libraries in subfolders #1986

245 changes: 130 additions & 115 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -1277,20 +1277,6 @@ protected void rebuildSketchbookMenu(JMenu menu) {
}
}

public LibraryList getIDELibs() {
if (libraries == null)
return new LibraryList();
LibraryList res = new LibraryList(libraries);
res.removeAll(getUserLibs());
return res;
}

public LibraryList getUserLibs() {
if (libraries == null)
return new LibraryList();
return libraries.filterLibrariesInSubfolder(getSketchbookFolder());
}

public void rebuildImportMenu(JMenu importMenu) {
importMenu.removeAll();

Expand All @@ -1304,34 +1290,10 @@ public void actionPerformed(ActionEvent e) {
}
});
importMenu.add(addLibraryMenuItem);
importMenu.addSeparator();

// Split between user supplied libraries and IDE libraries
TargetPlatform targetPlatform = getTargetPlatform();

if (targetPlatform != null) {
LibraryList ideLibs = getIDELibs();
LibraryList userLibs = getUserLibs();
if (libraries != null) {
try {
// Find the current target. Get the platform, and then select the
// correct name and core path.
PreferencesMap prefs = targetPlatform.getPreferences();
if (prefs != null) {
String platformName = prefs.get("name");
if (platformName != null) {
JMenuItem platformItem = new JMenuItem(_(platformName));
platformItem.setEnabled(false);
importMenu.add(platformItem);
}
}
if (ideLibs.size() > 0) {
importMenu.addSeparator();
addLibraries(importMenu, ideLibs);
}
if (userLibs.size() > 0) {
importMenu.addSeparator();
addLibraries(importMenu, userLibs);
}
addLibraries(importMenu, libraries);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -1344,101 +1306,137 @@ public void rebuildExamplesMenu(JMenu menu) {

// Add examples from distribution "example" folder
boolean found = addSketches(menu, examplesFolder, false);
if (found) menu.addSeparator();
if (found) {
// Prepend a label
JMenuItem label = new JMenuItem(_("Core examples"));
label.setEnabled(false);
menu.add(label, 0);
}

// Add examples from libraries
LibraryList ideLibs = getIDELibs();
ideLibs.sort();
for (Library lib : ideLibs)
addSketchesSubmenu(menu, lib, false);

LibraryList userLibs = getUserLibs();
if (userLibs.size()>0) {
menu.addSeparator();
userLibs.sort();
for (Library lib : userLibs)
addSketchesSubmenu(menu, lib, false);
if (libraries != null && !libraries.isEmpty()) {
for (LibraryList sub : libraries.getSubs()) {
if (found)
menu.addSeparator();

// Prepend a label
JMenuItem label = new JMenuItem(sub.getName());
label.setEnabled(false);
menu.add(label);

addLibraryExamples(menu, sub);

found = true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

public LibraryList scanLibraries(List<File> folders) throws IOException {
LibraryList res = new LibraryList();
for (File folder : folders)
res.addOrReplaceAll(scanLibraries(folder));
return res;
protected void addLibraryExamples(JMenu menu, LibraryList libs) throws IOException {
// Add any subdirectories first
for (LibraryList sub : libs.getSubs()) {
if (sub.isEmpty())
continue;

JMenu submenu = new JMenu(sub.getName());
addLibraryExamples(submenu, sub);
menu.add(submenu);
MenuScroller.setScrollerFor(submenu);
}

// Then add the libraries directly in this folder
for (Library lib : libs.getLibs()) {
addSketchesSubmenu(menu, lib, false);
}
}

public LibraryList scanLibraries(File folder) throws IOException {
LibraryList res = new LibraryList();
public void updateLibraries() throws IOException {
// Calculate paths for libraries and examples
examplesFolder = getContentFile("examples");
toolsFolder = getContentFile("tools");

String list[] = folder.list(new OnlyDirs());
// if a bad folder or something like that, this might come back null
if (list == null)
return res;
libraries = new LibraryList(null);
librariesFolders = new ArrayList<File>();

for (String libName : list) {
File subfolder = new File(folder, libName);
if (!Sketch.isSanitaryName(libName)) {
String mess = I18n.format(_("The library \"{0}\" cannot be used.\n"
+ "Library names must contain only basic letters and numbers.\n"
+ "(ASCII only and no spaces, and it cannot start with a number)"),
libName);
Base.showMessage(_("Ignoring bad library name"), mess);
continue;
File ideLibs = getContentFile("libraries");
libraries.addSub(scanLibraries(ideLibs, _("Libraries for all boards"), true));
librariesFolders.add(ideLibs);

TargetPlatform targetPlatform = getTargetPlatform();
if (targetPlatform != null) {
File platformFolder = targetPlatform.getFolder();
File platformLibs = new File(platformFolder, "libraries");
librariesFolders.add(platformLibs);
libraries.addSub(scanLibraries(platformLibs, I18n.format(_("Libraries for {0}"), targetPlatform.getName()), true));

String core = getBoardPreferences().get("build.core");
TargetPlatform referencedPlatform = null;
if (core.contains(":")) {
String referencedCore = core.split(":")[0];
referencedPlatform = Base.getTargetPlatform(referencedCore, targetPlatform.getId());
if (referencedPlatform != null) {
File referencedPlatformFolder = referencedPlatform.getFolder();
File referencedLibs = new File(referencedPlatformFolder, "libraries");
librariesFolders.add(referencedLibs);
libraries.addSub(scanLibraries(referencedLibs, I18n.format(_("Libraries for {0}"), referencedPlatform.getName()), true));
}
}
}

File sketchbookLibs = getSketchbookLibrariesFolder();
librariesFolders.add(sketchbookLibs);
libraries.addSub(scanLibraries(sketchbookLibs, _("Libraries from your sketchbook"), true));
}

public LibraryList scanLibraries(File folder, String name, boolean allow_legacy) throws IOException {
LibraryList res = new LibraryList(name != null ? name : folder.getName());

File[] subfolders = folder.listFiles(new OnlyDirs());
if (subfolders != null) {
for (File subfolder : subfolders)
scanLibraryFolder(res, subfolder, allow_legacy);
}

res.sort();

return res;
}

public void scanLibraryFolder(LibraryList list, File folder, boolean allow_legacy) throws IOException {
if (Library.isLibrary(folder)) {
// This looks like a library, add it
try {
Library lib = Library.create(subfolder);
Library lib = Library.create(folder);
// (also replace previously found libs with the same name)
if (lib != null)
res.addOrReplace(lib);
list.addOrReplace(lib);
} catch (IOException e) {
System.out.println(I18n.format(_("Invalid library found in {0}: {1}"),
subfolder, e.getMessage()));
folder, e.getMessage()));
}
} else {
// This didn't look like a library, see if we can find libraries
// in the subdirectories
LibraryList sub = scanLibraries(folder, null, false);

if (!sub.isEmpty())
list.addSub(sub);
}
return res;
}

public void onBoardOrPortChange() {
TargetPlatform targetPlatform = getTargetPlatform();
if (targetPlatform == null)
return;

// Calculate paths for libraries and examples
examplesFolder = getContentFile("examples");
toolsFolder = getContentFile("tools");

File platformFolder = targetPlatform.getFolder();
librariesFolders = new ArrayList<File>();
librariesFolders.add(getContentFile("libraries"));
String core = getBoardPreferences().get("build.core");
if (core.contains(":")) {
String referencedCore = core.split(":")[0];
TargetPlatform referencedPlatform = Base.getTargetPlatform(referencedCore, targetPlatform.getId());
if (referencedPlatform != null) {
File referencedPlatformFolder = referencedPlatform.getFolder();
librariesFolders.add(new File(referencedPlatformFolder, "libraries"));
}
}
librariesFolders.add(new File(platformFolder, "libraries"));
librariesFolders.add(getSketchbookLibrariesFolder());

// Scan for libraries in each library folder.
// Libraries located in the latest folders on the list can override
// other libraries with the same name.
// Scan for libraries
try {
libraries = scanLibraries(librariesFolders);
updateLibraries();
} catch (IOException e) {
showWarning(_("Error"), _("Error loading libraries"), e);
}

// Populate importToLibraryTable
importToLibraryTable = new HashMap<String, Library>();
for (Library lib : libraries) {
for (Library lib : getLibraries()) {
try {
String headers[] = headerListFromIncludePath(lib.getSrcFolder());
for (String header : headers) {
Expand Down Expand Up @@ -1493,9 +1491,8 @@ public void rebuildBoardsMenu(JMenu toolsMenu, Editor editor) throws Exception {
first = false;

// Add a title for each platform
String platformLabel = targetPlatform.getPreferences().get("name");
if (platformLabel != null && !targetPlatform.getBoards().isEmpty()) {
JMenuItem menuLabel = new JMenuItem(_(platformLabel));
if (!targetPlatform.getBoards().isEmpty()) {
JMenuItem menuLabel = new JMenuItem(_(targetPlatform.getName()));
menuLabel.setEnabled(false);
boardsMenu.add(menuLabel);
}
Expand Down Expand Up @@ -1838,11 +1835,31 @@ public void actionPerformed(ActionEvent e) {
}

protected void addLibraries(JMenu menu, LibraryList libs) throws IOException {
for (LibraryList sub : libs.getSubs()) {
menu.addSeparator();

LibraryList list = new LibraryList(libs);
list.sort();
// Prepend a label
JMenuItem label = new JMenuItem(sub.getName());
label.setEnabled(false);
menu.add(label);

for (Library lib : list) {
addLibrariesRecursive(menu, sub);
}
}

protected void addLibrariesRecursive(JMenu menu, LibraryList libs) throws IOException {
// Add any subdirectories first
for (LibraryList sub : libs.getSubs()) {
if (sub.isEmpty())
continue;

JMenu submenu = new JMenu(sub.getName());
addLibrariesRecursive(submenu, sub);
menu.add(submenu);
MenuScroller.setScrollerFor(submenu);
}
// Then add the libraries directly in this folder
for (Library lib : libs.getLibs()) {
@SuppressWarnings("serial")
AbstractAction action = new AbstractAction(lib.getName()) {
public void actionPerformed(ActionEvent event) {
Expand All @@ -1860,8 +1877,6 @@ public void actionPerformed(ActionEvent event) {
JMenuItem item = new JMenuItem(action);
item.putClientProperty("library", lib);
menu.add(item);

// XXX: DAM: should recurse here so that library folders can be nested
}
}

Expand Down Expand Up @@ -1899,7 +1914,7 @@ protected void loadHardware(File folder) {
try {
packages.put(target, new TargetPackage(target, subfolder));
} catch (TargetPlatformException e) {
System.out.println("WARNING: Error loading hardware folder " + target);
System.out.println("WARNING: Error loading hardware folder " + target + " from " + subfolder);
System.out.println(" " + e.getMessage());
}
}
Expand Down Expand Up @@ -2137,8 +2152,8 @@ static public File createTempFolder(String name) {
}


static public LibraryList getLibraries() {
return libraries;
static public List<Library> getLibraries() {
return libraries.getAll();
}


Expand Down
7 changes: 3 additions & 4 deletions app/src/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import processing.app.helpers.PreferencesMap;
import processing.app.helpers.FileUtils;
import processing.app.packages.Library;
import processing.app.packages.LibraryList;
import processing.app.preproc.*;
import processing.core.*;
import static processing.app.I18n._;
Expand Down Expand Up @@ -94,7 +93,7 @@ public class Sketch {
/**
* List of library folders.
*/
private LibraryList importedLibraries;
private ArrayList<Library> importedLibraries;

/**
* File inside the build directory that contains the build options
Expand Down Expand Up @@ -1386,7 +1385,7 @@ public void preprocess(String buildPath, PdePreprocessor preprocessor) throws Ru

// grab the imports from the code just preproc'd

importedLibraries = new LibraryList();
importedLibraries = new ArrayList<Library>();
for (String item : preprocessor.getExtraImports()) {
Library lib = Base.importToLibraryTable.get(item);
if (lib != null && !importedLibraries.contains(lib)) {
Expand Down Expand Up @@ -1419,7 +1418,7 @@ public void preprocess(String buildPath, PdePreprocessor preprocessor) throws Ru
}


public LibraryList getImportedLibraries() {
public List<Library> getImportedLibraries() {
return importedLibraries;
}

Expand Down
7 changes: 7 additions & 0 deletions app/src/processing/app/debug/TargetPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ public String getId() {
return id;
}

public String getName() {
String name = preferences.get("name");
if (name == null)
return containerPackage.getId() + ":" + id;
return name;
}

public File getFolder() {
return folder;
}
Expand Down
Loading