This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch runq
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 5ab046674e150ff63969dba2215bd62fdc3d6972
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Apr 20 09:18:23 2024 +0200

    CAMEL-19041: camel-jbang - Run with runtime for spring-boot and quarkus
---
 .../dsl/jbang/core/commands/ExportBaseCommand.java |  24 ++--
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 123 +++++++++++++++++++++
 2 files changed, 135 insertions(+), 12 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
index edb9da8c354..33dedbed9e3 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
@@ -64,6 +64,7 @@ import picocli.CommandLine;
 abstract class ExportBaseCommand extends CamelCommand {
 
     protected static final String BUILD_DIR = 
CommandLineHelper.CAMEL_JBANG_WORK_DIR + "/work";
+    protected static final String RUN_DIR = 
CommandLineHelper.CAMEL_JBANG_WORK_DIR + "/run";
 
     protected static final String[] SETTINGS_PROP_SOURCE_KEYS = new String[] {
             "camel.main.routesIncludePattern",
@@ -126,10 +127,10 @@ abstract class ExportBaseCommand extends CamelCommand {
     @CommandLine.Option(names = { "--main-classname" },
                         description = "The class name of the Camel Main 
application class",
                         defaultValue = "CamelApplication")
-    protected String mainClassname;
+    protected String mainClassname = "CamelApplication";
 
     @CommandLine.Option(names = { "--java-version" }, description = "Java 
version", defaultValue = "17")
-    protected String javaVersion;
+    protected String javaVersion = "17";
 
     @CommandLine.Option(names = { "--camel-version" },
                         description = "To export using a different Camel 
version than the default version.")
@@ -149,34 +150,34 @@ abstract class ExportBaseCommand extends CamelCommand {
 
     @CommandLine.Option(names = { "--spring-boot-version" }, description = 
"Spring Boot version",
                         defaultValue = "3.2.5")
-    protected String springBootVersion;
+    protected String springBootVersion = "3.2.5";
 
     @CommandLine.Option(names = { "--camel-spring-boot-version" }, description 
= "Camel version to use with Spring Boot")
     protected String camelSpringBootVersion;
 
     @CommandLine.Option(names = { "--quarkus-group-id" }, description = 
"Quarkus Platform Maven groupId",
                         defaultValue = "io.quarkus.platform")
-    protected String quarkusGroupId;
+    protected String quarkusGroupId = "io.quarkus.platform";
 
     @CommandLine.Option(names = { "--quarkus-artifact-id" }, description = 
"Quarkus Platform Maven artifactId",
                         defaultValue = "quarkus-bom")
-    protected String quarkusArtifactId;
+    protected String quarkusArtifactId = "quarkus-bom";
 
     @CommandLine.Option(names = { "--quarkus-version" }, description = 
"Quarkus Platform version",
                         defaultValue = "3.9.4")
-    protected String quarkusVersion;
+    protected String quarkusVersion = "3.9.4";
 
     @CommandLine.Option(names = { "--maven-wrapper" }, defaultValue = "true",
                         description = "Include Maven Wrapper files in exported 
project")
-    protected boolean mavenWrapper;
+    protected boolean mavenWrapper = true;
 
     @CommandLine.Option(names = { "--gradle-wrapper" }, defaultValue = "true",
                         description = "Include Gradle Wrapper files in 
exported project")
-    protected boolean gradleWrapper;
+    protected boolean gradleWrapper = true;
 
     @CommandLine.Option(names = { "--build-tool" }, defaultValue = "maven",
                         description = "Build tool to use (maven or gradle)")
-    protected String buildTool;
+    protected String buildTool = "maven";
 
     @CommandLine.Option(names = { "--open-api" }, description = "Adds an 
OpenAPI spec from the given file (json or yaml file)")
     protected String openapi;
@@ -187,12 +188,11 @@ abstract class ExportBaseCommand extends CamelCommand {
     protected String exportDir;
 
     @CommandLine.Option(names = { "--logging-level" }, defaultValue = "info", 
description = "Logging level")
-    protected String loggingLevel;
+    protected String loggingLevel = "info";
 
     @CommandLine.Option(names = { "--package-name" },
                         description = "For Java source files should they have 
the given package name. By default the package name is computed from the Maven 
GAV. "
-                                      +
-                                      "Use false to turn off and not include 
package name in the Java source files.")
+                                      + "Use false to turn off and not include 
package name in the Java source files.")
     protected String packageName;
 
     @CommandLine.Option(names = { "--fresh" }, description = "Make sure we use 
fresh (i.e. non-cached) resources")
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index c9d8b015b71..3d80643e35a 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -56,6 +56,7 @@ import org.apache.camel.catalog.CamelCatalog;
 import org.apache.camel.catalog.DefaultCamelCatalog;
 import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
 import org.apache.camel.dsl.jbang.core.common.LoggingLevelCompletionCandidates;
+import org.apache.camel.dsl.jbang.core.common.RuntimeCompletionCandidates;
 import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
 import org.apache.camel.dsl.jbang.core.common.VersionHelper;
 import org.apache.camel.generator.openapi.RestDslGenerator;
@@ -126,6 +127,10 @@ public class Run extends CamelCommand {
 
     public List<String> files = new ArrayList<>();
 
+    @Option(names = { "--runtime" }, completionCandidates = 
RuntimeCompletionCandidates.class,
+            defaultValue = "camel-main", description = "Runtime (spring-boot, 
quarkus, or camel-main)")
+    String runtime = "camel-main";
+
     @Option(names = { "--source-dir" },
             description = "Source directory for dynamically loading Camel 
file(s) to run. When using this, then files cannot be specified at the same 
time.")
     String sourceDir;
@@ -289,6 +294,11 @@ public class Run extends CamelCommand {
 
     @Override
     public boolean disarrangeLogging() {
+        if (runtime.equals("quarkus")) {
+            return true;
+        } else if (runtime.equals("spring-boot")) {
+            return true;
+        }
         return false;
     }
 
@@ -391,6 +401,12 @@ public class Run extends CamelCommand {
             return 1;
         }
 
+        if (runtime.equals("quarkus")) {
+            return runQuarkus();
+        } else if (runtime.equals("spring-boot")) {
+            return runSpringBoot();
+        }
+
         File work = CommandLineHelper.getWorkDir();
         removeDir(work);
         work.mkdirs();
@@ -823,6 +839,113 @@ public class Run extends CamelCommand {
         }
     }
 
+    protected int runQuarkus() throws Exception {
+        // export to hidden folder
+        ExportQuarkus eq = new ExportQuarkus(getMain());
+        eq.exportDir = ExportBaseCommand.RUN_DIR;
+        eq.exclude = this.exclude;
+        eq.filePaths = this.filePaths;
+        eq.files = this.files;
+        eq.gav = this.gav;
+        if (eq.gav == null) {
+            eq.gav = "org.dummy:dummy:1.0-SNAPSHOT";
+        }
+        eq.dependencies = this.dependencies;
+        if (eq.dependencies == null) {
+            eq.dependencies = "camel:cli-connector";
+        } else {
+            eq.dependencies += ",camel:cli-connector";
+        }
+        eq.fresh = this.fresh;
+        eq.download = this.download;
+        eq.quiet = true;
+        eq.logging = false;
+        eq.loggingLevel = "off";
+
+        System.out.println("Running using Quarkus v" + eq.quarkusVersion + " 
(have a little patience)");
+
+        // run export
+        int exit = eq.export();
+        if (exit != 0) {
+            return exit;
+        }
+        // run quarkus via maven
+        ProcessBuilder pb = new ProcessBuilder();
+        pb.command(ExportBaseCommand.RUN_DIR + "/mvnw", "--quiet", "--file", 
ExportBaseCommand.RUN_DIR, "quarkus:dev");
+
+        if (background) {
+            Process p = pb.start();
+            this.spawnPid = p.pid();
+            if (!silentRun && !transformRun && !transformMessageRun) {
+                printer().println("Running Camel Quarkus integration: " + name 
+ " (version: " + eq.quarkusVersion
+                                  + ") in background with PID: " + p.pid());
+            }
+            return 0;
+        } else {
+            pb.inheritIO(); // run in foreground (with IO so logs are visible)
+            Process p = pb.start();
+            this.spawnPid = p.pid();
+            // wait for that process to exit as we run in foreground
+            return p.waitFor();
+        }
+    }
+
+    protected int runSpringBoot() throws Exception {
+        // export to hidden folder
+        ExportSpringBoot eq = new ExportSpringBoot(getMain());
+        eq.exportDir = ExportBaseCommand.RUN_DIR;
+        eq.exclude = this.exclude;
+        eq.filePaths = this.filePaths;
+        eq.files = this.files;
+        eq.gav = this.gav;
+        if (eq.gav == null) {
+            eq.gav = "org.dummy:dummy:1.0-SNAPSHOT";
+        }
+        eq.dependencies = this.dependencies;
+        if (eq.dependencies == null) {
+            eq.dependencies = "camel:cli-connector";
+        } else {
+            eq.dependencies += ",camel:cli-connector";
+        }
+        eq.fresh = this.fresh;
+        eq.download = this.download;
+        eq.quiet = true;
+        eq.logging = false;
+        eq.loggingLevel = "off";
+
+        System.out.println("Running using Spring Boot v" + 
eq.springBootVersion + " (have a little patience)");
+        // TODO: if error exporting we should report error (log output)
+        // TODO: run in unique sub folder
+        // TODO: delete sub folder on exit
+        // TODO: camel log from spring-boot/quarkus is not possible
+        // TODO: copy files using symbolic link so you can edit the file
+
+        // run export
+        int exit = eq.export();
+        if (exit != 0) {
+            return exit;
+        }
+        // run quarkus via maven
+        ProcessBuilder pb = new ProcessBuilder();
+        pb.command(ExportBaseCommand.RUN_DIR + "/mvnw", "--quiet", "--file", 
ExportBaseCommand.RUN_DIR, "spring-boot:run");
+
+        if (background) {
+            Process p = pb.start();
+            this.spawnPid = p.pid();
+            if (!silentRun && !transformRun && !transformMessageRun) {
+                printer().println("Running Camel Spring Boot integration: " + 
name + " (version: " + camelVersion
+                                  + ") in background with PID: " + p.pid());
+            }
+            return 0;
+        } else {
+            pb.inheritIO(); // run in foreground (with IO so logs are visible)
+            Process p = pb.start();
+            this.spawnPid = p.pid();
+            // wait for that process to exit as we run in foreground
+            return p.waitFor();
+        }
+    }
+
     private boolean acceptPropertiesFile(String file) {
         String name = FileUtil.onlyName(file);
         if (profile != null && name.startsWith("application-")) {

Reply via email to