Skip to content

Commit f8deaa5

Browse files
committed
Factoring Library class
1 parent 5beea81 commit f8deaa5

File tree

6 files changed

+184
-92
lines changed

6 files changed

+184
-92
lines changed

app/src/processing/app/Base.java

Lines changed: 54 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import processing.app.helpers.PreferencesMap;
3838
import processing.app.helpers.filefilters.OnlyDirs;
3939
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
40-
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;import processing.app.tools.MapWithSubkeys;
40+
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;import processing.app.packages.Library;
41+
import processing.app.packages.LibraryList;
42+
import processing.app.tools.MapWithSubkeys;
4143
import processing.app.tools.ZipDeflater;
4244
import processing.core.*;
4345
import static processing.app.I18n._;
@@ -89,10 +91,10 @@ public class Base {
8991
static private List<File> librariesFolders;
9092

9193
// maps library name to their library folder
92-
static private Map<String, File> libraries;
94+
static private LibraryList libraries;
9395

9496
// maps #included files to their library folder
95-
static Map<String, File> importToLibraryTable;
97+
static Map<String, Library> importToLibraryTable;
9698

9799
// classpath for all known libraries for p5
98100
// (both those in the p5/libs folder and those with lib subfolders
@@ -1038,26 +1040,26 @@ protected void rebuildSketchbookMenu(JMenu menu) {
10381040
}
10391041
}
10401042

1041-
public Map<String, File> getIDELibs() {
1043+
public LibraryList getIDELibs() {
1044+
LibraryList libs = new LibraryList();
10421045
if (libraries == null)
1043-
return new HashMap<String, File>();
1044-
Map<String, File> ideLibs = new HashMap<String, File>(libraries);
1045-
for (String lib : libraries.keySet()) {
1046-
if (FileUtils.isSubDirectory(getSketchbookFolder(), libraries.get(lib)))
1047-
ideLibs.remove(lib);
1046+
return libs;
1047+
for (Library lib : libraries) {
1048+
if (!FileUtils.isSubDirectory(getSketchbookFolder(), lib.getRootFolder()))
1049+
libs.add(lib);
10481050
}
1049-
return ideLibs;
1051+
return libs;
10501052
}
10511053

1052-
public Map<String, File> getUserLibs() {
1054+
public LibraryList getUserLibs() {
1055+
LibraryList libs = new LibraryList();
10531056
if (libraries == null)
1054-
return new HashMap<String, File>();
1055-
Map<String, File> userLibs = new HashMap<String, File>(libraries);
1056-
for (String lib : libraries.keySet()) {
1057-
if (!FileUtils.isSubDirectory(getSketchbookFolder(), libraries.get(lib)))
1058-
userLibs.remove(lib);
1057+
return libs;
1058+
for (Library lib : libraries) {
1059+
if (FileUtils.isSubDirectory(getSketchbookFolder(), lib.getRootFolder()))
1060+
libs.add(lib);
10591061
}
1060-
return userLibs;
1062+
return libs;
10611063
}
10621064

10631065
public void rebuildImportMenu(JMenu importMenu, final Editor editor) {
@@ -1077,8 +1079,8 @@ public void actionPerformed(ActionEvent e) {
10771079
// Split between user supplied libraries and IDE libraries
10781080
TargetPlatform targetPlatform = getTargetPlatform();
10791081
if (targetPlatform != null) {
1080-
Map<String, File> ideLibs = getIDELibs();
1081-
Map<String, File> userLibs = getUserLibs();
1082+
LibraryList ideLibs = getIDELibs();
1083+
LibraryList userLibs = getUserLibs();
10821084
try {
10831085
// Find the current target. Get the platform, and then select the
10841086
// correct name and core path.
@@ -1118,24 +1120,24 @@ public void rebuildExamplesMenu(JMenu menu) {
11181120
if (found) menu.addSeparator();
11191121

11201122
// Add examples from libraries
1121-
Map<String, File> ideLibs = getIDELibs();
1122-
List<String> names = new ArrayList<String>(ideLibs.keySet());
1123-
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
1124-
for (String name : names) {
1125-
File folder = ideLibs.get(name);
1123+
LibraryList ideLibs = getIDELibs();
1124+
Collections.sort(ideLibs, Library.CASE_INSENSITIVE_ORDER);
1125+
for (Library lib : ideLibs) {
1126+
File folder = lib.getRootFolder();
1127+
String name = lib.getName();
11261128
addSketchesSubmenu(menu, name, folder, false);
11271129
// Allows "fat" libraries to have examples in the root folder
11281130
if (folder.getName().equals(Base.getTargetPlatform().getName()))
11291131
addSketchesSubmenu(menu, name, folder.getParentFile(), false);
11301132
}
11311133

1132-
Map<String, File> userLibs = getUserLibs();
1134+
LibraryList userLibs = getUserLibs();
11331135
if (userLibs.size()>0) {
11341136
menu.addSeparator();
1135-
names = new ArrayList<String>(userLibs.keySet());
1136-
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
1137-
for (String name : names) {
1138-
File folder = userLibs.get(name);
1137+
Collections.sort(userLibs, Library.CASE_INSENSITIVE_ORDER);
1138+
for (Library lib : userLibs) {
1139+
File folder = lib.getRootFolder();
1140+
String name = lib.getName();
11391141
addSketchesSubmenu(menu, name, folder, false);
11401142
// Allows "fat" libraries to have examples in the root folder
11411143
if (folder.getName().equals(Base.getTargetPlatform().getName()))
@@ -1147,15 +1149,16 @@ public void rebuildExamplesMenu(JMenu menu) {
11471149
}
11481150
}
11491151

1150-
public Map<String, File> scanLibraries(List<File> folders) {
1151-
Map<String, File> res = new HashMap<String, File>();
1152+
public LibraryList scanLibraries(List<File> folders) {
1153+
LibraryList res = new LibraryList();
11521154
for (File folder : folders)
1153-
res.putAll(scanLibraries(folder));
1155+
res.addAll(scanLibraries(folder));
11541156
return res;
11551157
}
11561158

1157-
public Map<String, File> scanLibraries(File folder) {
1158-
Map<String, File> res = new HashMap<String, File>();
1159+
public LibraryList scanLibraries(File folder) {
1160+
LibraryList res = new LibraryList();
1161+
11591162
String list[] = folder.list(new OnlyDirs());
11601163
// if a bad folder or something like that, this might come back null
11611164
if (list == null)
@@ -1172,41 +1175,15 @@ public Map<String, File> scanLibraries(File folder) {
11721175
continue;
11731176
}
11741177

1175-
subfolder = scanFatLibrary(subfolder);
1178+
Library lib = Library.fromFolder(subfolder, Base.getTargetPlatform().getName());
11761179

11771180
// (also replace previously found libs with the same name)
1178-
if (subfolder != null)
1179-
res.put(libName, subfolder);
1181+
if (lib != null)
1182+
res.addOrReplace(lib);
11801183
}
11811184
return res;
11821185
}
11831186

1184-
/**
1185-
* Scans inside a "FAT" (multi-platform) library folder to see if it contains
1186-
* a version suitable for the actual selected architecture. If a suitable
1187-
* version is found the folder containing that version is returned, otherwise
1188-
* <b>null</b> is returned.<br />
1189-
* <br />
1190-
* If a non-"FAT" library is detected, we assume that the library is suitable
1191-
* for the current architecture and the libFolder parameter is returned.<br />
1192-
*
1193-
* @param libFolder
1194-
* @return
1195-
*/
1196-
public File scanFatLibrary(File libFolder) {
1197-
// A library is considered "fat" if it contains a file called
1198-
// "library.properties"
1199-
File libraryPropFile = new File(libFolder, "library.properties");
1200-
if (!libraryPropFile.exists() || !libraryPropFile.isFile())
1201-
return libFolder;
1202-
1203-
// Search for a subfolder for actual architecture, return null if not found
1204-
File archSubfolder = new File(libFolder, Base.getTargetPlatform().getName());
1205-
if (!archSubfolder.exists() || !archSubfolder.isDirectory())
1206-
return null;
1207-
return archSubfolder;
1208-
}
1209-
12101187
public void onBoardOrPortChange() {
12111188
TargetPlatform targetPlatform = getTargetPlatform();
12121189
if (targetPlatform == null)
@@ -1228,15 +1205,16 @@ public void onBoardOrPortChange() {
12281205
libraries = scanLibraries(librariesFolders);
12291206

12301207
// Populate importToLibraryTable
1231-
importToLibraryTable = new HashMap<String, File>();
1232-
for (File subfolder : libraries.values()) {
1208+
importToLibraryTable = new HashMap<String, Library>();
1209+
for (Library lib : libraries) {
12331210
try {
1234-
String packages[] = headerListFromIncludePath(subfolder);
1235-
for (String pkg : packages) {
1236-
importToLibraryTable.put(pkg, subfolder);
1211+
String headers[] = headerListFromIncludePath(lib.getRootFolder());
1212+
for (String header : headers) {
1213+
importToLibraryTable.put(header, lib);
12371214
}
12381215
} catch (IOException e) {
1239-
showWarning(_("Error"), I18n.format("Unable to list header files in {0}", subfolder), e);
1216+
showWarning(_("Error"), I18n
1217+
.format("Unable to list header files in {0}", lib.getRootFolder()), e);
12401218
}
12411219
}
12421220

@@ -1580,10 +1558,10 @@ public void actionPerformed(ActionEvent e) {
15801558
return found;
15811559
}
15821560

1583-
protected void addLibraries(JMenu menu, Map<String, File> libs) throws IOException {
1561+
protected void addLibraries(JMenu menu, LibraryList libs) throws IOException {
15841562

1585-
List<String> list = new ArrayList<String>(libs.keySet());
1586-
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
1563+
LibraryList list = new LibraryList(libs);
1564+
Collections.sort(list, Library.CASE_INSENSITIVE_ORDER);
15871565

15881566
ActionListener listener = new ActionListener() {
15891567
public void actionPerformed(ActionEvent event) {
@@ -1596,11 +1574,11 @@ public void actionPerformed(ActionEvent event) {
15961574
}
15971575
};
15981576

1599-
for (String name : list) {
1600-
File folder = libs.get(name);
1577+
for (Library lib : list) {
1578+
File folder = lib.getRootFolder();
16011579

16021580
// Add new element at the bottom
1603-
JMenuItem item = new JMenuItem(name);
1581+
JMenuItem item = new JMenuItem(lib.getName());
16041582
item.addActionListener(listener);
16051583
item.setActionCommand(folder.getAbsolutePath());
16061584
menu.add(item);
@@ -1874,7 +1852,7 @@ static public File createTempFolder(String name) {
18741852
}
18751853

18761854

1877-
static public Map<String, File> getLibraries() {
1855+
static public LibraryList getLibraries() {
18781856
return libraries;
18791857
}
18801858

app/src/processing/app/Sketch.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import processing.app.debug.Sizer;
3030
import processing.app.debug.Uploader;
3131
import processing.app.helpers.PreferencesMap;
32+
import processing.app.packages.Library;
33+
import processing.app.packages.LibraryList;
3234
import processing.app.preproc.*;
3335
import processing.core.*;
3436
import static processing.app.I18n._;
@@ -96,7 +98,7 @@ public class Sketch {
9698
/**
9799
* List of library folders.
98100
*/
99-
private ArrayList<File> importedLibraries;
101+
private LibraryList importedLibraries;
100102

101103
/**
102104
* path is location of the main .pde file, because this is also
@@ -1421,18 +1423,18 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
14211423

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

1424-
importedLibraries = new ArrayList<File>();
1426+
importedLibraries = new LibraryList();
14251427
//Remember to clear library path before building it.
14261428
libraryPath = "";
14271429
for (String item : preprocessor.getExtraImports()) {
14281430

1429-
File libFolder = (File) Base.importToLibraryTable.get(item);
1430-
//If needed can Debug libraryPath here
1431+
Library lib = Base.importToLibraryTable.get(item);
1432+
//If needed can Debug libraryPath here
14311433

1432-
if (libFolder != null && !importedLibraries.contains(libFolder)) {
1433-
importedLibraries.add(libFolder);
1434+
if (lib != null && !importedLibraries.contains(lib)) {
1435+
importedLibraries.add(lib);
14341436
//classPath += Compiler.contentsToClassPath(libFolder);
1435-
libraryPath += File.pathSeparator + libFolder.getAbsolutePath();
1437+
libraryPath += File.pathSeparator + lib.getRootFolder().getAbsolutePath();
14361438
}
14371439
}
14381440

@@ -1462,7 +1464,7 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
14621464
}
14631465

14641466

1465-
public ArrayList<File> getImportedLibraries() {
1467+
public LibraryList getImportedLibraries() {
14661468
return importedLibraries;
14671469
}
14681470

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import processing.app.helpers.PreferencesMap;
4242
import processing.app.helpers.StringReplacer;
4343
import processing.app.helpers.filefilters.OnlyDirs;
44+
import processing.app.packages.Library;
4445
import processing.core.PApplet;
4546

4647
public class Compiler implements MessageConsumer {
@@ -84,8 +85,8 @@ public boolean compile(Sketch _sketch, String _buildPath,
8485
includePaths.add(prefs.get("build.core.path"));
8586
if (prefs.get("build.variant.path").length() != 0)
8687
includePaths.add(prefs.get("build.variant.path"));
87-
for (File file : sketch.getImportedLibraries())
88-
includePaths.add(file.getPath());
88+
for (Library lib : sketch.getImportedLibraries())
89+
includePaths.add(lib.getRootFolder().getPath());
8990

9091
// 1. compile the sketch (already in the buildPath)
9192
sketch.setCompilingProgress(30);
@@ -580,11 +581,12 @@ void compileSketch(List<String> includePaths) throws RunnerException {
580581
// <buildPath>/<library>/
581582
void compileLibraries(List<String> includePaths) throws RunnerException {
582583
File outputPath = new File(prefs.get("build.path"));
583-
for (File libraryFolder : sketch.getImportedLibraries()) {
584-
if (new File(libraryFolder.getParentFile(), "library.properties").exists()) {
585-
recursiveCompileLibrary(outputPath, libraryFolder, includePaths);
584+
for (Library lib : sketch.getImportedLibraries()) {
585+
File libFolder = lib.getRootFolder();
586+
if (lib.isNewLib()) {
587+
recursiveCompileLibrary(outputPath, libFolder, includePaths);
586588
} else {
587-
compileLibrary(outputPath, libraryFolder, includePaths);
589+
compileLibrary(outputPath, libFolder, includePaths);
588590
}
589591
}
590592
}

0 commit comments

Comments
 (0)