Extend programming model for script commands

This commit is contained in:
Dave Syer
2014-01-04 19:24:55 +00:00
parent b5db4d3ff4
commit 1e75c0a55b
17 changed files with 469 additions and 281 deletions

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2012-2013 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 cli.command;
import groovy.lang.Closure;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.cli.Command;
import org.springframework.boot.cli.command.InitCommand.Commands;
import org.springframework.boot.cli.command.OptionHandler;
import org.springframework.boot.cli.command.ScriptCompilationCustomizer;
import org.springframework.boot.cli.compiler.GroovyCompiler;
import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
import org.springframework.boot.cli.compiler.GroovyCompilerScope;
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*/
public class ScriptCompilationCustomizerTests {
private TestGroovyCompilerConfiguration configuration = new TestGroovyCompilerConfiguration();
private GroovyCompiler compiler = new GroovyCompiler(this.configuration);
@Rule
public OutputCapture output = new OutputCapture();
@Before
public void init() {
this.compiler.addCompilationCustomizers(new ScriptCompilationCustomizer());
}
@Test
public void simpleCompile() throws Exception {
Class<?>[] types = this.compiler.compile(new File(
"src/test/resources/scripts/command.groovy"));
Class<?> main = types[0];
assertEquals("org.test.command.TestCommand", main.getName());
assertTrue(Command.class.isAssignableFrom(main));
}
@Test
public void addsOptionHandler() throws Exception {
Class<?>[] types = this.compiler.compile(new File(
"src/test/resources/scripts/handler.groovy"));
Class<?> main = types[0];
assertTrue(OptionHandler.class.isAssignableFrom(main));
}
@Test
public void addsCommands() throws Exception {
Class<?>[] types = this.compiler.compile(new File(
"src/test/resources/scripts/options.groovy"));
Class<?> main = types[0];
assertTrue(Commands.class.isAssignableFrom(main));
}
@Test
public void commandsExecutable() throws Exception {
Class<?>[] types = this.compiler.compile(new File(
"src/test/resources/scripts/options.groovy"));
Class<?> main = types[0];
Map<String, Closure<?>> commands = ((Commands) main.newInstance()).getCommands();
assertEquals(1, commands.size());
assertEquals("foo", commands.keySet().iterator().next());
Closure<?> closure = commands.values().iterator().next();
closure.call(); // what about args?
assertTrue(this.output.toString().contains("Hello Command"));
}
private static class TestGroovyCompilerConfiguration implements
GroovyCompilerConfiguration {
@Override
public GroovyCompilerScope getScope() {
return GroovyCompilerScope.EXTENSION;
}
@Override
public boolean isGuessImports() {
return true;
}
@Override
public boolean isGuessDependencies() {
return false;
}
@Override
public boolean isAutoconfigure() {
return true;
}
@Override
public String[] getClasspath() {
return new String[0];
}
@Override
public List<RepositoryConfiguration> getRepositoryConfiguration() {
return Collections.emptyList();
}
}
}

View File

@@ -69,6 +69,12 @@ public class InitCommandTests {
assertTrue(this.output.toString().contains("Hello Grab"));
}
@Test
public void initCommand() throws Exception {
this.command.run("src/test/resources/command.groovy");
verify(this.cli, times(this.defaultCount + 1)).register(any(Command.class));
}
@Test(expected = IllegalArgumentException.class)
public void initNonExistentScript() throws Exception {
this.command.run("nonexistent.groovy");
@@ -78,7 +84,7 @@ public class InitCommandTests {
@Test
public void initDefault() throws Exception {
this.command.run();
assertTrue(this.output.toString().contains("Hello World"));
assertTrue(this.output.toString().contains("Hello Init"));
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2012-2013 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.boot.cli.command;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.cli.SpringCli;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*/
public class ScriptCommandTests {
@Rule
public OutputCapture output = new OutputCapture();
public static boolean executed = false;
private SpringCli cli;
private InitCommand init;
private ClassLoader classLoader;
@Before
public void init() {
this.classLoader = Thread.currentThread().getContextClassLoader();
this.cli = new SpringCli();
this.init = this.cli.getInitCommand();
executed = false;
}
@After
public void close() {
Thread.currentThread().setContextClassLoader(this.classLoader);
}
@Test
public void command() throws Exception {
this.init.run("src/test/resources/commands/command.groovy");
this.cli.find("foo").run("Foo");
assertTrue(this.output.toString().contains("Hello Foo"));
}
@Test
public void handler() throws Exception {
this.init.run("src/test/resources/commands/handler.groovy");
this.cli.find("foo").run("Foo", "--foo=bar");
assertTrue(executed);
}
@Test
@Ignore
public void options() throws Exception {
this.init.run("src/test/resources/commands/options.groovy");
this.cli.find("foo").run("Foo", "--foo=bar");
assertTrue(executed);
}
}

View File

@@ -0,0 +1,19 @@
class MyCommand implements Command {
String name = "foo"
String description = "My script command"
String help = "No options"
String usageHelp = "Not very useful"
Collection<String> optionsHelp = ["No options"]
boolean optionCommand = false
void run(String... args) {
println "Hello ${args[0]}"
}
}

View File

@@ -16,6 +16,8 @@
package org.test.command
import java.util.Collection;
class TestCommand implements Command {
String name = "foo"
@@ -25,9 +27,12 @@ class TestCommand implements Command {
String help = "No options"
String usageHelp = "Not very useful"
Collection<String> optionsHelp = ["No options"]
boolean optionCommand = false
void run(String... args) {
org.springframework.boot.cli.command.ScriptCommandTests.executed = true
println "Hello ${args[0]}"
}

View File

@@ -16,13 +16,13 @@
package org.test.command
import joptsimple.OptionSet
@Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r")
import org.eclipse.jgit.api.Git
class TestCommand extends OptionHandler {
String name = "foo"
void options() {
option "foo", "Foo set"
@@ -30,7 +30,7 @@ class TestCommand extends OptionHandler {
void run(OptionSet options) {
// Demonstrate use of Grape.grab to load dependencies before running
println "Clean : " + Git.open(".." as File).status().call().isClean()
println "Clean: " + Git.open(".." as File).status().call().isClean()
org.springframework.boot.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')}"
}

View File

@@ -14,9 +14,8 @@
* limitations under the License.
*/
def run = { msg ->
org.springframework.boot.cli.command.ScriptCommandTests.executed = true
println "Hello ${msg}"
}
command("foo") { args ->
run
org.springframework.boot.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')} ${options.valueOf('bar')}"
}

View File

@@ -1,23 +0,0 @@
/*
* Copyright 2012-2013 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.
*/
options {
option "foo", "Foo set"
option "bar", "Bar has an argument of type int" withOptionalArg() ofType Integer
}
org.springframework.boot.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')} ${options.valueOf('bar')}"

View File

@@ -1 +1 @@
println "Hello World"
println "Hello Init"

View File

@@ -14,14 +14,26 @@
* limitations under the License.
*/
class TestCommand implements Runnable {
def msg
TestCommand(String msg) {
this.msg = msg
}
void run() {
org.springframework.boot.cli.command.ScriptCommandTests.executed = true
println "Hello ${msg}"
package org.test.command
import java.util.Collection;
class TestCommand implements Command {
String name = "foo"
String description = "My script command"
String help = "No options"
String usageHelp = "Not very useful"
Collection<String> optionsHelp = ["No options"]
boolean optionCommand = false
void run(String... args) {
println "Hello ${args[0]}"
}
}
new TestCommand(args[0])

View File

@@ -14,9 +14,24 @@
* limitations under the License.
*/
void options() {
option "foo", "Foo set"
}
package org.test.command
org.springframework.boot.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')}"
@Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r")
import org.eclipse.jgit.api.Git
class TestCommand extends OptionHandler {
String name = "foo"
void options() {
option "foo", "Foo set"
}
void run(OptionSet options) {
// Demonstrate use of Grape.grab to load dependencies before running
println "Clean: " + Git.open(".." as File).status().call().isClean()
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')}"
}
}

View File

@@ -14,4 +14,8 @@
* limitations under the License.
*/
println "Hello ${args[0]}"
command("foo") { args ->
println "Hello Command"
}