This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch abs-init in repository https://gitbox.apache.org/repos/asf/camel.git
commit 7c430f548e1fbeb704f968d760852282c81b997d Author: Claus Ibsen <[email protected]> AuthorDate: Wed Sep 18 16:40:19 2024 +0200 CAMEL-21085: camel-jbang - Init command to accept absolute file instead of using dir option. --- .../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 = "";
