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 96322c49843 CAMEL-21085: camel-jbang - Init command to accept absolute 
file instead of using dir option. (#15612)
96322c49843 is described below

commit 96322c49843c8982712651cce34b7709e39875c6
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Sep 18 17:27:01 2024 +0200

    CAMEL-21085: camel-jbang - Init command to accept absolute file instead of 
using dir option. (#15612)
---
 .../apache/camel/dsl/jbang/core/commands/Init.java    | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
index f6a202a5544..7cefb356a66 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
@@ -158,7 +158,10 @@ public class Init extends CamelCommand {
             File dir = new File(directory);
             dir.mkdirs();
         }
-        File target = new File(directory, file);
+        File target = new File(file);
+        if (!target.isAbsolute()) {
+            target = new File(directory, file);
+        }
         content = content.replaceFirst("\\{\\{ \\.Name }}", name);
         if (fromKamelet != null) {
             content = content.replaceFirst("\\s\\sname:\\s" + fromKamelet, "  
name: " + name);
@@ -183,15 +186,21 @@ public class Init extends CamelCommand {
             String packageDeclaration = computeJavaPackageDeclaration(target);
             content = content.replaceFirst("\\{\\{ \\.PackageDeclaration }}", 
packageDeclaration);
         }
+        // in case of using relative paths in the file name
+        File p = target.getParentFile();
+        if (p != null) {
+            if (".".equals(p.getName())) {
+                target = new File(file);
+            } else {
+                p.mkdirs();
+            }
+        }
         IOHelper.writeText(content, new FileOutputStream(target, false));
         return 0;
     }
 
     /**
-     * @param  target
-     * @return             The package declaration lines to insert at the 
beginning of the file or empty string if no
-     *                     package found
-     * @throws IOException
+     * @return The package declaration lines to insert at the beginning of the 
file or empty string if no package found
      */
     private String computeJavaPackageDeclaration(File target) throws 
IOException {
         String packageDeclaration = "";

Reply via email to