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
This commit is contained in:
Janne Valkealahti
2022-01-23 09:23:16 +00:00
parent 0fb5b5ee7a
commit 52ff4a2d85
23 changed files with 1208 additions and 6 deletions

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
import java.util.Locale;
import org.jline.style.StyleExpression;
import org.stringtemplate.v4.AttributeRenderer;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* {@code ST} {@link AttributeRenderer} which knows to use format string to
* render strings into {@code jline} {@link StyleExpression} based on theming
* settings.
*
* @author Janne Valkealahti
*/
public class StringToStyleExpressionRenderer implements AttributeRenderer<String> {
private final ThemeResolver themeResolver;
public StringToStyleExpressionRenderer(ThemeResolver themeResolver) {
Assert.notNull(themeResolver, "themeResolver must be set");
this.themeResolver = themeResolver;
}
@Override
public String toString(String value, String formatString, Locale locale) {
if (!StringUtils.hasText(formatString)) {
return value;
}
else {
return String.format("@{%s %s}", themeResolver.resolveTag(formatString), value);
}
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
import java.util.Map;
import org.jline.utils.AttributedString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STErrorListener;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.misc.STMessage;
/**
* Template executor which knows to use styling.
*
* @author Janne Valkealahti
*/
public class TemplateExecutor {
private final static STErrorListener ERROR_LISTENER = new LoggingSTErrorListener();
private final ThemeResolver themeResolver;
private StringToStyleExpressionRenderer renderer;
public TemplateExecutor(ThemeResolver themeResolver) {
this.themeResolver = themeResolver;
renderer = new StringToStyleExpressionRenderer(themeResolver);
}
/**
* Render template with a given attributes.
*
* @param template the ST template
* @param attributes the ST template attributes
* @return a rendered template
*/
public AttributedString render(String template, Map<String, Object> attributes) {
STGroup group = new STGroup();
group.setListener(ERROR_LISTENER);
group.registerRenderer(String.class, renderer);
ST st = new ST(group, template);
if (attributes != null) {
attributes.entrySet().stream().forEach(e -> st.add(e.getKey(), e.getValue()));
}
String templateRendered = st.render();
return themeResolver.evaluateExpression(templateRendered);
}
private static class LoggingSTErrorListener implements STErrorListener {
private final static Logger log = LoggerFactory.getLogger(LoggingSTErrorListener.class);
@Override
public void compileTimeError(STMessage msg) {
log.error("compileTimeError [{}]", msg);
}
@Override
public void runTimeError(STMessage msg) {
log.error("runTimeError [{}]", msg);
}
@Override
public void IOError(STMessage msg) {
log.error("IOError [{}]", msg);
}
@Override
public void internalError(STMessage msg) {
log.error("internalError [{}]", msg);
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
/**
* Contract representing a theme with its name and settings.
*
* {@link Theme} is a concept where you can request a {@code style} by using a theme {@code tag}.
* At this point an actual style is not known as it's going to get resolved from an
* enable {@code theme}.
*
* @author Janne Valkealahti
*/
public interface Theme {
/**
* Gets a theme name.
*
* @return a theme name.
*/
String getName();
/**
* Gets a theme settings.
*
* @return a theme settings
*/
ThemeSettings getSettings();
/**
* Create a {@link Theme}.
*
* @param name the theme name
* @param themeSettings the theme settings
* @return a theme
*/
public static Theme of(String name, ThemeSettings themeSettings) {
return new Theme() {
@Override
public String getName() {
return name;
}
@Override
public ThemeSettings getSettings() {
return themeSettings;
}
};
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.Assert;
/**
* Registry which stores {@link Theme}'s with its name.
*
* @author Janne Valkealahti
*/
public class ThemeRegistry {
private final Map<String, Theme> themes = new HashMap<>();
/**
* Gets a theme from a registry.
*
* @param name the theme name
* @return a theme
*/
public Theme get(String name) {
return themes.get(name);
}
/**
* Register a theme.
*
* @param theme the theme
*/
public void register(Theme theme) {
Assert.notNull(theme, "theme cannot be null");
themes.put(theme.getName(), theme);
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
import org.jline.style.MemoryStyleSource;
import org.jline.style.StyleExpression;
import org.jline.style.StyleResolver;
import org.jline.style.StyleSource;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;
/**
* Service which helps to do various things with styles.
*
* @author Janne Valkealahti
*/
public class ThemeResolver {
private StyleSource styleSource = new MemoryStyleSource();
private StyleResolver styleResolver = new StyleResolver(styleSource, "default");
private StyleExpression styleExpression = new StyleExpression(styleResolver);
private final Theme theme;
public ThemeResolver(ThemeRegistry themeRegistry, String themeName) {
this.theme = themeRegistry.get(themeName);
}
/**
* Evaluate expression.
*
* @param expression the expression
* @return evaluated attributed string
*/
public AttributedString evaluateExpression(String expression) {
return styleExpression.evaluate(expression);
}
/**
* Resolve style from a tag with activated theme.
*
* @param tag the tag
* @return a style
*/
public String resolveTag(String tag) {
return theme.getSettings().resolveTag(tag);
}
/**
* Resolve {@link AttributedStyle} from a {@code spec}.
*
* @param spec the spec
* @return resolved attributed style
*/
public AttributedStyle resolveStyle(String spec) {
return styleResolver.resolve(spec);
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
/**
* {@link ThemeSettings} is storing {@link Theme} related settings together and
* is base class for further customizations.
*
* Settings in this base class are default styles.
*
* @author Janne Valkealahti
*/
public abstract class ThemeSettings {
/**
* Represents some arbitrary {@code title}.
*/
public final static String TAG_TITLE = "title";
/**
* Represents some arbitrary {@code value}.
*/
public final static String TAG_VALUE = "value";
/**
* Styling for keys or names in a lists:
* <list key1> : list value1
* <list key2> : list value2
*/
public final static String TAG_LIST_KEY = "list-key";
/**
* Styling for keys or names in a lists:
* list key1 : <list value1>
* list key2 : <list value2>
*/
public final static String TAG_LIST_VALUE = "list-value";
public String title() {
return "bold";
}
public String value() {
return "fg:blue";
}
public String listKey() {
return null;
}
public String listValue() {
return "bold,fg:green";
}
/**
* Resolve a theme setting from a given tag.
*
* @param tag the tag
* @return a theme setting
*/
public String resolveTag(String tag) {
switch (tag) {
case TAG_TITLE:
return title();
case TAG_VALUE:
return value();
case TAG_LIST_KEY:
return listKey();
case TAG_LIST_VALUE:
return listValue();
}
throw new IllegalArgumentException(String.format("Unknown tag '%s'", tag));
}
/**
* Creates an instance of a default settings.
*
* @return a default theme settings
*/
public static ThemeSettings themeSettings() {
return new DefaultThemeSettings();
}
private static class DefaultThemeSettings extends ThemeSettings {
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
import java.util.HashMap;
import java.util.Map;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.jline.utils.AttributedStyle.BOLD;
public class TemplateExecutorTests {
private ThemeResolver themeResolver;
@BeforeEach
public void setup() {
ThemeRegistry themeRegistry = new ThemeRegistry();
themeRegistry.register(new Theme() {
@Override
public String getName() {
return "default";
}
@Override
public ThemeSettings getSettings() {
return ThemeSettings.themeSettings();
}
});
themeResolver = new ThemeResolver(themeRegistry, "default");
}
@Test
public void testSimple() {
TemplateExecutor executor = new TemplateExecutor(themeResolver);
String template = "<(\"foo\")>";
AttributedString result = executor.render(template, null);
AttributedString equalTo = new AttributedStringBuilder().append("foo").toAttributedString();
assertThat(result).isEqualTo(equalTo);
}
@Test
public void testWithExpression() {
TemplateExecutor executor = new TemplateExecutor(themeResolver);
String template = "<foo>";
Map<String, Object> attributes = new HashMap<>();
attributes.put("foo", "bar");
AttributedString result = executor.render(template, attributes);
AttributedString equalTo = new AttributedStringBuilder().append("bar").toAttributedString();
assertThat(result).isEqualTo(equalTo);
}
@Test
public void testWithTheme() {
TemplateExecutor executor = new TemplateExecutor(themeResolver);
String template = "<foo; format=\"title\">";
Map<String, Object> attributes = new HashMap<>();
attributes.put("foo", "bar");
AttributedString result = executor.render(template, attributes);
AttributedString equalTo = new AttributedStringBuilder().append("bar", BOLD).toAttributedString();
assertThat(result).isEqualTo(equalTo);
}
@Test
public void testNullDontStyle() {
TemplateExecutor executor = new TemplateExecutor(themeResolver);
String template = "<foo; format=\"list-key\">";
Map<String, Object> attributes = new HashMap<>();
attributes.put("foo", "bar");
AttributedString result = executor.render(template, attributes);
AttributedString equalTo = new AttributedStringBuilder().append("bar").toAttributedString();
assertThat(result).isEqualTo(equalTo);
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ThemeRegistryTests {
@Test
public void test() {
ThemeRegistry registry = new ThemeRegistry();
Theme theme = Theme.of("name1", ThemeSettings.themeSettings());
registry.register(theme);
assertThat(registry.get("name1")).isSameAs(theme);
assertThat(registry.get("name2")).isNull();
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ThemeResolverTests {
@Test
public void test() {
ThemeRegistry themeRegistry = new ThemeRegistry();
themeRegistry.register(new Theme() {
@Override
public String getName() {
return "default";
}
@Override
public ThemeSettings getSettings() {
return ThemeSettings.themeSettings();
}
});
ThemeResolver themeResolver = new ThemeResolver(themeRegistry, "default");
assertThat(themeResolver.resolveTag(ThemeSettings.TAG_TITLE)).isEqualTo("bold");
assertThat(themeResolver.resolveStyle("bold")).isEqualTo(AttributedStyle.BOLD);
assertThat(themeResolver.evaluateExpression("@{bold foo}"))
.isEqualTo(new AttributedString("foo", AttributedStyle.BOLD));
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://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.springframework.shell.style;
import org.jline.style.MemoryStyleSource;
import org.jline.style.StyleResolver;
import org.jline.style.StyleSource;
import org.jline.utils.AttributedStyle;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ThemeSettingsTests {
@Test
public void test() {
ThemeSettings themeSettings = ThemeSettings.themeSettings();
String resolveTag = themeSettings.resolveTag(ThemeSettings.TAG_TITLE);
assertThat(resolveTag).isEqualTo("bold");
StyleSource styleSource = new MemoryStyleSource();
StyleResolver styleResolver = new StyleResolver(styleSource, "default");
AttributedStyle style = styleResolver.resolve(resolveTag);
assertThat(style).isEqualTo(AttributedStyle.BOLD);
}
}