diff --git a/pom.xml b/pom.xml index f7879d8a..5e46f5d1 100644 --- a/pom.xml +++ b/pom.xml @@ -82,10 +82,22 @@ org.apache.maven.plugins maven-compiler-plugin - 1.5 - 1.5 + 1.6 + 1.6 - + + + + org.apache.maven.plugins + maven-jar-plugin + + + + ${project.version} + + + + diff --git a/samples/helloworld/pom.xml b/samples/helloworld/pom.xml index 98fef2cc..bc18ee40 100644 --- a/samples/helloworld/pom.xml +++ b/samples/helloworld/pom.xml @@ -65,6 +65,9 @@ lib/ ${jar.mainclass} + + ${project.version} + diff --git a/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyBannerProvider.java b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyBannerProvider.java new file mode 100644 index 00000000..4708bf0d --- /dev/null +++ b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyBannerProvider.java @@ -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"; + } + +} diff --git a/src/main/java/org/springframework/roo/shell/AbstractShell.java b/src/main/java/org/springframework/roo/shell/AbstractShell.java index 1172347e..91a52afb 100644 --- a/src/main/java/org/springframework/roo/shell/AbstractShell.java +++ b/src/main/java/org/springframework/roo/shell/AbstractShell.java @@ -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() { diff --git a/src/main/java/org/springframework/roo/shell/SimpleParser.java b/src/main/java/org/springframework/roo/shell/SimpleParser.java index 84015c17..88210ee2 100644 --- a/src/main/java/org/springframework/roo/shell/SimpleParser.java +++ b/src/main/java/org/springframework/roo/shell/SimpleParser.java @@ -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) { diff --git a/src/main/java/org/springframework/roo/support/util/VersionUtils.java b/src/main/java/org/springframework/roo/support/util/VersionUtils.java new file mode 100644 index 00000000..b95970a4 --- /dev/null +++ b/src/main/java/org/springframework/roo/support/util/VersionUtils.java @@ -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(); + } + +} diff --git a/src/main/java/org/springframework/shell/JLineShell.java b/src/main/java/org/springframework/shell/JLineShell.java index 172f51f9..8422d8d2 100644 --- a/src/main/java/org/springframework/shell/JLineShell.java +++ b/src/main/java/org/springframework/shell/JLineShell.java @@ -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 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 Map getBeansInFactory(Class t){ Map result = BeanFactoryUtils.beansOfTypeIncludingAncestors( this.applicatonContext, t); return result; } + public String version(String text){ + return getBannerText(); + } + } diff --git a/src/main/java/org/springframework/shell/JLineShellComponent.java b/src/main/java/org/springframework/shell/JLineShellComponent.java index 2ad0aaa1..880f3a08 100644 --- a/src/main/java/org/springframework/shell/JLineShellComponent.java +++ b/src/main/java/org/springframework/shell/JLineShellComponent.java @@ -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(); - - } } \ No newline at end of file diff --git a/src/main/java/org/springframework/shell/plugin/BannerProvider.java b/src/main/java/org/springframework/shell/plugin/BannerProvider.java new file mode 100644 index 00000000..cd6fb89c --- /dev/null +++ b/src/main/java/org/springframework/shell/plugin/BannerProvider.java @@ -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. + * getOrder should be > 1 to override default. + * + * @author Jarred Li + * @since 1.01 + * + */ +public interface BannerProvider extends Ordered { + + String getBanner(); + + String getVersion(); + +} diff --git a/src/main/java/org/springframework/shell/plugin/HistoryFileProvider.java b/src/main/java/org/springframework/shell/plugin/HistoryFileProvider.java index 936c5b1b..b4489581 100644 --- a/src/main/java/org/springframework/shell/plugin/HistoryFileProvider.java +++ b/src/main/java/org/springframework/shell/plugin/HistoryFileProvider.java @@ -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. + * getOrder should be > 1 to override default. * * @author Jarred Li * @since 1.0 diff --git a/src/main/java/org/springframework/shell/plugin/PromptProvider.java b/src/main/java/org/springframework/shell/plugin/PromptProvider.java index 5ffd36ea..b0c8bdd4 100644 --- a/src/main/java/org/springframework/shell/plugin/PromptProvider.java +++ b/src/main/java/org/springframework/shell/plugin/PromptProvider.java @@ -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. + * + * getOrder should be > 1 to override default. * * @author Jarred Li * diff --git a/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java b/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java new file mode 100644 index 00000000..808a78fe --- /dev/null +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java @@ -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(); + } + +} diff --git a/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileProvider.java b/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileProvider.java index 169b7f5b..08d1112e 100644 --- a/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileProvider.java +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileProvider.java @@ -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 * */ diff --git a/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java b/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java index 39ad579e..36f29890 100644 --- a/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java @@ -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) diff --git a/src/test/java/org/springframework/shell/plugin/support/DefaultBannerProviderTest.java b/src/test/java/org/springframework/shell/plugin/support/DefaultBannerProviderTest.java new file mode 100644 index 00000000..6301324c --- /dev/null +++ b/src/test/java/org/springframework/shell/plugin/support/DefaultBannerProviderTest.java @@ -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()); + } + +} diff --git a/src/test/java/org/springframework/shell/plugin/support/DefaultHistoryFileProviderTest.java b/src/test/java/org/springframework/shell/plugin/support/DefaultHistoryFileProviderTest.java new file mode 100644 index 00000000..057a3aaf --- /dev/null +++ b/src/test/java/org/springframework/shell/plugin/support/DefaultHistoryFileProviderTest.java @@ -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()); + } + +} diff --git a/src/test/java/org/springframework/shell/plugin/support/DefaultPromptProviderTest.java b/src/test/java/org/springframework/shell/plugin/support/DefaultPromptProviderTest.java new file mode 100644 index 00000000..cac00c18 --- /dev/null +++ b/src/test/java/org/springframework/shell/plugin/support/DefaultPromptProviderTest.java @@ -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()); + } + +}