diff --git a/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java b/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java index 8be17c112..b123c760d 100644 --- a/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java +++ b/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java @@ -198,13 +198,12 @@ public class SketchBuilder extends IncrementalProjectBuilder{ IProject sketch = sketchProject.getProject(); if ( sketch == null || !sketch.isAccessible() ){ - System.out.println("Sketch is null!"); + ProcessingLog.logError("Sketch is inaccessible!", null); return null; } // Setup the folders codeFolder = sketch.getFolder("code"); -// dataFolder = sketch.getFolder("data"); buildFolder = sketch.getFolder("bin"); // TODO relocate to MyPlugin.getPlugin().getStateLocation().getFolder("bin") appletFolder = sketch.getFolder("applet"); @@ -415,34 +414,36 @@ public class SketchBuilder extends IncrementalProjectBuilder{ monitor.worked(10); if(checkCancel(monitor)) { return null; } +////////// LIBRARY STUFF MOVED TO SKETCH PROJECT + // Get the imports from the code that was preproc'd - importedLibraries = new ArrayList(); +// importedLibraries = new ArrayList(); - coreLibs = getCoreLibsFolder().getAbsoluteFile(); - sketchBookLibs = getSketchBookLibsFolder(sketch).getAbsoluteFile(); +// coreLibs = getCoreLibsFolder().getAbsoluteFile(); +// sketchBookLibs = getSketchBookLibsFolder(sketch).getAbsoluteFile(); // Clean the library table and rebuild it - importToLibraryTable = new HashMap(); +// importToLibraryTable = new HashMap(); // addLibraries internally checks for null folders - try{ - addLibraries(coreLibs); - addLibraries(sketchBookLibs); - } catch (IOException e){ - ProcessingLog.logError("Libraries could not be loaded.", e); - } - - for (String item : result.extraImports){ - // remove things up to the last dot - int dot = item.lastIndexOf('.'); - String entry = (dot == -1) ? item : item.substring(0, dot); - File libFolder = importToLibraryTable.get(entry); - if (libFolder != null ){ - importedLibraries.add(libFolder); - classPath += Utilities.contentsToClassPath(libFolder); - libraryPath += File.pathSeparator + libFolder.getAbsolutePath(); - } - } +// try{ +// addLibraries(coreLibs); +// addLibraries(sketchBookLibs); +// } catch (IOException e){ +// ProcessingLog.logError("Libraries could not be loaded.", e); +// } +// +// for (String item : result.extraImports){ +// // remove things up to the last dot +// int dot = item.lastIndexOf('.'); +// String entry = (dot == -1) ? item : item.substring(0, dot); +// File libFolder = importToLibraryTable.get(entry); +// if (libFolder != null ){ +// importedLibraries.add(libFolder); +// classPath += Utilities.contentsToClassPath(libFolder); +// libraryPath += File.pathSeparator + libFolder.getAbsolutePath(); +// } +// } // Finally add the regular Java CLASSPATH String javaClassPath = System.getProperty("java.class.path"); @@ -499,40 +500,40 @@ public class SketchBuilder extends IncrementalProjectBuilder{ if(checkCancel(monitor)) { return null; } //COMPILE - - // setup the VM - IPath containerPath = new Path(JavaRuntime.JRE_CONTAINER); - IVMInstall vm = JavaRuntime.getDefaultVMInstall(); - IPath vmPath = containerPath.append(vm.getVMInstallType().getId()).append(vm.getName()); - - // Collect all the paths in one place - ArrayList paths = new ArrayList(); - - // Split up the classPath, convert each member to a path - // and store them individually - System.out.println("classPath entries:"); - for( String s : classPath.split(File.pathSeparator)){ - if (!s.isEmpty()){ - System.out.println(s); - paths.add(new Path(s)); - } - } - - IClasspathEntry[] classpathEntries = new IClasspathEntry[paths.size()+1]; - - System.out.println("IClasspathEntry[] items:"); - for (int i = 0; i paths = new ArrayList(); +// +// // Split up the classPath, convert each member to a path +// // and store them individually +// System.out.println("classPath entries:"); +// for( String s : classPath.split(File.pathSeparator)){ +// if (!s.isEmpty()){ +// System.out.println(s); +// paths.add(new Path(s)); +// } +// } +// +// IClasspathEntry[] classpathEntries = new IClasspathEntry[paths.size()+1]; +// +// System.out.println("IClasspathEntry[] items:"); +// for (int i = 0; i"processing.plugin.core.processingnature" */ public static final String NATURE_ID = ProcessingCore.PLUGIN_ID + ".sketchNature"; - + /** The basic project entry being managed */ protected IProject project; @@ -128,7 +137,10 @@ public class SketchProject implements IProjectNature { IFolder dataFolder = project.getFolder("data"); IFolder buildFolder = project.getFolder("bin"); // TODO relocate to MyPlugin.getPlugin().getStateLocation().getFolder("bin") IFolder appletFolder = project.getFolder("applet"); - IFolder javaBuildFolder = buildFolder.getFolder("compile"); + IFolder javaBuildFolder = project.getFolder("compile"); + + File coreResources = getCoreResourcesFolder(); // includes core Libs + File sketchbookLibs = getSketchBookLibsFolder(project); if(!codeFolder.exists()) buildFolder.create(IResource.NONE, true, null); @@ -143,10 +155,33 @@ public class SketchProject implements IProjectNature { // Setup the Java project underlying the Sketch IJavaProject jproject = JavaCore.create(project); - - // Mark the output and resource folders - jproject.setOutputLocation(javaBuildFolder.getFullPath(), new NullProgressMonitor()); + // Get a default VM to toss in the mix + IPath containerPath = new Path(JavaRuntime.JRE_CONTAINER); + IVMInstall vm = JavaRuntime.getDefaultVMInstall(); + IPath vmPath = containerPath.append(vm.getVMInstallType().getId()).append(vm.getName()); + + // Setup dynamic classpath containers so we don't have to recalculate them and set them every time + List entries = new ArrayList(); + + entries.add(JavaCore.newContainerEntry(vmPath.makeAbsolute())); // JVM + entries.add(JavaCore.newSourceEntry(buildFolder.getFullPath().makeAbsolute())); // java source + entries.add(JavaCore.newContainerEntry(codeFolder.getFullPath().makeAbsolute())); // data source + if(coreResources != null) + entries.add(JavaCore.newContainerEntry(new Path(coreResources.getAbsolutePath()))); // core libs container + if(sketchbookLibs != null) + entries.add(JavaCore.newContainerEntry(new Path(sketchbookLibs.getAbsolutePath()))); // sketchbook libs container + + // casting doesn't work so we have to explicitly unpack the list. + IClasspathEntry[] classpathEntries = new IClasspathEntry[entries.size()]; + for(int i=0; i< entries.size(); i++ ){ + classpathEntries[i] = entries.get(i); + } + + // Combine all of these entries and set the raw classpath of the project. + // None of these should require further modification because they are dynamic + // Also provide an explicit output folder and a null progress monitor + jproject.setRawClasspath( classpathEntries, javaBuildFolder.getFullPath(), null); // Check the description to see if it already has the builder IProjectDescription description = this.project.getDescription(); @@ -239,4 +274,82 @@ public class SketchProject implements IProjectNature { project.build(IncrementalProjectBuilder.FULL_BUILD, monitor); } + + /** + * Finds the folder containing the Processing core libraries, which are bundled with the + * plugin. This folder doesn't exist in the workspace, so we return it as a File, not IFile. + * If something goes wrong, logs an error and returns null. + * + * @return File containing the core libraries folder or null + */ + public File getCoreLibsFolder() { + URL fileLocation = ProcessingCore.getProcessingCore().getPluginResource("libraries"); + try { + File folder = new File(FileLocator.toFileURL(fileLocation).getPath()); + if (folder.exists()) + return folder; + } catch (Exception e) { + ProcessingLog.logError(e); + } + return null; + } + + /** + * Resolves the plug-in resources folder to a File and returns it. This will include the + * Processing libraries and the core libraries folder. + * + * @return File reference to the core resources + */ + public File getCoreResourcesFolder(){ + URL fileLocation = ProcessingCore.getProcessingCore().getPluginResource(""); + try { + File folder = new File(FileLocator.toFileURL(fileLocation).getPath()); + if (folder.exists()) + return folder; + } catch (Exception e) { + ProcessingLog.logError(e); + } + return null; + } + + /** + * Find the folder containing the users libraries, which should be in the sketchbook. + * Looks in the user's preferences first, then look relative to the sketch location. + * + * @return File containing the Sketch book library folder, or null if it can't be located + */ + public File getSketchBookLibsFolder(IProject proj) { + IPath sketchbook = ProcessingCorePreferences.current().getSketchbookPath(); + if (sketchbook == null) + sketchbook = findSketchBookLibsFolder(proj); + if (sketchbook == null) + return null; + return new File(sketchbook.toOSString()); + } + + /** + * Tries to locate the sketchbook library folder relative to the project path + * based on the default sketch / sketchbook setup. If such a folder exists, loop + * through its contents until a valid library is found and then return the path + * to the sketchbook. If no valid libraries are found (empty folder, improper + * sketchbook setup), or if no valid folder is found, return null. + * + * @return IPath containing the location of the new library folder, or null + */ + public IPath findSketchBookLibsFolder(IProject proj) { + try{ + IPath guess = proj.getLocation().removeLastSegments(1).append("libraries"); + File folder = new File(guess.toOSString()); + if(folder.isDirectory()) + for( File file : folder.listFiles()){ + if(file.isDirectory()) + if (ProcessingCore.isLibrary(file)) + return guess; + } + } catch (Exception e){ + ProcessingLog.logError(e); + } + return null; + } + }