You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/08/23 20:00:48 UTC

[camel] branch main updated: CAMEL-19778: camel-jbang - Add DSL transform command

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 43c2e03ce68 CAMEL-19778: camel-jbang - Add DSL transform command
43c2e03ce68 is described below

commit 43c2e03ce680efe49e55910a498934055115f24e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Aug 23 22:00:34 2023 +0200

    CAMEL-19778: camel-jbang - Add DSL transform command
---
 .../camel/impl/DefaultDumpRoutesStrategy.java      |  8 +++
 .../dsl/jbang/core/commands/CamelJBangMain.java    |  1 +
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 22 ++++++
 .../camel/dsl/jbang/core/commands/Transform.java   | 82 ++++++++++++++++++++++
 4 files changed, 113 insertions(+)

diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java
index cf0e4093d3f..88b0e1f09ca 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java
@@ -183,6 +183,10 @@ public class DefaultDumpRoutesStrategy extends ServiceSupport implements DumpRou
             if (size > 0) {
                 Map<Resource, RoutesDefinition> groups = new LinkedHashMap<>();
                 for (RouteDefinition route : model.getRouteDefinitions()) {
+                    if ((route.isRest() != null && route.isRest()) || (route.isTemplate() != null && route.isTemplate())) {
+                        // skip routes that are rest/templates
+                        continue;
+                    }
                     Resource res = route.getResource();
                     if (res == null) {
                         res = dummy;
@@ -364,6 +368,10 @@ public class DefaultDumpRoutesStrategy extends ServiceSupport implements DumpRou
             if (size > 0) {
                 Map<Resource, RoutesDefinition> groups = new LinkedHashMap<>();
                 for (RouteDefinition route : model.getRouteDefinitions()) {
+                    if ((route.isRest() != null && route.isRest()) || (route.isTemplate() != null && route.isTemplate())) {
+                        // skip routes that are rest/templates
+                        continue;
+                    }
                     Resource res = route.getResource();
                     if (res == null) {
                         res = dummy;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index b602c8a7426..4c303bcd73b 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -91,6 +91,7 @@ public class CamelJBangMain implements Callable<Integer> {
                 .addSubcommand("ps", new CommandLine(new ListProcess(main)))
                 .addSubcommand("stop", new CommandLine(new StopProcess(main)))
                 .addSubcommand("trace", new CommandLine(new CamelTraceAction(main)))
+                .addSubcommand("transform", new CommandLine(new Transform(main)))
                 .addSubcommand("get", new CommandLine(new CamelStatus(main))
                         .addSubcommand("context", new CommandLine(new CamelContextStatus(main)))
                         .addSubcommand("route", new CommandLine(new CamelRouteStatus(main)))
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 95b6ce49c96..d4025154fd3 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
@@ -110,6 +110,7 @@ public class Run extends CamelCommand {
 
     private boolean silentRun;
     private boolean pipeRun;
+    private boolean transformRun;
 
     private File logFile;
 
@@ -269,6 +270,12 @@ public class Run extends CamelCommand {
         return run();
     }
 
+    protected Integer runTransform() throws Exception {
+        // just boot silently and exit
+        transformRun = true;
+        return run();
+    }
+
     protected Integer runPipe(String file) throws Exception {
         this.files.add(file);
         pipeRun = true;
@@ -449,10 +456,21 @@ public class Run extends CamelCommand {
             // do not run for very long in silent run
             main.addInitialProperty("camel.main.autoStartup", "false");
             main.addInitialProperty("camel.main.durationMaxSeconds", "1");
+        } else if (transformRun) {
+            main.setSilent(true);
+            // enable stub in silent mode so we do not use real components
+            main.setStubPattern("*");
+            // do not run for very long in silent run
+            main.addInitialProperty("camel.main.autoStartup", "false");
+            main.addInitialProperty("camel.main.durationMaxSeconds", "1");
+            main.addInitialProperty("camel.main.durationMaxSeconds", "1");
         } else if (pipeRun) {
             // auto terminate if being idle
             main.addInitialProperty("camel.main.durationMaxIdleSeconds", "1");
         }
+        // any custom initial property
+        doAddInitialProperty(main);
+
         writeSetting(main, profileProperties, "camel.main.durationMaxMessages",
                 () -> maxMessages > 0 ? String.valueOf(maxMessages) : null);
         writeSetting(main, profileProperties, "camel.main.durationMaxSeconds",
@@ -669,6 +687,10 @@ public class Run extends CamelCommand {
         }
     }
 
+    protected void doAddInitialProperty(KameletMain main) {
+        // noop
+    }
+
     private void setupReload(KameletMain main, StringJoiner sjReload) {
         if (dev && (sourceDir != null || sjReload.length() > 0)) {
             main.addInitialProperty("camel.main.routesReloadEnabled", "true");
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Transform.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Transform.java
new file mode 100644
index 00000000000..c5b2196c807
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Transform.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Stack;
+
+import org.apache.camel.main.KameletMain;
+import picocli.CommandLine;
+import picocli.CommandLine.Command;
+
+@Command(name = "transform", description = "Transform Camel route to XML or YAML format", sortOptions = false)
+public class Transform extends CamelCommand {
+
+    @CommandLine.Parameters(description = "The Camel file(s) to run. If no files specified then application.properties is used as source for which files to run.",
+                            arity = "0..9", paramLabel = "<files>", parameterConsumer = FilesConsumer.class)
+    Path[] filePaths; // Defined only for file path completion; the field never used
+
+    List<String> files = new ArrayList<>();
+
+    @CommandLine.Option(names = {
+            "--dir",
+            "--directory" }, description = "Directory where the transformed files will be saved", required = true)
+    private String directory;
+
+    @CommandLine.Option(names = { "--format" },
+                        description = "Output format (xml or yaml)", defaultValue = "yaml")
+    String format;
+
+    @CommandLine.Option(names = { "--uri-as-parameters" },
+                        description = "Whether to expand URIs into separated key/value parameters (only in use for YAML format)")
+    boolean uriAsParameters;
+
+    public Transform(CamelJBangMain main) {
+        super(main);
+    }
+
+    @Override
+    public Integer doCall() throws Exception {
+        Run run = new Run(getMain()) {
+            @Override
+            protected void doAddInitialProperty(KameletMain main) {
+                main.addInitialProperty("camel.main.dumpRoutes", format);
+                main.addInitialProperty("camel.main.dumpRoutesInclude", "routes,rests,routeConfigurations,beans");
+                main.addInitialProperty("camel.main.dumpRoutesUriAsParameters", "" + uriAsParameters);
+                main.addInitialProperty("camel.main.dumpRoutesDirectory", directory);
+            }
+        };
+        run.files = files;
+        run.maxSeconds = 1;
+        Integer exit = run.runSilent();
+        if (exit != null && exit != 0) {
+            return exit;
+        }
+        return 0;
+    }
+
+    static class FilesConsumer extends ParameterConsumer<Transform> {
+        @Override
+        protected void doConsumeParameters(Stack<String> args, Transform cmd) {
+            String arg = args.pop();
+            cmd.files.add(arg);
+        }
+    }
+
+}