Skip to content

Commit b36eca9

Browse files
author
Jonathan Feinberg
committed
Tell PApplet where the proper sketch home is when running an unsaved, modified sketch.
Fixes jdf/Processing.py-Bugs#65, and has the excellent bonus effect of not copying enormous data files on modified, unsaved sketches.
1 parent 8e216d8 commit b36eca9

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

Base - Sketch Runner First.launch

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
1515
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
1616
<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;"/>
17-
<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;"/>
1817
<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;"/>
1918
<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;"/>
2019
<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;"/>
2120
<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;"/>
2221
<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;"/>
22+
<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;"/>
23+
<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;"/>
2324
</listAttribute>
2425
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
2526
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="processing.app.Base"/>

runtime/src/jycessing/PAppletJythonDriver.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,10 @@ private static String maybeMakeFriendlyMessage(final String message) {
236236
return message;
237237
}
238238

239-
public PAppletJythonDriver(final InteractiveConsole interp, final String sketchPath,
239+
public PAppletJythonDriver(final InteractiveConsole interp, final String pySketchPath,
240240
final String programText) {
241241
this.programText = programText;
242-
this.pySketchPath = sketchPath;
243-
this.sketchPath = new File(sketchPath).getParent();
242+
this.pySketchPath = pySketchPath;
244243
this.mode = ACTIVE_METHOD_DEF.matcher(programText).find() ? Mode.DRAW_LOOP : Mode.STATIC;
245244
Runner.log("Mode: ", mode.name());
246245
this.builtins = (PyStringMap)interp.getSystemState().getBuiltins();
@@ -960,10 +959,10 @@ public void mouseDragged(final MouseEvent e) {
960959
wrapMouseVariables();
961960
mouseDraggedFunc.invoke(e);
962961
}
963-
962+
964963
@Override
965-
public void mouseWheel(final MouseEvent e){
966-
if (mouseWheelMeth != null){
964+
public void mouseWheel(final MouseEvent e) {
965+
if (mouseWheelMeth != null) {
967966
wrapMouseVariables();
968967
mouseWheelMeth.__call__(Py.java2py(e));
969968
}

runtime/src/jycessing/mode/PyEditor.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import java.awt.event.WindowEvent;
1010
import java.io.File;
1111
import java.io.IOException;
12-
import java.nio.file.DirectoryIteratorException;
13-
import java.nio.file.DirectoryStream;
1412
import java.nio.file.Files;
1513
import java.nio.file.Path;
1614
import java.util.UUID;
@@ -228,27 +226,17 @@ public void handleExportApplication() {
228226
}
229227

230228
/**
231-
* Save the current state of the sketch into a temp dir, and return
229+
* Save the current state of the sketch code into a temp dir, and return
232230
* the created directory.
233231
* @return a new directory containing a saved version of the current
234-
* (presumably modified) sketch.
232+
* (presumably modified) sketch code.
235233
* @throws IOException
236234
*/
237235
private Path createTempSketch() throws IOException {
238236
final Path tmp = Files.createTempDirectory(sketch.getName());
239237
for (final SketchCode code : sketch.getCode()) {
240238
Files.write(tmp.resolve(code.getFileName()), code.getProgram().getBytes("utf-8"));
241239
}
242-
final Path sketchFolder = sketch.getFolder().toPath();
243-
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(sketchFolder)) {
244-
for (final Path entry : stream) {
245-
if (!mode.canEdit(entry.toFile())) {
246-
IOUtil.copy(entry, tmp);
247-
}
248-
}
249-
} catch (final DirectoryIteratorException ex) {
250-
throw ex.getCause();
251-
}
252240
return tmp;
253241
}
254242

@@ -280,6 +268,7 @@ private void runSketch(final RunMode mode) {
280268
new SketchInfo.Builder().sketchName(sketch.getName()).runMode(mode)
281269
.addLibraryDir(Base.getContentFile("modes/java/libraries"))
282270
.addLibraryDir(Base.getSketchbookLibrariesFolder())
271+
.sketchHome(sketch.getFolder().getAbsoluteFile())
283272
.mainSketchFile(new File(sketchPath).getAbsoluteFile())
284273
.code(sketch.getCode(0).getProgram()).codeFileNames(codeFileNames)
285274
.libraryPolicy(LibraryPolicy.SELECTIVE);

runtime/src/jycessing/mode/RunMode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public String[] args(final SketchInfo info) {
3535
abstract public String[] args(SketchInfo info);
3636

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

4141
}

runtime/src/jycessing/mode/run/SketchInfo.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public class SketchInfo implements Serializable {
1515
public final RunMode runMode;
1616
public final List<File> libraryDirs;
1717

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

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

2732
private SketchInfo(final String sketchName, final RunMode runMode, final List<File> libDirs,
28-
final File mainSketchFile, final String code, final String[] codeFileNames,
29-
final Point editorLoc, final Point sketchLoc, final LibraryPolicy libraryPolicy) {
33+
final File sketchHome, final File mainSketchFile, final String code,
34+
final String[] codeFileNames, final Point editorLoc, final Point sketchLoc,
35+
final LibraryPolicy libraryPolicy) {
3036
this.sketchName = sketchName;
3137
this.runMode = runMode;
3238
this.libraryDirs = Collections.unmodifiableList(libDirs);
39+
this.sketchHome = sketchHome;
3340
this.mainSketchFile = mainSketchFile;
3441
this.code = code;
3542
this.codeFileNames = codeFileNames;
@@ -42,16 +49,20 @@ public static class Builder {
4249
private String sketchName;
4350
private RunMode runMode;
4451
private final List<File> libDirs = new ArrayList<>();
45-
private File sketch;
52+
private File sketchHome;
53+
private File mainSketchFile;
4654
private String code;
4755
private String[] codeFileNames;
4856
private Point editorLoc;
4957
private Point sketchLoc;
5058
private LibraryPolicy libraryPolicy;
5159

5260
public SketchInfo build() {
53-
return new SketchInfo(sketchName, runMode, libDirs, sketch, code, codeFileNames, editorLoc,
54-
sketchLoc, libraryPolicy);
61+
if (sketchHome == null) {
62+
sketchHome = mainSketchFile.getParentFile();
63+
}
64+
return new SketchInfo(sketchName, runMode, libDirs, sketchHome, mainSketchFile, code,
65+
codeFileNames, editorLoc, sketchLoc, libraryPolicy);
5566
}
5667

5768
public Builder sketchName(final String sketchName) {
@@ -69,8 +80,13 @@ public Builder addLibraryDir(final File dir) {
6980
return this;
7081
}
7182

72-
public Builder mainSketchFile(final File sketch) {
73-
this.sketch = sketch;
83+
public Builder sketchHome(final File sketchHome) {
84+
this.sketchHome = sketchHome;
85+
return this;
86+
}
87+
88+
public Builder mainSketchFile(final File mainSketchFile) {
89+
this.mainSketchFile = mainSketchFile;
7490
return this;
7591
}
7692

0 commit comments

Comments
 (0)