This commit is contained in:
Phillip Webb
2014-10-21 20:33:00 -07:00
parent d1ce83e3c3
commit 2e7aa4685b
33 changed files with 121 additions and 161 deletions

View File

@@ -20,6 +20,9 @@ import java.io.File;
import java.util.List;
/**
* Resolve artifact identifiers (typically in the form {@literal group:artifact:version})
* to {@link File}s.
*
* @author Andy Wilkinson
* @since 1.2.0
*/
@@ -28,7 +31,6 @@ interface DependencyResolver {
/**
* Resolves the given {@code artifactIdentifiers}, typically in the form
* "group:artifact:version", and their dependencies.
*
* @param artifactIdentifiers The artifacts to resolve
* @return The {@code File}s for the resolved artifacts
* @throws Exception if dependency resolution fails

View File

@@ -55,11 +55,9 @@ class GroovyGrabDependencyResolver implements DependencyResolver {
groovyCompiler.compile(createSources(artifactIdentifiers));
List<URL> artifactUrls = getClassPathUrls(groovyCompiler);
artifactUrls.removeAll(initialUrls);
for (URL artifactUrl : artifactUrls) {
artifactFiles.add(toFile(artifactUrl));
}
}
return artifactFiles;
}

View File

@@ -24,6 +24,7 @@ import org.springframework.boot.cli.command.OptionParsingCommand;
import org.springframework.boot.cli.command.options.CompilerOptionHandler;
import org.springframework.boot.cli.command.status.ExitStatus;
import org.springframework.boot.cli.util.Log;
import org.springframework.util.Assert;
/**
* {@link Command} to install additional dependencies into the CLI.
@@ -47,24 +48,18 @@ public class InstallCommand extends OptionParsingCommand {
private static final class InstallOptionHandler extends CompilerOptionHandler {
@Override
@SuppressWarnings("unchecked")
protected ExitStatus run(OptionSet options) throws Exception {
@SuppressWarnings("unchecked")
List<String> args = (List<String>) options.nonOptionArguments();
if (args.isEmpty()) {
throw new IllegalArgumentException(
"Please specify at least one dependency, in the form group:artifact:version, to install");
}
Assert.notEmpty(args, "Please specify at least one "
+ "dependency, in the form group:artifact:version, to install");
try {
new Installer(options, this).install(args);
}
catch (Exception e) {
String message = e.getMessage();
Log.error(message != null ? message : e.getClass().toString());
catch (Exception ex) {
String message = ex.getMessage();
Log.error(message != null ? message : ex.getClass().toString());
}
return ExitStatus.OK;
}

View File

@@ -35,6 +35,8 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.util.SystemPropertyUtils;
/**
* Shared logic for the {@link InstallCommand} and {@link UninstallCommand}.
*
* @author Andy Wilkinson
* @since 1.2.0
*/
@@ -44,13 +46,13 @@ class Installer {
private final Properties installCounts;
Installer(OptionSet options, CompilerOptionHandler compilerOptionHandler)
public Installer(OptionSet options, CompilerOptionHandler compilerOptionHandler)
throws IOException {
this(new GroovyGrabDependencyResolver(createCompilerConfiguration(options,
compilerOptionHandler)));
}
Installer(DependencyResolver resolver) throws IOException {
public Installer(DependencyResolver resolver) throws IOException {
this.dependencyResolver = resolver;
this.installCounts = loadInstallCounts();
}
@@ -59,7 +61,6 @@ class Installer {
OptionSet options, CompilerOptionHandler compilerOptionHandler) {
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
.createDefaultRepositoryConfiguration();
return new OptionSetGroovyCompilerConfiguration(options, compilerOptionHandler,
repositoryConfiguration) {
@Override
@@ -69,17 +70,14 @@ class Installer {
};
}
private static Properties loadInstallCounts() throws IOException {
private Properties loadInstallCounts() throws IOException {
Properties properties = new Properties();
File installed = getInstalled();
if (installed.exists()) {
FileReader reader = new FileReader(installed);
properties.load(reader);
reader.close();
}
return properties;
}
@@ -96,9 +94,7 @@ class Installer {
public void install(List<String> artifactIdentifiers) throws Exception {
File libDirectory = getDefaultLibDirectory();
libDirectory.mkdirs();
Log.info("Installing into: " + libDirectory);
List<File> artifactFiles = this.dependencyResolver.resolve(artifactIdentifiers);
for (File artifactFile : artifactFiles) {
int installCount = getInstallCount(artifactFile);
@@ -108,7 +104,6 @@ class Installer {
}
setInstallCount(artifactFile, installCount + 1);
}
saveInstallCounts();
}
@@ -131,9 +126,7 @@ class Installer {
public void uninstall(List<String> artifactIdentifiers) throws Exception {
File libDirectory = getDefaultLibDirectory();
Log.info("Uninstalling from: " + libDirectory);
List<File> artifactFiles = this.dependencyResolver.resolve(artifactIdentifiers);
for (File artifactFile : artifactFiles) {
int installCount = getInstallCount(artifactFile);
@@ -142,31 +135,27 @@ class Installer {
}
setInstallCount(artifactFile, installCount - 1);
}
saveInstallCounts();
}
public void uninstallAll() throws Exception {
File libDirectory = getDefaultLibDirectory();
Log.info("Uninstalling from: " + libDirectory);
for (String name : this.installCounts.stringPropertyNames()) {
new File(libDirectory, name).delete();
}
this.installCounts.clear();
saveInstallCounts();
}
private static File getDefaultLibDirectory() {
private File getDefaultLibDirectory() {
String home = SystemPropertyUtils
.resolvePlaceholders("${spring.home:${SPRING_HOME:.}}");
final File lib = new File(home, "lib");
return lib;
return new File(home, "lib");
}
private static File getInstalled() {
private File getInstalled() {
return new File(getDefaultLibDirectory(), ".installed");
}
}

View File

@@ -56,34 +56,28 @@ public class UninstallCommand extends OptionParsingCommand {
}
@Override
@SuppressWarnings("unchecked")
protected ExitStatus run(OptionSet options) throws Exception {
@SuppressWarnings("unchecked")
List<String> args = (List<String>) options.nonOptionArguments();
try {
if (options.has(this.allOption)) {
if (!args.isEmpty()) {
throw new IllegalArgumentException(
"Please use --all without specifying any dependencies");
}
new Installer(options, this).uninstallAll();
}
if (args.isEmpty()) {
throw new IllegalArgumentException(
"Please specify at least one dependency, in the form group:artifact:version, to uninstall");
}
new Installer(options, this).uninstall(args);
}
catch (Exception e) {
String message = e.getMessage();
Log.error(message != null ? message : e.getClass().toString());
catch (Exception ex) {
String message = ex.getMessage();
Log.error(message != null ? message : ex.getClass().toString());
}
return ExitStatus.OK;
}
}

View File

@@ -22,7 +22,6 @@ import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.boot.cli.compiler.AstUtils;
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
import org.springframework.boot.cli.compiler.DependencyCustomizer;
import org.springframework.boot.groovy.EnableRabbitMessaging;
/**
* {@link CompilerAutoConfiguration} for Spring Rabbit.
@@ -46,6 +45,7 @@ public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration {
}
@Override
@SuppressWarnings("deprecation")
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("org.springframework.amqp.rabbit.annotation",
"org.springframework.amqp.rabbit.core",
@@ -54,7 +54,7 @@ public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration {
"org.springframework.amqp.rabbit.listener",
"org.springframework.amqp.rabbit.listener.adapter",
"org.springframework.amqp.core").addImports(
EnableRabbitMessaging.class.getCanonicalName());
org.springframework.boot.groovy.EnableRabbitMessaging.class.getName());
}
}