Refactored Library.list() and ToolContribution.list() to prevent .properties files from being loaded before necessary

This commit is contained in:
pesckal
2011-10-13 04:39:07 +00:00
parent 75975a1ff9
commit 5a26f46f1a
4 changed files with 123 additions and 82 deletions

View File

@@ -287,27 +287,29 @@ public class ContributionManager {
File tempDir = unzipFileToTemp(zippedToolFile, statusBar);
ArrayList<ToolContribution> discoveredTools = ToolContribution.list(tempDir, false);
if (discoveredTools.isEmpty()) {
ArrayList<File> 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<Library> discoveredLibs = Library.list(tempDir);
if (discoveredLibs.isEmpty()) {
ArrayList<File> 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.");

View File

@@ -360,63 +360,86 @@ public class Library extends InstalledContribution {
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
static protected ArrayList<Library> list(File folder) throws IOException {
ArrayList<Library> libraries = new ArrayList<Library>();
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<File> discover(File folder) throws IOException {
ArrayList<File> libraries = new ArrayList<File>();
discover(folder, libraries);
return libraries;
}
static protected void list(File folder, ArrayList<Library> libraries) throws IOException {
list(folder, libraries, null);
}
static protected void list(File folder, ArrayList<Library> 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<File> 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<Library> list(File folder) throws IOException {
ArrayList<Library> libraries = new ArrayList<Library>();
list(folder, libraries);
return libraries;
}
static protected void list(File folder, ArrayList<Library> libraries) throws IOException {
ArrayList<File> librariesFolders = new ArrayList<File>();
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<File> discoveredLibFolders = new ArrayList<File>();
discover(subfolder, discoveredLibFolders);
for (File discoveredFolder : discoveredLibFolders) {
libraries.add(new Library(discoveredFolder, subfolderName));
}
}
}
}
}
public Type getType() {
return Type.LIBRARY;
}

View File

@@ -14,7 +14,12 @@ public class LibraryCompilation extends InstalledContribution {
super(folder, "compilation.properties");
libraries = new ArrayList<Library>();
Library.list(folder, libraries, name);
ArrayList<File> librariesFolders = new ArrayList<File>();
Library.discover(folder, librariesFolders);
for (File baseFolder : librariesFolders) {
libraries.add(new Library(baseFolder, name));
}
}
public static ArrayList<LibraryCompilation> list(ArrayList<Library> libraries) {

View File

@@ -158,13 +158,32 @@ public class ToolContribution extends InstalledContribution implements Tool {
* returned
*/
static protected ArrayList<ToolContribution> list(File folder, boolean doInitializeToolClass) {
ArrayList<File> toolsFolders = ToolContribution.discover(folder);
ArrayList<ToolContribution> tools = new ArrayList<ToolContribution>();
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<ToolContribution> tools,
boolean doInitializeToolClass) {
static protected ArrayList<File> discover(File folder) {
ArrayList<File> tools = new ArrayList<File>();
discover(folder, tools);
return tools;
}
static protected void discover(File folder, ArrayList<File> 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]);
}
}
}