Skip to content

Setup a Command Line Interface #1050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sourceSets{

compose.desktop {
application {
mainClass = "processing.app.ui.Start"
mainClass = "processing.app.ProcessingKt"

jvmArgs(*listOf(
Pair("processing.version", rootProject.version),
Expand Down Expand Up @@ -97,6 +97,7 @@ compose.desktop {

dependencies {
implementation(project(":core"))
runtimeOnly(project(":java"))

implementation(libs.flatlaf)

Expand All @@ -121,6 +122,8 @@ dependencies {
testImplementation(libs.mockitoKotlin)
testImplementation(libs.junitJupiter)
testImplementation(libs.junitJupiterParams)

implementation(libs.clikt)
}

tasks.test {
Expand Down
90 changes: 90 additions & 0 deletions app/src/processing/app/Processing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package processing.app

import com.github.ajalt.clikt.command.SuspendingCliktCommand
import com.github.ajalt.clikt.command.main
import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.help
import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.help
import com.github.ajalt.clikt.parameters.options.option
import processing.app.ui.Start

class Processing: SuspendingCliktCommand("processing"){
val version by option("-v","--version")
.flag()
.help("Print version information")

val sketches by argument()
.multiple(default = emptyList())
.help("Sketches to open")

override fun help(context: Context) = "Start the Processing IDE"
override val invokeWithoutSubcommand = true
override suspend fun run() {
if(version){
println("processing-${Base.getVersionName()}-${Base.getRevision()}")
return
}

val subcommand = currentContext.invokedSubcommand
if (subcommand == null) {
Start.main(sketches.toTypedArray())
}
}
}

suspend fun main(args: Array<String>){
Processing()
.subcommands(
LSP(),
LegacyCLI(args)
)
.main(args)
}

class LSP: SuspendingCliktCommand("lsp"){
override fun help(context: Context) = "Start the Processing Language Server"
override suspend fun run(){
try {
// Indirect invocation since app does not depend on java mode
Class.forName("processing.mode.java.lsp.PdeLanguageServer")
.getMethod("main", Array<String>::class.java)
.invoke(null, *arrayOf<Any>(emptyList<String>()))
} catch (e: Exception) {
throw InternalError("Failed to invoke main method", e)
}
}
}

class LegacyCLI(val args: Array<String>): SuspendingCliktCommand( "cli"){
override fun help(context: Context) = "Legacy processing-java command line interface"

val help by option("--help").flag()
val build by option("--build").flag()
val run by option("--run").flag()
val present by option("--present").flag()
val sketch: String? by option("--sketch")
val force by option("--force").flag()
val output: String? by option("--output")
val export by option("--export").flag()
val noJava by option("--no-java").flag()
val variant: String? by option("--variant")

override suspend fun run(){
val cliArgs = args.filter { it != "cli" }
try {
if(build){
System.setProperty("java.awt.headless", "true")
}
// Indirect invocation since app does not depend on java mode
Class.forName("processing.mode.java.Commander")
.getMethod("main", Array<String>::class.java)
.invoke(null, *arrayOf<Any>(cliArgs.toTypedArray()))
} catch (e: Exception) {
throw InternalError("Failed to invoke main method", e)
}
}
}
59 changes: 35 additions & 24 deletions app/src/processing/app/tools/InstallCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,41 @@ public void run() {
PrintWriter writer = PApplet.createWriter(file);
writer.print("#!/bin/sh\n\n");

writer.print("# Prevents processing-java from stealing focus, see:\n" +
"# https://github.com/processing/processing/issues/3996.\n" +
"OPTION_FOR_HEADLESS_RUN=\"\"\n" +
"for ARG in \"$@\"\n" +
"do\n" +
" if [ \"$ARG\" = \"--build\" ]; then\n" +
" OPTION_FOR_HEADLESS_RUN=\"-Djava.awt.headless=true\"\n" +
" fi\n" +
"done\n\n");

String javaRoot = Platform.getContentFile(".").getCanonicalPath();

StringList jarList = new StringList();
addJarList(jarList, new File(javaRoot));
addJarList(jarList, new File(javaRoot, "core/library"));
addJarList(jarList, new File(javaRoot, "modes/java/mode"));
String classPath = jarList.join(":").replaceAll(javaRoot + "\\/?", "");

writer.println("cd \"" + javaRoot + "\" && " +
Platform.getJavaPath().replaceAll(" ", "\\\\ ") +
" -Djna.nosys=true" +
" $OPTION_FOR_HEADLESS_RUN" +
" -cp \"" + classPath + "\"" +
" processing.mode.java.Commander \"$@\"");
var resourcesDir = System.getProperty("compose.application.resources.dir");
if(resourcesDir != null) {
// Gradle based distributable
var appBinary = (resourcesDir
.split("\\.app")[0] + ".app/Contents/MacOS/Processing")
.replaceAll(" ", "\\\\ ");
writer.print(appBinary + " cli $@");

} else {
// Ant based distributable
writer.print("# Prevents processing-java from stealing focus, see:\n" +
"# https://github.com/processing/processing/issues/3996.\n" +
"OPTION_FOR_HEADLESS_RUN=\"\"\n" +
"for ARG in \"$@\"\n" +
"do\n" +
" if [ \"$ARG\" = \"--build\" ]; then\n" +
" OPTION_FOR_HEADLESS_RUN=\"-Djava.awt.headless=true\"\n" +
" fi\n" +
"done\n\n");

String javaRoot = Platform.getContentFile(".").getCanonicalPath();

StringList jarList = new StringList();
addJarList(jarList, new File(javaRoot));
addJarList(jarList, new File(javaRoot, "core/library"));
addJarList(jarList, new File(javaRoot, "modes/java/mode"));
String classPath = jarList.join(":").replaceAll(javaRoot + "\\/?", "");

writer.println("cd \"" + javaRoot + "\" && " +
Platform.getJavaPath().replaceAll(" ", "\\\\ ") +
" -Djna.nosys=true" +
" $OPTION_FOR_HEADLESS_RUN" +
" -cp \"" + classPath + "\"" +
" processing.mode.java.Commander \"$@\"");
}
writer.flush();
writer.close();
file.setExecutable(true);
Expand Down
15 changes: 14 additions & 1 deletion core/examples/src/main/java/Basic.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import processing.core.PApplet;

import java.io.IOException;

public class Basic extends PApplet {
public void settings(){
size(500, 500);

try {
Runtime.getRuntime().exec("echo Hello World");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void draw(){
ellipse(width / 2f, height / 2f, 125f, 125f);
background(255);
fill(0);
ellipse(mouseX, mouseY, 125f, 125f);
println(frameRate);


}


Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ lsp4j = { module = "org.eclipse.lsp4j:org.eclipse.lsp4j", version = "0.22.0" }
jsoup = { module = "org.jsoup:jsoup", version = "1.17.2" }
markdown = { module = "com.mikepenz:multiplatform-markdown-renderer-m2", version = "0.31.0" }
markdownJVM = { module = "com.mikepenz:multiplatform-markdown-renderer-jvm", version = "0.31.0" }
clikt = { module = "com.github.ajalt.clikt:clikt", version = "5.0.2" }

[plugins]
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
Expand Down
2 changes: 1 addition & 1 deletion java/src/processing/mode/java/lsp/PdeLanguageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.lsp4j.services.LanguageClient;


class PdeLanguageServer implements LanguageServer, LanguageClientAware {
public class PdeLanguageServer implements LanguageServer, LanguageClientAware {
Map<File, PdeAdapter> adapters = new HashMap<>();
LanguageClient client = null;
PdeTextDocumentService textDocumentService = new PdeTextDocumentService(this);
Expand Down
Loading