Skip to content

Commit

Permalink
Tell PApplet where the proper sketch home is when running an unsaved,…
Browse files Browse the repository at this point in the history
… modified sketch.

Fixes jdf/Processing.py-Bugs#65, and has the excellent bonus
effect of not copying enormous data files on modified, unsaved sketches.
  • Loading branch information
Jonathan Feinberg committed Jun 20, 2014
1 parent 8e216d8 commit b36eca9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Base - Sketch Runner First.launch
Expand Up @@ -14,12 +14,13 @@
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER&quot; javaProject=&quot;processing-app&quot; path=&quot;1&quot; type=&quot;4&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry path=&quot;3&quot; projectName=&quot;processing.py&quot; type=&quot;1&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry path=&quot;3&quot; projectName=&quot;processing-app&quot; type=&quot;1&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry internalArchive=&quot;/processing-app/lib/ant.jar&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry internalArchive=&quot;/processing-app/lib/ant-launcher.jar&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry path=&quot;3&quot; projectName=&quot;processing-core&quot; type=&quot;1&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry id=&quot;org.eclipse.jdt.launching.classpathentry.defaultClasspath&quot;&gt;&#10;&lt;memento exportedEntriesOnly=&quot;false&quot; project=&quot;processing-app&quot;/&gt;&#10;&lt;/runtimeClasspathEntry&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry path=&quot;3&quot; projectName=&quot;processing.py&quot; type=&quot;1&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry internalArchive=&quot;/processing.py/buildtime/lib/java/guava-17.0.jar&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#10;"/>
</listAttribute>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="processing.app.Base"/>
Expand Down
11 changes: 5 additions & 6 deletions runtime/src/jycessing/PAppletJythonDriver.java
Expand Up @@ -236,11 +236,10 @@ private static String maybeMakeFriendlyMessage(final String message) {
return message;
}

public PAppletJythonDriver(final InteractiveConsole interp, final String sketchPath,
public PAppletJythonDriver(final InteractiveConsole interp, final String pySketchPath,
final String programText) {
this.programText = programText;
this.pySketchPath = sketchPath;
this.sketchPath = new File(sketchPath).getParent();
this.pySketchPath = pySketchPath;
this.mode = ACTIVE_METHOD_DEF.matcher(programText).find() ? Mode.DRAW_LOOP : Mode.STATIC;
Runner.log("Mode: ", mode.name());
this.builtins = (PyStringMap)interp.getSystemState().getBuiltins();
Expand Down Expand Up @@ -960,10 +959,10 @@ public void mouseDragged(final MouseEvent e) {
wrapMouseVariables();
mouseDraggedFunc.invoke(e);
}

@Override
public void mouseWheel(final MouseEvent e){
if (mouseWheelMeth != null){
public void mouseWheel(final MouseEvent e) {
if (mouseWheelMeth != null) {
wrapMouseVariables();
mouseWheelMeth.__call__(Py.java2py(e));
}
Expand Down
17 changes: 3 additions & 14 deletions runtime/src/jycessing/mode/PyEditor.java
Expand Up @@ -9,8 +9,6 @@
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
Expand Down Expand Up @@ -228,27 +226,17 @@ public void handleExportApplication() {
}

/**
* Save the current state of the sketch into a temp dir, and return
* Save the current state of the sketch code into a temp dir, and return
* the created directory.
* @return a new directory containing a saved version of the current
* (presumably modified) sketch.
* (presumably modified) sketch code.
* @throws IOException
*/
private Path createTempSketch() throws IOException {
final Path tmp = Files.createTempDirectory(sketch.getName());
for (final SketchCode code : sketch.getCode()) {
Files.write(tmp.resolve(code.getFileName()), code.getProgram().getBytes("utf-8"));
}
final Path sketchFolder = sketch.getFolder().toPath();
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(sketchFolder)) {
for (final Path entry : stream) {
if (!mode.canEdit(entry.toFile())) {
IOUtil.copy(entry, tmp);
}
}
} catch (final DirectoryIteratorException ex) {
throw ex.getCause();
}
return tmp;
}

Expand Down Expand Up @@ -280,6 +268,7 @@ private void runSketch(final RunMode mode) {
new SketchInfo.Builder().sketchName(sketch.getName()).runMode(mode)
.addLibraryDir(Base.getContentFile("modes/java/libraries"))
.addLibraryDir(Base.getSketchbookLibrariesFolder())
.sketchHome(sketch.getFolder().getAbsoluteFile())
.mainSketchFile(new File(sketchPath).getAbsoluteFile())
.code(sketch.getCode(0).getProgram()).codeFileNames(codeFileNames)
.libraryPolicy(LibraryPolicy.SELECTIVE);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/jycessing/mode/RunMode.java
Expand Up @@ -35,7 +35,7 @@ public String[] args(final SketchInfo info) {
abstract public String[] args(SketchInfo info);

private static String pathArg(final SketchInfo info) {
return PApplet.ARGS_SKETCH_FOLDER + "=" + info.mainSketchFile.getParent();
return PApplet.ARGS_SKETCH_FOLDER + "=" + info.sketchHome.getAbsolutePath();
}

}
30 changes: 23 additions & 7 deletions runtime/src/jycessing/mode/run/SketchInfo.java
Expand Up @@ -15,6 +15,11 @@ public class SketchInfo implements Serializable {
public final RunMode runMode;
public final List<File> libraryDirs;

// This may be different from the directory containing mainSketchFile,
// because the sketch may have been temporarily copied into a temp
// folder to make unsaved changes to source available.
public final File sketchHome;

public final File mainSketchFile;

public final String sketchName;
Expand All @@ -25,11 +30,13 @@ public class SketchInfo implements Serializable {
public final LibraryPolicy libraryPolicy;

private SketchInfo(final String sketchName, final RunMode runMode, final List<File> libDirs,
final File mainSketchFile, final String code, final String[] codeFileNames,
final Point editorLoc, final Point sketchLoc, final LibraryPolicy libraryPolicy) {
final File sketchHome, final File mainSketchFile, final String code,
final String[] codeFileNames, final Point editorLoc, final Point sketchLoc,
final LibraryPolicy libraryPolicy) {
this.sketchName = sketchName;
this.runMode = runMode;
this.libraryDirs = Collections.unmodifiableList(libDirs);
this.sketchHome = sketchHome;
this.mainSketchFile = mainSketchFile;
this.code = code;
this.codeFileNames = codeFileNames;
Expand All @@ -42,16 +49,20 @@ public static class Builder {
private String sketchName;
private RunMode runMode;
private final List<File> libDirs = new ArrayList<>();
private File sketch;
private File sketchHome;
private File mainSketchFile;
private String code;
private String[] codeFileNames;
private Point editorLoc;
private Point sketchLoc;
private LibraryPolicy libraryPolicy;

public SketchInfo build() {
return new SketchInfo(sketchName, runMode, libDirs, sketch, code, codeFileNames, editorLoc,
sketchLoc, libraryPolicy);
if (sketchHome == null) {
sketchHome = mainSketchFile.getParentFile();
}
return new SketchInfo(sketchName, runMode, libDirs, sketchHome, mainSketchFile, code,
codeFileNames, editorLoc, sketchLoc, libraryPolicy);
}

public Builder sketchName(final String sketchName) {
Expand All @@ -69,8 +80,13 @@ public Builder addLibraryDir(final File dir) {
return this;
}

public Builder mainSketchFile(final File sketch) {
this.sketch = sketch;
public Builder sketchHome(final File sketchHome) {
this.sketchHome = sketchHome;
return this;
}

public Builder mainSketchFile(final File mainSketchFile) {
this.mainSketchFile = mainSketchFile;
return this;
}

Expand Down

0 comments on commit b36eca9

Please sign in to comment.