Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.dsl.jbang.core.commands;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -582,6 +583,18 @@ public void postAddCommands(CommandTree commandLine, String[] args) {
// noop
}

/**
* The plugins that this CLI has built in (the Camel launcher embeds some), and that contribute to the given
* command, such as the test plugin, whose exporter adds the Citrus tests to camel export. The plugins of the plugin
* configuration are not among them.
*
* @param target the command, such as run or export
* @return the plugins, none by default
*/
public Collection<Plugin> getEmbeddedPlugins(String target) {
Comment thread
davsclaus marked this conversation as resolved.
Resolved
return List.of();
}

/**
* Called just before the command line is executed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.apache.camel.dsl.jbang.core.commands;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import org.apache.camel.dsl.jbang.core.commands.ai.YamlLoadFailureReport;
Expand Down Expand Up @@ -59,7 +61,14 @@ public static <T extends ExportContext> T configure(
T context, CamelJBangMain main, Printer printer, boolean yes, String target) {
context.withPrinter(printer)
.withPlugins(repos -> {
Collection<Plugin> plugins = PluginHelper.getActivePlugins(main, repos, target, true).values();
Map<String, Plugin> active = new LinkedHashMap<>(PluginHelper.getActivePlugins(main, repos, target, true));
// and the plugins built into the CLI, unless the plugin configuration has the same plugin
for (Plugin embedded : main.getEmbeddedPlugins(target)) {
if (active.values().stream().noneMatch(p -> p.getClass().equals(embedded.getClass()))) {
active.put(embedded.getClass().getName(), embedded);
}
}
Collection<Plugin> plugins = active.values();
return new ExportContext.ActivePlugins(
plugins.stream().map(Plugin::getRunCustomizer).flatMap(Optional::stream).toList(),
plugins.stream().map(Plugin::getExporter).flatMap(Optional::stream).toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.apache.camel.dsl.jbang.core.common.CommandTree;
import org.apache.camel.dsl.jbang.core.common.Plugin;
import org.apache.camel.dsl.jbang.run.common.PluginExporter;
import org.apache.camel.dsl.jbang.run.common.Printer;
import org.apache.camel.dsl.jbang.run.export.ExportContext;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* The plugins built into the CLI take part in camel export: the Camel launcher embeds the test plugin without a plugin
* configuration, so an export from the launcher had no Citrus tests (CAMEL-26933).
*/
class CliExportContextEmbeddedPluginsTest extends CamelCommandBaseTestSupport {

@Test
void theExportHasTheExporterOfAnEmbeddedPlugin() throws Exception {
useTestHomeDir();
ExporterPlugin plugin = new ExporterPlugin();
CamelJBangMain main = new CamelJBangMain() {
@Override
public Collection<Plugin> getEmbeddedPlugins(String target) {
return "export".equals(target) ? List.of(plugin) : List.of();
}
}.withPrinter(printer);

ExportContext.ActivePlugins plugins = CliExportContext.create(main, printer, true).getPlugins(null);

assertThat(plugins.exporters()).containsExactly(plugin.exporter);
}

@Test
void withoutEmbeddedPluginsTheExportHasThePluginsOfTheConfiguration() throws Exception {
useTestHomeDir();
CamelJBangMain main = new CamelJBangMain().withPrinter(printer);

assertThat(CliExportContext.create(main, printer, true).getPlugins(null).exporters()).isEmpty();
}

private static class ExporterPlugin implements Plugin {

final PluginExporter exporter = (Path buildDir, String packageName, Printer printer) -> {
};

@Override
public void customize(CommandTree commandLine, CamelJBangMain main) {
}

@Override
public Optional<PluginExporter> getExporter() {
return Optional.of(exporter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
*/
package org.apache.camel.dsl.jbang.launcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

Expand All @@ -28,6 +32,7 @@
import org.apache.camel.dsl.jbang.core.commands.validate.ValidatePlugin;
import org.apache.camel.dsl.jbang.core.common.CommandTree;
import org.apache.camel.dsl.jbang.core.common.Plugin;
import org.apache.camel.dsl.jbang.core.common.PluginType;
import org.apache.camel.dsl.jbang.launcher.selfupdate.SelfUpdatePlugin;
import org.apache.camel.dsl.jbang.launcher.selfupdate.UpdateChecker;

Expand Down Expand Up @@ -64,6 +69,25 @@ protected boolean isPluginCommand(String command) {
return PLUGINS.stream().anyMatch(p -> p.commands().contains(command)) || super.isPluginCommand(command);
}

@Override
public Collection<Plugin> getEmbeddedPlugins(String target) {
// such as the test plugin for camel export: the launcher does not register its plugins in the plugin
// configuration, so the export did not have the Citrus tests (CAMEL-26933)
List<Plugin> answer = new ArrayList<>();
for (EmbeddedPlugin embedded : PLUGINS) {
boolean contributes = embedded.commands().stream()
.map(PluginType::findByName)
.flatMap(Optional::stream)
.anyMatch(type -> Arrays.asList(type.getAppliesTo()).contains(target));
if (contributes) {
Plugin plugin = embedded.factory().get();
plugin.setClassLoader(getClass().getClassLoader());
answer.add(plugin);
}
}
return answer;
}

@Override
public void postAddCommands(CommandTree commandLine, String[] args) {
if (hasOnlyCommand(commandLine, args)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.launcher;

import org.apache.camel.dsl.jbang.core.commands.test.TestPlugin;
import org.apache.camel.dsl.jbang.core.common.Plugin;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* camel export from the launcher has the Citrus tests: the embedded test plugin contributes its exporter (CAMEL-26933).
*/
class CamelLauncherMainEmbeddedPluginsTest {

@Test
void theTestPluginContributesToExport() {
assertThat(new CamelLauncherMain().getEmbeddedPlugins("export"))
.hasSize(1)
.allSatisfy(plugin -> {
assertThat(plugin).isInstanceOf(TestPlugin.class);
assertThat(plugin.getExporter()).isPresent();
});
}

@Test
void noEmbeddedPluginContributesToRun() {
assertThat(new CamelLauncherMain().getEmbeddedPlugins("run")).extracting(Plugin::getClass)
.doesNotContain(TestPlugin.class.asSubclass(Plugin.class));
}
}
Loading