Skip to content

Commit

Permalink
Create BuilderSupplier interface
Browse files Browse the repository at this point in the history
- Fixes #607
  • Loading branch information
jvalkeal committed Jan 8, 2023
1 parent 9b4e347 commit f91b255
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 the original author or authors.
* Copyright 2021-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@
package org.springframework.shell.boot;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.springframework.beans.factory.ObjectProvider;
Expand All @@ -29,6 +28,7 @@
import org.springframework.shell.command.CommandCatalog;
import org.springframework.shell.command.CommandCatalogCustomizer;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.command.CommandRegistration.BuilderSupplier;
import org.springframework.shell.command.CommandResolver;

@AutoConfiguration
Expand Down Expand Up @@ -76,7 +76,7 @@ public CommandRegistrationCustomizer helpOptionsCommandRegistrationCustomizer(Sp

@Bean
@ConditionalOnMissingBean
public Supplier<CommandRegistration.Builder> commandRegistrationBuilderSupplier(
public BuilderSupplier commandRegistrationBuilderSupplier(
ObjectProvider<CommandRegistrationCustomizer> customizerProvider) {
return () -> {
CommandRegistration.Builder builder = CommandRegistration.builder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,6 @@

package org.springframework.shell.boot;

import java.util.function.Supplier;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -55,7 +53,7 @@ public ValueProvider fileValueProvider() {

@Bean
public MethodTargetRegistrar standardMethodTargetResolver(ApplicationContext applicationContext,
Supplier<CommandRegistration.Builder> builder) {
CommandRegistration.BuilderSupplier builder) {
return new StandardMethodTargetRegistrar(applicationContext, builder);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -137,6 +137,14 @@ public static Builder builder() {
return new DefaultBuilder();
}

/**
* Interface used to supply instance of a {@link Builder}. Meant to be a single
* point access to centrally configured builder in an application context.
*/
@FunctionalInterface
public interface BuilderSupplier extends Supplier<Builder> {
}

/**
* Spec defining an option.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include::{snippets}/CommandRegistrationBeanSnippets.java[tag=plain]
====

If all your commands have something in common, an instance of
a _Supplier<CommandRegistration.Builder>_ is created which can
a _CommandRegistration.BuilderSupplier_ is created which can
be autowired. Default implementation of this supplier returns
a new builder so you don't need to worry about its internal state.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
*/
package org.springframework.shell.docs;

import java.util.function.Supplier;

import org.springframework.context.annotation.Bean;
import org.springframework.shell.boot.CommandRegistrationCustomizer;
import org.springframework.shell.command.CommandRegistration;
Expand All @@ -37,7 +35,7 @@ CommandRegistration commandRegistration() {
class Dump2 {
// tag::fromsupplier[]
@Bean
CommandRegistration commandRegistration(Supplier<CommandRegistration.Builder> builder) {
CommandRegistration commandRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command("mycommand")
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
*/
package org.springframework.shell.samples.e2e;

import java.util.function.Supplier;

import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.command.CommandRegistration.OptionArity;
Expand All @@ -40,7 +38,7 @@ public String testBooleanArity1DefaultTrue(
}

@Bean
public CommandRegistration testBooleanArity1DefaultTrueRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testBooleanArity1DefaultTrueRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "boolean-arity1-default-true")
.group(GROUP)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
*/
package org.springframework.shell.samples.e2e;

import java.util.function.Supplier;

import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.standard.ShellComponent;
Expand All @@ -39,7 +37,7 @@ public String testDefaultValue(
}

@Bean
public CommandRegistration testDefaultValueRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testDefaultValueRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "default-value")
.group(GROUP)
Expand All @@ -64,7 +62,7 @@ public String testDefaultValueBoolean1(
}

@Bean
public CommandRegistration testDefaultValueBoolean1Registration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testDefaultValueBoolean1Registration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "default-value-boolean1")
.group(GROUP)
Expand All @@ -90,7 +88,7 @@ public String testDefaultValueBoolean2(
}

@Bean
public CommandRegistration testDefaultValueBoolean2Registration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testDefaultValueBoolean2Registration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "default-value-boolean2")
.group(GROUP)
Expand All @@ -116,7 +114,7 @@ public String testDefaultValueBoolean3(
}

@Bean
public CommandRegistration testDefaultValueBoolean3Registration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testDefaultValueBoolean3Registration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "default-value-boolean3")
.group(GROUP)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.util.function.Supplier;

import org.jline.terminal.Terminal;

Expand Down Expand Up @@ -87,7 +86,7 @@ void errorHandler3(CustomException4 e, Terminal terminal) {
}

@Bean
CommandRegistration testErrorHandlingRegistration(Supplier<CommandRegistration.Builder> builder) {
CommandRegistration testErrorHandlingRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "error-handling")
.group(GROUP)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
*/
package org.springframework.shell.samples.e2e;

import java.util.function.Supplier;

import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.standard.ShellComponent;
Expand All @@ -30,7 +28,7 @@
public class ExitCodeCommands extends BaseE2ECommands {

@Bean
public CommandRegistration testExitCodeRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testExitCodeRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "exit-code")
.group(GROUP)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
*/
package org.springframework.shell.samples.e2e;

import java.util.function.Supplier;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;
Expand All @@ -28,7 +26,7 @@
public class HelpOptionCommands extends BaseE2ECommands {

@Autowired
Supplier<CommandRegistration.Builder> builder;
CommandRegistration.BuilderSupplier builder;

@ShellMethod(key = LEGACY_ANNO + "help-option-default", group = GROUP)
public String testHelpOptionDefault(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
*/
package org.springframework.shell.samples.e2e;

import java.util.function.Supplier;

import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.standard.ShellComponent;
Expand All @@ -25,7 +23,7 @@
public class HiddenCommands extends BaseE2ECommands {

@Bean
public CommandRegistration testHidden1Registration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testHidden1Registration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "hidden-1")
.group(GROUP)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.springframework.context.annotation.Bean;
Expand All @@ -41,7 +40,7 @@ public String testInteractiveCompletion1(
}

@Bean
CommandRegistration testInteractiveCompletion1Registration(Supplier<CommandRegistration.Builder> builder) {
CommandRegistration testInteractiveCompletion1Registration(CommandRegistration.BuilderSupplier builder) {
Test1ValuesProvider test1ValuesProvider = new Test1ValuesProvider();
Test2ValuesProvider test2ValuesProvider = new Test2ValuesProvider();
return builder.get()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@
package org.springframework.shell.samples.e2e;

import java.io.PrintWriter;
import java.util.function.Supplier;

import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;
Expand All @@ -33,7 +32,7 @@
public class OptionTypeCommands extends BaseE2ECommands {

@Bean
public CommandRegistration testOptionTypeRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testOptionTypeRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "option-type")
.group(GROUP)
Expand Down Expand Up @@ -77,7 +76,7 @@ public String optionTypeStringAnnotation(
}

@Bean
public CommandRegistration optionTypeStringRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration optionTypeStringRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "option-type-string")
.group(GROUP)
Expand Down Expand Up @@ -113,7 +112,7 @@ public String optionTypeBooleanAnnotation(
}

@Bean
public CommandRegistration optionTypeBooleanRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration optionTypeBooleanRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "option-type-boolean")
.group(GROUP)
Expand Down Expand Up @@ -173,7 +172,7 @@ public String optionTypeIntegerAnnotation(
}

@Bean
public CommandRegistration optionTypeIntegerRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration optionTypeIntegerRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "option-type-integer")
.group(GROUP)
Expand Down Expand Up @@ -213,7 +212,7 @@ public String optionTypeEnumAnnotation(
}

@Bean
public CommandRegistration optionTypeEnumRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration optionTypeEnumRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "option-type-enum")
.group(GROUP)
Expand All @@ -236,7 +235,7 @@ public CommandRegistration optionTypeEnumRegistration(Supplier<CommandRegistrati
//

@Bean
public CommandRegistration optionTypeVoidRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration optionTypeVoidRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "option-type-void")
.group(GROUP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.springframework.shell.samples.e2e;

import java.util.function.Supplier;

import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.standard.ShellComponent;
Expand All @@ -39,7 +37,7 @@ public String testOptionalValue(
}

@Bean
public CommandRegistration testOptionalValueRegistration(Supplier<CommandRegistration.Builder> builder) {
public CommandRegistration testOptionalValueRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command(REG, "optional-value")
.group(GROUP)
Expand Down
Loading

0 comments on commit f91b255

Please sign in to comment.