diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index b1ead8892..c6dd8de23 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -86,12 +86,6 @@ public class Base { static private List librariesFolders; - // maps library name to their library folder - static private LibraryList libraries; - - // maps #included files to their library folder - public static Map importToLibraryTable; - // classpath for all known libraries for p5 // (both those in the p5/libs folder and those with lib subfolders // found in the sketchbook) @@ -1224,17 +1218,15 @@ public class Base { } public LibraryList getIDELibs() { - if (libraries == null) + if (getLibraries() == null) return new LibraryList(); - LibraryList res = new LibraryList(libraries); + LibraryList res = new LibraryList(getLibraries()); res.removeAll(getUserLibs()); return res; } public LibraryList getUserLibs() { - if (libraries == null) - return new LibraryList(); - return libraries.filterLibrariesInSubfolder(getSketchbookFolder()); + return BaseNoGui.getUserLibs(); } public void rebuildImportMenu(JMenu importMenu) { @@ -1311,42 +1303,11 @@ public class Base { } public LibraryList scanLibraries(List folders) throws IOException { - LibraryList res = new LibraryList(); - for (File folder : folders) - res.addOrReplaceAll(scanLibraries(folder)); - return res; + return BaseNoGui.scanLibraries(folders); } public LibraryList scanLibraries(File folder) throws IOException { - LibraryList res = new LibraryList(); - - String list[] = folder.list(new OnlyDirs()); - // if a bad folder or something like that, this might come back null - if (list == null) - return res; - - 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; - } - - try { - Library lib = Library.create(subfolder); - // (also replace previously found libs with the same name) - if (lib != null) - res.addOrReplace(lib); - } catch (IOException e) { - System.out.println(I18n.format(_("Invalid library found in {0}: {1}"), - subfolder, e.getMessage())); - } - } - return res; + return BaseNoGui.scanLibraries(folder); } public void onBoardOrPortChange() { @@ -1377,18 +1338,18 @@ public class Base { // Libraries located in the latest folders on the list can override // other libraries with the same name. try { - libraries = scanLibraries(librariesFolders); + BaseNoGui.scanAndUpdateLibraries(librariesFolders); } catch (IOException e) { showWarning(_("Error"), _("Error loading libraries"), e); } // Populate importToLibraryTable - importToLibraryTable = new HashMap(); - for (Library lib : libraries) { + BaseNoGui.newImportToLibraryTable(); + for (Library lib : getLibraries()) { try { String headers[] = headerListFromIncludePath(lib.getSrcFolder()); for (String header : headers) { - Library old = importToLibraryTable.get(header); + Library old = BaseNoGui.importToLibraryTable.get(header); if (old != null) { // If a library was already found with this header, keep // it if the library's name matches the header name. @@ -1396,7 +1357,7 @@ public class Base { if (old.getFolder().getPath().endsWith(name)) continue; } - importToLibraryTable.put(header, lib); + BaseNoGui.importToLibraryTable.put(header, lib); } } catch (IOException e) { showWarning(_("Error"), I18n @@ -2013,7 +1974,7 @@ public class Base { static public LibraryList getLibraries() { - return libraries; + return BaseNoGui.getLibraries(); } diff --git a/app/src/processing/app/BaseNoGui.java b/app/src/processing/app/BaseNoGui.java index fbf091fad..2e155316e 100644 --- a/app/src/processing/app/BaseNoGui.java +++ b/app/src/processing/app/BaseNoGui.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import processing.app.debug.TargetBoard; @@ -15,7 +16,10 @@ import processing.app.debug.TargetPlatformException; import processing.app.helpers.OSUtils; import processing.app.helpers.PreferencesMap; import processing.app.helpers.filefilters.OnlyDirs; +import processing.app.helpers.filefilters.OnlyFilesWithExtension; import processing.app.legacy.PApplet; +import processing.app.packages.Library; +import processing.app.packages.LibraryList; public class BaseNoGui { @@ -29,6 +33,12 @@ public class BaseNoGui { // commandline static String currentDirectory = System.getProperty("user.dir"); + // maps #included files to their library folder + public static Map importToLibraryTable; + + // maps library name to their library folder + static private LibraryList libraries; + static public Map packages; static Platform platform; @@ -102,6 +112,10 @@ public class BaseNoGui { return getHardwareFolder().getAbsolutePath(); } + static public LibraryList getLibraries() { + return libraries; + } + static public Platform getPlatform() { return platform; } @@ -151,6 +165,25 @@ public class BaseNoGui { return p.get(platformName); } + static public LibraryList getUserLibs() { + if (libraries == null) + return new LibraryList(); + return libraries.filterLibrariesInSubfolder(getSketchbookFolder()); + } + + /** + * Given a folder, return a list of the header files in that folder (but not + * the header files in its sub-folders, as those should be included from + * within the header files at the top-level). + */ + static public String[] headerListFromIncludePath(File path) throws IOException { + String[] list = path.list(new OnlyFilesWithExtension(".h")); + if (list == null) { + throw new IOException(); + } + return list; + } + static public void initPackages() { packages = new HashMap(); loadHardware(getHardwareFolder()); @@ -213,6 +246,10 @@ public class BaseNoGui { } } + static public void newImportToLibraryTable() { + importToLibraryTable = new HashMap(); + } + /** * Spew the contents of a String object out to a file. */ @@ -237,4 +274,47 @@ public class BaseNoGui { } } + static public void scanAndUpdateLibraries(List folders) throws IOException { + libraries = scanLibraries(folders); + } + + static public LibraryList scanLibraries(List folders) throws IOException { + LibraryList res = new LibraryList(); + for (File folder : folders) + res.addOrReplaceAll(scanLibraries(folder)); + return res; + } + + static public LibraryList scanLibraries(File folder) throws IOException { + LibraryList res = new LibraryList(); + + String list[] = folder.list(new OnlyDirs()); + // if a bad folder or something like that, this might come back null + if (list == null) + return res; + + 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; + } + + try { + Library lib = Library.create(subfolder); + // (also replace previously found libs with the same name) + if (lib != null) + res.addOrReplace(lib); + } catch (IOException e) { + System.out.println(I18n.format(_("Invalid library found in {0}: {1}"), + subfolder, e.getMessage())); + } + } + return res; + } + } diff --git a/app/src/processing/app/debug/Compiler.java b/app/src/processing/app/debug/Compiler.java index 5daa88837..d515f9545 100644 --- a/app/src/processing/app/debug/Compiler.java +++ b/app/src/processing/app/debug/Compiler.java @@ -953,7 +953,7 @@ public class Compiler implements MessageConsumer { importedLibraries = new LibraryList(); for (String item : preprocessor.getExtraImports()) { - Library lib = Base.importToLibraryTable.get(item); + Library lib = BaseNoGui.importToLibraryTable.get(item); if (lib != null && !importedLibraries.contains(lib)) { importedLibraries.add(lib); } diff --git a/app/test/processing/app/AbstractGUITest.java b/app/test/processing/app/AbstractGUITest.java index b43ec010c..b5a209bb0 100644 --- a/app/test/processing/app/AbstractGUITest.java +++ b/app/test/processing/app/AbstractGUITest.java @@ -21,7 +21,7 @@ public abstract class AbstractGUITest { Preferences.init(null); JPopupMenu.setDefaultLightWeightPopupEnabled(false); Theme.init(); - Base.platform.setLookAndFeel(); + Base.getPlatform().setLookAndFeel(); Base.untitledFolder = Base.createTempFolder("untitled"); Base.untitledFolder.deleteOnExit();