Moved libraries and importToLibraryTable (and related methods) from Base to BaseNoGui (work in progress).

This commit is contained in:
Claudio Indellicati 2014-08-21 14:06:25 +02:00 committed by Cristian Maglie
parent 64c6fe536c
commit c666274bf9
4 changed files with 93 additions and 52 deletions

View File

@ -86,12 +86,6 @@ public class Base {
static private List<File> librariesFolders; static private List<File> librariesFolders;
// maps library name to their library folder
static private LibraryList libraries;
// maps #included files to their library folder
public static Map<String, Library> importToLibraryTable;
// classpath for all known libraries for p5 // classpath for all known libraries for p5
// (both those in the p5/libs folder and those with lib subfolders // (both those in the p5/libs folder and those with lib subfolders
// found in the sketchbook) // found in the sketchbook)
@ -1224,17 +1218,15 @@ public class Base {
} }
public LibraryList getIDELibs() { public LibraryList getIDELibs() {
if (libraries == null) if (getLibraries() == null)
return new LibraryList(); return new LibraryList();
LibraryList res = new LibraryList(libraries); LibraryList res = new LibraryList(getLibraries());
res.removeAll(getUserLibs()); res.removeAll(getUserLibs());
return res; return res;
} }
public LibraryList getUserLibs() { public LibraryList getUserLibs() {
if (libraries == null) return BaseNoGui.getUserLibs();
return new LibraryList();
return libraries.filterLibrariesInSubfolder(getSketchbookFolder());
} }
public void rebuildImportMenu(JMenu importMenu) { public void rebuildImportMenu(JMenu importMenu) {
@ -1311,42 +1303,11 @@ public class Base {
} }
public LibraryList scanLibraries(List<File> folders) throws IOException { public LibraryList scanLibraries(List<File> folders) throws IOException {
LibraryList res = new LibraryList(); return BaseNoGui.scanLibraries(folders);
for (File folder : folders)
res.addOrReplaceAll(scanLibraries(folder));
return res;
} }
public LibraryList scanLibraries(File folder) throws IOException { public LibraryList scanLibraries(File folder) throws IOException {
LibraryList res = new LibraryList(); return BaseNoGui.scanLibraries(folder);
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;
} }
public void onBoardOrPortChange() { public void onBoardOrPortChange() {
@ -1377,18 +1338,18 @@ public class Base {
// Libraries located in the latest folders on the list can override // Libraries located in the latest folders on the list can override
// other libraries with the same name. // other libraries with the same name.
try { try {
libraries = scanLibraries(librariesFolders); BaseNoGui.scanAndUpdateLibraries(librariesFolders);
} catch (IOException e) { } catch (IOException e) {
showWarning(_("Error"), _("Error loading libraries"), e); showWarning(_("Error"), _("Error loading libraries"), e);
} }
// Populate importToLibraryTable // Populate importToLibraryTable
importToLibraryTable = new HashMap<String, Library>(); BaseNoGui.newImportToLibraryTable();
for (Library lib : libraries) { for (Library lib : getLibraries()) {
try { try {
String headers[] = headerListFromIncludePath(lib.getSrcFolder()); String headers[] = headerListFromIncludePath(lib.getSrcFolder());
for (String header : headers) { for (String header : headers) {
Library old = importToLibraryTable.get(header); Library old = BaseNoGui.importToLibraryTable.get(header);
if (old != null) { if (old != null) {
// If a library was already found with this header, keep // If a library was already found with this header, keep
// it if the library's name matches the header name. // it if the library's name matches the header name.
@ -1396,7 +1357,7 @@ public class Base {
if (old.getFolder().getPath().endsWith(name)) if (old.getFolder().getPath().endsWith(name))
continue; continue;
} }
importToLibraryTable.put(header, lib); BaseNoGui.importToLibraryTable.put(header, lib);
} }
} catch (IOException e) { } catch (IOException e) {
showWarning(_("Error"), I18n showWarning(_("Error"), I18n
@ -2013,7 +1974,7 @@ public class Base {
static public LibraryList getLibraries() { static public LibraryList getLibraries() {
return libraries; return BaseNoGui.getLibraries();
} }

View File

@ -6,6 +6,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import processing.app.debug.TargetBoard; import processing.app.debug.TargetBoard;
@ -15,7 +16,10 @@ import processing.app.debug.TargetPlatformException;
import processing.app.helpers.OSUtils; import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMap; import processing.app.helpers.PreferencesMap;
import processing.app.helpers.filefilters.OnlyDirs; import processing.app.helpers.filefilters.OnlyDirs;
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
import processing.app.legacy.PApplet; import processing.app.legacy.PApplet;
import processing.app.packages.Library;
import processing.app.packages.LibraryList;
public class BaseNoGui { public class BaseNoGui {
@ -29,6 +33,12 @@ public class BaseNoGui {
// commandline // commandline
static String currentDirectory = System.getProperty("user.dir"); static String currentDirectory = System.getProperty("user.dir");
// maps #included files to their library folder
public static Map<String, Library> importToLibraryTable;
// maps library name to their library folder
static private LibraryList libraries;
static public Map<String, TargetPackage> packages; static public Map<String, TargetPackage> packages;
static Platform platform; static Platform platform;
@ -102,6 +112,10 @@ public class BaseNoGui {
return getHardwareFolder().getAbsolutePath(); return getHardwareFolder().getAbsolutePath();
} }
static public LibraryList getLibraries() {
return libraries;
}
static public Platform getPlatform() { static public Platform getPlatform() {
return platform; return platform;
} }
@ -151,6 +165,25 @@ public class BaseNoGui {
return p.get(platformName); 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() { static public void initPackages() {
packages = new HashMap<String, TargetPackage>(); packages = new HashMap<String, TargetPackage>();
loadHardware(getHardwareFolder()); loadHardware(getHardwareFolder());
@ -213,6 +246,10 @@ public class BaseNoGui {
} }
} }
static public void newImportToLibraryTable() {
importToLibraryTable = new HashMap<String, Library>();
}
/** /**
* Spew the contents of a String object out to a file. * Spew the contents of a String object out to a file.
*/ */
@ -237,4 +274,47 @@ public class BaseNoGui {
} }
} }
static public void scanAndUpdateLibraries(List<File> folders) throws IOException {
libraries = scanLibraries(folders);
}
static public LibraryList scanLibraries(List<File> 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;
}
} }

View File

@ -953,7 +953,7 @@ public class Compiler implements MessageConsumer {
importedLibraries = new LibraryList(); importedLibraries = new LibraryList();
for (String item : preprocessor.getExtraImports()) { for (String item : preprocessor.getExtraImports()) {
Library lib = Base.importToLibraryTable.get(item); Library lib = BaseNoGui.importToLibraryTable.get(item);
if (lib != null && !importedLibraries.contains(lib)) { if (lib != null && !importedLibraries.contains(lib)) {
importedLibraries.add(lib); importedLibraries.add(lib);
} }

View File

@ -21,7 +21,7 @@ public abstract class AbstractGUITest {
Preferences.init(null); Preferences.init(null);
JPopupMenu.setDefaultLightWeightPopupEnabled(false); JPopupMenu.setDefaultLightWeightPopupEnabled(false);
Theme.init(); Theme.init();
Base.platform.setLookAndFeel(); Base.getPlatform().setLookAndFeel();
Base.untitledFolder = Base.createTempFolder("untitled"); Base.untitledFolder = Base.createTempFolder("untitled");
Base.untitledFolder.deleteOnExit(); Base.untitledFolder.deleteOnExit();