Skip to content

Commit 8a4ea97

Browse files
committed
fix(cli): support sketches with custom main file names
Previously, the CLI only accepted sketches where the main .pde file matched the sketch folder name (e.g., sketch/sketch.pde). This caused issues when users renamed their main file in the IDE, which stores the custom filename in sketch.properties. Now the CLI checks sketch.properties for a 'main' property before falling back to the default naming convention, matching the IDE's behavior implemented in Sketch.findMain(). Fixes #1219
1 parent 11f1a1e commit 8a4ea97

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

java/src/processing/mode/java/Commander.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import processing.app.Platform;
3333
import processing.app.Preferences;
3434
import processing.app.RunnerListener;
35+
import processing.app.Settings;
3536
import processing.app.Sketch;
3637
import processing.utils.SketchException;
3738
import processing.app.Util;
@@ -145,7 +146,24 @@ public Commander(String[] args) {
145146
if (!sketchFolder.exists()) {
146147
complainAndQuit(sketchFolder + " does not exist.", false);
147148
}
148-
File pdeFile = new File(sketchFolder, sketchFolder.getName() + ".pde");
149+
150+
// Check for main file in sketch.properties first, then fall back to default
151+
File pdeFile = null;
152+
try {
153+
Settings props = new Settings(new File(sketchFolder, "sketch.properties"));
154+
String mainFileName = props.get("main");
155+
if (mainFileName != null) {
156+
pdeFile = new File(sketchFolder, mainFileName);
157+
}
158+
} catch (IOException e) {
159+
// sketch.properties doesn't exist or couldn't be read, will use default
160+
}
161+
162+
// Fall back to default naming convention if no custom main file specified
163+
if (pdeFile == null || !pdeFile.exists()) {
164+
pdeFile = new File(sketchFolder, sketchFolder.getName() + ".pde");
165+
}
166+
149167
if (!pdeFile.exists()) {
150168
complainAndQuit("Not a valid sketch folder. " + pdeFile + " does not exist.", true);
151169
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package processing.mode.java;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import processing.app.Settings;
7+
8+
import java.io.File;
9+
import java.io.FileWriter;
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
13+
import static org.junit.Assert.*;
14+
15+
16+
public class CommanderTest {
17+
18+
private File tempSketchFolder;
19+
20+
@Before
21+
public void setUp() throws IOException {
22+
tempSketchFolder = Files.createTempDirectory("sketch_test").toFile();
23+
}
24+
25+
@After
26+
public void tearDown() {
27+
if (tempSketchFolder != null && tempSketchFolder.exists()) {
28+
deleteDirectory(tempSketchFolder);
29+
}
30+
}
31+
32+
@Test
33+
public void testSketchWithDefaultMainFile() throws IOException {
34+
String sketchName = tempSketchFolder.getName();
35+
File mainFile = new File(tempSketchFolder, sketchName + ".pde");
36+
37+
try (FileWriter writer = new FileWriter(mainFile)) {
38+
writer.write("void setup() {}\nvoid draw() {}");
39+
}
40+
41+
assertTrue("Default main file should exist", mainFile.exists());
42+
}
43+
44+
@Test
45+
public void testSketchWithCustomMainFile() throws IOException {
46+
File customMainFile = new File(tempSketchFolder, "custom_main.pde");
47+
try (FileWriter writer = new FileWriter(customMainFile)) {
48+
writer.write("void setup() {}\nvoid draw() {}");
49+
}
50+
51+
File propsFile = new File(tempSketchFolder, "sketch.properties");
52+
Settings props = new Settings(propsFile);
53+
props.set("main", "custom_main.pde");
54+
props.save();
55+
56+
assertTrue("Custom main file should exist", customMainFile.exists());
57+
assertTrue("sketch.properties should exist", propsFile.exists());
58+
59+
Settings readProps = new Settings(propsFile);
60+
assertEquals("custom_main.pde", readProps.get("main"));
61+
}
62+
63+
@Test
64+
public void testSketchPropertiesMainProperty() throws IOException {
65+
File propsFile = new File(tempSketchFolder, "sketch.properties");
66+
Settings props = new Settings(propsFile);
67+
props.set("main", "my_sketch.pde");
68+
props.save();
69+
70+
Settings readProps = new Settings(propsFile);
71+
String mainFile = readProps.get("main");
72+
73+
assertEquals("Main property should match", "my_sketch.pde", mainFile);
74+
}
75+
76+
private void deleteDirectory(File directory) {
77+
File[] files = directory.listFiles();
78+
if (files != null) {
79+
for (File file : files) {
80+
if (file.isDirectory()) {
81+
deleteDirectory(file);
82+
} else {
83+
file.delete();
84+
}
85+
}
86+
}
87+
directory.delete();
88+
}
89+
}

0 commit comments

Comments
 (0)