FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(cli): support sketches with custom main file names by avinxshKD · Pull Request #1329 · processing/processing4 · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (2) .kt  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 31 additions & 0 deletions app/test/processing/app/CLITest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package processing.app

import java.io.File
import java.nio.file.Files
import kotlin.test.Test

/*
Expand All @@ -25,6 +26,36 @@ class CLITest {
runCLIWithArguments("cli --help")
}

@Test
fun testSketchWithCustomMainFile(){
val tempDir = Files.createTempDirectory("cli_custom_main_test")
try {
val sketchFolder = tempDir.resolve("TestSketch").toFile()
sketchFolder.mkdirs()

// Create custom main file (not matching folder name)
val customMain = File(sketchFolder, "custom_main.pde")
customMain.writeText("""
void setup() {
println("Custom main file test");
}

void draw() {
exit();
}
""".trimIndent())

// Create sketch.properties with custom main
val propsFile = File(sketchFolder, "sketch.properties")
propsFile.writeText("main=custom_main.pde")

// Test with CLI
runCLIWithArguments("cli --sketch=${sketchFolder.absolutePath} --build")
} finally {
tempDir.toFile().deleteRecursively()
}
}

/*
This function runs the CLI with the given arguments.
*/
Expand Down
20 changes: 19 additions & 1 deletion java/src/processing/mode/java/Commander.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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();
}
}

Back | FazBrowse Home | New Git URL