From b36eca95ac3106c5b3066371fa527108b3a3ed1c Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Thu, 19 Jun 2014 23:43:05 -0400 Subject: [PATCH] Tell PApplet where the proper sketch home is when running an unsaved, modified sketch. Fixes https://github.com/jdf/Processing.py-Bugs/issues/65, and has the excellent bonus effect of not copying enormous data files on modified, unsaved sketches. --- Base - Sketch Runner First.launch | 3 +- .../src/jycessing/PAppletJythonDriver.java | 11 ++++--- runtime/src/jycessing/mode/PyEditor.java | 17 ++--------- runtime/src/jycessing/mode/RunMode.java | 2 +- .../src/jycessing/mode/run/SketchInfo.java | 30 ++++++++++++++----- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/Base - Sketch Runner First.launch b/Base - Sketch Runner First.launch index d9fe15c7..3a1e1d48 100644 --- a/Base - Sketch Runner First.launch +++ b/Base - Sketch Runner First.launch @@ -14,12 +14,13 @@ - + + diff --git a/runtime/src/jycessing/PAppletJythonDriver.java b/runtime/src/jycessing/PAppletJythonDriver.java index 03dee1e8..f233ce42 100644 --- a/runtime/src/jycessing/PAppletJythonDriver.java +++ b/runtime/src/jycessing/PAppletJythonDriver.java @@ -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(); @@ -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)); } diff --git a/runtime/src/jycessing/mode/PyEditor.java b/runtime/src/jycessing/mode/PyEditor.java index b6faa89d..59218eab 100644 --- a/runtime/src/jycessing/mode/PyEditor.java +++ b/runtime/src/jycessing/mode/PyEditor.java @@ -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; @@ -228,10 +226,10 @@ 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 { @@ -239,16 +237,6 @@ private Path createTempSketch() throws IOException { 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 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; } @@ -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); diff --git a/runtime/src/jycessing/mode/RunMode.java b/runtime/src/jycessing/mode/RunMode.java index b6ac74cf..7f62f8a0 100644 --- a/runtime/src/jycessing/mode/RunMode.java +++ b/runtime/src/jycessing/mode/RunMode.java @@ -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(); } } diff --git a/runtime/src/jycessing/mode/run/SketchInfo.java b/runtime/src/jycessing/mode/run/SketchInfo.java index 8d568a53..7e56aeb6 100644 --- a/runtime/src/jycessing/mode/run/SketchInfo.java +++ b/runtime/src/jycessing/mode/run/SketchInfo.java @@ -15,6 +15,11 @@ public class SketchInfo implements Serializable { public final RunMode runMode; public final List 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; @@ -25,11 +30,13 @@ public class SketchInfo implements Serializable { public final LibraryPolicy libraryPolicy; private SketchInfo(final String sketchName, final RunMode runMode, final List 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; @@ -42,7 +49,8 @@ public static class Builder { private String sketchName; private RunMode runMode; private final List libDirs = new ArrayList<>(); - private File sketch; + private File sketchHome; + private File mainSketchFile; private String code; private String[] codeFileNames; private Point editorLoc; @@ -50,8 +58,11 @@ public static class Builder { 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) { @@ -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; }