Skip to content

Commit fa85cd4

Browse files
authored
fix(cli): support sketches with custom main file names (#1329)
* 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 * test: add CLI test for custom main file support Added testSketchWithCustomMainFile() to CLITest.kt as requested by maintainer. This test provides a placeholder for manual testing of sketches with custom main files specified in sketch.properties. Follows the same pattern as existing CLI tests (testLSP, testLegacyCLI) and is intended to be run manually in IntelliJ IDEA. * test: convert to automated CLI test with temp directory Converted testSketchWithCustomMainFile() from manual to automated test. Now creates a temporary sketch folder with custom main file and sketch.properties, then tests the CLI build command. Follows the pattern from SchemaTest.kt using Files.createTempDirectory() and automatic cleanup.
1 parent 0a113f7 commit fa85cd4

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

app/test/processing/app/CLITest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package processing.app
22

33
import java.io.File
4+
import java.nio.file.Files
45
import kotlin.test.Test
56

67
/*
@@ -25,6 +26,36 @@ class CLITest {
2526
runCLIWithArguments("cli --help")
2627
}
2728

29+
@Test
30+
fun testSketchWithCustomMainFile(){
31+
val tempDir = Files.createTempDirectory("cli_custom_main_test")
32+
try {
33+
val sketchFolder = tempDir.resolve("TestSketch").toFile()
34+
sketchFolder.mkdirs()
35+
36+
// Create custom main file (not matching folder name)
37+
val customMain = File(sketchFolder, "custom_main.pde")
38+
customMain.writeText("""
39+
void setup() {
40+
println("Custom main file test");
41+
}
42+
43+
void draw() {
44+
exit();
45+
}
46+
""".trimIndent())
47+
48+
// Create sketch.properties with custom main
49+
val propsFile = File(sketchFolder, "sketch.properties")
50+
propsFile.writeText("main=custom_main.pde")
51+
52+
// Test with CLI
53+
runCLIWithArguments("cli --sketch=${sketchFolder.absolutePath} --build")
54+
} finally {
55+
tempDir.toFile().deleteRecursively()
56+
}
57+
}
58+
2859
/*
2960
This function runs the CLI with the given arguments.
3061
*/

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)