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 2023/12/04 17:31:02 UTC
(camel) branch main updated: CAMEL-20165: camel-jbang - Should we have modeline for JBang style of DEPS
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 11f9c1e9244 CAMEL-20165: camel-jbang - Should we have modeline for JBang style of DEPS
11f9c1e9244 is described below
commit 11f9c1e924420e150c5c248951aac6461be22db6
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Dec 4 18:30:49 2023 +0100
CAMEL-20165: camel-jbang - Should we have modeline for JBang style of DEPS
---
.../modules/ROOT/pages/camel-jbang.adoc | 59 ++++++++++++++++++++++
.../src/main/docs/dsl-modeline.adoc | 6 +++
.../camel/dsl/modeline/DefaultModelineParser.java | 23 ++++++++-
.../camel/dsl/modeline/ModelineParserTest.java | 30 +++++++++++
4 files changed, 116 insertions(+), 2 deletions(-)
diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index 331ca43fa1d..debd93fb05e 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -2017,6 +2017,65 @@ To set a breakpoint at line 18 and 34
camel debug hello.yaml --breakpoint=18,34
----
+==== Editing code using VSCode or IDEA editors
+
+You can use JBang to edit the source code by using the `jbang` CLI to download dependencies
+and setup project for being ready to load into an IDE of choice, such as IDEA, VSCode, or Eclipse.
+
+This is typically in use when you use Java DSL and have Camel routes in .java source code.
+To let JBang understand which dependencies are in use, then you need to use JBang style for specifying
+dependencies by using `//DEPS` code comments in Java code.
+
+Given the following source file in `foo.java` file:
+
+[source,java]
+----
+//DEPS org.apache.camel:camel-bom:4.3.0@pom
+//DEPS org.apache.camel:camel-endpointdsl
+//DEPS org.apache.camel:camel-netty-http
+//DEPS org.apache.camel:camel-stream
+
+// add more dependencies here
+
+import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
+import org.apache.camel.component.netty.http.NettyHttpMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class foo extends EndpointRouteBuilder {
+
+ private static final Logger LOG = LoggerFactory.getLogger(foo.class);
+
+ @Override
+ public void configure() {
+ from(timer("trigger").period(5000).repeatCount(3))
+ .to(nettyHttp("https://random-data-api.com/api/v2/banks").keepAlive(true))
+ .process(e -> {
+ // use classes from camel-netty-http dependency in the source code
+ // and have jbang able to generate project with the dependencies ready
+ // to use in your IDE of choice
+ NettyHttpMessage msg = e.getMessage(NettyHttpMessage.class);
+ LOG.info("Netty HTTP response:\n\n\n{}\n\n\n", msg.getHttpResponse());
+ })
+ .log("Found bank:")
+ .to(stream("out"));
+ }
+}
+----
+
+Notice how we in the top of the file specify https://www.jbang.dev/documentation/guide/latest/dependencies.html[JBang dependencies] we want JBang to know and prepare for the IDE of choice.
+The first `//DEPS` is the `@pom` which set up the Camel version to use.
+The following `//DEPS` declares the Camel component we use.
+
+You can now open this source file for editing in your IDE of choice by using jbang CLI as follows:
+
+[source,bash]
+----
+$ jbang edit -b foo.java
+----
+
+You can find this example at: https://github.com/apache/camel-kamelets-examples/tree/main/jbang/jbang-edit
+
==== Camel route debugging using VSCode or IDEA editors
diff --git a/dsl/camel-dsl-modeline/src/main/docs/dsl-modeline.adoc b/dsl/camel-dsl-modeline/src/main/docs/dsl-modeline.adoc
index 912058f26a7..89f8d7fd86c 100644
--- a/dsl/camel-dsl-modeline/src/main/docs/dsl-modeline.adoc
+++ b/dsl/camel-dsl-modeline/src/main/docs/dsl-modeline.adoc
@@ -11,6 +11,8 @@
*Since Camel {since}*
+== Camel K
+
Support for Camel K style modeline when running Camel standalone such as with Camel JBang.
The following traits is supported:
@@ -19,3 +21,7 @@ The following traits is supported:
- env
- name
- property
+
+== Camel JBang
+
+There is also support for https://www.jbang.dev/documentation/guide/latest/dependencies.html[JBang dependencies] using the `//DEPS` comments style.
diff --git a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/DefaultModelineParser.java b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/DefaultModelineParser.java
index 3a9e61a3448..7d2569d93b4 100644
--- a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/DefaultModelineParser.java
+++ b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/DefaultModelineParser.java
@@ -31,6 +31,7 @@ import org.apache.camel.util.StringQuoteHelper;
public class DefaultModelineParser implements ModelineParser {
public static final String MODELINE_START = "camel-k:";
+ public static final String JBANG_DEPS_START = "//DEPS";
private final Map<String, Trait> traits = new HashMap<>();
@@ -94,6 +95,24 @@ public class DefaultModelineParser implements ModelineParser {
}
}
+ if (line.startsWith(JBANG_DEPS_START)) {
+ line = line.substring(JBANG_DEPS_START.length()).trim();
+ line = line.trim();
+ Trait dep = traits.get("dependency");
+ String[] parts = StringQuoteHelper.splitSafeQuote(line, ' ', false);
+ for (String part : parts) {
+ part = part.trim();
+ if (part.endsWith("@pom")) {
+ // skip @pom
+ continue;
+ }
+ CamelContextCustomizer customizer = dep.parseTrait(resource, part);
+ if (customizer != null) {
+ answer.add(customizer);
+ }
+ }
+ }
+
return answer;
}
@@ -109,7 +128,7 @@ public class DefaultModelineParser implements ModelineParser {
return false;
}
line = removeLeadingComments(line);
- return line.startsWith(MODELINE_START);
+ return line.startsWith(MODELINE_START) || line.startsWith(JBANG_DEPS_START);
}
private static String removeLeadingComments(String line) {
@@ -118,7 +137,7 @@ public class DefaultModelineParser implements ModelineParser {
}
line = line.trim();
- while (line.startsWith("/") || line.startsWith("#")) {
+ while (!line.startsWith(JBANG_DEPS_START) && line.startsWith("/") || line.startsWith("#")) {
line = line.substring(1);
}
diff --git a/dsl/camel-dsl-modeline/src/test/java/org/apache/camel/dsl/modeline/ModelineParserTest.java b/dsl/camel-dsl-modeline/src/test/java/org/apache/camel/dsl/modeline/ModelineParserTest.java
index fcc8fc3eece..941336521f7 100644
--- a/dsl/camel-dsl-modeline/src/test/java/org/apache/camel/dsl/modeline/ModelineParserTest.java
+++ b/dsl/camel-dsl-modeline/src/test/java/org/apache/camel/dsl/modeline/ModelineParserTest.java
@@ -81,6 +81,20 @@ public class ModelineParserTest extends CamelTestSupport {
Assertions.assertEquals("mvn:org.my:application:1.0", deps.get(0));
}
+ @Test
+ public void testModelineSingleJBangDependency() throws Exception {
+ context.start();
+
+ Assertions.assertEquals(0, deps.size());
+
+ String line = "//DEPS org.my:application:1.0";
+ ModelineFactory factory = resolveModelineFactory(context);
+ factory.parseModeline(ResourceHelper.fromString(null, line));
+
+ Assertions.assertEquals(1, deps.size());
+ Assertions.assertEquals("org.my:application:1.0", deps.get(0));
+ }
+
@Test
public void testModelineMultiDependency() throws Exception {
context.start();
@@ -97,6 +111,22 @@ public class ModelineParserTest extends CamelTestSupport {
Assertions.assertEquals("mvn:com.foo:myapp:2.1", deps.get(1));
}
+ @Test
+ public void testModelineMultiJBangDependency() throws Exception {
+ context.start();
+ deps.clear();
+
+ Assertions.assertEquals(0, deps.size());
+
+ String line = "//DEPS org.my:application:1.0 com.foo:myapp:2.1";
+ ModelineFactory factory = resolveModelineFactory(context);
+ factory.parseModeline(ResourceHelper.fromString(null, line));
+
+ Assertions.assertEquals(2, deps.size());
+ Assertions.assertEquals("org.my:application:1.0", deps.get(0));
+ Assertions.assertEquals("com.foo:myapp:2.1", deps.get(1));
+ }
+
@Test
public void testModelineSingleProperty() throws Exception {
context.start();