Add grab command to collect script's dependencies
Previously, run --local could be used to collect a script's dependencies in ./repository. However, with this mechanism it wasn't possible to collect the dependencies without running the application. This commit adds a new command, grab, that can be used to collect a script's dependencies in ./repository without having to run it. run is configured with ./repository as a location in which it can find its dependencies so that the previously collected dependencies can be used when subsequently running the app. As part of this work RunCommand and TestCommand have been refactored to use common code for their common options: --no-guess-imports --no-guess-dependencies --classpath Previously, the declaration and handling of the options was duplicated in the two classes. GrabCommand also has these three options and uses the same common code.pull/132/head
parent
c50fe0733b
commit
820f43d3bc
@ -0,0 +1,4 @@
|
||||
@Grab('spring-jdbc')
|
||||
class GrabTest {
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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 joptsimple.OptionSpec;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
* An {@link OptionHandler} for commands that result in the compilation of one or more
|
||||
* Groovy scripts
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class CompilerOptionHandler extends OptionHandler {
|
||||
|
||||
private OptionSpec<Void> noGuessImportsOption;
|
||||
|
||||
private OptionSpec<Void> noGuessDependenciesOption;
|
||||
|
||||
private OptionSpec<String> classpathOption;
|
||||
|
||||
@Override
|
||||
protected final void options() {
|
||||
this.noGuessImportsOption = option("no-guess-imports",
|
||||
"Do not attempt to guess imports");
|
||||
this.noGuessDependenciesOption = option("no-guess-dependencies",
|
||||
"Do not attempt to guess dependencies");
|
||||
this.classpathOption = option(asList("classpath", "cp"),
|
||||
"Additional classpath entries").withRequiredArg();
|
||||
doOptions();
|
||||
}
|
||||
|
||||
protected void doOptions() {
|
||||
}
|
||||
|
||||
public OptionSpec<Void> getNoGuessImportsOption() {
|
||||
return this.noGuessImportsOption;
|
||||
}
|
||||
|
||||
public OptionSpec<Void> getNoGuessDependenciesOption() {
|
||||
return this.noGuessDependenciesOption;
|
||||
}
|
||||
|
||||
public OptionSpec<String> getClasspathOption() {
|
||||
return this.classpathOption;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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 java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import joptsimple.OptionSet;
|
||||
|
||||
import org.springframework.boot.cli.Command;
|
||||
import org.springframework.boot.cli.compiler.GroovyCompiler;
|
||||
import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
|
||||
import org.springframework.boot.cli.compiler.GroovyCompilerConfigurationAdapter;
|
||||
import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory;
|
||||
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link Command} to grab the dependencies of one or more Groovy scripts
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class GrabCommand extends OptionParsingCommand {
|
||||
|
||||
public GrabCommand() {
|
||||
super("grab", "Download a spring groovy script's dependencies to ./repository",
|
||||
new GrabOptionHandler());
|
||||
}
|
||||
|
||||
private static final class GrabOptionHandler extends CompilerOptionHandler {
|
||||
|
||||
@Override
|
||||
protected void run(OptionSet options) throws Exception {
|
||||
FileOptions fileOptions = new FileOptions(options);
|
||||
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File(
|
||||
getM2HomeDirectory(), "repository").toURI(), true));
|
||||
|
||||
GroovyCompilerConfiguration configuration = new GroovyCompilerConfigurationAdapter(
|
||||
options, this, repositoryConfiguration);
|
||||
|
||||
if (System.getProperty("grape.root") == null) {
|
||||
System.setProperty("grape.root", ".");
|
||||
}
|
||||
|
||||
GroovyCompiler groovyCompiler = new GroovyCompiler(configuration);
|
||||
groovyCompiler.compile(fileOptions.getFilesArray());
|
||||
}
|
||||
|
||||
private File getM2HomeDirectory() {
|
||||
String mavenRoot = System.getProperty("maven.home");
|
||||
if (StringUtils.hasLength(mavenRoot)) {
|
||||
return new File(mavenRoot);
|
||||
}
|
||||
return new File(System.getProperty("user.home"), ".m2");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.compiler;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import joptsimple.OptionSet;
|
||||
import joptsimple.OptionSpec;
|
||||
|
||||
import org.springframework.boot.cli.command.CompilerOptionHandler;
|
||||
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
|
||||
|
||||
/**
|
||||
* Simple adapter class to present an {@link OptionSet} as a
|
||||
* {@link GroovyCompilerConfiguration}
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class GroovyCompilerConfigurationAdapter implements GroovyCompilerConfiguration {
|
||||
|
||||
private final OptionSet options;
|
||||
|
||||
private final CompilerOptionHandler optionHandler;
|
||||
|
||||
private final List<RepositoryConfiguration> repositoryConfiguration;
|
||||
|
||||
protected GroovyCompilerConfigurationAdapter(OptionSet optionSet,
|
||||
CompilerOptionHandler compilerOptionHandler) {
|
||||
this(optionSet, compilerOptionHandler, RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration());
|
||||
}
|
||||
|
||||
public GroovyCompilerConfigurationAdapter(OptionSet optionSet,
|
||||
CompilerOptionHandler compilerOptionHandler,
|
||||
List<RepositoryConfiguration> repositoryConfiguration) {
|
||||
this.options = optionSet;
|
||||
this.optionHandler = compilerOptionHandler;
|
||||
this.repositoryConfiguration = repositoryConfiguration;
|
||||
}
|
||||
|
||||
protected OptionSet getOptions() {
|
||||
return this.options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroovyCompilerScope getScope() {
|
||||
return GroovyCompilerScope.DEFAULT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGuessImports() {
|
||||
return !this.options.has(this.optionHandler.getNoGuessImportsOption());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGuessDependencies() {
|
||||
return !this.options.has(this.optionHandler.getNoGuessDependenciesOption());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getClasspath() {
|
||||
OptionSpec<String> classpathOption = this.optionHandler.getClasspathOption();
|
||||
if (this.options.has(classpathOption)) {
|
||||
return this.options.valueOf(classpathOption).split(":");
|
||||
}
|
||||
return NO_CLASSPATH;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RepositoryConfiguration> getRepositoryConfiguration() {
|
||||
return this.repositoryConfiguration;
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.compiler;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
|
||||
|
||||
/**
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public final class RepositoryConfigurationFactory {
|
||||
|
||||
private static final RepositoryConfiguration MAVEN_CENTRAL = new RepositoryConfiguration(
|
||||
"central", URI.create("http://repo1.maven.org/maven2/"), false);
|
||||
|
||||
private static final RepositoryConfiguration SPRING_MILESTONE = new RepositoryConfiguration(
|
||||
"spring-milestone", URI.create("http://repo.spring.io/milestone"), false);
|
||||
|
||||
private static final RepositoryConfiguration SPRING_SNAPSHOT = new RepositoryConfiguration(
|
||||
"spring-snapshot", URI.create("http://repo.spring.io/snapshot"), true);
|
||||
|
||||
/**
|
||||
* @return the newly-created default repository configuration
|
||||
*/
|
||||
public static List<RepositoryConfiguration> createDefaultRepositoryConfiguration() {
|
||||
List<RepositoryConfiguration> repositoryConfiguration = new ArrayList<RepositoryConfiguration>();
|
||||
|
||||
repositoryConfiguration.add(MAVEN_CENTRAL);
|
||||
|
||||
if (!Boolean.getBoolean("disableSpringSnapshotRepos")) {
|
||||
repositoryConfiguration.add(SPRING_SNAPSHOT);
|
||||
repositoryConfiguration.add(SPRING_MILESTONE);
|
||||
}
|
||||
|
||||
return repositoryConfiguration;
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.compiler.grape;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* The configuration of a repository
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public final class RepositoryConfiguration {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final URI uri;
|
||||
|
||||
private final boolean snapshotsEnabled;
|
||||
|
||||
/**
|
||||
* Creates a new {@code RepositoryConfiguration}.
|
||||
*
|
||||
* @param name The name of the repository
|
||||
* @param uri The uri of the repository
|
||||
* @param snapshotsEnabled {@code true} if the repository should enable access to
|
||||
* snapshots, {@code false} otherwise
|
||||
*/
|
||||
public RepositoryConfiguration(String name, URI uri, boolean snapshotsEnabled) {
|
||||
this.name = name;
|
||||
this.uri = uri;
|
||||
this.snapshotsEnabled = snapshotsEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name of the repository
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the uri of the repository
|
||||
*/
|
||||
public URI getUri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@code true} if the repository should enable access to snapshots,
|
||||
* {@code false} otherwise
|
||||
*/
|
||||
public boolean getSnapshotsEnabled() {
|
||||
return this.snapshotsEnabled;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.cli.command.GrabCommand;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link GrabCommand}
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class GrabCommandIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public CliTester cli = new CliTester("grab-samples/");
|
||||
|
||||
@Before
|
||||
@After
|
||||
public void deleteLocalRepository() {
|
||||
FileSystemUtils.deleteRecursively(new File("target/repository"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void grab() throws Exception {
|
||||
System.setProperty("grape.root", "target");
|
||||
System.setProperty("groovy.grape.report.downloads", "true");
|
||||
|
||||
try {
|
||||
String output = this.cli.grab("grab.groovy");
|
||||
assertTrue(output.contains("Downloading: file:"));
|
||||
assertTrue(new File("target/repository/org/springframework/spring-jdbc")
|
||||
.isDirectory());
|
||||
}
|
||||
finally {
|
||||
System.clearProperty("grape.root");
|
||||
System.clearProperty("groovy.grape.report.downloads");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue