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 b2e4861f683e CAMEL-26931: camel-jbang - camel init writes the right
file (#29929)
b2e4861f683e is described below
commit b2e4861f683e5f4340c107da9823d2236282722c
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 14 19:27:21 2026 +0100
CAMEL-26931: camel-jbang - camel init writes the right file (#29929)
The kind of a Kamelet came from the whole path, so a Kamelet under
src/main/resources
became a source Kamelet; a file given as ./x.yaml was written in the
current directory
whatever --dir said; and camel init my-route.java wrote a class that does
not compile,
with exit 0. The kind comes from the file name, the file is resolved in the
directory,
and a Java file not named like a class is a usage error.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/dsl/jbang/core/commands/Init.java | 23 +++++++-----
.../camel/dsl/jbang/core/commands/InitTest.java | 41 ++++++++++++++++++++++
2 files changed, 56 insertions(+), 8 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 6643e4dc292b..99670d6cca89 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
@@ -29,10 +29,13 @@ import java.util.Map;
import java.util.Scanner;
import java.util.StringJoiner;
+import javax.lang.model.SourceVersion;
+
import org.aesh.command.CommandDefinition;
import org.aesh.command.option.Argument;
import org.aesh.command.option.Option;
import org.apache.camel.CamelContext;
+import org.apache.camel.dsl.jbang.core.common.CliException;
import org.apache.camel.dsl.jbang.core.common.CliExitCode;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
import org.apache.camel.dsl.jbang.core.common.EnvironmentHelper;
@@ -144,6 +147,13 @@ public class Init extends CamelCommand {
}
String name = FileUtil.onlyName(file, false);
+ if ("java".equals(ext) && !SourceVersion.isName(name)) {
+ // the class of the file would not compile (CAMEL-26931:
my-route.java, 1bad.java)
+ throw CliException.usage(name + " is not a Java class name: name
the file like a class, such as MyRoute.java");
+ }
+ // the kind of a Kamelet from its file name, not the path:
src/main/resources/kamelets/my-filter.kamelet.yaml
+ // became a source, as resources contains source (CAMEL-26931)
+ String fileName = FileUtil.stripPath(file);
InputStream is = null;
if ("kamelet.yaml".equals(ext)) {
if (fromKamelet != null) {
@@ -152,9 +162,9 @@ public class Init extends CamelCommand {
}
// load existing kamelet
is = KameletCatalogHelper.loadKameletYamlSchema(fromKamelet,
kameletsVersion, repositories);
- } else if (file.contains("source")) {
+ } else if (fileName.contains("source")) {
ext = "kamelet-source.yaml";
- } else if (file.contains("sink")) {
+ } else if (fileName.contains("sink")) {
ext = "kamelet-sink.yaml";
} else {
ext = "kamelet-action.yaml";
@@ -193,9 +203,10 @@ public class Init extends CamelCommand {
Path dirPath = Paths.get(directory);
Files.createDirectories(dirPath);
}
+ // in the directory, also for a file given as ./x.yaml (CAMEL-26931:
it was written in the current directory)
Path targetPath = Paths.get(file);
if (!targetPath.isAbsolute()) {
- targetPath = Paths.get(directory, file);
+ targetPath = Paths.get(directory, file).normalize();
}
content = content.replace("{{ .Name }}", name);
content = content.replace("[=Name]", name);
@@ -226,11 +237,7 @@ public class Init extends CamelCommand {
// in case of using relative paths in the file name
Path parentPath = targetPath.getParent();
if (parentPath != null) {
- if (".".equals(parentPath.getFileName().toString())) {
- targetPath = Paths.get(file);
- } else {
- Files.createDirectories(parentPath);
- }
+ Files.createDirectories(parentPath);
}
if (existsAlready(targetPath)) {
return CliExitCode.FAILURE;
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/InitTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/InitTest.java
index 66c9d74bedf7..44c1c685a5c5 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/InitTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/InitTest.java
@@ -25,6 +25,8 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
+import org.apache.camel.dsl.jbang.core.common.CliException;
+import org.apache.camel.dsl.jbang.core.common.CliExitCode;
import org.apache.camel.dsl.jbang.core.common.CommandLineTestHelper;
import org.apache.camel.dsl.jbang.run.common.PathUtils;
import org.apache.camel.dsl.jbang.run.common.StringPrinter;
@@ -34,6 +36,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class InitTest extends CamelCommandBaseTestSupport {
@@ -79,6 +82,44 @@ class InitTest extends CamelCommandBaseTestSupport {
assertTrue(Files.exists(f), "Yaml file not created: " + f);
}
+ @Test
+ void aFileGivenWithTheCurrentFolderGoesInTheDirectory() throws Exception {
+ // ./my.camel.yaml was written in the current directory, whatever
--dir said (CAMEL-26931)
+ Init initCommand = new Init(new CamelJBangMain());
+ CommandLineTestHelper.parse(initCommand, "./my-dot.camel.yaml",
"--dir=" + workingDir);
+
+ assertEquals(0, initCommand.doCall());
+
+ assertTrue(Files.exists(workingDir.resolve("my-dot.camel.yaml")));
+ assertFalse(Files.exists(Paths.get("my-dot.camel.yaml")), "not in the
current directory");
+ }
+
+ @Test
+ void theKindOfAKameletComesFromItsFileNameNotItsFolder() throws Exception {
+ // resources contains source: the Kamelet was a source Kamelet
(CAMEL-26931)
+ Init initCommand = new Init(new CamelJBangMain());
+ CommandLineTestHelper.parse(initCommand,
"src/main/resources/kamelets/my-filter.kamelet.yaml",
+ "--dir=" + workingDir);
+
+ assertEquals(0, initCommand.doCall());
+
+ String kamelet =
Files.readString(workingDir.resolve("src/main/resources/kamelets/my-filter.kamelet.yaml"));
+ assertTrue(kamelet.contains("camel.apache.org/kamelet.type:
\"action\""), kamelet);
+ }
+
+ @Test
+ void aJavaFileMustBeNamedLikeAClass() {
+ // camel init my-route.java wrote a class that does not compile, with
exit code 0 (CAMEL-26931)
+ Init initCommand = new Init(new CamelJBangMain());
+ CommandLineTestHelper.parse(initCommand, "my-route.java", "--dir=" +
workingDir);
+
+ CliException e = assertThrows(CliException.class, initCommand::doCall);
+
+ assertEquals(CliExitCode.USAGE, e.getExitCode());
+ assertEquals("my-route is not a Java class name: name the file like a
class, such as MyRoute.java", e.getMessage());
+ assertFalse(Files.exists(workingDir.resolve("my-route.java")));
+ }
+
@Test
void initYamlInDirectoryWithExistingFiles() throws Exception {
Path existingFileInTargetDirectory =
Files.createFile(workingDir.resolve("anotherfile.txt"));