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 2022/06/06 13:07:29 UTC

[camel] 03/04: CAMEL-18151: camel-jbang - Base command for export

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

commit b32c1ba33cf11bddbd0134d5d302afd4cf8125ef
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 6 14:50:41 2022 +0200

    CAMEL-18151: camel-jbang - Base command for export
---
 .../camel/dsl/jbang/core/commands/BaseExport.java  | 56 +++++++++++++++++++
 .../dsl/jbang/core/commands/ExportCamelMain.java   | 54 -------------------
 .../dsl/jbang/core/commands/ExportQuarkus.java     | 54 +------------------
 .../dsl/jbang/core/commands/ExportSpringBoot.java  | 62 +++-------------------
 4 files changed, 63 insertions(+), 163 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/BaseExport.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/BaseExport.java
index e7ab0c246b2..2d3568f7a8c 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/BaseExport.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/BaseExport.java
@@ -7,12 +7,14 @@ import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.StandardCopyOption;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.stream.Collectors;
 
 import org.apache.camel.main.MavenGav;
 import org.apache.camel.util.FileUtil;
@@ -163,6 +165,60 @@ abstract class BaseExport extends CamelCommand {
         // noop
     }
 
+    protected void copySettingsAndProfile(File settings, File profile, File targetDir, String packageName) throws Exception {
+        OrderedProperties prop = new OrderedProperties();
+        prop.load(new FileInputStream(settings));
+        OrderedProperties prop2 = new OrderedProperties();
+        if (profile.exists()) {
+            prop2.load(new FileInputStream(profile));
+        }
+
+        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
+            String key = entry.getKey().toString();
+            boolean skip = "camel.main.routesCompileDirectory".equals(key) || "camel.main.routesReloadEnabled".equals(key);
+            if (!skip && key.startsWith("camel.main")) {
+                prop2.put(entry.getKey(), entry.getValue());
+            }
+        }
+
+        // should have package name set for package scan
+        if (packageName != null && !prop2.containsKey("camel.main.basePackageScan")
+                && !prop2.containsKey("camel.main.base-package-scan")) {
+            prop2.put("camel.main.basePackageScan", packageName);
+        }
+
+        FileOutputStream fos = new FileOutputStream(new File(targetDir, "application.properties"), false);
+        for (Map.Entry<Object, Object> entry : prop2.entrySet()) {
+            String k = entry.getKey().toString();
+            String v = entry.getValue().toString();
+
+            boolean skip = k.startsWith("camel.jbang.");
+            if (skip) {
+                continue;
+            }
+
+            // files are now loaded in classpath
+            v = v.replaceAll("file:", "classpath:");
+            if ("camel.main.routesIncludePattern".equals(k)) {
+                // camel.main.routesIncludePattern should remove all .java as we use spring boot to load them
+                // camel.main.routesIncludePattern should remove all file: classpath: as we copy them to src/main/resources/camel where camel auto-load from
+                v = Arrays.stream(v.split(","))
+                        .filter(n -> !n.endsWith(".java") && !n.startsWith("file:") && !n.startsWith("classpath:"))
+                        .collect(Collectors.joining(","));
+            }
+            if (!v.isBlank()) {
+                String line = adjustApplicationProperties(k, v);
+                fos.write(line.getBytes(StandardCharsets.UTF_8));
+                fos.write("\n".getBytes(StandardCharsets.UTF_8));
+            }
+        }
+        IOHelper.close(fos);
+    }
+
+    protected String adjustApplicationProperties(String key, String value) {
+        return key + "=" + value;
+    }
+
     protected static void safeCopy(File source, File target, boolean override) throws Exception {
         if (!source.exists()) {
             return;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
index fff27b1ae08..9930a6345c6 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
@@ -17,21 +17,15 @@
 package org.apache.camel.dsl.jbang.core.commands;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.Map;
 import java.util.Set;
-import java.util.stream.Collectors;
 
 import org.apache.camel.catalog.CamelCatalog;
 import org.apache.camel.catalog.DefaultCamelCatalog;
 import org.apache.camel.main.MavenGav;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.commons.io.FileUtils;
 import picocli.CommandLine;
 
@@ -198,52 +192,4 @@ class ExportCamelMain extends BaseExport {
         safeCopy(is, new File(srcResourcesDir, "assembly/runner.xml"));
     }
 
-    private void copySettingsAndProfile(File settings, File profile, File targetDir, String packageName) throws Exception {
-        OrderedProperties prop = new OrderedProperties();
-        prop.load(new FileInputStream(settings));
-        OrderedProperties prop2 = new OrderedProperties();
-        if (profile.exists()) {
-            prop2.load(new FileInputStream(profile));
-        }
-
-        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
-            String key = entry.getKey().toString();
-            boolean skip = "camel.main.routesCompileDirectory".equals(key) || "camel.main.routesReloadEnabled".equals(key);
-            if (!skip && key.startsWith("camel.main")) {
-                prop2.put(entry.getKey(), entry.getValue());
-            }
-        }
-        // should have package name set for package scan
-        if (!prop2.containsKey("camel.main.basePackageScan") && !prop2.containsKey("camel.main.base-package-scan")) {
-            prop2.put("camel.main.basePackageScan", packageName);
-        }
-
-        FileOutputStream fos = new FileOutputStream(new File(targetDir, "application.properties"), false);
-        for (Map.Entry<Object, Object> entry : prop2.entrySet()) {
-            String k = entry.getKey().toString();
-            String v = entry.getValue().toString();
-
-            boolean skip = k.startsWith("camel.jbang.");
-            if (skip) {
-                continue;
-            }
-
-            // files are now loaded in classpath
-            v = v.replaceAll("file:", "classpath:");
-            if ("camel.main.routesIncludePattern".equals(k)) {
-                // camel.main.routesIncludePattern should remove all .java as they become regular java classes
-                // camel.main.routesIncludePattern should remove all file: classpath: as we copy them to src/main/resources/camel where camel auto-load from
-                v = Arrays.stream(v.split(","))
-                        .filter(n -> !n.endsWith(".java") && !n.startsWith("file:") && !n.startsWith("classpath:"))
-                        .collect(Collectors.joining(","));
-            }
-            if (!v.isBlank()) {
-                String line = k + "=" + v;
-                fos.write(line.getBytes(StandardCharsets.UTF_8));
-                fos.write("\n".getBytes(StandardCharsets.UTF_8));
-            }
-        }
-        IOHelper.close(fos);
-    }
-
 }
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
index d0c02dac967..0f43e58acb0 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
@@ -17,19 +17,13 @@
 package org.apache.camel.dsl.jbang.core.commands;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.Map;
 import java.util.Set;
-import java.util.stream.Collectors;
 
 import org.apache.camel.main.MavenGav;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.commons.io.FileUtils;
 import picocli.CommandLine;
 
@@ -84,7 +78,7 @@ class ExportQuarkus extends BaseExport {
         srcCamelResourcesDir.mkdirs();
         copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
         // copy from settings to profile
-        copySettingsAndProfile(settings, profile, srcResourcesDir);
+        copySettingsAndProfile(settings, profile, srcResourcesDir, null);
         // gather dependencies
         Set<String> deps = resolveDependencies(settings);
         // create pom
@@ -158,50 +152,4 @@ class ExportQuarkus extends BaseExport {
         return answer;
     }
 
-    private void copySettingsAndProfile(File settings, File profile, File targetDir) throws Exception {
-        OrderedProperties prop = new OrderedProperties();
-        prop.load(new FileInputStream(settings));
-        OrderedProperties prop2 = new OrderedProperties();
-        if (profile.exists()) {
-            prop2.load(new FileInputStream(profile));
-        }
-
-        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
-            String key = entry.getKey().toString();
-            // modeline not supported in camel-quarkus
-            boolean skip = "camel.main.modeline".equals(key) || "camel.main.routesCompileDirectory".equals(key)
-                    || "camel.main.routesReloadEnabled".equals(key);
-            if (!skip && key.startsWith("camel.main")) {
-                prop2.put(entry.getKey(), entry.getValue());
-            }
-        }
-
-        FileOutputStream fos = new FileOutputStream(new File(targetDir, "application.properties"), false);
-        for (Map.Entry<Object, Object> entry : prop2.entrySet()) {
-            String k = entry.getKey().toString();
-            String v = entry.getValue().toString();
-
-            boolean skip = k.startsWith("camel.jbang.");
-            if (skip) {
-                continue;
-            }
-
-            // files are now loaded in classpath
-            v = v.replaceAll("file:", "classpath:");
-            if ("camel.main.routesIncludePattern".equals(k)) {
-                // camel.main.routesIncludePattern should remove all .java as we use quarkus to load them
-                // camel.main.routesIncludePattern should remove all file: classpath: as we copy them to src/main/resources/camel where camel auto-load from
-                v = Arrays.stream(v.split(","))
-                        .filter(n -> !n.endsWith(".java") && !n.startsWith("file:") && !n.startsWith("classpath:"))
-                        .collect(Collectors.joining(","));
-            }
-            if (!v.isBlank()) {
-                String line = k + "=" + v;
-                fos.write(line.getBytes(StandardCharsets.UTF_8));
-                fos.write("\n".getBytes(StandardCharsets.UTF_8));
-            }
-        }
-        IOHelper.close(fos);
-    }
-
 }
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
index 47dbc37fc44..b61735dfc3b 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
@@ -17,21 +17,16 @@
 package org.apache.camel.dsl.jbang.core.commands;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.Map;
 import java.util.Set;
-import java.util.stream.Collectors;
 
 import org.apache.camel.catalog.CamelCatalog;
 import org.apache.camel.catalog.DefaultCamelCatalog;
 import org.apache.camel.main.MavenGav;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.commons.io.FileUtils;
 import picocli.CommandLine;
 
@@ -91,7 +86,7 @@ class ExportSpringBoot extends BaseExport {
         srcCamelResourcesDir.mkdirs();
         copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, srcCamelResourcesDir, packageName);
         // copy from settings to profile
-        copySettingsAndProfile(settings, profile, srcResourcesDir);
+        copySettingsAndProfile(settings, profile, srcResourcesDir, null);
         // create main class
         createMainClassSource(srcJavaDir, packageName, mainClassname);
         // gather dependencies
@@ -195,58 +190,13 @@ class ExportSpringBoot extends BaseExport {
         }
     }
 
-    private void copySettingsAndProfile(File settings, File profile, File targetDir) throws Exception {
-        OrderedProperties prop = new OrderedProperties();
-        prop.load(new FileInputStream(settings));
-        OrderedProperties prop2 = new OrderedProperties();
-        if (profile.exists()) {
-            prop2.load(new FileInputStream(profile));
-        }
-
-        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
-            String key = entry.getKey().toString();
-            boolean skip = "camel.main.routesCompileDirectory".equals(key) || "camel.main.routesReloadEnabled".equals(key);
-            if (!skip && key.startsWith("camel.main")) {
-                prop2.put(entry.getKey(), entry.getValue());
-            }
-        }
-
+    @Override
+    protected String adjustApplicationProperties(String key, String value) {
         // camel.main.x should be renamed to camel.springboot.x
-        OrderedProperties prop3 = new OrderedProperties();
-        for (Map.Entry<Object, Object> entry : prop2.entrySet()) {
-            String key = entry.getKey().toString();
-            if (key.startsWith("camel.main.")) {
-                key = "camel.springboot." + key.substring(11);
-            }
-            prop3.put(key, entry.getValue());
-        }
-
-        FileOutputStream fos = new FileOutputStream(new File(targetDir, "application.properties"), false);
-        for (Map.Entry<Object, Object> entry : prop3.entrySet()) {
-            String k = entry.getKey().toString();
-            String v = entry.getValue().toString();
-
-            boolean skip = k.startsWith("camel.jbang.");
-            if (skip) {
-                continue;
-            }
-
-            // files are now loaded in classpath
-            v = v.replaceAll("file:", "classpath:");
-            if ("camel.springboot.routesIncludePattern".equals(k)) {
-                // camel.main.routesIncludePattern should remove all .java as we use spring boot to load them
-                // camel.main.routesIncludePattern should remove all file: classpath: as we copy them to src/main/resources/camel where camel auto-load from
-                v = Arrays.stream(v.split(","))
-                        .filter(n -> !n.endsWith(".java") && !n.startsWith("file:") && !n.startsWith("classpath:"))
-                        .collect(Collectors.joining(","));
-            }
-            if (!v.isBlank()) {
-                String line = k + "=" + v;
-                fos.write(line.getBytes(StandardCharsets.UTF_8));
-                fos.write("\n".getBytes(StandardCharsets.UTF_8));
-            }
+        if (key.startsWith("camel.main.")) {
+            key = "camel.springboot." + key.substring(11);
         }
-        IOHelper.close(fos);
+        return super.adjustApplicationProperties(key, value);
     }
 
 }