Skip to content

Commit aa2d0e0

Browse files
committed
Factoring Library class, step 2: first try parsing library metadata
1 parent a2fc433 commit aa2d0e0

File tree

7 files changed

+207
-113
lines changed

7 files changed

+207
-113
lines changed

app/src/processing/app/Base.java

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,25 +1044,17 @@ protected void rebuildSketchbookMenu(JMenu menu) {
10441044
}
10451045

10461046
public LibraryList getIDELibs() {
1047-
LibraryList libs = new LibraryList();
10481047
if (libraries == null)
1049-
return libs;
1050-
for (Library lib : libraries) {
1051-
if (!FileUtils.isSubDirectory(getSketchbookFolder(), lib.getRootFolder()))
1052-
libs.add(lib);
1053-
}
1054-
return libs;
1048+
return new LibraryList();
1049+
LibraryList res = new LibraryList(libraries);
1050+
res.removeAll(getUserLibs());
1051+
return res;
10551052
}
10561053

10571054
public LibraryList getUserLibs() {
1058-
LibraryList libs = new LibraryList();
10591055
if (libraries == null)
1060-
return libs;
1061-
for (Library lib : libraries) {
1062-
if (FileUtils.isSubDirectory(getSketchbookFolder(), lib.getRootFolder()))
1063-
libs.add(lib);
1064-
}
1065-
return libs;
1056+
return new LibraryList();
1057+
return libraries.filterLibrariesInSubfolder(getSketchbookFolder());
10661058
}
10671059

10681060
public void rebuildImportMenu(JMenu importMenu, final Editor editor) {
@@ -1124,28 +1116,16 @@ public void rebuildExamplesMenu(JMenu menu) {
11241116

11251117
// Add examples from libraries
11261118
LibraryList ideLibs = getIDELibs();
1127-
Collections.sort(ideLibs, Library.CASE_INSENSITIVE_ORDER);
1128-
for (Library lib : ideLibs) {
1129-
File folder = lib.getRootFolder();
1130-
String name = lib.getName();
1131-
addSketchesSubmenu(menu, name, folder, false);
1132-
// Allows "fat" libraries to have examples in the root folder
1133-
if (folder.getName().equals(Base.getTargetPlatform().getName()))
1134-
addSketchesSubmenu(menu, name, folder.getParentFile(), false);
1135-
}
1119+
ideLibs.sort();
1120+
for (Library lib : ideLibs)
1121+
addSketchesSubmenu(menu, lib, false);
11361122

11371123
LibraryList userLibs = getUserLibs();
11381124
if (userLibs.size()>0) {
11391125
menu.addSeparator();
1140-
Collections.sort(userLibs, Library.CASE_INSENSITIVE_ORDER);
1141-
for (Library lib : userLibs) {
1142-
File folder = lib.getRootFolder();
1143-
String name = lib.getName();
1144-
addSketchesSubmenu(menu, name, folder, false);
1145-
// Allows "fat" libraries to have examples in the root folder
1146-
if (folder.getName().equals(Base.getTargetPlatform().getName()))
1147-
addSketchesSubmenu(menu, name, folder.getParentFile(), false);
1148-
}
1126+
userLibs.sort();
1127+
for (Library lib : userLibs)
1128+
addSketchesSubmenu(menu, lib, false);
11491129
}
11501130
} catch (IOException e) {
11511131
e.printStackTrace();
@@ -1155,7 +1135,7 @@ public void rebuildExamplesMenu(JMenu menu) {
11551135
public LibraryList scanLibraries(List<File> folders) {
11561136
LibraryList res = new LibraryList();
11571137
for (File folder : folders)
1158-
res.addAll(scanLibraries(folder));
1138+
res.addOrReplaceAll(scanLibraries(folder));
11591139
return res;
11601140
}
11611141

@@ -1178,8 +1158,7 @@ public LibraryList scanLibraries(File folder) {
11781158
continue;
11791159
}
11801160

1181-
Library lib = Library.fromFolder(subfolder, Base.getTargetPlatform().getName());
1182-
1161+
Library lib = Library.create(subfolder);
11831162
// (also replace previously found libs with the same name)
11841163
if (lib != null)
11851164
res.addOrReplace(lib);
@@ -1206,18 +1185,20 @@ public void onBoardOrPortChange() {
12061185
// Libraries located in the latest folders on the list can override
12071186
// other libraries with the same name.
12081187
libraries = scanLibraries(librariesFolders);
1209-
1188+
String currentArch = Base.getTargetPlatform().getName();
1189+
libraries = libraries.filterByArchitecture(currentArch);
1190+
12101191
// Populate importToLibraryTable
12111192
importToLibraryTable = new HashMap<String, Library>();
12121193
for (Library lib : libraries) {
12131194
try {
1214-
String headers[] = headerListFromIncludePath(lib.getRootFolder());
1195+
String headers[] = headerListFromIncludePath(lib.getSrcFolder());
12151196
for (String header : headers) {
12161197
importToLibraryTable.put(header, lib);
12171198
}
12181199
} catch (IOException e) {
12191200
showWarning(_("Error"), I18n
1220-
.format("Unable to list header files in {0}", lib.getRootFolder()), e);
1201+
.format("Unable to list header files in {0}", lib.getSrcFolder()), e);
12211202
}
12221203
}
12231204

@@ -1495,6 +1476,13 @@ protected boolean addSketches(JMenu menu, File folder,
14951476
return ifound; // actually ignored, but..
14961477
}
14971478

1479+
private boolean addSketchesSubmenu(JMenu menu, Library lib,
1480+
boolean replaceExisting)
1481+
throws IOException {
1482+
return addSketchesSubmenu(menu, lib.getName(), lib.getFolder(),
1483+
replaceExisting);
1484+
}
1485+
14981486
private boolean addSketchesSubmenu(JMenu menu, String name, File folder,
14991487
final boolean replaceExisting) throws IOException {
15001488

@@ -1564,26 +1552,25 @@ public void actionPerformed(ActionEvent e) {
15641552
protected void addLibraries(JMenu menu, LibraryList libs) throws IOException {
15651553

15661554
LibraryList list = new LibraryList(libs);
1567-
Collections.sort(list, Library.CASE_INSENSITIVE_ORDER);
1568-
1569-
ActionListener listener = new ActionListener() {
1570-
public void actionPerformed(ActionEvent event) {
1571-
String jarPath = event.getActionCommand();
1572-
try {
1573-
activeEditor.getSketch().importLibrary(jarPath);
1574-
} catch (IOException e) {
1575-
showWarning(_("Error"), I18n.format("Unable to list header files in {0}", jarPath), e);
1576-
}
1577-
}
1578-
};
1555+
list.sort();
15791556

15801557
for (Library lib : list) {
1581-
File folder = lib.getRootFolder();
1582-
1558+
@SuppressWarnings("serial")
1559+
AbstractAction action = new AbstractAction(lib.getName()) {
1560+
public void actionPerformed(ActionEvent event) {
1561+
Library l = (Library) getValue("library");
1562+
try {
1563+
activeEditor.getSketch().importLibrary(l);
1564+
} catch (IOException e) {
1565+
showWarning(_("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e);
1566+
}
1567+
}
1568+
};
1569+
action.putValue("library", lib);
1570+
15831571
// Add new element at the bottom
1584-
JMenuItem item = new JMenuItem(lib.getName());
1585-
item.addActionListener(listener);
1586-
item.setActionCommand(folder.getAbsolutePath());
1572+
JMenuItem item = new JMenuItem(action);
1573+
item.putClientProperty("library", lib);
15871574
menu.add(item);
15881575

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

app/src/processing/app/Sketch.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ public class Sketch {
8989
/** Class path determined during build. */
9090
private String classPath;
9191

92-
/**
93-
* This is *not* the "Processing" libraries path, this is the Java libraries
94-
* path, as in java.library.path=BlahBlah, which identifies search paths for
95-
* DLLs or JNILIBs.
96-
*/
97-
private String libraryPath;
9892
/**
9993
* List of library folders.
10094
*/
@@ -1122,15 +1116,19 @@ public boolean addFile(File sourceFile) {
11221116
}
11231117

11241118

1119+
public void importLibrary(Library lib) throws IOException {
1120+
importLibrary(lib.getSrcFolder());
1121+
}
1122+
11251123
/**
11261124
* Add import statements to the current tab for all of packages inside
11271125
* the specified jar file.
11281126
*/
1129-
public void importLibrary(String jarPath) throws IOException {
1127+
public void importLibrary(File jarPath) throws IOException {
11301128
// make sure the user didn't hide the sketch folder
11311129
ensureExistence();
11321130

1133-
String list[] = Base.headerListFromIncludePath(new File(jarPath));
1131+
String list[] = Base.headerListFromIncludePath(jarPath);
11341132

11351133
// import statements into the main sketch file (code[0])
11361134
// if the current code is a .java file, insert into current
@@ -1424,17 +1422,13 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
14241422
// grab the imports from the code just preproc'd
14251423

14261424
importedLibraries = new LibraryList();
1427-
//Remember to clear library path before building it.
1428-
libraryPath = "";
14291425
for (String item : preprocessor.getExtraImports()) {
14301426

14311427
Library lib = Base.importToLibraryTable.get(item);
14321428
//If needed can Debug libraryPath here
14331429

14341430
if (lib != null && !importedLibraries.contains(lib)) {
14351431
importedLibraries.add(lib);
1436-
//classPath += Compiler.contentsToClassPath(libFolder);
1437-
libraryPath += File.pathSeparator + lib.getRootFolder().getAbsolutePath();
14381432
}
14391433
}
14401434

@@ -1889,11 +1883,6 @@ public String getClassPath() {
18891883
}
18901884

18911885

1892-
public String getLibraryPath() {
1893-
return libraryPath;
1894-
}
1895-
1896-
18971886
public SketchCode[] getCode() {
18981887
return code;
18991888
}

app/src/processing/app/debug/Compiler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public boolean compile(Sketch _sketch, String _buildPath,
8686
if (prefs.get("build.variant.path").length() != 0)
8787
includePaths.add(prefs.get("build.variant.path"));
8888
for (Library lib : sketch.getImportedLibraries())
89-
includePaths.add(lib.getRootFolder().getPath());
89+
includePaths.add(lib.getSrcFolder().getPath());
9090

9191
// 1. compile the sketch (already in the buildPath)
9292
sketch.setCompilingProgress(30);
@@ -582,11 +582,11 @@ void compileSketch(List<String> includePaths) throws RunnerException {
582582
void compileLibraries(List<String> includePaths) throws RunnerException {
583583
File outputPath = new File(prefs.get("build.path"));
584584
for (Library lib : sketch.getImportedLibraries()) {
585-
File libFolder = lib.getRootFolder();
586-
if (lib.isNewLib()) {
587-
recursiveCompileLibrary(outputPath, libFolder, includePaths);
588-
} else {
585+
File libFolder = lib.getSrcFolder();
586+
if (lib.isPre15Lib()) {
589587
compileLibrary(outputPath, libFolder, includePaths);
588+
} else {
589+
recursiveCompileLibrary(outputPath, libFolder, includePaths);
590590
}
591591
}
592592
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package processing.app.helpers;
2+
3+
public class StringMatchers {
4+
5+
/**
6+
* Tries to match <b>input</b> with <b>pattern</b>. The pattern can use the
7+
* "*" and "?" globs to match any-char-sequence and any-char respectively.
8+
*
9+
* @param input
10+
* The string to be checked
11+
* @param pattern
12+
* The pattern to match
13+
* @return <b>true</b> if the <b>input</b> matches the <b>pattern</b>,
14+
* <b>false</b> otherwise.
15+
*/
16+
public static boolean wildcardMatch(String input, String pattern) {
17+
String regex = pattern.replace("?", ".?").replace("*", ".*?");
18+
return input.matches(regex);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)