Skip to content

Commit 9fc00a3

Browse files
committed
Refactored Library.list() and ToolContribution.list() to prevent .properties files from being loaded before necessary
1 parent 5bb26f3 commit 9fc00a3

4 files changed

Lines changed: 122 additions & 81 deletions

File tree

app/src/processing/app/ContributionManager.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -287,27 +287,29 @@ static public ToolContribution installTool(Editor editor,
287287

288288
File tempDir = unzipFileToTemp(zippedToolFile, statusBar);
289289

290-
ArrayList<ToolContribution> discoveredTools = ToolContribution.list(tempDir, false);
291-
if (discoveredTools.isEmpty()) {
290+
ArrayList<File> toolFolders = ToolContribution.discover(tempDir);
291+
if (toolFolders.isEmpty()) {
292292
// Sometimes tool authors place all their folders in the base
293293
// directory of a zip file instead of in single folder as the
294294
// guidelines suggest. If this is the case, we might be able to find the
295295
// library by stepping up a directory and searching for libraries again.
296-
discoveredTools = ToolContribution.list(tempDir.getParentFile(), false);
296+
toolFolders = ToolContribution.discover(tempDir.getParentFile());
297297
}
298298

299-
if (discoveredTools != null && discoveredTools.size() == 1) {
300-
ToolContribution discoveredTool = discoveredTools.get(0);
301-
File propFile = new File(discoveredTool.getFolder(), "tool.properties");
299+
if (toolFolders != null && toolFolders.size() == 1) {
300+
File toolFolder = toolFolders.get(0);
301+
final ToolContribution tool = ToolContribution.getTool(toolFolder);
302+
303+
File propFile = new File(tool.getFolder(), "tool.properties");
302304

303305
if (ad == null || writePropertiesFile(propFile, ad)) {
304-
return installTool(editor, discoveredTool, statusBar);
306+
return installTool(editor, tool, statusBar);
305307
} else {
306308
statusBar.setErrorMessage(ERROR_OVERWRITING_PROPERTIES_MESSAGE);
307309
}
308310
} else {
309311
// Diagnose the problem and notify the user
310-
if (discoveredTools == null || discoveredTools.isEmpty()) {
312+
if (toolFolders == null || toolFolders.isEmpty()) {
311313
statusBar.setErrorMessage(DISCOVERY_INTERNAL_ERROR_MESSAGE);
312314
} else {
313315
statusBar.setErrorMessage("There were multiple tools in the file, so we're ignoring it.");
@@ -402,29 +404,30 @@ static public Library installLibrary(Editor editor, File libFile,
402404
File tempDir = unzipFileToTemp(libFile, statusBar);
403405

404406
try {
405-
ArrayList<Library> discoveredLibs = Library.list(tempDir);
406-
if (discoveredLibs.isEmpty()) {
407+
ArrayList<File> libfolders = Library.discover(tempDir);
408+
if (libfolders.isEmpty()) {
407409
// Sometimes library authors place all their folders in the base
408410
// directory of a zip file instead of in single folder as the
409411
// guidelines suggest. If this is the case, we might be able to find the
410412
// library by stepping up a directory and searching for libraries again.
411-
discoveredLibs = Library.list(tempDir.getParentFile());
413+
libfolders = Library.discover(tempDir.getParentFile());
412414
}
413415

414-
if (discoveredLibs != null && discoveredLibs.size() == 1) {
415-
Library discoveredLib = discoveredLibs.get(0);
416-
File propFile = new File(discoveredLib.getFolder(), "library.properties");
416+
if (libfolders != null && libfolders.size() == 1) {
417+
File libfolder = libfolders.get(0);
418+
File propFile = new File(libfolder, "library.properties");
417419

418420
if (ad == null || writePropertiesFile(propFile, ad)) {
419-
return installLibrary(editor, discoveredLib, confirmReplace, statusBar);
421+
Library newlib = new Library(libfolder, null);
422+
return installLibrary(editor, newlib, confirmReplace, statusBar);
420423
} else {
421424
statusBar.setErrorMessage(ERROR_OVERWRITING_PROPERTIES_MESSAGE);
422425
}
423426
} else {
424427
// Diagnose the problem and notify the user
425-
if (discoveredLibs == null) {
428+
if (libfolders == null) {
426429
statusBar.setErrorMessage(ContributionManager.DISCOVERY_INTERNAL_ERROR_MESSAGE);
427-
} else if (discoveredLibs.isEmpty()) {
430+
} else if (libfolders.isEmpty()) {
428431
statusBar.setErrorMessage(ContributionManager.DISCOVERY_NONE_FOUND_ERROR_MESSAGE);
429432
} else {
430433
statusBar.setErrorMessage("There were multiple libraries in the file, so we're ignoring it.");

app/src/processing/app/Library.java

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -360,63 +360,86 @@ static public boolean hasMultipleArch(int platform, ArrayList<Library> libraries
360360
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
361361

362362

363+
static protected FilenameFilter junkFolderFilter = new FilenameFilter() {
364+
public boolean accept(File dir, String name) {
365+
// skip .DS_Store files, .svn folders, etc
366+
if (name.charAt(0) == '.') return false;
367+
if (name.equals("CVS")) return false;
368+
return (new File(dir, name).isDirectory());
369+
}
370+
};
371+
372+
static protected ArrayList<File> discover(File folder) throws IOException {
373+
ArrayList<File> libraries = new ArrayList<File>();
374+
discover(folder, libraries);
375+
return libraries;
376+
}
377+
378+
static protected void discover(File folder, ArrayList<File> libraries) throws IOException {
379+
String[] list = folder.list(junkFolderFilter);
380+
381+
// if a bad folder or something like that, this might come back null
382+
if (list != null) {
383+
// alphabetize list, since it's not always alpha order
384+
// replaced hella slow bubble sort with this feller for 0093
385+
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
386+
387+
for (String potentialName : list) {
388+
File baseFolder = new File(folder, potentialName);
389+
File libraryFolder = new File(baseFolder, "library");
390+
File libraryJar = new File(libraryFolder, potentialName + ".jar");
391+
// If a .jar file of the same prefix as the folder exists
392+
// inside the 'library' subfolder of the sketch
393+
if (libraryJar.exists()) {
394+
String sanityCheck = Sketch.sanitizeName(potentialName);
395+
if (sanityCheck.equals(potentialName)) {
396+
libraries.add(baseFolder);
397+
398+
} else {
399+
String mess = "The library \""
400+
+ potentialName
401+
+ "\" cannot be used.\n"
402+
+ "Library names must contain only basic letters and numbers.\n"
403+
+ "(ASCII only and no spaces, and it cannot start with a number)";
404+
Base.showMessage("Ignoring bad library name", mess);
405+
continue;
406+
}
407+
}
408+
}
409+
}
410+
}
411+
363412
static protected ArrayList<Library> list(File folder) throws IOException {
364413
ArrayList<Library> libraries = new ArrayList<Library>();
365414
list(folder, libraries);
366415
return libraries;
367416
}
368417

369-
370418
static protected void list(File folder, ArrayList<Library> libraries) throws IOException {
371-
list(folder, libraries, null);
372-
}
373-
374-
375-
static protected void list(File folder, ArrayList<Library> libraries, String subfolder) throws IOException {
376-
if (folder.isDirectory()) {
377-
String[] list = folder.list(new FilenameFilter() {
378-
public boolean accept(File dir, String name) {
379-
// skip .DS_Store files, .svn folders, etc
380-
if (name.charAt(0) == '.') return false;
381-
if (name.equals("CVS")) return false;
382-
return (new File(dir, name).isDirectory());
383-
}
384-
});
385-
// if a bad folder or something like that, this might come back null
386-
if (list != null) {
387-
// alphabetize list, since it's not always alpha order
388-
// replaced hella slow bubble sort with this feller for 0093
389-
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
390-
391-
for (String potentialName : list) {
392-
File baseFolder = new File(folder, potentialName);
393-
File libraryFolder = new File(baseFolder, "library");
394-
File libraryJar = new File(libraryFolder, potentialName + ".jar");
395-
// If a .jar file of the same prefix as the folder exists
396-
// inside the 'library' subfolder of the sketch
397-
if (libraryJar.exists()) {
398-
String sanityCheck = Sketch.sanitizeName(potentialName);
399-
if (sanityCheck.equals(potentialName)) {
400-
libraries.add(new Library(baseFolder, subfolder));
401-
402-
} else {
403-
String mess =
404-
"The library \"" + potentialName + "\" cannot be used.\n" +
405-
"Library names must contain only basic letters and numbers.\n" +
406-
"(ASCII only and no spaces, and it cannot start with a number)";
407-
Base.showMessage("Ignoring bad library name", mess);
408-
continue;
409-
}
410-
} else if (subfolder == null) { // no library jar, maybe a subfolder?
411-
// Add the recursive library folders back for toxi
412-
// http://code.google.com/p/processing/issues/detail?id=578
413-
list(new File(folder, potentialName), libraries, potentialName);
419+
ArrayList<File> librariesFolders = new ArrayList<File>();
420+
discover(folder, librariesFolders);
421+
422+
for (File baseFolder : librariesFolders) {
423+
libraries.add(new Library(baseFolder, null));
424+
}
425+
426+
String[] list = folder.list(junkFolderFilter);
427+
if (list != null) {
428+
for (String subfolderName : list) {
429+
File subfolder = new File(folder, subfolderName);
430+
431+
if (!libraries.contains(subfolder)) {
432+
ArrayList<File> discoveredLibFolders = new ArrayList<File>();
433+
discover(subfolder, discoveredLibFolders);
434+
435+
for (File discoveredFolder : discoveredLibFolders) {
436+
libraries.add(new Library(discoveredFolder, subfolderName));
414437
}
415438
}
416439
}
417440
}
418441
}
419-
442+
420443
public Type getType() {
421444
return Type.LIBRARY;
422445
}

app/src/processing/app/LibraryCompilation.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ private LibraryCompilation(File folder) throws IOException {
1414
super(folder, "compilation.properties");
1515

1616
libraries = new ArrayList<Library>();
17-
Library.list(folder, libraries, name);
17+
ArrayList<File> librariesFolders = new ArrayList<File>();
18+
Library.discover(folder, librariesFolders);
19+
20+
for (File baseFolder : librariesFolders) {
21+
libraries.add(new Library(baseFolder, name));
22+
}
1823
}
1924

2025
public static ArrayList<LibraryCompilation> list(ArrayList<Library> libraries) {

app/src/processing/app/ToolContribution.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,32 @@ static protected String findClassInZipFile(String base, File file) {
158158
* returned
159159
*/
160160
static protected ArrayList<ToolContribution> list(File folder, boolean doInitializeToolClass) {
161+
ArrayList<File> toolsFolders = ToolContribution.discover(folder);
162+
161163
ArrayList<ToolContribution> tools = new ArrayList<ToolContribution>();
162-
list(folder, tools, doInitializeToolClass);
164+
for (File toolFolder : toolsFolders) {
165+
final ToolContribution tool = ToolContribution.getTool(toolFolder);
166+
if (tool != null) {
167+
try {
168+
if (doInitializeToolClass)
169+
tool.initializeToolClass();
170+
171+
tools.add(tool);
172+
} catch (Exception e) {
173+
e.printStackTrace();
174+
}
175+
}
176+
}
163177
return tools;
164178
}
165179

166-
static protected void list(File folder, ArrayList<ToolContribution> tools,
167-
boolean doInitializeToolClass) {
180+
static protected ArrayList<File> discover(File folder) {
181+
ArrayList<File> tools = new ArrayList<File>();
182+
discover(folder, tools);
183+
return tools;
184+
}
185+
186+
static protected void discover(File folder, ArrayList<File> toolFolders) {
168187

169188
File[] folders = folder.listFiles(new FileFilter() {
170189
public boolean accept(File folder) {
@@ -187,21 +206,12 @@ public boolean accept(File folder) {
187206
// }
188207
});
189208

190-
if (folders == null || folders.length == 0) {
191-
return;
192-
}
193-
194-
for (int i = 0; i < folders.length; i++) {
195-
try {
196-
final ToolContribution tool = getTool(folders[i]);
197-
if (tool != null) {
198-
if (doInitializeToolClass) {
199-
tool.initializeToolClass();
200-
}
201-
tools.add(tool);
202-
}
203-
} catch (Exception e) {
204-
e.printStackTrace();
209+
if (folders != null) {
210+
for (int i = 0; i < folders.length; i++) {
211+
Tool tool = ToolContribution.getTool(folders[i]);
212+
213+
if (tool != null)
214+
toolFolders.add(folders[i]);
205215
}
206216
}
207217
}

0 commit comments

Comments
 (0)