1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-21 15:54:39 +01:00

Fine tuned libraries search. Reintroduced ./libraries folder for shipped

platform independent libraries.
This commit is contained in:
Cristian Maglie 2012-01-09 18:20:55 +01:00
parent 57b8713cab
commit b7c54b79d7
5 changed files with 155 additions and 154 deletions

View File

@ -80,12 +80,14 @@ public class Base {
// these are static because they're used by Sketch // these are static because they're used by Sketch
static private File examplesFolder; static private File examplesFolder;
static private File librariesFolder;
static private File toolsFolder; static private File toolsFolder;
static Set<File> libraries; static private List<File> librariesFolders;
// maps imported packages to their library folder // maps library name to their library folder
static private Map<String, File> libraries;
// maps #included files to their library folder
static Map<String, File> importToLibraryTable; static Map<String, File> importToLibraryTable;
// classpath for all known libraries for p5 // classpath for all known libraries for p5
@ -632,8 +634,6 @@ public class Base {
// Only show .pde files as eligible bachelors // Only show .pde files as eligible bachelors
fd.setFilenameFilter(new FilenameFilter() { fd.setFilenameFilter(new FilenameFilter() {
public boolean accept(File dir, String name) { public boolean accept(File dir, String name) {
// TODO this doesn't seem to ever be used. AWESOME.
//System.out.println("check filter on " + dir + " " + name);
return name.toLowerCase().endsWith(".ino") return name.toLowerCase().endsWith(".ino")
|| name.toLowerCase().endsWith(".pde"); || name.toLowerCase().endsWith(".pde");
} }
@ -891,7 +891,6 @@ public class Base {
JMenuItem item; JMenuItem item;
menu.removeAll(); menu.removeAll();
//System.out.println("rebuilding toolbar menu");
// Add the single "Open" item // Add the single "Open" item
item = Editor.newJMenuItem(_("Open..."), 'O'); item = Editor.newJMenuItem(_("Open..."), 'O');
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
@ -905,20 +904,15 @@ public class Base {
// Add a list of all sketches and subfolders // Add a list of all sketches and subfolders
try { try {
boolean sketches = addSketches(menu, getSketchbookFolder(), true); boolean sketches = addSketches(menu, getSketchbookFolder(), true);
//boolean sketches = addSketches(menu, getSketchbookFolder());
if (sketches) menu.addSeparator(); if (sketches) menu.addSeparator();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
//System.out.println("rebuilding examples menu");
// Add each of the subfolders of examples directly to the menu // Add each of the subfolders of examples directly to the menu
try { try {
boolean found = addSketches(menu, examplesFolder, true); boolean found = addSketches(menu, examplesFolder, true);
if (found) menu.addSeparator(); if (found) menu.addSeparator();
found = addSketches(menu, getSketchbookLibrariesFolder(), true);
if (found) menu.addSeparator();
addSketches(menu, librariesFolder, true);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -940,15 +934,6 @@ public class Base {
public void rebuildImportMenu(JMenu importMenu) { public void rebuildImportMenu(JMenu importMenu) {
importMenu.removeAll(); importMenu.removeAll();
// reset the set of libraries
libraries = new HashSet<File>();
// reset the table mapping imports to libraries
importToLibraryTable = new HashMap<String, File>();
// Add from the "libraries" subfolder in the Processing directory
// Choose which library to add by chip platform
try { try {
// Find the current target. Get the platform, and then select the // Find the current target. Get the platform, and then select the
// correct name and core path. // correct name and core path.
@ -959,55 +944,92 @@ public class Base {
platformItem.setEnabled(false); platformItem.setEnabled(false);
importMenu.add(platformItem); importMenu.add(platformItem);
importMenu.addSeparator(); importMenu.addSeparator();
addLibraries(importMenu, librariesFolder); addLibraries(importMenu, libraries);
} catch (IOException e) {
e.printStackTrace();
}
// Add libraries found in the sketchbook folder
int separatorIndex = importMenu.getItemCount();
try {
File sketchbookLibraries = getSketchbookLibrariesFolder();
boolean found = addLibraries(importMenu, sketchbookLibraries);
if (found) {
JMenuItem contrib = new JMenuItem(_("Contributed"));
contrib.setEnabled(false);
importMenu.insert(contrib, separatorIndex);
importMenu.insertSeparator(separatorIndex);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void rebuildExamplesMenu(JMenu menu) { public void rebuildExamplesMenu(JMenu menu) {
//System.out.println("rebuilding examples menu");
try { try {
menu.removeAll(); menu.removeAll();
// Add examples from distribution "example" folder
boolean found = addSketches(menu, examplesFolder, false); boolean found = addSketches(menu, examplesFolder, false);
if (found) menu.addSeparator(); if (found) menu.addSeparator();
found = addSketches(menu, getSketchbookLibrariesFolder(), false);
if (found) menu.addSeparator(); // Add examples from libraries
addSketches(menu, librariesFolder, false); List<String> names = new ArrayList<String>(libraries.keySet());
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
for (String name : names) {
File folder = libraries.get(name);
addSketchesSubmenu(menu, name, folder, false);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public Map<String, File> scanLibraries(List<File> folders) {
Map<String, File> res = new HashMap<String, File>();
for (File folder : folders)
res.putAll(scanLibraries(folder));
return res;
}
public Map<String, File> scanLibraries(File folder) {
Map<String, File> res = new HashMap<String, File>();
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;
}
// (also replace previously found libs with the same name)
res.put(libName, subfolder);
}
return res;
}
public void onBoardOrPortChange() { public void onBoardOrPortChange() {
// Calculate paths for libraries and examples // Calculate paths for libraries and examples
examplesFolder = getContentFile("examples"); examplesFolder = getContentFile("examples");
TargetPlatform targetPlatform = getTargetPlatform();
PreferencesMap prefs = targetPlatform.getPreferences();
librariesFolder = new File(targetPlatform.getFolder(), prefs
.get("library.core.path"));
toolsFolder = getContentFile("tools"); toolsFolder = getContentFile("tools");
// Update editors status bar TargetPlatform targetPlatform = getTargetPlatform();
for (Editor editor : editors) { PreferencesMap prefs = targetPlatform.getPreferences();
editor.onBoardOrPortChange(); librariesFolders = new ArrayList<File>();
librariesFolders.add(getContentFile("libraries"));
librariesFolders.add(new File(targetPlatform.getFolder(), prefs
.get("library.core.path")));
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.
libraries = scanLibraries(librariesFolders);
// Populate importToLibraryTable
importToLibraryTable = new HashMap<String, File>();
for (File subfolder : libraries.values()) {
String packages[] = headerListFromIncludePath(subfolder);
for (String pkg : packages)
importToLibraryTable.put(pkg, subfolder);
} }
// Update editors status bar
for (Editor editor : editors)
editor.onBoardOrPortChange();
} }
@ -1103,8 +1125,25 @@ public class Base {
// Alphabetize list, since it's not always alpha order // Alphabetize list, since it's not always alpha order
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER); Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
//processing.core.PApplet.println("adding sketches " + folder.getAbsolutePath());
//PApplet.println(list); boolean ifound = false;
for (String name : list) {
if ((name.charAt(0) == '.') ||
name.equals("CVS")) continue;
File subfolder = new File(folder, name);
if (!subfolder.isDirectory()) continue;
if (addSketchesSubmenu(menu, name, subfolder, replaceExisting))
ifound = true;
}
return ifound; // actually ignored, but..
}
private boolean addSketchesSubmenu(JMenu menu, String name, File folder,
final boolean replaceExisting) throws IOException {
ActionListener listener = new ActionListener() { ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -1127,82 +1166,52 @@ public class Base {
} }
} }
}; };
// offers no speed improvement
//menu.addActionListener(listener);
boolean ifound = false; File entry = new File(folder, name + ".ino");
if (!entry.exists() && (new File(folder, name + ".pde")).exists())
entry = new File(folder, name + ".pde");
for (int i = 0; i < list.length; i++) { // if a .pde file of the same prefix as the folder exists..
if ((list[i].charAt(0) == '.') || if (entry.exists()) {
list[i].equals("CVS")) continue;
File subfolder = new File(folder, list[i]); if (!Sketch.isSanitaryName(name)) {
if (!subfolder.isDirectory()) continue; if (!builtOnce) {
String complaining = I18n
File entry = new File(subfolder, list[i] + ".ino"); .format(
if (!entry.exists() && (new File(subfolder, list[i] + ".pde")).exists()) { _("The sketch \"{0}\" cannot be used.\n"
entry = new File(subfolder, list[i] + ".pde"); + "Sketch names must contain only basic letters and numbers\n"
} + "(ASCII-only with no spaces, "
// if a .pde file of the same prefix as the folder exists.. + "and it cannot start with a number).\n"
if (entry.exists()) { + "To get rid of this message, remove the sketch from\n"
//String sanityCheck = sanitizedName(list[i]); + "{1}"), name, entry.getAbsolutePath());
//if (!sanityCheck.equals(list[i])) { Base.showMessage(_("Ignoring sketch with bad name"), complaining);
if (!Sketch.isSanitaryName(list[i])) {
if (!builtOnce) {
String complaining = I18n.format(
_("The sketch \"{0}\" cannot be used.\n" +
"Sketch names must contain only basic letters and numbers\n" +
"(ASCII-only with no spaces, " +
"and it cannot start with a number).\n" +
"To get rid of this message, remove the sketch from\n" +
"{1}"), list[i], entry.getAbsolutePath()
);
Base.showMessage(_("Ignoring sketch with bad name"), complaining);
}
continue;
}
JMenuItem item = new JMenuItem(list[i]);
item.addActionListener(listener);
item.setActionCommand(entry.getAbsolutePath());
menu.add(item);
ifound = true;
} else {
// don't create an extra menu level for a folder named "examples"
if (subfolder.getName().equals("examples")) {
boolean found = addSketches(menu, subfolder, replaceExisting);
if (found) ifound = true;
} else {
// not a sketch folder, but maybe a subfolder containing sketches
JMenu submenu = new JMenu(list[i]);
// needs to be separate var
// otherwise would set ifound to false
boolean found = addSketches(submenu, subfolder, replaceExisting);
//boolean found = addSketches(submenu, subfolder); //, false);
if (found) {
menu.add(submenu);
ifound = true;
} }
return false;
} }
JMenuItem item = new JMenuItem(name);
item.addActionListener(listener);
item.setActionCommand(entry.getAbsolutePath());
menu.add(item);
return true;
} }
}
return ifound; // actually ignored, but.. // don't create an extra menu level for a folder named "examples"
if (folder.getName().equals("examples"))
return addSketches(menu, folder, replaceExisting);
// not a sketch folder, but maybe a subfolder containing sketches
JMenu submenu = new JMenu(name);
boolean found = addSketches(submenu, folder, replaceExisting);
if (found)
menu.add(submenu);
return found;
} }
protected boolean addLibraries(JMenu menu, Map<String, File> libs) throws IOException {
protected boolean addLibraries(JMenu menu, File folder) throws IOException { List<String> list = new ArrayList<String>(libs.keySet());
if (!folder.isDirectory()) Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
return false;
String list[] = folder.list(new OnlyDirs());
// if a bad folder or something like that, this might come back null
if (list == null)
return false;
// alphabetize list, since it's not always alpha order
// replaced hella slow bubble sort with this feller for 0093
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
ActionListener listener = new ActionListener() { ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -1212,28 +1221,13 @@ public class Base {
boolean found = false; boolean found = false;
for (String libraryName : list) { for (String name : list) {
File subfolder = new File(folder, libraryName); File folder = libs.get(name);
String sanityCheck = Sketch.sanitizeName(libraryName);
if (!sanityCheck.equals(libraryName)) {
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)"),
libraryName);
Base.showMessage(_("Ignoring bad library name"), mess);
continue;
}
libraries.add(subfolder); // Add new element at the bottom
JMenuItem item = new JMenuItem(name);
String packages[] = headerListFromIncludePath(subfolder);
for (String pkg : packages) {
importToLibraryTable.put(pkg, subfolder);
}
JMenuItem item = new JMenuItem(libraryName);
item.addActionListener(listener); item.addActionListener(listener);
item.setActionCommand(subfolder.getAbsolutePath()); item.setActionCommand(folder.getAbsolutePath());
menu.add(item); menu.add(item);
found = true; found = true;
@ -1497,7 +1491,7 @@ public class Base {
} }
static public Set<File> getLibraries() { static public Map<String, File> getLibraries() {
return libraries; return libraries;
} }
@ -1507,8 +1501,8 @@ public class Base {
} }
static public String getLibrariesPath() { static public List<File> getLibrariesPath() {
return librariesFolder.getAbsolutePath(); return librariesFolders;
} }

View File

@ -1821,29 +1821,29 @@ public class Sketch {
*/ */
public boolean isReadOnly() { public boolean isReadOnly() {
String apath = folder.getAbsolutePath(); String apath = folder.getAbsolutePath();
for (File folder : Base.getLibrariesPath()) {
if (apath.startsWith(folder.getAbsolutePath()))
return true;
}
if (apath.startsWith(Base.getExamplesPath()) || if (apath.startsWith(Base.getExamplesPath()) ||
apath.startsWith(Base.getLibrariesPath()) ||
apath.startsWith(Base.getSketchbookLibrariesPath())) { apath.startsWith(Base.getSketchbookLibrariesPath())) {
return true; return true;
}
// canWrite() doesn't work on directories // canWrite() doesn't work on directories
//} else if (!folder.canWrite()) { // } else if (!folder.canWrite()) {
} else {
// check to see if each modified code file can be written to // check to see if each modified code file can be written to
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (code[i].isModified() && if (code[i].isModified() && code[i].fileReadOnly() &&
code[i].fileReadOnly() && code[i].fileExists()) {
code[i].fileExists()) { // System.err.println("found a read-only file " + code[i].file);
//System.err.println("found a read-only file " + code[i].file); return true;
return true;
}
} }
//return true;
} }
return false; return false;
} }
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// Breaking out extension types in order to clean up the code, and make it // Breaking out extension types in order to clean up the code, and make it

View File

@ -59,7 +59,7 @@ public class PdeKeywords extends CTokenMarker {
keywordColoring = new KeywordMap(false); keywordColoring = new KeywordMap(false);
keywordToReference = new Hashtable(); keywordToReference = new Hashtable();
getKeywords(Base.getLibStream("keywords.txt")); getKeywords(Base.getLibStream("keywords.txt"));
for (File lib : Base.getLibraries()) { for (File lib : Base.getLibraries().values()) {
File keywords = new File(lib, "keywords.txt"); File keywords = new File(lib, "keywords.txt");
if (keywords.exists()) getKeywords(new FileInputStream(keywords)); if (keywords.exists()) getKeywords(new FileInputStream(keywords));
} }

View File

@ -81,6 +81,11 @@
<fileset dir="shared/tools" /> <fileset dir="shared/tools" />
</copy> </copy>
<!-- copy library folder -->
<copy todir="${target.path}/libraries">
<fileset dir="../libraries" />
</copy>
<!-- copy hardware folder --> <!-- copy hardware folder -->
<copy todir="${target.path}/hardware"> <copy todir="${target.path}/hardware">
<fileset dir="../hardware" /> <fileset dir="../hardware" />

2
libraries/keep Normal file
View File

@ -0,0 +1,2 @@