Fix counting error in SourceOptions

This commit is contained in:
Dave Syer 2014-01-14 14:44:12 +00:00
parent a5f16d46fe
commit 8ff2a88712
2 changed files with 21 additions and 5 deletions

View File

@ -16,10 +16,13 @@
package org.springframework.boot.cli.command;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import joptsimple.OptionSet;
@ -34,6 +37,7 @@ import org.springframework.boot.cli.compiler.GroovyCompilerConfigurationAdapter;
import org.springframework.boot.cli.compiler.GroovyCompilerScope;
import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory;
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
import org.springframework.core.env.JOptCommandLinePropertySource;
import org.springframework.util.StringUtils;
/**
@ -116,10 +120,17 @@ public class InitCommand extends OptionParsingCommand {
for (Class<?> type : classes) {
if (Script.class.isAssignableFrom(type)) {
Script script = (Script) type.newInstance();
JOptCommandLinePropertySource properties = new JOptCommandLinePropertySource(
options);
Map<String, Object> map = new HashMap<String, Object>();
for (String key : properties.getPropertyNames()) {
map.put(key, properties.getProperty(key));
}
script.setBinding(new Binding(map));
script.run();
}
enhanced = true;
}
enhanced = true;
}
if (enhanced) {

View File

@ -61,6 +61,7 @@ public class SourceOptions {
String... defaultPaths) {
List<?> nonOptionArguments = optionSet.nonOptionArguments();
List<String> sources = new ArrayList<String>();
int sourceArgCount = 0;
for (Object option : nonOptionArguments) {
if (option instanceof String) {
String filename = (String) option;
@ -73,14 +74,18 @@ public class SourceOptions {
sources.add(url);
}
}
if ((filename.endsWith(".groovy") || filename.endsWith(".java"))
&& urls.isEmpty()) {
throw new IllegalArgumentException("Can't find " + filename);
if ((filename.endsWith(".groovy") || filename.endsWith(".java"))) {
if (urls.isEmpty()) {
throw new IllegalArgumentException("Can't find " + filename);
}
else {
sourceArgCount++;
}
}
}
}
this.args = Collections.unmodifiableList(nonOptionArguments.subList(
sources.size(), nonOptionArguments.size()));
sourceArgCount, nonOptionArguments.size()));
if (sources.size() == 0) {
if (defaultPaths.length == 0) {
throw new IllegalArgumentException(