mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Order repo info extensions (#2041)
Set order priority for repository information extensions. Also add new annotation to set custom resource bundles for cli commands.
This commit is contained in:
4
gradle/changelog/order_repo_info_ext.yaml
Normal file
4
gradle/changelog/order_repo_info_ext.yaml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
- type: changed
|
||||||
|
description: Set order priority for repository information extensions ([#2041](https://github.com/scm-manager/scm-manager/pull/2041))
|
||||||
|
- type: fixed
|
||||||
|
description: Resource bundle loading from plugins ([#2041](https://github.com/scm-manager/scm-manager/pull/2041))
|
||||||
46
scm-core/src/main/java/sonia/scm/cli/CliResourceBundle.java
Normal file
46
scm-core/src/main/java/sonia/scm/cli/CliResourceBundle.java
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sonia.scm.cli;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a custom resource bundle for the CLI command
|
||||||
|
*
|
||||||
|
* We need to use this workaround instead the picocli way because we cannot ensure that the resource bundles can be found by the classloader.
|
||||||
|
* Currently there is no solution for picocli to chose which classloader should be used to load command-related resource bundle.
|
||||||
|
*
|
||||||
|
* @since 2.35.0
|
||||||
|
*/
|
||||||
|
@Documented
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface CliResourceBundle {
|
||||||
|
String value();
|
||||||
|
}
|
||||||
@@ -22,23 +22,17 @@
|
|||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import "@scm-manager/ui-tests/i18n";
|
import "@scm-manager/ui-tests";
|
||||||
|
import { Repository } from "@scm-manager/ui-types";
|
||||||
import { gitPredicate } from "./index";
|
import { gitPredicate } from "./index";
|
||||||
|
|
||||||
|
const repository: Repository = { _links: {}, namespace: "hitchhiker", name: "HeartOfGold", type: "git" };
|
||||||
|
|
||||||
describe("test git predicate", () => {
|
describe("test git predicate", () => {
|
||||||
it("should return false", () => {
|
it("should return false", () => {
|
||||||
expect(gitPredicate(undefined)).toBe(false);
|
|
||||||
expect(gitPredicate({})).toBe(false);
|
|
||||||
expect(
|
expect(
|
||||||
gitPredicate({
|
gitPredicate({
|
||||||
repository: {},
|
repository: { ...repository, type: "hg" },
|
||||||
})
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
gitPredicate({
|
|
||||||
repository: {
|
|
||||||
type: "hg",
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
).toBe(false);
|
).toBe(false);
|
||||||
});
|
});
|
||||||
@@ -46,9 +40,7 @@ describe("test git predicate", () => {
|
|||||||
it("should return true", () => {
|
it("should return true", () => {
|
||||||
expect(
|
expect(
|
||||||
gitPredicate({
|
gitPredicate({
|
||||||
repository: {
|
repository,
|
||||||
type: "git",
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
).toBe(true);
|
).toBe(true);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -36,15 +36,14 @@ import GitTagInformation from "./GitTagInformation";
|
|||||||
// repository
|
// repository
|
||||||
|
|
||||||
// @visibleForTesting
|
// @visibleForTesting
|
||||||
export const gitPredicate = (props: any) => {
|
export const gitPredicate = (props: extensionPoints.RepositoryDetailsInformation["props"]) => {
|
||||||
return !!(props && props.repository && props.repository.type === "git");
|
return !!(props && props.repository && props.repository.type === "git");
|
||||||
};
|
};
|
||||||
|
|
||||||
binder.bind<extensionPoints.RepositoryDetailsInformation>(
|
binder.bind<extensionPoints.RepositoryDetailsInformation>("repos.repository-details.information", ProtocolInformation, {
|
||||||
"repos.repository-details.information",
|
predicate: gitPredicate,
|
||||||
ProtocolInformation,
|
priority: 100,
|
||||||
gitPredicate
|
});
|
||||||
);
|
|
||||||
binder.bind("repos.branch-details.information", GitBranchInformation, { priority: 100, predicate: gitPredicate });
|
binder.bind("repos.branch-details.information", GitBranchInformation, { priority: 100, predicate: gitPredicate });
|
||||||
|
|
||||||
binder.bind<extensionPoints.RepositoryTagDetailsInformation>(
|
binder.bind<extensionPoints.RepositoryTagDetailsInformation>(
|
||||||
|
|||||||
@@ -31,15 +31,14 @@ import HgBranchInformation from "./HgBranchInformation";
|
|||||||
import HgTagInformation from "./HgTagInformation";
|
import HgTagInformation from "./HgTagInformation";
|
||||||
import HgRepositoryConfigurationForm from "./HgRepositoryConfigurationForm";
|
import HgRepositoryConfigurationForm from "./HgRepositoryConfigurationForm";
|
||||||
|
|
||||||
const hgPredicate = (props: any) => {
|
const hgPredicate = (props: extensionPoints.RepositoryDetailsInformation["props"]) => {
|
||||||
return props.repository && props.repository.type === "hg";
|
return props.repository && props.repository.type === "hg";
|
||||||
};
|
};
|
||||||
|
|
||||||
binder.bind<extensionPoints.RepositoryDetailsInformation>(
|
binder.bind<extensionPoints.RepositoryDetailsInformation>("repos.repository-details.information", ProtocolInformation, {
|
||||||
"repos.repository-details.information",
|
predicate: hgPredicate,
|
||||||
ProtocolInformation,
|
priority: 100,
|
||||||
hgPredicate
|
});
|
||||||
);
|
|
||||||
binder.bind("repos.branch-details.information", HgBranchInformation, { priority: 100, predicate: hgPredicate });
|
binder.bind("repos.branch-details.information", HgBranchInformation, { priority: 100, predicate: hgPredicate });
|
||||||
|
|
||||||
binder.bind<extensionPoints.RepositoryTagDetailsInformation>(
|
binder.bind<extensionPoints.RepositoryTagDetailsInformation>(
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ package sonia.scm.cli;
|
|||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
import picocli.AutoComplete;
|
import picocli.AutoComplete;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
import sonia.scm.plugin.PluginLoader;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@@ -34,16 +35,19 @@ import java.util.ResourceBundle;
|
|||||||
|
|
||||||
public class CliProcessor {
|
public class CliProcessor {
|
||||||
|
|
||||||
|
private static final String CORE_RESOURCE_BUNDLE = "sonia.scm.cli.i18n";
|
||||||
private final CommandRegistry registry;
|
private final CommandRegistry registry;
|
||||||
private final Injector injector;
|
private final Injector injector;
|
||||||
private final CommandLine.Model.CommandSpec usageHelp;
|
private final CommandLine.Model.CommandSpec usageHelp;
|
||||||
private final CliExceptionHandlerFactory exceptionHandlerFactory;
|
private final CliExceptionHandlerFactory exceptionHandlerFactory;
|
||||||
|
private final PluginLoader pluginLoader;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public CliProcessor(CommandRegistry registry, Injector injector, CliExceptionHandlerFactory exceptionHandlerFactory) {
|
public CliProcessor(CommandRegistry registry, Injector injector, CliExceptionHandlerFactory exceptionHandlerFactory, PluginLoader pluginLoader) {
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.injector = injector;
|
this.injector = injector;
|
||||||
this.exceptionHandlerFactory = exceptionHandlerFactory;
|
this.exceptionHandlerFactory = exceptionHandlerFactory;
|
||||||
|
this.pluginLoader = pluginLoader;
|
||||||
this.usageHelp = new CommandLine(HelpMixin.class).getCommandSpec();
|
this.usageHelp = new CommandLine(HelpMixin.class).getCommandSpec();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +55,7 @@ public class CliProcessor {
|
|||||||
CommandFactory factory = new CommandFactory(injector, context);
|
CommandFactory factory = new CommandFactory(injector, context);
|
||||||
CommandLine cli = new CommandLine(ScmManagerCommand.class, factory);
|
CommandLine cli = new CommandLine(ScmManagerCommand.class, factory);
|
||||||
cli.getCommandSpec().addMixin("help", usageHelp);
|
cli.getCommandSpec().addMixin("help", usageHelp);
|
||||||
cli.setResourceBundle(getBundle("sonia.scm.cli.i18n", context.getLocale()));
|
cli.setResourceBundle(getBundle(CORE_RESOURCE_BUNDLE, context.getLocale()));
|
||||||
for (RegisteredCommandNode c : registry.createCommandTree()) {
|
for (RegisteredCommandNode c : registry.createCommandTree()) {
|
||||||
CommandLine commandline = createCommandline(context, factory, c);
|
CommandLine commandline = createCommandline(context, factory, c);
|
||||||
cli.getCommandSpec().addSubcommand(c.getName(), commandline);
|
cli.getCommandSpec().addSubcommand(c.getName(), commandline);
|
||||||
@@ -68,10 +72,13 @@ public class CliProcessor {
|
|||||||
CommandLine commandLine = new CommandLine(command.getCommand(), factory);
|
CommandLine commandLine = new CommandLine(command.getCommand(), factory);
|
||||||
commandLine.getCommandSpec().addMixin("help", usageHelp);
|
commandLine.getCommandSpec().addMixin("help", usageHelp);
|
||||||
|
|
||||||
ResourceBundle resourceBundle = commandLine.getCommandSpec().resourceBundle();
|
CliResourceBundle customResourceBundle = command.getCommand().getAnnotation(CliResourceBundle.class);
|
||||||
if (resourceBundle != null) {
|
if (customResourceBundle != null) {
|
||||||
String resourceBundleBaseName = resourceBundle.getBaseBundleName();
|
String resourceBundleBaseName = customResourceBundle.value();
|
||||||
commandLine.setResourceBundle(getBundle(resourceBundleBaseName, context.getLocale()));
|
ResourceBundle pluginResourceBundle = getBundle(resourceBundleBaseName, context.getLocale());
|
||||||
|
ResourceBundle coreResourceBundle = getBundle(CORE_RESOURCE_BUNDLE, context.getLocale());
|
||||||
|
CombinedResourceBundle combinedResourceBundle = new CombinedResourceBundle(pluginResourceBundle, coreResourceBundle);
|
||||||
|
commandLine.setResourceBundle(combinedResourceBundle);
|
||||||
}
|
}
|
||||||
for (RegisteredCommandNode child : command.getChildren()) {
|
for (RegisteredCommandNode child : command.getChildren()) {
|
||||||
if (!commandLine.getCommandSpec().subcommands().containsKey(child.getName())) {
|
if (!commandLine.getCommandSpec().subcommands().containsKey(child.getName())) {
|
||||||
@@ -84,7 +91,7 @@ public class CliProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ResourceBundle getBundle(String baseName, Locale locale) {
|
private ResourceBundle getBundle(String baseName, Locale locale) {
|
||||||
return ResourceBundle.getBundle(baseName, locale, new ResourceBundle.Control() {
|
return ResourceBundle.getBundle(baseName, locale, pluginLoader.getUberClassLoader(), new ResourceBundle.Control() {
|
||||||
@Override
|
@Override
|
||||||
public Locale getFallbackLocale(String baseName, Locale locale) {
|
public Locale getFallbackLocale(String baseName, Locale locale) {
|
||||||
return Locale.ROOT;
|
return Locale.ROOT;
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sonia.scm.cli;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.ListResourceBundle;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class CombinedResourceBundle extends ListResourceBundle {
|
||||||
|
|
||||||
|
private final Object[][] contents;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object[][] getContents() {
|
||||||
|
return contents;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CombinedResourceBundle(ResourceBundle... bundles) {
|
||||||
|
this.contents = Arrays
|
||||||
|
.stream(bundles)
|
||||||
|
.flatMap(resourceBundle ->
|
||||||
|
Collections
|
||||||
|
.list(resourceBundle.getKeys())
|
||||||
|
.stream().map(key -> new Object[]{key, resourceBundle.getObject(key)}))
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
.toArray(new Object[0][0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@ import org.mockito.Answers;
|
|||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
import sonia.scm.plugin.PluginLoader;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -55,6 +56,8 @@ class CliProcessorTest {
|
|||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private CliContext context;
|
private CliContext context;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PluginLoader pluginLoader;
|
||||||
@Mock
|
@Mock
|
||||||
private CliExceptionHandlerFactory exceptionHandlerFactory;
|
private CliExceptionHandlerFactory exceptionHandlerFactory;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -62,6 +65,11 @@ class CliProcessorTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private CliParameterExceptionHandler parameterExceptionHandler;
|
private CliParameterExceptionHandler parameterExceptionHandler;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void mockPluginLoader() {
|
||||||
|
when(pluginLoader.getUberClassLoader()).thenReturn(this.getClass().getClassLoader());
|
||||||
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class ForDefaultLanguageTest {
|
class ForDefaultLanguageTest {
|
||||||
|
|
||||||
@@ -76,7 +84,7 @@ class CliProcessorTest {
|
|||||||
void shouldExecutePingCommand() {
|
void shouldExecutePingCommand() {
|
||||||
when(registry.createCommandTree()).thenReturn(ImmutableList.of(new RegisteredCommandNode("ping", PingCommand.class)));
|
when(registry.createCommandTree()).thenReturn(ImmutableList.of(new RegisteredCommandNode("ping", PingCommand.class)));
|
||||||
Injector injector = Guice.createInjector();
|
Injector injector = Guice.createInjector();
|
||||||
CliProcessor cliProcessor = new CliProcessor(registry, injector, exceptionHandlerFactory);
|
CliProcessor cliProcessor = new CliProcessor(registry, injector, exceptionHandlerFactory, pluginLoader);
|
||||||
|
|
||||||
cliProcessor.execute(context, "ping");
|
cliProcessor.execute(context, "ping");
|
||||||
|
|
||||||
@@ -87,7 +95,7 @@ class CliProcessorTest {
|
|||||||
void shouldExecutePingCommandWithExitCode0() {
|
void shouldExecutePingCommandWithExitCode0() {
|
||||||
when(registry.createCommandTree()).thenReturn(ImmutableList.of(new RegisteredCommandNode("ping", PingCommand.class)));
|
when(registry.createCommandTree()).thenReturn(ImmutableList.of(new RegisteredCommandNode("ping", PingCommand.class)));
|
||||||
Injector injector = Guice.createInjector();
|
Injector injector = Guice.createInjector();
|
||||||
CliProcessor cliProcessor = new CliProcessor(registry, injector, exceptionHandlerFactory);
|
CliProcessor cliProcessor = new CliProcessor(registry, injector, exceptionHandlerFactory, pluginLoader);
|
||||||
|
|
||||||
int exitCode = cliProcessor.execute(context, "ping");
|
int exitCode = cliProcessor.execute(context, "ping");
|
||||||
|
|
||||||
@@ -170,7 +178,7 @@ class CliProcessorTest {
|
|||||||
when(context.getStdout()).thenReturn(new PrintWriter(baos));
|
when(context.getStdout()).thenReturn(new PrintWriter(baos));
|
||||||
|
|
||||||
Injector injector = Guice.createInjector();
|
Injector injector = Guice.createInjector();
|
||||||
CliProcessor cliProcessor = new CliProcessor(registry, injector, exceptionHandlerFactory);
|
CliProcessor cliProcessor = new CliProcessor(registry, injector, exceptionHandlerFactory, pluginLoader);
|
||||||
|
|
||||||
cliProcessor.execute(context, args);
|
cliProcessor.execute(context, args);
|
||||||
return baos.toString();
|
return baos.toString();
|
||||||
@@ -194,7 +202,8 @@ class CliProcessorTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@CommandLine.Command(name = "three", resourceBundle = "sonia.scm.cli.test")
|
@CommandLine.Command(name = "three")
|
||||||
|
@CliResourceBundle("sonia.scm.cli.test")
|
||||||
static class SubSubCommand implements Runnable {
|
static class SubSubCommand implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|||||||
Reference in New Issue
Block a user