SHL-11 customie banner

This commit is contained in:
Jarred Li
2012-04-12 16:05:34 +08:00
parent cb7f85b9b6
commit 2b05b13db6
17 changed files with 437 additions and 65 deletions

18
pom.xml
View File

@@ -82,10 +82,22 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<version>${project.version}</version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>

View File

@@ -65,6 +65,9 @@
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${jar.mainclass}</mainClass>
</manifest>
<manifestEntries>
<version>${project.version}</version>
</manifestEntries>
</archive>
</configuration>
</plugin>

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2011-2012 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
*
* http://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.samples.helloworld.commands;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CommandMarker;
import org.springframework.shell.plugin.BannerProvider;
import org.springframework.stereotype.Component;
/**
* @author Jarred Li
*
*/
@Component
public class MyBannerProvider implements BannerProvider, CommandMarker {
/* (non-Javadoc)
* @see org.springframework.core.Ordered#getOrder()
*/
public int getOrder() {
return 1;
}
/* (non-Javadoc)
* @see org.springframework.shell.plugin.BannerProvider#getBanner()
*/
@CliCommand(value = { "version" }, help = "Displays shell version")
public String getBanner() {
return "vHelper. Version: " + this.getVersion();
}
/* (non-Javadoc)
* @see org.springframework.shell.plugin.BannerProvider#getVersion()
*/
public String getVersion() {
return "1.0.1";
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.roo.support.util.Assert;
import org.springframework.roo.support.util.IOUtils;
import org.springframework.roo.support.util.MathUtils;
import org.springframework.roo.support.util.StringUtils;
import org.springframework.roo.support.util.VersionUtils;
/**
* Provides a base {@link Shell} implementation.
@@ -395,47 +396,8 @@ public abstract class AbstractShell extends AbstractShellStatusPublisher impleme
return sb.toString();
}
public static String versionInfo() {
// Try to determine the bundle version
String bundleVersion = null;
String gitCommitHash = null;
JarFile jarFile = null;
try {
URL classContainer = AbstractShell.class.getProtectionDomain().getCodeSource().getLocation();
if (classContainer.toString().endsWith(".jar")) {
// Attempt to obtain the "Bundle-Version" version from the manifest
jarFile = new JarFile(new File(classContainer.toURI()), false);
ZipEntry manifestEntry = jarFile.getEntry("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(jarFile.getInputStream(manifestEntry));
bundleVersion = manifest.getMainAttributes().getValue("Bundle-Version");
gitCommitHash = manifest.getMainAttributes().getValue("Git-Commit-Hash");
}
} catch (IOException ignoreAndMoveOn) {
} catch (URISyntaxException ignoreAndMoveOn) {
} finally {
IOUtils.closeQuietly(jarFile);
}
StringBuilder sb = new StringBuilder();
if (bundleVersion != null) {
sb.append(bundleVersion);
}
if (gitCommitHash != null && gitCommitHash.length() > 7) {
if (sb.length() > 0) {
sb.append(" "); // to separate from version
}
sb.append("[rev ");
sb.append(gitCommitHash.substring(0,7));
sb.append("]");
}
if (sb.length() == 0) {
sb.append("UNKNOWN VERSION");
}
return sb.toString();
public String versionInfo(){
return VersionUtils.versionInfo();
}
public String getShellPrompt() {

View File

@@ -34,6 +34,7 @@ import org.springframework.roo.support.util.CollectionUtils;
import org.springframework.roo.support.util.ExceptionUtils;
import org.springframework.roo.support.util.FileCopyUtils;
import org.springframework.roo.support.util.StringUtils;
import org.springframework.roo.support.util.VersionUtils;
import org.springframework.roo.support.util.XmlElementBuilder;
import org.springframework.roo.support.util.XmlUtils;
import org.w3c.dom.CDATASection;
@@ -914,7 +915,7 @@ public class SimpleParser implements Parser {
appendix.setAttribute("version", "5.0");
appendix.setAttribute("xml:id", "command-index");
appendix.appendChild(new XmlElementBuilder("title", document).setText("Command Index").build());
appendix.appendChild(new XmlElementBuilder("para", document).setText("This appendix was automatically built from Roo " + AbstractShell.versionInfo() + ".").build());
appendix.appendChild(new XmlElementBuilder("para", document).setText("This appendix was automatically built from Roo " + VersionUtils.versionInfo() + ".").build());
appendix.appendChild(new XmlElementBuilder("para", document).setText("Commands are listed in alphabetic order, and are shown in monospaced font with any mandatory options you must specify when using the command. Most commands accept a large number of options, and all of the possible options for each command are presented in this appendix.").build());
for (Element section : builtSections) {

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2011-2012 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
*
* http://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.roo.support.util;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
/**
* @author Jarred Li
*
*/
public class VersionUtils {
public static String versionInfo() {
// Try to determine the bundle version
String bundleVersion = null;
JarFile jarFile = null;
try {
URL classContainer = VersionUtils.class.getProtectionDomain().getCodeSource().getLocation();
if (classContainer.toString().endsWith(".jar")) {
// Attempt to obtain the "Bundle-Version" version from the manifest
jarFile = new JarFile(new File(classContainer.toURI()), false);
ZipEntry manifestEntry = jarFile.getEntry("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(jarFile.getInputStream(manifestEntry));
bundleVersion = manifest.getMainAttributes().getValue("version");
}
} catch (IOException ignoreAndMoveOn) {
} catch (URISyntaxException ignoreAndMoveOn) {
} finally {
IOUtils.closeQuietly(jarFile);
}
StringBuilder sb = new StringBuilder();
if (bundleVersion != null) {
sb.append(bundleVersion);
}
if (sb.length() == 0) {
sb.append("UNKNOWN VERSION");
}
return sb.toString();
}
}

View File

@@ -41,6 +41,7 @@ import org.springframework.roo.support.util.FileCopyUtils;
import org.springframework.roo.support.util.IOUtils;
import org.springframework.roo.support.util.OsUtils;
import org.springframework.roo.support.util.StringUtils;
import org.springframework.shell.plugin.BannerProvider;
import org.springframework.shell.plugin.HistoryFileProvider;
import org.springframework.shell.plugin.PromptProvider;
@@ -553,10 +554,29 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker,
return promptText;
}
private String getBannerText(){
String bannerText = null;
Map<String, BannerProvider> bannerProviders = getBeansInFactory(BannerProvider.class);
int order = Integer.MIN_VALUE;
for(BannerProvider p : bannerProviders.values()){
if(p.getOrder() > order){
order = p.getOrder();
bannerText = p.getBanner();
}
}
bannerText = (bannerText == null)?Constant.COMMAND_LINE_PROMPT:bannerText;
return bannerText;
}
private <T> Map<String, T> getBeansInFactory(Class<T> t){
Map<String, T> result = BeanFactoryUtils.beansOfTypeIncludingAncestors(
this.applicatonContext, t);
return result;
}
public String version(String text){
return getBannerText();
}
}

View File

@@ -72,23 +72,5 @@ public class JLineShellComponent extends JLineShell implements Lifecycle {
return null;
}
@CliCommand(value = { "version" }, help = "Displays shell version")
public String version(@CliOption(key = "", help = "Special version flags") final String extra) {
StringBuilder sb = new StringBuilder();
sb.append(" _____ _ ").append(LINE_SEPARATOR);
sb.append("/ ___| (_)").append(LINE_SEPARATOR);
sb.append("\\ `--, _ __ _ __ _ _ __ __ _ ").append(LINE_SEPARATOR);
sb.append(" `--. \\ '_ \\| '__| | '_ \\ / _` |").append(LINE_SEPARATOR);
sb.append("/\\__/ / |_) | | | | | | | (_| |").append(LINE_SEPARATOR);
sb.append("\\____/| .__/|_| |_|_| |_|\\__, |").append(LINE_SEPARATOR);
sb.append(" | | __/ |").append(LINE_SEPARATOR);
sb.append(" |_| |___/ ").append(" ").append(versionInfo()).append(LINE_SEPARATOR);
sb.append(LINE_SEPARATOR);
return sb.toString();
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2011-2012 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
*
* http://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.plugin;
import org.springframework.core.Ordered;
/**
* Banner provider. Plugin should implements this interface to replace the version banner.
* <code>getOrder</code> should be > 1 to override default.
*
* @author Jarred Li
* @since 1.01
*
*/
public interface BannerProvider extends Ordered {
String getBanner();
String getVersion();
}

View File

@@ -18,7 +18,8 @@ package org.springframework.shell.plugin;
import org.springframework.core.Ordered;
/**
* To get history file name
* History file name provider. Plugin should implements this interface to customize history file.
* <code>getOrder</code> should be > 1 to override default.
*
* @author Jarred Li
* @since 1.0

View File

@@ -18,7 +18,9 @@ package org.springframework.shell.plugin;
import org.springframework.core.Ordered;
/**
* Provider for prompt
* Shell prompt provider. Plugin should implements this interface to customize prompt.
*
* <code>getOrder</code> should be > 1 to override default.
*
* @author Jarred Li
*

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2011-2012 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
*
* http://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.plugin.support;
import static org.springframework.roo.support.util.StringUtils.LINE_SEPARATOR;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CommandMarker;
import org.springframework.roo.support.util.VersionUtils;
import org.springframework.shell.plugin.BannerProvider;
import org.springframework.stereotype.Component;
/**
* Default Banner provider.
*
* @author Jarred Li
*
*/
@Component
public class DefaultBannerProvider implements BannerProvider, CommandMarker{
/* (non-Javadoc)
* @see org.springframework.core.Ordered#getOrder()
*/
public int getOrder() {
return 0;
}
/* (non-Javadoc)
* @see org.springframework.shell.plugin.BannerProvider#getBanner()
*/
@CliCommand(value = { "shell-version" }, help = "Displays shell version")
public String getBanner() {
StringBuilder sb = new StringBuilder();
sb.append(" _____ _ ").append(LINE_SEPARATOR);
sb.append("/ ___| (_)").append(LINE_SEPARATOR);
sb.append("\\ `--, _ __ _ __ _ _ __ __ _ ").append(LINE_SEPARATOR);
sb.append(" `--. \\ '_ \\| '__| | '_ \\ / _` |").append(LINE_SEPARATOR);
sb.append("/\\__/ / |_) | | | | | | | (_| |").append(LINE_SEPARATOR);
sb.append("\\____/| .__/|_| |_|_| |_|\\__, |").append(LINE_SEPARATOR);
sb.append(" | | __/ |").append(LINE_SEPARATOR);
sb.append(" |_| |___/ ").append(" ").append(this.getVersion()).append(LINE_SEPARATOR);
sb.append(LINE_SEPARATOR);
return sb.toString();
}
/* (non-Javadoc)
* @see org.springframework.shell.plugin.BannerProvider#getVersion()
*/
public String getVersion() {
return VersionUtils.versionInfo();
}
}

View File

@@ -20,6 +20,8 @@ import org.springframework.shell.plugin.HistoryFileProvider;
import org.springframework.stereotype.Component;
/**
* Default history file provider. Default file is {@link org.springframework.shell.Constant.HISTORY_FILE_NAME}
*
* @author Jarred Li
*
*/

View File

@@ -17,11 +17,15 @@ package org.springframework.shell.plugin.support;
import org.springframework.shell.Constant;
import org.springframework.shell.plugin.PromptProvider;
import org.springframework.stereotype.Component;
/**
* Default prompt provider. The promp text is {@link org.springframework.shell.Constant.COMMAND_LINE_PROMPT}
*
* @author Jarred Li
*
*/
@Component
public class DefaultPromptProvider implements PromptProvider{
/* (non-Javadoc)

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2011-2012 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
*
* http://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.plugin.support;
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Jarred Li
*
*/
public class DefaultBannerProviderTest {
private DefaultBannerProvider banner = new DefaultBannerProvider();
/**
* Test method for {@link org.springframework.shell.plugin.support.DefaultBannerProvider#getOrder()}.
*/
@Test
public void testGetOrder() {
Assert.assertTrue(banner.getOrder() == 0);
}
/**
* Test method for {@link org.springframework.shell.plugin.support.DefaultBannerProvider#getBanner()}.
*/
@Test
public void testGetBanner() {
assertNotNull(banner.getBanner());
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2011-2012 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
*
* http://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.plugin.support;
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.shell.Constant;
import org.springframework.shell.plugin.HistoryFileProvider;
/**
* @author Jarred Li
*
*/
public class DefaultHistoryFileProviderTest {
private HistoryFileProvider historyFile = new DefaultHistoryFileProvider();
/**
* Test method for {@link org.springframework.shell.plugin.support.DefaultHistoryFileProvider#getOrder()}.
*/
@Test
public void testGetOrder() {
Assert.assertTrue(historyFile.getOrder() == 0);
}
/**
* Test method for {@link org.springframework.shell.plugin.support.DefaultHistoryFileProvider#getHistoryFileName()}.
*/
@Test
public void testGetHistoryFileName() {
assertNotNull(historyFile.getHistoryFileName());
assertEquals(Constant.HISTORY_FILE_NAME, historyFile.getHistoryFileName());
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2011-2012 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
*
* http://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.plugin.support;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.shell.Constant;
import org.springframework.shell.plugin.PromptProvider;
/**
* @author Jarred Li
*
*/
public class DefaultPromptProviderTest {
private static PromptProvider prompt;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
prompt = new DefaultPromptProvider();
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
prompt = null;
}
/**
* Test method for {@link org.springframework.shell.plugin.support.DefaultPromptProvider#getOrder()}.
*/
@Test
public void testGetOrder() {
assertEquals(0, prompt.getOrder());
}
/**
* Test method for {@link org.springframework.shell.plugin.support.DefaultPromptProvider#getPromptText()}.
*/
@Test
public void testGetPromptText() {
assertEquals(Constant.COMMAND_LINE_PROMPT, prompt.getPromptText());
}
}