Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
<antora-maven-plugin.version>1.0.0-alpha.5</antora-maven-plugin.version>

<!-- plugin versions -->
<spring-javaformat-maven-plugin.version>0.0.43</spring-javaformat-maven-plugin.version>
<disable.format.checks>false</disable.format.checks>
<maven-compiler-plugin.version>3.14.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>3.5.3</maven-surefire-plugin.version>
<maven-failsafe-plugin.version>3.5.3</maven-failsafe-plugin.version>
Expand All @@ -86,6 +88,23 @@

<build>
<plugins>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring-javaformat-maven-plugin.version}</version>
<executions>
<execution>
<phase>validate</phase>
<inherited>true</inherited>
<configuration>
<skip>${disable.format.checks}</skip>
</configuration>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ public ApplicationReadyEventListener applicationReadyEventListener() {
return new ApplicationReadyEventListener();
}

static class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent>{
static class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// request context close after application runners so that
// shell exits in case that context is kept alive
event.getApplicationContext().close();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public class CommandCatalogAutoConfiguration {
@ConditionalOnMissingBean(CommandCatalog.class)
public CommandCatalog commandCatalog(ObjectProvider<MethodTargetRegistrar> methodTargetRegistrars,
ObjectProvider<CommandResolver> commandResolvers,
ObjectProvider<CommandCatalogCustomizer> commandCatalogCustomizers,
ShellContext shellContext) {
ObjectProvider<CommandCatalogCustomizer> commandCatalogCustomizers, ShellContext shellContext) {
List<CommandResolver> resolvers = commandResolvers.orderedStream().collect(Collectors.toList());
CommandCatalog catalog = CommandCatalog.of(resolvers, shellContext);
methodTargetRegistrars.orderedStream().forEach(resolver -> {
Expand All @@ -58,7 +57,8 @@ public CommandCatalog commandCatalog(ObjectProvider<MethodTargetRegistrar> metho
}

@Bean
public CommandCatalogCustomizer defaultCommandCatalogCustomizer(ObjectProvider<CommandRegistration> commandRegistrations) {
public CommandCatalogCustomizer defaultCommandCatalogCustomizer(
ObjectProvider<CommandRegistration> commandRegistrations) {
return catalog -> {
commandRegistrations.orderedStream().forEach(registration -> {
catalog.register(registration);
Expand All @@ -82,7 +82,8 @@ public CommandRegistrationCustomizer helpOptionsCommandRegistrationCustomizer(Sp

@Bean
@ConditionalOnBean(OptionNameModifier.class)
public CommandRegistrationCustomizer customOptionNameModifierCommandRegistrationCustomizer(OptionNameModifier modifier) {
public CommandRegistrationCustomizer customOptionNameModifierCommandRegistrationCustomizer(
OptionNameModifier modifier) {
return builder -> {
builder.defaultOptionNameModifier(modifier);
};
Expand All @@ -91,7 +92,8 @@ public CommandRegistrationCustomizer customOptionNameModifierCommandRegistration
@Bean
@ConditionalOnMissingBean(OptionNameModifier.class)
@ConditionalOnProperty(prefix = "spring.shell.option.naming", name = "case-type")
public CommandRegistrationCustomizer defaultOptionNameModifierCommandRegistrationCustomizer(SpringShellProperties properties) {
public CommandRegistrationCustomizer defaultOptionNameModifierCommandRegistrationCustomizer(
SpringShellProperties properties) {
return builder -> {
switch (properties.getOption().getNaming().getCaseType()) {
case NOOP:
Expand Down Expand Up @@ -124,4 +126,5 @@ public BuilderSupplier commandRegistrationBuilderSupplier(
return builder;
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public interface CommandRegistrationCustomizer {

/**
* Callback to customize a {@link CommandRegistration.Builder} instance.
*
* @param commandRegistrationBuilder the command registration builder to customize
*/
void customize(CommandRegistration.Builder commandRegistrationBuilder);

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,13 @@ public static class CompleterAdapter implements Completer {
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
CompletingParsedLine cpl = (line instanceof CompletingParsedLine) ? ((CompletingParsedLine) line) : t -> t;

CompletionContext context = new CompletionContext(sanitizeInput(line.words()), line.wordIndex(), line.wordCursor(), null, null);
CompletionContext context = new CompletionContext(sanitizeInput(line.words()), line.wordIndex(),
line.wordCursor(), null, null);

List<CompletionProposal> proposals = shell.complete(context);
proposals.stream()
.map(p -> new Candidate(
p.dontQuote() ? p.value() : cpl.emit(p.value()).toString(),
p.displayText(),
p.category(),
p.description(),
null,
null,
p.complete())
)
.map(p -> new Candidate(p.dontQuote() ? p.value() : cpl.emit(p.value()).toString(), p.displayText(),
p.category(), p.description(), null, null, p.complete()))
.forEach(candidates::add);
}

Expand All @@ -74,11 +68,15 @@ public void setShell(Shell shell) {

static List<String> sanitizeInput(List<String> words) {
words = words.stream()
.map(s -> s.replaceAll("^\\n+|\\n+$", "")) // CR at beginning/end of line introduced by backslash continuation
.map(s -> s.replaceAll("\\n+", " ")) // CR in middle of word introduced by return inside a quoted string
.map(s -> s.replaceAll("^\\n+|\\n+$", "")) // CR at beginning/end of line
// introduced by backslash
// continuation
.map(s -> s.replaceAll("\\n+", " ")) // CR in middle of word introduced by
// return inside a quoted string
.collect(Collectors.toList());
return words;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public ComponentFlowCustomizer shellCommonComponentFlowCustomizer(ObjectProvider
private static class CommonComponentFlowCustomizer implements ComponentFlowCustomizer {

private final ObjectProvider<Terminal> terminal;

private final ObjectProvider<ResourceLoader> resourceLoader;

private final ObjectProvider<TemplateExecutor> templateExecutor;

CommonComponentFlowCustomizer(ObjectProvider<Terminal> terminal, ObjectProvider<ResourceLoader> resourceLoader,
Expand All @@ -81,5 +83,7 @@ public void customize(Builder componentFlowBuilder) {
resourceLoader.ifAvailable(dep -> componentFlowBuilder.resourceLoader(dep));
templateExecutor.ifAvailable(dep -> componentFlowBuilder.templateExecutor(dep));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ public interface ComponentFlowCustomizer {
* @param componentFlowBuilder the component flow builder to customize
*/
void customize(ComponentFlow.Builder componentFlowBuilder);

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ else if (exception.getCause() instanceof CommandExecution.CommandParserException
// only map parsing error so that other mappers can do their job
return 0;
}

}

static class ShellExitCodeMappingsExceptionMapper implements ExitCodeExceptionMapper, ExitCodeMappings {
Expand Down Expand Up @@ -100,6 +101,7 @@ public int getExitCode(Throwable exception) {
}
return exitCode;
}

}

static class ShellExitCodeException extends RuntimeException implements ExitCodeGenerator {
Expand All @@ -115,5 +117,7 @@ static class ShellExitCodeException extends RuntimeException implements ExitCode
public int getExitCode() {
return code;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ public static class JLineHistoryConfiguration {
public org.jline.reader.History history() {
return new DefaultHistory();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ public Parser parser() {
parser.setEofOnEscapedNewLine(true);
return parser;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class LineReaderAutoConfiguration {
private String fallbackHistoryFileName;

private SpringShellProperties springShellProperties;

private UserConfigPathProvider userConfigPathProvider;

public LineReaderAutoConfiguration(Terminal terminal, Completer completer, Parser parser,
Expand All @@ -84,39 +85,41 @@ public void onContextClosedEvent(ContextClosedEvent event) throws IOException {
@Bean
public LineReader lineReader() {
LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder()
.terminal(terminal)
.appName("Spring Shell")
.completer(completer)
.history(jLineHistory)
.highlighter(new Highlighter() {

@Override
public AttributedString highlight(LineReader reader, String buffer) {
int l = 0;
String best = null;
for (String command : commandRegistry.getRegistrations().keySet()) {
if (buffer.startsWith(command) && command.length() > l) {
l = command.length();
best = command;
}
}
if (best != null) {
return new AttributedStringBuilder(buffer.length()).append(best, AttributedStyle.BOLD).append(buffer.substring(l)).toAttributedString();
}
else {
return new AttributedString(buffer, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
.terminal(terminal)
.appName("Spring Shell")
.completer(completer)
.history(jLineHistory)
.highlighter(new Highlighter() {

@Override
public AttributedString highlight(LineReader reader, String buffer) {
int l = 0;
String best = null;
for (String command : commandRegistry.getRegistrations().keySet()) {
if (buffer.startsWith(command) && command.length() > l) {
l = command.length();
best = command;
}
}

@Override
public void setErrorPattern(Pattern errorPattern) {
if (best != null) {
return new AttributedStringBuilder(buffer.length()).append(best, AttributedStyle.BOLD)
.append(buffer.substring(l))
.toAttributedString();
}

@Override
public void setErrorIndex(int errorIndex) {
else {
return new AttributedString(buffer, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
}
})
.parser(parser);
}

@Override
public void setErrorPattern(Pattern errorPattern) {
}

@Override
public void setErrorIndex(int errorIndex) {
}
})
.parser(parser);

LineReader lineReader = lineReaderBuilder.build();
if (this.springShellProperties.getHistory().isEnabled()) {
Expand All @@ -133,7 +136,9 @@ public void setErrorIndex(int errorIndex) {
// set history file
lineReader.setVariable(LineReader.HISTORY_FILE, Paths.get(historyPath));
}
lineReader.unsetOpt(LineReader.Option.INSERT_TAB); // This allows completion on an empty buffer, rather than inserting a tab
lineReader.unsetOpt(LineReader.Option.INSERT_TAB); // This allows completion on an
// empty buffer, rather than
// inserting a tab
jLineHistory.attach(lineReader);
return lineReader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
@FunctionalInterface
public interface NonInteractiveShellRunnerCustomizer {

/**
* Customize the {@link NonInteractiveShellRunner}.
* @param shellRunner the non-interactive shell runner to customize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CompletionResolver defaultCompletionResolver() {

@Bean
public CommandExecutionHandlerMethodArgumentResolvers commandExecutionHandlerMethodArgumentResolvers(
ShellConversionServiceSupplier shellConversionServiceSupplier) {
ShellConversionServiceSupplier shellConversionServiceSupplier) {
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
resolvers.add(new ArgumentHeaderMethodArgumentResolver(shellConversionServiceSupplier.get(), null));
resolvers.add(new HeadersMethodArgumentResolver());
Expand All @@ -50,4 +50,5 @@ public CommandExecutionHandlerMethodArgumentResolvers commandExecutionHandlerMet
resolvers.add(new OptionMethodArgumentResolver(shellConversionServiceSupplier.get(), null));
return new CommandExecutionHandlerMethodArgumentResolvers(resolvers);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ public ShellContext shellContext(Terminal terminal) {
}
return new DefaultShellContext(pty);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class ShellRunnerAutoConfiguration {
public static class PrimaryCommandConfiguration {

@Bean
@ConditionalOnProperty(prefix = "spring.shell.noninteractive", value = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(prefix = "spring.shell.noninteractive", value = "enabled", havingValue = "true",
matchIfMissing = true)
public NonInteractiveShellRunner nonInteractiveApplicationRunner(Shell shell, ShellContext shellContext,
ObjectProvider<NonInteractiveShellRunnerCustomizer> customizer, SpringShellProperties properties) {
NonInteractiveShellRunner shellRunner = new NonInteractiveShellRunner(shell, shellContext,
Expand All @@ -59,14 +60,16 @@ public NonInteractiveShellRunner nonInteractiveApplicationRunner(Shell shell, Sh
public static class NonePrimaryCommandConfiguration {

@Bean
@ConditionalOnProperty(prefix = "spring.shell.interactive", value = "enabled", havingValue = "true", matchIfMissing = false)
@ConditionalOnProperty(prefix = "spring.shell.interactive", value = "enabled", havingValue = "true",
matchIfMissing = false)
public InteractiveShellRunner interactiveApplicationRunner(LineReader lineReader, PromptProvider promptProvider,
Shell shell, ShellContext shellContext) {
return new InteractiveShellRunner(lineReader, promptProvider, shell, shellContext);
}

@Bean
@ConditionalOnProperty(prefix = "spring.shell.noninteractive", value = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(prefix = "spring.shell.noninteractive", value = "enabled", havingValue = "true",
matchIfMissing = true)
public NonInteractiveShellRunner nonInteractiveApplicationRunner(Shell shell, ShellContext shellContext,
ObjectProvider<NonInteractiveShellRunnerCustomizer> customizer) {
NonInteractiveShellRunner shellRunner = new NonInteractiveShellRunner(shell, shellContext);
Expand All @@ -75,10 +78,12 @@ public NonInteractiveShellRunner nonInteractiveApplicationRunner(Shell shell, Sh
}

@Bean
@ConditionalOnProperty(prefix = "spring.shell.script", value = "enabled", havingValue = "true", matchIfMissing = false)
@ConditionalOnProperty(prefix = "spring.shell.script", value = "enabled", havingValue = "true",
matchIfMissing = false)
public ScriptShellRunner scriptApplicationRunner(Parser parser, Shell shell) {
return new ScriptShellRunner(parser, shell);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ public Shell shell(ResultHandlerService resultHandlerService, CommandCatalog com
shell.setConversionService(shellConversionServiceSupplier.get());
return shell;
}

}
Loading