diff --git a/java/src/processing/mode/java/Commander.java b/java/src/processing/mode/java/Commander.java index b13c1a5f1..d9618578f 100644 --- a/java/src/processing/mode/java/Commander.java +++ b/java/src/processing/mode/java/Commander.java @@ -32,6 +32,7 @@ import processing.app.Platform; import processing.app.Preferences; import processing.app.RunnerListener; +import processing.app.Settings; import processing.app.Sketch; import processing.utils.SketchException; import processing.app.Util; @@ -145,7 +146,24 @@ public Commander(String[] args) { if (!sketchFolder.exists()) { complainAndQuit(sketchFolder + " does not exist.", false); } - File pdeFile = new File(sketchFolder, sketchFolder.getName() + ".pde"); + + // Check for main file in sketch.properties first, then fall back to default + File pdeFile = null; + try { + Settings props = new Settings(new File(sketchFolder, "sketch.properties")); + String mainFileName = props.get("main"); + if (mainFileName != null) { + pdeFile = new File(sketchFolder, mainFileName); + } + } catch (IOException e) { + // sketch.properties doesn't exist or couldn't be read, will use default + } + + // Fall back to default naming convention if no custom main file specified + if (pdeFile == null || !pdeFile.exists()) { + pdeFile = new File(sketchFolder, sketchFolder.getName() + ".pde"); + } + if (!pdeFile.exists()) { complainAndQuit("Not a valid sketch folder. " + pdeFile + " does not exist.", true); } diff --git a/java/test/processing/mode/java/CommanderTest.java b/java/test/processing/mode/java/CommanderTest.java new file mode 100644 index 000000000..263f8c93b --- /dev/null +++ b/java/test/processing/mode/java/CommanderTest.java @@ -0,0 +1,89 @@ +package processing.mode.java; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import processing.app.Settings; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; + +import static org.junit.Assert.*; + + +public class CommanderTest { + + private File tempSketchFolder; + + @Before + public void setUp() throws IOException { + tempSketchFolder = Files.createTempDirectory("sketch_test").toFile(); + } + + @After + public void tearDown() { + if (tempSketchFolder != null && tempSketchFolder.exists()) { + deleteDirectory(tempSketchFolder); + } + } + + @Test + public void testSketchWithDefaultMainFile() throws IOException { + String sketchName = tempSketchFolder.getName(); + File mainFile = new File(tempSketchFolder, sketchName + ".pde"); + + try (FileWriter writer = new FileWriter(mainFile)) { + writer.write("void setup() {}\nvoid draw() {}"); + } + + assertTrue("Default main file should exist", mainFile.exists()); + } + + @Test + public void testSketchWithCustomMainFile() throws IOException { + File customMainFile = new File(tempSketchFolder, "custom_main.pde"); + try (FileWriter writer = new FileWriter(customMainFile)) { + writer.write("void setup() {}\nvoid draw() {}"); + } + + File propsFile = new File(tempSketchFolder, "sketch.properties"); + Settings props = new Settings(propsFile); + props.set("main", "custom_main.pde"); + props.save(); + + assertTrue("Custom main file should exist", customMainFile.exists()); + assertTrue("sketch.properties should exist", propsFile.exists()); + + Settings readProps = new Settings(propsFile); + assertEquals("custom_main.pde", readProps.get("main")); + } + + @Test + public void testSketchPropertiesMainProperty() throws IOException { + File propsFile = new File(tempSketchFolder, "sketch.properties"); + Settings props = new Settings(propsFile); + props.set("main", "my_sketch.pde"); + props.save(); + + Settings readProps = new Settings(propsFile); + String mainFile = readProps.get("main"); + + assertEquals("Main property should match", "my_sketch.pde", mainFile); + } + + private void deleteDirectory(File directory) { + File[] files = directory.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isDirectory()) { + deleteDirectory(file); + } else { + file.delete(); + } + } + } + directory.delete(); + } +}