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

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


The following commit(s) were added to refs/heads/camel-3.20.x by this push:
     new 9be9d94b3d0 CAMEL-19122: camel-jbang - Export java code with existing 
package name
9be9d94b3d0 is described below

commit 9be9d94b3d02e06e2e426755779db969dccde364
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Tue Mar 7 08:09:35 2023 +0100

    CAMEL-19122: camel-jbang - Export java code with existing package name
---
 .../dsl/jbang/core/commands/ExportBaseCommand.java | 42 ++++++++++++++++++----
 .../dsl/jbang/core/commands/ExportCamelMain.java   | 11 +++---
 .../dsl/jbang/core/commands/ExportQuarkus.java     |  5 +--
 .../dsl/jbang/core/commands/ExportSpringBoot.java  |  5 +--
 4 files changed, 48 insertions(+), 15 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 4016c4cc556..9dd56f0562f 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
@@ -18,6 +18,7 @@ package org.apache.camel.dsl.jbang.core.commands;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
@@ -27,10 +28,13 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.function.Function;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 import org.apache.camel.catalog.DefaultCamelCatalog;
@@ -56,6 +60,9 @@ abstract class ExportBaseCommand extends CamelCommand {
             "camel.jbang.localKameletDir"
     };
 
+    private static final Pattern PACKAGE_PATTERN = Pattern.compile(
+            "^\\s*package\\s+([a-zA-Z][.\\w]*)\\s*;.*$", Pattern.MULTILINE);
+
     @CommandLine.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.")
     protected String profile;
@@ -291,7 +298,7 @@ abstract class ExportBaseCommand extends CamelCommand {
     }
 
     protected void copySourceFiles(
-            File settings, File profile, File srcJavaDir, File 
srcResourcesDir, File srcCamelResourcesDir,
+            File settings, File profile, File srcJavaDirRoot, File srcJavaDir, 
File srcResourcesDir, File srcCamelResourcesDir,
             String packageName)
             throws Exception {
         // read the settings file and find the files to copy
@@ -323,13 +330,28 @@ abstract class ExportBaseCommand extends CamelCommand {
                     } else {
                         out = new File(target, source.getName());
                     }
-                    safeCopy(source, out, true);
-                    if (java) {
+                    if (!java) {
+                        safeCopy(source, out, true);
+                    } else {
                         // need to append package name in java source file
-                        List<String> lines = Files.readAllLines(out.toPath());
-                        lines.add(0, "");
-                        lines.add(0, "package " + packageName + ";");
-                        FileOutputStream fos = new FileOutputStream(out);
+                        List<String> lines = 
Files.readAllLines(source.toPath());
+                        Optional<String> hasPackage = lines.stream().filter(l 
-> l.trim().startsWith("package ")).findFirst();
+                        FileOutputStream fos;
+                        if (hasPackage.isPresent()) {
+                            String pn = determinePackageName(hasPackage.get());
+                            if (pn != null) {
+                                File dir = new File(srcJavaDirRoot, 
pn.replace('.', File.separatorChar));
+                                dir.mkdirs();
+                                out = new File(dir, source.getName());
+                                fos = new FileOutputStream(out);
+                            } else {
+                                throw new IOException("Cannot determine 
package name from source: " + source);
+                            }
+                        } else {
+                            fos = new FileOutputStream(out);
+                            lines.add(0, "");
+                            lines.add(0, "package " + packageName + ";");
+                        }
                         for (String line : lines) {
                             adjustJavaSourceFileLine(line, fos);
                             fos.write(line.getBytes(StandardCharsets.UTF_8));
@@ -541,4 +563,10 @@ abstract class ExportBaseCommand extends CamelCommand {
             Files.copy(source, target.toPath());
         }
     }
+
+    private static String determinePackageName(String content) {
+        final Matcher matcher = PACKAGE_PATTERN.matcher(content);
+        return matcher.find() ? matcher.group(1) : null;
+    }
+
 }
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 73e6c079f52..3303b0c8472 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
@@ -84,13 +84,14 @@ class ExportCamelMain extends Export {
 
         // copy source files
         String packageName = exportPackageName(ids[0], ids[1]);
-        File srcJavaDir = new File(BUILD_DIR, "src/main/java/" + 
packageName.replace('.', '/'));
+        File srcJavaDirRoot = new File(BUILD_DIR, "src/main/java");
+        File srcJavaDir = new File(srcJavaDirRoot, packageName.replace('.', 
File.separatorChar));
         srcJavaDir.mkdirs();
         File srcResourcesDir = new File(BUILD_DIR, "src/main/resources");
         srcResourcesDir.mkdirs();
         File srcCamelResourcesDir = new File(BUILD_DIR, 
"src/main/resources/camel");
         srcCamelResourcesDir.mkdirs();
-        copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, 
srcCamelResourcesDir, packageName);
+        copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, 
srcResourcesDir, srcCamelResourcesDir, packageName);
         // copy from settings to profile
         copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
             if (!prop.containsKey("camel.main.basePackageScan") && 
!prop.containsKey("camel.main.base-package-scan")) {
@@ -234,10 +235,12 @@ class ExportCamelMain extends Export {
 
     @Override
     protected void copySourceFiles(
-            File settings, File profile, File srcJavaDir, File 
srcResourcesDir, File srcCamelResourcesDir, String packageName)
+            File settings, File profile, File srcJavaDirRoot, File srcJavaDir, 
File srcResourcesDir, File srcCamelResourcesDir,
+            String packageName)
             throws Exception {
 
-        super.copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, 
srcCamelResourcesDir, packageName);
+        super.copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, 
srcResourcesDir, srcCamelResourcesDir,
+                packageName);
 
         // log4j configuration
         InputStream is = 
ExportCamelMain.class.getResourceAsStream("/log4j2.properties");
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 bdfcda47383..4c1010eaafe 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
@@ -105,13 +105,14 @@ class ExportQuarkus extends Export {
 
         // copy source files
         String packageName = exportPackageName(ids[0], ids[1]);
-        File srcJavaDir = new File(BUILD_DIR, "src/main/java/" + 
packageName.replace('.', '/'));
+        File srcJavaDirRoot = new File(BUILD_DIR, "src/main/java");
+        File srcJavaDir = new File(srcJavaDirRoot, packageName.replace('.', 
File.separatorChar));
         srcJavaDir.mkdirs();
         File srcResourcesDir = new File(BUILD_DIR, "src/main/resources");
         srcResourcesDir.mkdirs();
         File srcCamelResourcesDir = new File(BUILD_DIR, 
"src/main/resources/camel");
         srcCamelResourcesDir.mkdirs();
-        copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, 
srcCamelResourcesDir, packageName);
+        copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, 
srcResourcesDir, srcCamelResourcesDir, packageName);
         // copy from settings to profile
         copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
             if (!hasModeline(settings)) {
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 ad1f94149ad..b956a4aac63 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
@@ -95,13 +95,14 @@ class ExportSpringBoot extends Export {
 
         // copy source files
         String packageName = exportPackageName(ids[0], ids[1]);
-        File srcJavaDir = new File(BUILD_DIR, "src/main/java/" + 
packageName.replace('.', '/'));
+        File srcJavaDirRoot = new File(BUILD_DIR, "src/main/java");
+        File srcJavaDir = new File(srcJavaDirRoot, packageName.replace('.', 
File.separatorChar));
         srcJavaDir.mkdirs();
         File srcResourcesDir = new File(BUILD_DIR, "src/main/resources");
         srcResourcesDir.mkdirs();
         File srcCamelResourcesDir = new File(BUILD_DIR, 
"src/main/resources/camel");
         srcCamelResourcesDir.mkdirs();
-        copySourceFiles(settings, profile, srcJavaDir, srcResourcesDir, 
srcCamelResourcesDir, packageName);
+        copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, 
srcResourcesDir, srcCamelResourcesDir, packageName);
         // copy from settings to profile
         copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
             if (!hasModeline(settings)) {

Reply via email to