christophd commented on code in PR #29663:
URL: https://github.com/apache/camel/pull/29663#discussion_r4546986946
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java:
##########
@@ -361,9 +361,25 @@ public static Map<String, Plugin>
getActivePlugins(CamelJBangMain main, String r
final String firstVersion =
properties.getOrDefault("firstVersion", "").toString();
final String gav = properties.getOrDefault("dependency",
"").toString();
+ // auto-migrate old config entries for known plugins that now
declare appliesTo
+ if (properties.get("appliesTo") == null) {
+ Optional<PluginType> knownType =
PluginType.findByName(name);
+ if (knownType.isPresent() &&
knownType.get().getAppliesTo().length > 0) {
+ properties.put("appliesTo",
toJsonArray(knownType.get().getAppliesTo()));
+ configDirty = true;
+ main.getOut().printWarn(
+ ("Plugin '%s' configuration updated with
'appliesTo' filter. "
+ + "Run 'camel plugin add %s' to refresh the
full configuration.")
+ .formatted(name, name));
+ }
+ }
+
// skip plugins that don't match the target command to avoid
unnecessary downloads
if (target != null && !"shell".equals(target) &&
!target.equals(command)) {
- continue;
+ Object appliesToObj = properties.get("appliesTo");
+ if (!(appliesToObj instanceof Collection<?> appliesToList)
|| !appliesToList.contains(target)) {
+ continue;
+ }
}
Review Comment:
Done in the third commit — reworked the filtering: missing `appliesTo` is
now treated as legacy (load for all built-in commands). `effectiveAppliesTo()`
checks config → resolved block → PluginType enum → null (legacy). Filtering
only applies when `appliesTo` is explicitly present. Third-party plugins like
Forage continue to load for run/export unchanged.
_Claude Code on behalf of Christoph Deppisch_
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java:
##########
@@ -361,9 +361,25 @@ public static Map<String, Plugin>
getActivePlugins(CamelJBangMain main, String r
final String firstVersion =
properties.getOrDefault("firstVersion", "").toString();
final String gav = properties.getOrDefault("dependency",
"").toString();
+ // auto-migrate old config entries for known plugins that now
declare appliesTo
+ if (properties.get("appliesTo") == null) {
+ Optional<PluginType> knownType =
PluginType.findByName(name);
+ if (knownType.isPresent() &&
knownType.get().getAppliesTo().length > 0) {
+ properties.put("appliesTo",
toJsonArray(knownType.get().getAppliesTo()));
+ configDirty = true;
+ main.getOut().printWarn(
+ ("Plugin '%s' configuration updated with
'appliesTo' filter. "
+ + "Run 'camel plugin add %s' to refresh the
full configuration.")
+ .formatted(name, name));
Review Comment:
Done in the third commit — auto-migration block and warning removed
entirely. `PluginType.getAppliesTo()` is merged at runtime by
`effectiveAppliesTo()` without writing to config or printing warnings.
_Claude Code on behalf of Christoph Deppisch_
##########
dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginFilteringTest.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * 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.common;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import org.aesh.command.Command;
+import org.aesh.command.CommandResult;
+import org.aesh.command.invocation.CommandInvocation;
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.commands.CliExportContext;
+import org.apache.camel.dsl.jbang.run.common.StringPrinter;
+import org.apache.camel.dsl.jbang.run.export.ExportContext;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * Tests that plugins not relevant to the current command are skipped entirely
— not downloaded, not customized.
+ * <p>
+ * Each test configures cached fake plugins (so no Maven download is needed).
To detect whether a plugin was loaded or
+ * skipped, some tests use a "broken" plugin entry whose cache points to a
non-existent jar: if the resolution code
+ * attempts to load the broken plugin, it will fail with a {@link
CliException}. If the plugin is properly filtered out,
+ * the resolution succeeds.
+ */
+class PluginFilteringTest {
+
+ @TempDir
+ Path tempDir;
+
+ private Path previousHome;
+ private String camelVersion;
+
+ @BeforeEach
+ void setup() {
+ previousHome = CommandLineHelper.getHomeDir();
+ CommandLineHelper.useHomeDir(tempDir.toString());
+ camelVersion = new
org.apache.camel.catalog.DefaultCamelCatalog().getCatalogVersion();
+ }
+
+ @AfterEach
+ void restoreHome() {
+ CommandLineHelper.useHomeDir(previousHome.toString());
+ }
+
+ // ---- Target-based filtering in getActivePlugins ----
+
+ @Test
+ void pluginNotMatchingTargetIsNotLoaded() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "alpha");
+
+ assertThat(plugins).containsKey("alpha")
+ .doesNotContainKey("beta");
+ }
+
+ @Test
+ void nullTargetLoadsAllPlugins() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, null);
+
+ assertThat(plugins).containsKey("alpha")
+ .containsKey("beta");
+ }
+
+ @Test
+ void shellTargetLoadsAllPlugins() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "shell");
+
+ assertThat(plugins).containsKey("alpha")
+ .containsKey("beta");
+ }
+
+ // ---- The "run" command should not load unrelated plugins ----
+
+ @Test
+ void runCommandDoesNotLoadUnrelatedPlugin() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "run");
+
+ assertThat(plugins).doesNotContainKey("alpha")
+ .doesNotContainKey("beta");
+ }
+
+ // ---- The "export" command should not load unrelated plugins ----
+
+ @Test
+ void exportCommandDoesNotLoadUnrelatedPlugin() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "export");
+
+ assertThat(plugins).doesNotContainKey("alpha")
+ .doesNotContainKey("beta");
+ }
+
+ // ---- A broken unrelated plugin should not break the "run" target ----
+
+ @Test
+ void brokenUnrelatedPluginDoesNotBreakTargetedResolution() throws
Exception {
+ Path goodJar = tempDir.resolve("fake-good.jar");
+ FakePluginJar.write(goodJar, "good");
+
+ writeConfig(
+ buildEntry("good", camelVersion, goodJar,
Files.getLastModifiedTime(goodJar).toMillis()),
+ buildBrokenEntry("broken"));
+
+ CamelJBangMain main = new CamelJBangMain();
+
+ // targeting "good" should skip the broken plugin entirely
+ assertThatCode(() -> PluginHelper.getActivePlugins(main, null, "good"))
+ .doesNotThrowAnyException();
+
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "good");
+ assertThat(plugins).containsKey("good")
+ .doesNotContainKey("broken");
+ }
+
+ @Test
+ void brokenUnrelatedPluginBreaksNullTarget() throws Exception {
+ Path goodJar = tempDir.resolve("fake-good.jar");
+ FakePluginJar.write(goodJar, "good");
+
+ writeConfig(
+ buildEntry("good", camelVersion, goodJar,
Files.getLastModifiedTime(goodJar).toMillis()),
+ buildBrokenEntry("broken"));
+
+ CamelJBangMain main = new CamelJBangMain();
+
+ // null target loads ALL plugins, so the broken one causes a failure
+ assertThatThrownBy(() -> PluginHelper.getActivePlugins(main, null,
null))
+ .isInstanceOf(CliException.class)
+ .hasMessageContaining("camel-jbang-plugin-broken not found");
+ }
+
+ // ---- appliesTo filtering ----
+
+ @Test
+ void pluginWithAppliesToIsLoadedForMatchingTarget() throws Exception {
+ Path jar = tempDir.resolve("fake-exporter.jar");
+ FakePluginJar.write(jar, "exporter");
+
+ JsonObject entry = buildEntry("exporter", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ entry.put("appliesTo", jsonArray("export"));
+ writeConfig(entry);
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "export");
+
+ assertThat(plugins).containsKey("exporter");
+ }
+
+ @Test
+ void pluginWithAppliesToIsNotLoadedForNonMatchingTarget() throws Exception
{
+ Path jar = tempDir.resolve("fake-exporter.jar");
+ FakePluginJar.write(jar, "exporter");
+
+ JsonObject entry = buildEntry("exporter", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ entry.put("appliesTo", jsonArray("export"));
+ writeConfig(entry);
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "run");
+
+ assertThat(plugins).doesNotContainKey("exporter");
+ }
+
+ @Test
+ void pluginWithAppliesToExportIsLoadedByCliExportContext() throws
Exception {
+ Path jar = tempDir.resolve("fake-exporter.jar");
+ FakePluginJar.write(jar, "exporter");
+
+ JsonObject entry = buildEntry("exporter", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ entry.put("appliesTo", jsonArray("export"));
+ writeConfig(entry);
+
+ StringPrinter printer = new StringPrinter();
+ CamelJBangMain main = new CamelJBangMain().withPrinter(printer);
+
+ ExportContext context = CliExportContext.create(main, printer, true);
+
+ assertThatCode(() -> context.getPlugins(null))
+ .doesNotThrowAnyException();
Review Comment:
Done in the third commit — now uses a broken entry with `appliesTo:
["export"]` and `assertThatThrownBy(CliException.class)` to prove inclusion.
_Claude Code on behalf of Christoph Deppisch_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]