Skip to content

Commit 52ff4a2

Browse files
committed
Add version command
- Add new styling system which works around concept that you use tags to request jline styles where tags comes from an activated theme. - There is a default theme with options to add custom ones and change it via property. - Add templating system which uses antlr stringtemplate which allows to write output with a template instead of manually crafting code. - Add version command which integrates to Boot's BuildProperties and GitProperties. Only version field is visible on default and others can be enabled/disable via properties. - Fixes #352 - Fixes #353
1 parent 0fb5b5e commit 52ff4a2

File tree

23 files changed

+1208
-6
lines changed

23 files changed

+1208
-6
lines changed

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@
154154
</execution>
155155
</executions>
156156
</plugin>
157+
<plugin>
158+
<groupId>pl.project13.maven</groupId>
159+
<artifactId>git-commit-id-plugin</artifactId>
160+
<executions>
161+
<execution>
162+
<goals>
163+
<goal>revision</goal>
164+
</goals>
165+
</execution>
166+
</executions>
167+
<configuration>
168+
<verbose>true</verbose>
169+
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
170+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
171+
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
172+
</configuration>
173+
</plugin>
157174
</plugins>
158175
</build>
159176
<profiles>

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellProperties.java

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class SpringShellProperties {
2828
private Script script = new Script();
2929
private Interactive interactive = new Interactive();
3030
private Noninteractive noninteractive = new Noninteractive();
31+
private Theme theme = new Theme();
3132
private Command command = new Command();
3233

3334
public void setScript(Script script) {
@@ -54,6 +55,14 @@ public void setNoninteractive(Noninteractive noninteractive) {
5455
this.noninteractive = noninteractive;
5556
}
5657

58+
public Theme getTheme() {
59+
return theme;
60+
}
61+
62+
public void setTheme(Theme theme) {
63+
this.theme = theme;
64+
}
65+
5766
public Command getCommand() {
5867
return command;
5968
}
@@ -101,6 +110,19 @@ public void setEnabled(boolean enabled) {
101110
}
102111
}
103112

113+
public static class Theme {
114+
115+
private String name = "default";
116+
117+
public String getName() {
118+
return name;
119+
}
120+
121+
public void setName(String name) {
122+
this.name = name;
123+
}
124+
}
125+
104126
public static class HelpCommand {
105127

106128
private boolean enabled = true;
@@ -210,6 +232,7 @@ public static class Command {
210232
private ScriptCommand script = new ScriptCommand();
211233
private HistoryCommand history = new HistoryCommand();
212234
private CompletionCommand completion = new CompletionCommand();
235+
private VersionCommand version = new VersionCommand();
213236

214237
public void setHelp(HelpCommand help) {
215238
this.help = help;
@@ -266,5 +289,116 @@ public CompletionCommand getCompletion() {
266289
public void setCompletion(CompletionCommand completion) {
267290
this.completion = completion;
268291
}
292+
293+
public VersionCommand getVersion() {
294+
return version;
295+
}
296+
297+
public void setVersion(VersionCommand version) {
298+
this.version = version;
299+
}
300+
}
301+
302+
public static class VersionCommand {
303+
304+
private boolean enabled = true;
305+
private String template = "classpath:template/version-default.st";
306+
private boolean showBuildGroup = false;
307+
private boolean showBuildArtifact = false;
308+
private boolean showBuildName = false;
309+
private boolean showBuildVersion = true;
310+
private boolean showBuildTime = false;
311+
private boolean showGitBranch = false;
312+
private boolean showGitCommitId = false;
313+
private boolean showGitShortCommitId = false;
314+
private boolean showGitCommitTime = false;
315+
316+
public boolean isEnabled() {
317+
return enabled;
318+
}
319+
320+
public void setEnabled(boolean enabled) {
321+
this.enabled = enabled;
322+
}
323+
324+
public String getTemplate() {
325+
return template;
326+
}
327+
328+
public void setTemplate(String template) {
329+
this.template = template;
330+
}
331+
332+
public boolean isShowBuildGroup() {
333+
return showBuildGroup;
334+
}
335+
336+
public void setShowBuildGroup(boolean showBuildGroup) {
337+
this.showBuildGroup = showBuildGroup;
338+
}
339+
340+
public boolean isShowBuildArtifact() {
341+
return showBuildArtifact;
342+
}
343+
344+
public void setShowBuildArtifact(boolean showBuildArtifact) {
345+
this.showBuildArtifact = showBuildArtifact;
346+
}
347+
348+
public boolean isShowBuildName() {
349+
return showBuildName;
350+
}
351+
352+
public void setShowBuildName(boolean showBuildName) {
353+
this.showBuildName = showBuildName;
354+
}
355+
356+
public boolean isShowBuildVersion() {
357+
return showBuildVersion;
358+
}
359+
360+
public void setShowBuildVersion(boolean showBuildVersion) {
361+
this.showBuildVersion = showBuildVersion;
362+
}
363+
364+
public boolean isShowBuildTime() {
365+
return showBuildTime;
366+
}
367+
368+
public void setShowBuildTime(boolean showBuildTime) {
369+
this.showBuildTime = showBuildTime;
370+
}
371+
372+
public boolean isShowGitBranch() {
373+
return showGitBranch;
374+
}
375+
376+
public void setShowGitBranch(boolean showGitBranch) {
377+
this.showGitBranch = showGitBranch;
378+
}
379+
380+
public boolean isShowGitCommitId() {
381+
return showGitCommitId;
382+
}
383+
384+
public void setShowGitCommitId(boolean showGitCommitId) {
385+
this.showGitCommitId = showGitCommitId;
386+
}
387+
388+
public boolean isShowGitShortCommitId() {
389+
return showGitShortCommitId;
390+
}
391+
392+
public void setShowGitShortCommitId(boolean showGitShortCommitId) {
393+
this.showGitShortCommitId = showGitShortCommitId;
394+
}
395+
396+
public boolean isShowGitCommitTime() {
397+
return showGitCommitTime;
398+
}
399+
400+
public void setShowGitCommitTime(boolean showGitCommitTime) {
401+
this.showGitCommitTime = showGitCommitTime;
402+
}
269403
}
270404
}

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/StandardCommandsAutoConfiguration.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2525
import org.springframework.boot.context.properties.EnableConfigurationProperties;
26+
import org.springframework.boot.info.BuildProperties;
27+
import org.springframework.boot.info.GitProperties;
2628
import org.springframework.context.annotation.Bean;
2729
import org.springframework.context.annotation.Conditional;
2830
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.shell.boot.SpringShellProperties.VersionCommand;
2932
import org.springframework.shell.boot.condition.OnCompletionCommandCondition;
3033
import org.springframework.shell.result.ThrowableResultHandler;
3134
import org.springframework.shell.standard.commands.Clear;
@@ -35,6 +38,8 @@
3538
import org.springframework.shell.standard.commands.Quit;
3639
import org.springframework.shell.standard.commands.Script;
3740
import org.springframework.shell.standard.commands.Stacktrace;
41+
import org.springframework.shell.standard.commands.Version;
42+
import org.springframework.shell.style.TemplateExecutor;
3843

3944
/**
4045
* Creates beans for standard commands.
@@ -94,4 +99,26 @@ public History historyCommand(org.jline.reader.History jLineHistory) {
9499
public Completion completion(SpringShellProperties properties) {
95100
return new Completion(properties.getCommand().getCompletion().getRootCommand());
96101
}
102+
103+
@Bean
104+
@ConditionalOnMissingBean(Version.Command.class)
105+
@ConditionalOnProperty(prefix = "spring.shell.command.version", value = "enabled", havingValue = "true", matchIfMissing = true)
106+
public Version version(SpringShellProperties properties, ObjectProvider<BuildProperties> buildProperties,
107+
ObjectProvider<GitProperties> gitProperties, ObjectProvider<TemplateExecutor> templateExecutor) {
108+
Version version = new Version(templateExecutor.getIfAvailable());
109+
version.setBuildProperties(buildProperties.getIfAvailable());
110+
version.setGitProperties(gitProperties.getIfAvailable());
111+
VersionCommand versionProperties = properties.getCommand().getVersion();
112+
version.setTemplate(versionProperties.getTemplate());
113+
version.setShowBuildArtifact(versionProperties.isShowBuildArtifact());
114+
version.setShowBuildGroup(versionProperties.isShowBuildGroup());
115+
version.setShowBuildName(versionProperties.isShowBuildName());
116+
version.setShowBuildTime(versionProperties.isShowBuildTime());
117+
version.setShowBuildVersion(versionProperties.isShowBuildVersion());
118+
version.setShowGitBranch(versionProperties.isShowGitBranch());
119+
version.setShowGitCommitId(versionProperties.isShowGitCommitId());
120+
version.setShowGitShortCommitId(versionProperties.isShowGitShortCommitId());
121+
version.setShowGitCommitTime(versionProperties.isShowGitCommitTime());
122+
return version;
123+
}
97124
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.boot;
17+
18+
import org.springframework.beans.factory.ObjectProvider;
19+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.shell.style.TemplateExecutor;
23+
import org.springframework.shell.style.Theme;
24+
import org.springframework.shell.style.ThemeRegistry;
25+
import org.springframework.shell.style.ThemeResolver;
26+
import org.springframework.shell.style.ThemeSettings;
27+
28+
@Configuration(proxyBeanMethods = false)
29+
@EnableConfigurationProperties(SpringShellProperties.class)
30+
public class ThemingAutoConfiguration {
31+
32+
@Bean
33+
public ThemeRegistry themeRegistry(ObjectProvider<Theme> themes) {
34+
ThemeRegistry registry = new ThemeRegistry();
35+
registry.register(Theme.of("default", ThemeSettings.themeSettings()));
36+
themes.orderedStream().forEachOrdered(theme -> registry.register(theme));
37+
return registry;
38+
}
39+
40+
@Bean
41+
public ThemeResolver themeResolver(ThemeRegistry themeRegistry, SpringShellProperties properties) {
42+
return new ThemeResolver(themeRegistry, properties.getTheme().getName());
43+
}
44+
45+
@Bean
46+
public TemplateExecutor templateExecutor(ThemeResolver themeResolver) {
47+
return new TemplateExecutor(themeResolver);
48+
}
49+
50+
}

spring-shell-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ org.springframework.shell.boot.JLineShellAutoConfiguration,\
1111
org.springframework.shell.boot.JCommanderParameterResolverAutoConfiguration,\
1212
org.springframework.shell.boot.ParameterResolverAutoConfiguration,\
1313
org.springframework.shell.boot.StandardAPIAutoConfiguration,\
14+
org.springframework.shell.boot.ThemingAutoConfiguration,\
1415
org.springframework.shell.boot.StandardCommandsAutoConfiguration

spring-shell-autoconfigure/src/test/java/org/springframework/shell/boot/SpringShellPropertiesTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void defaultNoPropertiesSet() {
3535
assertThat(properties.getScript().isEnabled()).isTrue();
3636
assertThat(properties.getInteractive().isEnabled()).isTrue();
3737
assertThat(properties.getNoninteractive().isEnabled()).isTrue();
38+
assertThat(properties.getTheme().getName()).isEqualTo("default");
3839
assertThat(properties.getCommand().getClear().isEnabled()).isTrue();
3940
assertThat(properties.getCommand().getHelp().isEnabled()).isTrue();
4041
assertThat(properties.getCommand().getHistory().isEnabled()).isTrue();
@@ -43,6 +44,17 @@ public void defaultNoPropertiesSet() {
4344
assertThat(properties.getCommand().getStacktrace().isEnabled()).isTrue();
4445
assertThat(properties.getCommand().getCompletion().isEnabled()).isTrue();
4546
assertThat(properties.getCommand().getCompletion().getRootCommand()).isNull();
47+
assertThat(properties.getCommand().getVersion().isEnabled()).isTrue();
48+
assertThat(properties.getCommand().getVersion().getTemplate()).isNotNull();
49+
assertThat(properties.getCommand().getVersion().isShowBuildArtifact()).isFalse();
50+
assertThat(properties.getCommand().getVersion().isShowBuildGroup()).isFalse();
51+
assertThat(properties.getCommand().getVersion().isShowBuildName()).isFalse();
52+
assertThat(properties.getCommand().getVersion().isShowBuildTime()).isFalse();
53+
assertThat(properties.getCommand().getVersion().isShowBuildVersion()).isTrue();
54+
assertThat(properties.getCommand().getVersion().isShowGitBranch()).isFalse();
55+
assertThat(properties.getCommand().getVersion().isShowGitCommitId()).isFalse();
56+
assertThat(properties.getCommand().getVersion().isShowGitShortCommitId()).isFalse();
57+
assertThat(properties.getCommand().getVersion().isShowGitCommitTime()).isFalse();
4658
});
4759
}
4860

@@ -52,6 +64,7 @@ public void setProperties() {
5264
.withPropertyValues("spring.shell.script.enabled=false")
5365
.withPropertyValues("spring.shell.interactive.enabled=false")
5466
.withPropertyValues("spring.shell.noninteractive.enabled=false")
67+
.withPropertyValues("spring.shell.theme.name=fake")
5568
.withPropertyValues("spring.shell.command.clear.enabled=false")
5669
.withPropertyValues("spring.shell.command.help.enabled=false")
5770
.withPropertyValues("spring.shell.command.history.enabled=false")
@@ -60,12 +73,24 @@ public void setProperties() {
6073
.withPropertyValues("spring.shell.command.stacktrace.enabled=false")
6174
.withPropertyValues("spring.shell.command.completion.enabled=false")
6275
.withPropertyValues("spring.shell.command.completion.root-command=fake")
76+
.withPropertyValues("spring.shell.command.version.enabled=false")
77+
.withPropertyValues("spring.shell.command.version.template=fake")
78+
.withPropertyValues("spring.shell.command.version.show-build-artifact=true")
79+
.withPropertyValues("spring.shell.command.version.show-build-group=true")
80+
.withPropertyValues("spring.shell.command.version.show-build-name=true")
81+
.withPropertyValues("spring.shell.command.version.show-build-time=true")
82+
.withPropertyValues("spring.shell.command.version.show-build-version=false")
83+
.withPropertyValues("spring.shell.command.version.show-git-branch=true")
84+
.withPropertyValues("spring.shell.command.version.show-git-commit-id=true")
85+
.withPropertyValues("spring.shell.command.version.show-git-short-commit-id=true")
86+
.withPropertyValues("spring.shell.command.version.show-git-commit-time=true")
6387
.withUserConfiguration(Config1.class)
6488
.run((context) -> {
6589
SpringShellProperties properties = context.getBean(SpringShellProperties.class);
6690
assertThat(properties.getScript().isEnabled()).isFalse();
6791
assertThat(properties.getInteractive().isEnabled()).isFalse();
6892
assertThat(properties.getNoninteractive().isEnabled()).isFalse();
93+
assertThat(properties.getTheme().getName()).isEqualTo("fake");
6994
assertThat(properties.getCommand().getClear().isEnabled()).isFalse();
7095
assertThat(properties.getCommand().getHelp().isEnabled()).isFalse();
7196
assertThat(properties.getCommand().getHistory().isEnabled()).isFalse();
@@ -74,6 +99,17 @@ public void setProperties() {
7499
assertThat(properties.getCommand().getStacktrace().isEnabled()).isFalse();
75100
assertThat(properties.getCommand().getCompletion().isEnabled()).isFalse();
76101
assertThat(properties.getCommand().getCompletion().getRootCommand()).isEqualTo("fake");
102+
assertThat(properties.getCommand().getVersion().isEnabled()).isFalse();
103+
assertThat(properties.getCommand().getVersion().getTemplate()).isEqualTo("fake");
104+
assertThat(properties.getCommand().getVersion().isShowBuildArtifact()).isTrue();
105+
assertThat(properties.getCommand().getVersion().isShowBuildGroup()).isTrue();
106+
assertThat(properties.getCommand().getVersion().isShowBuildName()).isTrue();
107+
assertThat(properties.getCommand().getVersion().isShowBuildTime()).isTrue();
108+
assertThat(properties.getCommand().getVersion().isShowBuildVersion()).isFalse();
109+
assertThat(properties.getCommand().getVersion().isShowGitBranch()).isTrue();
110+
assertThat(properties.getCommand().getVersion().isShowGitCommitId()).isTrue();
111+
assertThat(properties.getCommand().getVersion().isShowGitShortCommitId()).isTrue();
112+
assertThat(properties.getCommand().getVersion().isShowGitCommitTime()).isTrue();
77113
});
78114
}
79115

0 commit comments

Comments
 (0)