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/05 08:39:24 UTC
(camel) 01/01: CAMEL-20186: camel-jbang - dependency update
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch dep-upd
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 6890b74d203de1f9b90b6801027ba92a4c15adb6
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Dec 5 09:39:04 2023 +0100
CAMEL-20186: camel-jbang - dependency update
---
.../modules/ROOT/pages/camel-jbang.adoc | 18 +++
.../dsl/jbang/core/commands/CamelJBangMain.java | 3 +-
.../dsl/jbang/core/commands/DependencyCopy.java | 2 +-
.../dsl/jbang/core/commands/DependencyList.java | 5 +-
.../dsl/jbang/core/commands/DependencyUpdate.java | 125 +++++++++++++++++++++
5 files changed, 149 insertions(+), 4 deletions(-)
diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index debd93fb05e..fc9e5a96e25 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -2063,6 +2063,8 @@ public class foo extends EndpointRouteBuilder {
}
----
+TIP: You can use `camel dependency update --source=foo.java` to update the dependencies.
+
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.
@@ -2076,6 +2078,22 @@ $ jbang edit -b foo.java
You can find this example at: https://github.com/apache/camel-kamelets-examples/tree/main/jbang/jbang-edit
+==== Updating dependencies in source code
+
+When working with Java source code, then you can keep the JBang dependencies up-to-date using the following command:
+
+[source,bash]
+----
+$ camel dependency update --source=foo.java
+----
+
+TIP: You can use `--clean` to not keep any existing dependencies and generate a clean fresh list.
+
+This will then automatic insert or update the JBang depencies (`//DEPS`) in the top of the source file.
+
+You may want to use this for making it easier to load the source into an IDE editor to do coding.
+See previous section for more details.
+
==== Camel route debugging using VSCode or IDEA editors
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index c0806c1baa7..c5a22e8c30c 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -134,7 +134,8 @@ public class CamelJBangMain implements Callable<Integer> {
.addSubcommand("gc", new CommandLine(new CamelGCAction(main))))
.addSubcommand("dependency", new CommandLine(new DependencyCommand(main))
.addSubcommand("list", new CommandLine(new DependencyList(main)))
- .addSubcommand("copy", new CommandLine(new DependencyCopy(main))))
+ .addSubcommand("copy", new CommandLine(new DependencyCopy(main)))
+ .addSubcommand("update", new CommandLine(new DependencyUpdate(main))))
.addSubcommand("generate", new CommandLine(new CodeGenerator(main))
.addSubcommand("rest", new CommandLine(new CodeRestGenerator(main))))
.addSubcommand("sbom", new CommandLine(new SBOMGenerator(main)))
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopy.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopy.java
index e27ded4c1ff..395b9a79a6b 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopy.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopy.java
@@ -66,7 +66,7 @@ public class DependencyCopy extends DependencyList {
}
@Override
- protected void outputGav(MavenGav gav, int index) {
+ protected void outputGav(MavenGav gav, int index, int total) {
try {
List<MavenArtifact> artifacts = getDownloader().resolveArtifacts(
List.of(gav.toString()), Set.of(), true, gav.getVersion().contains("SNAPSHOT"));
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java
index 919c3eadd10..a81dfa2d3bb 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java
@@ -144,8 +144,9 @@ public class DependencyList extends Export {
// sort GAVs
gavs.sort(mavenGavComparator());
int i = 0;
+ int total = gavs.size();
for (MavenGav gav : gavs) {
- outputGav(gav, i);
+ outputGav(gav, i, total);
i++;
}
}
@@ -156,7 +157,7 @@ public class DependencyList extends Export {
return answer;
}
- protected void outputGav(MavenGav gav, int index) {
+ protected void outputGav(MavenGav gav, int index, int total) {
if ("gav".equals(output)) {
System.out.println(gav);
} else if ("maven".equals(output)) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyUpdate.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyUpdate.java
new file mode 100644
index 00000000000..4603014c0bd
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyUpdate.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.camel.tooling.maven.MavenGav;
+import org.apache.camel.util.IOHelper;
+import picocli.CommandLine;
+
+@CommandLine.Command(name = "update",
+ description = "Updates JBang style dependencies in source file")
+public class DependencyUpdate extends DependencyList {
+
+ @CommandLine.Option(names = { "--source" },
+ description = "Camel source such as .java file to have dependencies updated (//DEPS)",
+ required = true)
+ protected String source;
+
+ @CommandLine.Option(names = { "--clean" },
+ description = "Regenerate list of dependencies (do not keep existing dependencies)")
+ protected boolean clean;
+
+ private final List<String> deps = new ArrayList<>();
+ private File target;
+
+ public DependencyUpdate(CamelJBangMain main) {
+ super(main);
+ }
+
+ @Override
+ public Integer doCall() throws Exception {
+ // source file must exists
+ target = new File(source);
+ if (!target.exists()) {
+ System.err.println("Source file does not exist: " + target);
+ return -1;
+ }
+
+ if (clean) {
+ // remove DEPS in source file first
+ updateSource();
+ }
+
+ return super.doCall();
+ }
+
+ @Override
+ protected void outputGav(MavenGav gav, int index, int total) {
+ if (index == 0) {
+ deps.add("//DEPS org.apache.camel:camel-bom:" + gav.getVersion() + "@pom");
+ }
+ if (gav.getGroupId().equals("org.apache.camel")) {
+ // jbang has version in @pom so we should remove this
+ gav.setVersion(null);
+ }
+ String line = "//DEPS " + gav;
+ if (!deps.contains(line)) {
+ deps.add(line);
+ }
+ boolean last = total - index <= 1;
+ if (last) {
+ updateSource();
+ }
+ }
+
+ private void updateSource() {
+ try {
+ List<String> lines = Files.readAllLines(target.toPath());
+ List<String> answer = new ArrayList<>();
+
+ // find position of where the old DEPS was
+ int pos = -1;
+ for (int i = 0; i < lines.size(); i++) {
+ String l = lines.get(i);
+ if (l.trim().startsWith("//DEPS ")) {
+ if (pos == -1) {
+ pos = i;
+ }
+ } else {
+ answer.add(l);
+ }
+ }
+ // add after shebang in top
+ if (pos == -1) {
+ if (answer.get(0).trim().startsWith("///usr/bin/env jbang")) {
+ pos = 1;
+ }
+ }
+ if (pos == -1) {
+ pos = 0;
+ }
+
+ // reverse collection as we insert pos based
+ Collections.reverse(deps);
+ for (String dep : deps) {
+ answer.add(pos, dep);
+ }
+
+ String text = String.join(System.lineSeparator(), answer);
+ IOHelper.writeText(text, target);
+ } catch (Exception e) {
+ System.err.println("Error updating source file: " + target + " due to: " + e.getMessage());
+ }
+ }
+
+}