Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion java/src/processing/mode/java/Commander.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
89 changes: 89 additions & 0 deletions java/test/processing/mode/java/CommanderTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}