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:27 UTC

[camel] 01/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 3be249ff1a0a9cae5ec0c4f3a54b7844a0eb1cda
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 6 14:16:27 2022 +0200

    CAMEL-18151: camel-jbang - Base command for export
---
 .../camel/dsl/jbang/core/commands/BaseExport.java  | 84 ++++++++++++++++++++++
 .../dsl/jbang/core/commands/ExportCamelMain.java   | 72 +------------------
 .../dsl/jbang/core/commands/ExportQuarkus.java     | 57 +--------------
 .../dsl/jbang/core/commands/ExportSpringBoot.java  | 57 +--------------
 4 files changed, 87 insertions(+), 183 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
new file mode 100644
index 00000000000..ae05d48355b
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/BaseExport.java
@@ -0,0 +1,84 @@
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.io.File;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+
+import picocli.CommandLine;
+
+abstract class BaseExport extends CamelCommand {
+
+    protected static final String BUILD_DIR = ".camel-jbang/work";
+
+    protected static final String[] SETTINGS_PROP_SOURCE_KEYS = new String[] {
+            "camel.main.routesIncludePattern",
+            "camel.component.properties.location",
+            "camel.component.kamelet.location",
+            "camel.jbang.classpathFiles"
+    };
+
+    @CommandLine.Option(names = { "--gav" }, description = "The Maven group:artifact:version", required = true)
+    protected String gav;
+
+    @CommandLine.Option(names = { "--java-version" }, description = "Java version (11 or 17)",
+                        defaultValue = "11")
+    protected String javaVersion;
+
+    @CommandLine.Option(names = { "--kamelets-version" }, description = "Apache Camel Kamelets version",
+                        defaultValue = "0.8.1")
+    protected String kameletsVersion;
+
+    @CommandLine.Option(names = { "-dir", "--directory" }, description = "Directory where the project will be exported",
+                        defaultValue = ".")
+    protected String exportDir;
+
+    @CommandLine.Option(names = { "--fresh" }, description = "Make sure we use fresh (i.e. non-cached) resources")
+    protected boolean fresh;
+
+    public BaseExport(CamelJBangMain main) {
+        super(main);
+    }
+
+    protected static void safeCopy(File source, File target, boolean override) throws Exception {
+        if (!source.exists()) {
+            return;
+        }
+
+        if (!target.exists()) {
+            Files.copy(source.toPath(), target.toPath());
+        } else if (override) {
+            Files.copy(source.toPath(), target.toPath(),
+                    StandardCopyOption.REPLACE_EXISTING);
+        }
+    }
+
+    protected static String getScheme(String name) {
+        int pos = name.indexOf(":");
+        if (pos != -1) {
+            return name.substring(0, pos);
+        }
+        return null;
+    }
+
+    protected Integer runSilently() throws Exception {
+        Run run = new Run(getMain());
+        Integer code = run.runSilent();
+        return code;
+    }
+
+    protected void safeCopy(InputStream source, File target) throws Exception {
+        if (source == null) {
+            return;
+        }
+
+        File dir = target.getParentFile();
+        if (!dir.exists()) {
+            dir.mkdirs();
+        }
+
+        if (!target.exists()) {
+            Files.copy(source, target.toPath());
+        }
+    }
+}
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 07def8d51fc..6ba7edec790 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
@@ -22,7 +22,6 @@ import java.io.FileOutputStream;
 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;
@@ -43,40 +42,13 @@ import org.apache.commons.io.FileUtils;
 import picocli.CommandLine;
 
 @CommandLine.Command(name = "standalone", description = "Export as standalone Camel Main project")
-class ExportCamelMain extends CamelCommand {
-
-    private static final String BUILD_DIR = ".camel-jbang/work";
-
-    private static final String[] SETTINGS_PROP_SOURCE_KEYS = new String[] {
-            "camel.main.routesIncludePattern",
-            "camel.component.properties.location",
-            "camel.component.kamelet.location",
-            "camel.jbang.classpathFiles"
-    };
-
-    @CommandLine.Option(names = { "--gav" }, description = "The Maven group:artifact:version", required = true)
-    private String gav;
+class ExportCamelMain extends BaseExport {
 
     @CommandLine.Option(names = { "--main-classname" },
                         description = "The class name of the Camel Main application class",
                         defaultValue = "CamelApplication")
     private String mainClassname;
 
-    @CommandLine.Option(names = { "--java-version" }, description = "Java version (11 or 17)",
-                        defaultValue = "11")
-    private String javaVersion;
-
-    @CommandLine.Option(names = { "--kamelets-version" }, description = "Apache Camel Kamelets version",
-                        defaultValue = "0.8.1")
-    private String kameletsVersion;
-
-    @CommandLine.Option(names = { "-dir", "--directory" }, description = "Directory where the project will be exported",
-                        defaultValue = ".")
-    private String exportDir;
-
-    @CommandLine.Option(names = { "--fresh" }, description = "Make sure we use fresh (i.e. non-cached) resources")
-    private boolean fresh;
-
     public ExportCamelMain(CamelJBangMain main) {
         super(main);
     }
@@ -253,12 +225,6 @@ class ExportCamelMain extends CamelCommand {
         IOHelper.writeText(context, new FileOutputStream(srcJavaDir + "/" + mainClassname + ".java", false));
     }
 
-    private Integer runSilently() throws Exception {
-        Run run = new Run(getMain());
-        Integer code = run.runSilent();
-        return code;
-    }
-
     private void copySourceFiles(
             File settings, File profile, File srcJavaDir, File srcResourcesDir, File srcCamelResourcesDir, String packageName)
             throws Exception {
@@ -358,40 +324,4 @@ class ExportCamelMain extends CamelCommand {
         IOHelper.close(fos);
     }
 
-    private void safeCopy(InputStream source, File target) throws Exception {
-        if (source == null) {
-            return;
-        }
-
-        File dir = target.getParentFile();
-        if (!dir.exists()) {
-            dir.mkdirs();
-        }
-
-        if (!target.exists()) {
-            Files.copy(source, target.toPath());
-        }
-    }
-
-    private static void safeCopy(File source, File target, boolean override) throws Exception {
-        if (!source.exists()) {
-            return;
-        }
-
-        if (!target.exists()) {
-            Files.copy(source.toPath(), target.toPath());
-        } else if (override) {
-            Files.copy(source.toPath(), target.toPath(),
-                    StandardCopyOption.REPLACE_EXISTING);
-        }
-    }
-
-    private static String getScheme(String name) {
-        int pos = name.indexOf(":");
-        if (pos != -1) {
-            return name.substring(0, pos);
-        }
-        return null;
-    }
-
 }
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 d9d9b130aca..20559451dff 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
@@ -22,7 +22,6 @@ import java.io.FileOutputStream;
 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;
@@ -41,39 +40,12 @@ import org.apache.commons.io.FileUtils;
 import picocli.CommandLine;
 
 @CommandLine.Command(name = "quarkus", description = "Export as Quarkus project")
-class ExportQuarkus extends CamelCommand {
-
-    private static final String BUILD_DIR = ".camel-jbang/work";
-
-    private static final String[] SETTINGS_PROP_SOURCE_KEYS = new String[] {
-            "camel.main.routesIncludePattern",
-            "camel.component.properties.location",
-            "camel.component.kamelet.location",
-            "camel.jbang.classpathFiles"
-    };
-
-    @CommandLine.Option(names = { "--gav" }, description = "The Maven group:artifact:version", required = true)
-    private String gav;
-
-    @CommandLine.Option(names = { "--java-version" }, description = "Java version (11 or 17)",
-                        defaultValue = "11")
-    private String javaVersion;
+class ExportQuarkus extends BaseExport {
 
     @CommandLine.Option(names = { "--quarkus-version" }, description = "Quarkus version",
                         defaultValue = "2.9.2.Final")
     private String quarkusVersion;
 
-    @CommandLine.Option(names = { "--kamelets-version" }, description = "Apache Camel Kamelets version",
-                        defaultValue = "0.8.1")
-    private String kameletsVersion;
-
-    @CommandLine.Option(names = { "-dir", "--directory" }, description = "Directory where the project will be exported",
-                        defaultValue = ".")
-    private String exportDir;
-
-    @CommandLine.Option(names = { "--fresh" }, description = "Make sure we use fresh (i.e. non-cached) resources")
-    private boolean fresh;
-
     public ExportQuarkus(CamelJBangMain main) {
         super(main);
     }
@@ -232,12 +204,6 @@ class ExportQuarkus extends CamelCommand {
         return answer;
     }
 
-    private Integer runSilently() throws Exception {
-        Run run = new Run(getMain());
-        Integer code = run.runSilent();
-        return code;
-    }
-
     private void copySourceFiles(
             File settings, File profile, File srcJavaDir, File srcResourcesDir, File srcCamelResourcesDir, String packageName)
             throws Exception {
@@ -327,25 +293,4 @@ class ExportQuarkus extends CamelCommand {
         IOHelper.close(fos);
     }
 
-    private static void safeCopy(File source, File target, boolean override) throws Exception {
-        if (!source.exists()) {
-            return;
-        }
-
-        if (!target.exists()) {
-            Files.copy(source.toPath(), target.toPath());
-        } else if (override) {
-            Files.copy(source.toPath(), target.toPath(),
-                    StandardCopyOption.REPLACE_EXISTING);
-        }
-    }
-
-    private static String getScheme(String name) {
-        int pos = name.indexOf(":");
-        if (pos != -1) {
-            return name.substring(0, pos);
-        }
-        return null;
-    }
-
 }
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 2dc282509f3..dddec63dfe1 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
@@ -22,7 +22,6 @@ import java.io.FileOutputStream;
 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;
@@ -43,44 +42,17 @@ import org.apache.commons.io.FileUtils;
 import picocli.CommandLine;
 
 @CommandLine.Command(name = "spring-boot", description = "Export as Spring Boot project")
-class ExportSpringBoot extends CamelCommand {
-
-    private static final String BUILD_DIR = ".camel-jbang/work";
-
-    private static final String[] SETTINGS_PROP_SOURCE_KEYS = new String[] {
-            "camel.main.routesIncludePattern",
-            "camel.component.properties.location",
-            "camel.component.kamelet.location",
-            "camel.jbang.classpathFiles"
-    };
-
-    @CommandLine.Option(names = { "--gav" }, description = "The Maven group:artifact:version", required = true)
-    private String gav;
+class ExportSpringBoot extends BaseExport {
 
     @CommandLine.Option(names = { "--main-classname" },
                         description = "The class name of the Camel Spring Boot application class",
                         defaultValue = "CamelApplication")
     private String mainClassname;
 
-    @CommandLine.Option(names = { "--java-version" }, description = "Java version (11 or 17)",
-                        defaultValue = "11")
-    private String javaVersion;
-
     @CommandLine.Option(names = { "--spring-boot-version" }, description = "Spring Boot version",
                         defaultValue = "2.7.0")
     private String springBootVersion;
 
-    @CommandLine.Option(names = { "--kamelets-version" }, description = "Apache Camel Kamelets version",
-                        defaultValue = "0.8.1")
-    private String kameletsVersion;
-
-    @CommandLine.Option(names = { "-dir", "--directory" }, description = "Directory where the project will be exported",
-                        defaultValue = ".")
-    private String exportDir;
-
-    @CommandLine.Option(names = { "--fresh" }, description = "Make sure we use fresh (i.e. non-cached) resources")
-    private boolean fresh;
-
     public ExportSpringBoot(CamelJBangMain main) {
         super(main);
     }
@@ -252,12 +224,6 @@ class ExportSpringBoot extends CamelCommand {
         IOHelper.writeText(context, new FileOutputStream(srcJavaDir + "/" + mainClassname + ".java", false));
     }
 
-    private Integer runSilently() throws Exception {
-        Run run = new Run(getMain());
-        Integer code = run.runSilent();
-        return code;
-    }
-
     private void copySourceFiles(
             File settings, File profile, File srcJavaDir, File srcResourcesDir, File srcCamelResourcesDir, String packageName)
             throws Exception {
@@ -361,25 +327,4 @@ class ExportSpringBoot extends CamelCommand {
         IOHelper.close(fos);
     }
 
-    private static void safeCopy(File source, File target, boolean override) throws Exception {
-        if (!source.exists()) {
-            return;
-        }
-
-        if (!target.exists()) {
-            Files.copy(source.toPath(), target.toPath());
-        } else if (override) {
-            Files.copy(source.toPath(), target.toPath(),
-                    StandardCopyOption.REPLACE_EXISTING);
-        }
-    }
-
-    private static String getScheme(String name) {
-        int pos = name.indexOf(":");
-        if (pos != -1) {
-            return name.substring(0, pos);
-        }
-        return null;
-    }
-
 }