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

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


The following commit(s) were added to refs/heads/main by this push:
     new f909a39b43a CAMEL-18497: camel-jbang - Run command now supports 
specifying a different Camel version.
f909a39b43a is described below

commit f909a39b43a915e21aa0e578150db2a30981b1a9
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sun Jan 8 18:11:20 2023 +0100

    CAMEL-18497: camel-jbang - Run command now supports specifying a different 
Camel version.
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 50 +++++++++++++++++++++-
 1 file changed, 48 insertions(+), 2 deletions(-)

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 ffd3062d002..517dfb1a4ef 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
@@ -105,6 +105,9 @@ class Run extends CamelCommand {
     @Option(names = { "--background" }, defaultValue = "false", description = 
"Run in the background")
     boolean background;
 
+    @Option(names = { "--camel-version" }, description = "To run using a 
different Camel version than the default version.")
+    String camelVersion;
+
     @Option(names = { "--profile" }, scope = CommandLine.ScopeType.INHERIT, 
defaultValue = "application",
             description = "Profile to use, which refers to loading properties 
file with the given profile name. By default application.properties is loaded.")
     String profile;
@@ -290,6 +293,7 @@ class Run extends CamelCommand {
             openapi = profileProperties.getProperty("camel.jbang.openApi", 
openapi);
             download = 
"true".equals(profileProperties.getProperty("camel.jbang.download", download ? 
"true" : "false"));
             background = 
"true".equals(profileProperties.getProperty("camel.jbang.background", 
background ? "true" : "false"));
+            camelVersion = 
profileProperties.getProperty("camel.jbang.camel-version", camelVersion);
         }
 
         // generate open-api early
@@ -565,13 +569,55 @@ class Run extends CamelCommand {
             writeSettings("camel.component.properties.location", loc);
         }
 
-        if (background) {
+        // okay we have validated all input and are ready to run
+
+        if (camelVersion != null) {
+            // run in another JVM with different camel version (foreground or 
background)
+            return runCamelVersion(main);
+        } else if (background) {
+            // spawn new JVM to run in background
             return runBackground(main);
         } else {
+            // run default in current JVM with same camel version
             return runKameletMain(main);
         }
     }
 
+    protected int runCamelVersion(KameletMain main) throws Exception {
+        String cmd = ProcessHandle.current().info().commandLine().orElse(null);
+        if (cmd != null) {
+            cmd = StringHelper.after(cmd, "main.CamelJBang ");
+        }
+        if (cmd == null) {
+            System.err.println("No Camel integration files to run");
+            return 1;
+        }
+        if (background) {
+            cmd = cmd.replaceFirst("--background=true", "");
+            cmd = cmd.replaceFirst("--background", "");
+        }
+        cmd = cmd.replaceFirst("--camel-version=" + camelVersion, "");
+        // need to use jbang command to specify camel version
+        cmd = "jbang run -Dcamel.jbang.version=" + camelVersion + " 
camel@apache/camel " + cmd;
+
+        ProcessBuilder pb = new ProcessBuilder();
+        String[] arr = cmd.split("\\s+"); // TODO: safe split
+        List<String> args = Arrays.asList(arr);
+        pb.command(args);
+        if (background) {
+            Process p = pb.start();
+            System.out.println("Running Camel integration: " + name + " 
(version: " + camelVersion
+                               + ") in background with PID: " + p.pid());
+            return 0;
+        } else {
+            // TODO: this makes camel CLI confused: camel get
+            pb.inheritIO(); // run in foreground (with IO so logs are visible)
+            Process p = pb.start();
+            // wait for that process to exit as we run in foreground
+            return p.waitFor();
+        }
+    }
+
     protected int runBackground(KameletMain main) throws Exception {
         String cmd = ProcessHandle.current().info().commandLine().orElse(null);
         if (cmd != null) {
@@ -586,7 +632,7 @@ class Run extends CamelCommand {
         cmd = "camel " + cmd;
 
         ProcessBuilder pb = new ProcessBuilder();
-        String[] arr = cmd.split("\\s+");
+        String[] arr = cmd.split("\\s+"); // TODO: safe split
         List<String> args = Arrays.asList(arr);
         pb.command(args);
         Process p = pb.start();

Reply via email to