From 5a26f46f1a3418a270145b6ebd92d18c19ff1e14 Mon Sep 17 00:00:00 2001 From: pesckal Date: Thu, 13 Oct 2011 04:39:07 +0000 Subject: [PATCH] Refactored Library.list() and ToolContribution.list() to prevent .properties files from being loaded before necessary --- .../processing/app/ContributionManager.java | 37 +++--- app/src/processing/app/Library.java | 115 +++++++++++------- .../processing/app/LibraryCompilation.java | 7 +- app/src/processing/app/ToolContribution.java | 46 ++++--- 4 files changed, 123 insertions(+), 82 deletions(-) diff --git a/app/src/processing/app/ContributionManager.java b/app/src/processing/app/ContributionManager.java index 7e0062eb3..24effee5b 100644 --- a/app/src/processing/app/ContributionManager.java +++ b/app/src/processing/app/ContributionManager.java @@ -287,27 +287,29 @@ public class ContributionManager { File tempDir = unzipFileToTemp(zippedToolFile, statusBar); - ArrayList discoveredTools = ToolContribution.list(tempDir, false); - if (discoveredTools.isEmpty()) { + ArrayList toolFolders = ToolContribution.discover(tempDir); + if (toolFolders.isEmpty()) { // Sometimes tool authors place all their folders in the base // directory of a zip file instead of in single folder as the // guidelines suggest. If this is the case, we might be able to find the // library by stepping up a directory and searching for libraries again. - discoveredTools = ToolContribution.list(tempDir.getParentFile(), false); + toolFolders = ToolContribution.discover(tempDir.getParentFile()); } - if (discoveredTools != null && discoveredTools.size() == 1) { - ToolContribution discoveredTool = discoveredTools.get(0); - File propFile = new File(discoveredTool.getFolder(), "tool.properties"); + if (toolFolders != null && toolFolders.size() == 1) { + File toolFolder = toolFolders.get(0); + final ToolContribution tool = ToolContribution.getTool(toolFolder); + + File propFile = new File(tool.getFolder(), "tool.properties"); if (ad == null || writePropertiesFile(propFile, ad)) { - return installTool(editor, discoveredTool, statusBar); + return installTool(editor, tool, statusBar); } else { statusBar.setErrorMessage(ERROR_OVERWRITING_PROPERTIES_MESSAGE); } } else { // Diagnose the problem and notify the user - if (discoveredTools == null || discoveredTools.isEmpty()) { + if (toolFolders == null || toolFolders.isEmpty()) { statusBar.setErrorMessage(DISCOVERY_INTERNAL_ERROR_MESSAGE); } else { statusBar.setErrorMessage("There were multiple tools in the file, so we're ignoring it."); @@ -402,29 +404,30 @@ public class ContributionManager { File tempDir = unzipFileToTemp(libFile, statusBar); try { - ArrayList discoveredLibs = Library.list(tempDir); - if (discoveredLibs.isEmpty()) { + ArrayList libfolders = Library.discover(tempDir); + if (libfolders.isEmpty()) { // Sometimes library authors place all their folders in the base // directory of a zip file instead of in single folder as the // guidelines suggest. If this is the case, we might be able to find the // library by stepping up a directory and searching for libraries again. - discoveredLibs = Library.list(tempDir.getParentFile()); + libfolders = Library.discover(tempDir.getParentFile()); } - if (discoveredLibs != null && discoveredLibs.size() == 1) { - Library discoveredLib = discoveredLibs.get(0); - File propFile = new File(discoveredLib.getFolder(), "library.properties"); + if (libfolders != null && libfolders.size() == 1) { + File libfolder = libfolders.get(0); + File propFile = new File(libfolder, "library.properties"); if (ad == null || writePropertiesFile(propFile, ad)) { - return installLibrary(editor, discoveredLib, confirmReplace, statusBar); + Library newlib = new Library(libfolder, null); + return installLibrary(editor, newlib, confirmReplace, statusBar); } else { statusBar.setErrorMessage(ERROR_OVERWRITING_PROPERTIES_MESSAGE); } } else { // Diagnose the problem and notify the user - if (discoveredLibs == null) { + if (libfolders == null) { statusBar.setErrorMessage(ContributionManager.DISCOVERY_INTERNAL_ERROR_MESSAGE); - } else if (discoveredLibs.isEmpty()) { + } else if (libfolders.isEmpty()) { statusBar.setErrorMessage(ContributionManager.DISCOVERY_NONE_FOUND_ERROR_MESSAGE); } else { statusBar.setErrorMessage("There were multiple libraries in the file, so we're ignoring it."); diff --git a/app/src/processing/app/Library.java b/app/src/processing/app/Library.java index cf3c6feb9..7bf61614c 100644 --- a/app/src/processing/app/Library.java +++ b/app/src/processing/app/Library.java @@ -360,63 +360,86 @@ public class Library extends InstalledContribution { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - static protected ArrayList list(File folder) throws IOException { - ArrayList libraries = new ArrayList(); - list(folder, libraries); + static protected FilenameFilter junkFolderFilter = new FilenameFilter() { + public boolean accept(File dir, String name) { + // skip .DS_Store files, .svn folders, etc + if (name.charAt(0) == '.') return false; + if (name.equals("CVS")) return false; + return (new File(dir, name).isDirectory()); + } + }; + + static protected ArrayList discover(File folder) throws IOException { + ArrayList libraries = new ArrayList(); + discover(folder, libraries); return libraries; } - - - static protected void list(File folder, ArrayList libraries) throws IOException { - list(folder, libraries, null); - } - - static protected void list(File folder, ArrayList libraries, String subfolder) throws IOException { - if (folder.isDirectory()) { - String[] list = folder.list(new FilenameFilter() { - public boolean accept(File dir, String name) { - // skip .DS_Store files, .svn folders, etc - if (name.charAt(0) == '.') return false; - if (name.equals("CVS")) return false; - return (new File(dir, name).isDirectory()); - } - }); - // if a bad folder or something like that, this might come back null - if (list != null) { - // 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); + static protected void discover(File folder, ArrayList libraries) throws IOException { + String[] list = folder.list(junkFolderFilter); - for (String potentialName : list) { - File baseFolder = new File(folder, potentialName); - File libraryFolder = new File(baseFolder, "library"); - File libraryJar = new File(libraryFolder, potentialName + ".jar"); - // If a .jar file of the same prefix as the folder exists - // inside the 'library' subfolder of the sketch - if (libraryJar.exists()) { - String sanityCheck = Sketch.sanitizeName(potentialName); - if (sanityCheck.equals(potentialName)) { - libraries.add(new Library(baseFolder, subfolder)); + // if a bad folder or something like that, this might come back null + if (list != null) { + // 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); - } else { - String mess = - "The library \"" + potentialName + "\" 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)"; - Base.showMessage("Ignoring bad library name", mess); - continue; - } - } else if (subfolder == null) { // no library jar, maybe a subfolder? - // Add the recursive library folders back for toxi - // http://code.google.com/p/processing/issues/detail?id=578 - list(new File(folder, potentialName), libraries, potentialName); + for (String potentialName : list) { + File baseFolder = new File(folder, potentialName); + File libraryFolder = new File(baseFolder, "library"); + File libraryJar = new File(libraryFolder, potentialName + ".jar"); + // If a .jar file of the same prefix as the folder exists + // inside the 'library' subfolder of the sketch + if (libraryJar.exists()) { + String sanityCheck = Sketch.sanitizeName(potentialName); + if (sanityCheck.equals(potentialName)) { + libraries.add(baseFolder); + + } else { + String mess = "The library \"" + + potentialName + + "\" 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)"; + Base.showMessage("Ignoring bad library name", mess); + continue; } } } } } + static protected ArrayList list(File folder) throws IOException { + ArrayList libraries = new ArrayList(); + list(folder, libraries); + return libraries; + } + + static protected void list(File folder, ArrayList libraries) throws IOException { + ArrayList librariesFolders = new ArrayList(); + discover(folder, librariesFolders); + + for (File baseFolder : librariesFolders) { + libraries.add(new Library(baseFolder, null)); + } + + String[] list = folder.list(junkFolderFilter); + if (list != null) { + for (String subfolderName : list) { + File subfolder = new File(folder, subfolderName); + + if (!libraries.contains(subfolder)) { + ArrayList discoveredLibFolders = new ArrayList(); + discover(subfolder, discoveredLibFolders); + + for (File discoveredFolder : discoveredLibFolders) { + libraries.add(new Library(discoveredFolder, subfolderName)); + } + } + } + } + } + public Type getType() { return Type.LIBRARY; } diff --git a/app/src/processing/app/LibraryCompilation.java b/app/src/processing/app/LibraryCompilation.java index 08f202ace..3af7964f2 100644 --- a/app/src/processing/app/LibraryCompilation.java +++ b/app/src/processing/app/LibraryCompilation.java @@ -14,7 +14,12 @@ public class LibraryCompilation extends InstalledContribution { super(folder, "compilation.properties"); libraries = new ArrayList(); - Library.list(folder, libraries, name); + ArrayList librariesFolders = new ArrayList(); + Library.discover(folder, librariesFolders); + + for (File baseFolder : librariesFolders) { + libraries.add(new Library(baseFolder, name)); + } } public static ArrayList list(ArrayList libraries) { diff --git a/app/src/processing/app/ToolContribution.java b/app/src/processing/app/ToolContribution.java index e8cd881a5..a5eecdbf8 100644 --- a/app/src/processing/app/ToolContribution.java +++ b/app/src/processing/app/ToolContribution.java @@ -158,13 +158,32 @@ public class ToolContribution extends InstalledContribution implements Tool { * returned */ static protected ArrayList list(File folder, boolean doInitializeToolClass) { + ArrayList toolsFolders = ToolContribution.discover(folder); + ArrayList tools = new ArrayList(); - list(folder, tools, doInitializeToolClass); + for (File toolFolder : toolsFolders) { + final ToolContribution tool = ToolContribution.getTool(toolFolder); + if (tool != null) { + try { + if (doInitializeToolClass) + tool.initializeToolClass(); + + tools.add(tool); + } catch (Exception e) { + e.printStackTrace(); + } + } + } return tools; } - static protected void list(File folder, ArrayList tools, - boolean doInitializeToolClass) { + static protected ArrayList discover(File folder) { + ArrayList tools = new ArrayList(); + discover(folder, tools); + return tools; + } + + static protected void discover(File folder, ArrayList toolFolders) { File[] folders = folder.listFiles(new FileFilter() { public boolean accept(File folder) { @@ -187,21 +206,12 @@ public class ToolContribution extends InstalledContribution implements Tool { // } }); - if (folders == null || folders.length == 0) { - return; - } - - for (int i = 0; i < folders.length; i++) { - try { - final ToolContribution tool = getTool(folders[i]); - if (tool != null) { - if (doInitializeToolClass) { - tool.initializeToolClass(); - } - tools.add(tool); - } - } catch (Exception e) { - e.printStackTrace(); + if (folders != null) { + for (int i = 0; i < folders.length; i++) { + Tool tool = ToolContribution.getTool(folders[i]); + + if (tool != null) + toolFolders.add(folders[i]); } } }