Remove CleanCommand

Fixes gh-230
This commit is contained in:
Dave Syer 2014-01-16 17:29:52 +00:00
parent c8a1d8830c
commit 796816e8c3
6 changed files with 1 additions and 288 deletions

View File

@ -21,7 +21,6 @@ import java.util.Collection;
import java.util.List;
import org.springframework.boot.cli.command.core.VersionCommand;
import org.springframework.boot.cli.command.grab.CleanCommand;
import org.springframework.boot.cli.command.grab.GrabCommand;
import org.springframework.boot.cli.command.run.RunCommand;
import org.springframework.boot.cli.command.test.TestCommand;
@ -34,8 +33,7 @@ import org.springframework.boot.cli.command.test.TestCommand;
public class DefaultCommandFactory implements CommandFactory {
private static final List<Command> DEFAULT_COMMANDS = Arrays.<Command> asList(
new VersionCommand(), new RunCommand(), new TestCommand(), new GrabCommand(),
new CleanCommand());
new VersionCommand(), new RunCommand(), new TestCommand(), new GrabCommand());
@Override
public Collection<Command> getCommands() {

View File

@ -1,198 +0,0 @@
/*
* Copyright 2012-2014 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.grab;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.springframework.boot.cli.Log;
import org.springframework.boot.cli.command.Command;
import org.springframework.boot.cli.command.OptionHandler;
import org.springframework.boot.cli.command.OptionParsingCommand;
/**
* {@link Command} to 'clean' up grapes, removing cached dependencies and forcing a
* download on the next attempt to resolve.
*
* @author Dave Syer
*/
public class CleanCommand extends OptionParsingCommand {
public CleanCommand() {
super("clean", "Clean up groovy grapes "
+ "(useful if snapshots are needed and you need an update)",
new CleanOptionHandler());
}
@Override
public String getUsageHelp() {
return "[options] <dependencies>";
}
private static class CleanOptionHandler extends OptionHandler {
private OptionSpec<Void> allOption;
private OptionSpec<Void> ivyOption;
private OptionSpec<Void> mvnOption;
@Override
protected void options() {
this.allOption = option("all", "Clean all files (not just snapshots)");
this.ivyOption = option("ivy", "Clean just ivy (grapes) cache. "
+ "Default is on unless --maven is used.");
this.mvnOption = option("maven", "Clean just maven cache. Default is off.");
}
@Override
protected void run(OptionSet options) throws Exception {
if (!options.has(this.ivyOption)) {
clean(options, getGrapesHome(), Layout.IVY);
}
if (options.has(this.mvnOption)) {
if (options.has(this.ivyOption)) {
clean(options, getGrapesHome(), Layout.IVY);
}
clean(options, getMavenHome(), Layout.MAVEN);
}
}
private void clean(OptionSet options, File root, Layout layout) {
if (root == null || !root.exists()) {
return;
}
ArrayList<Object> specs = new ArrayList<Object>(options.nonOptionArguments());
if (!specs.contains("org.springframework.boot") && layout == Layout.IVY) {
specs.add(0, "org.springframework.boot");
}
for (Object spec : specs) {
if (spec instanceof String) {
clean(options, root, layout, (String) spec);
}
}
}
private void clean(OptionSet options, File root, Layout layout, String spec) {
String group = spec;
String module = null;
if (spec.contains(":")) {
group = spec.substring(0, spec.indexOf(':'));
module = spec.substring(spec.indexOf(':') + 1);
}
File file = getModulePath(root, group, module, layout);
if (!file.exists()) {
return;
}
if (options.has(this.allOption) || group.equals("org.springframework.boot")) {
delete(file);
return;
}
for (Object obj : recursiveList(file)) {
File candidate = (File) obj;
if (candidate.getName().contains("SNAPSHOT")) {
delete(candidate);
}
}
}
private void delete(File file) {
Log.info("Deleting: " + file);
recursiveDelete(file);
}
private File getModulePath(File root, String group, String module, Layout layout) {
File parent = root;
if (layout == Layout.IVY) {
parent = new File(parent, group);
}
else {
for (String path : group.split("\\.")) {
parent = new File(parent, path);
}
}
if (module == null) {
return parent;
}
return new File(parent, module);
}
private File getGrapesHome() {
String dir = System.getenv("GROOVY_HOME");
String userdir = System.getProperty("user.home");
File home;
if (dir == null || !new File(dir).exists()) {
dir = userdir;
home = new File(dir, ".groovy");
}
else {
home = new File(dir);
}
if (dir == null || !new File(dir).exists()) {
return null;
}
return new File(home, "grapes");
}
private File getMavenHome() {
String dir = System.getProperty("user.home");
if (dir == null || !new File(dir).exists()) {
return null;
}
File home = new File(dir);
return new File(new File(home, ".m2"), "repository");
}
private static enum Layout {
IVY, MAVEN;
}
private void recursiveDelete(File file) {
if (file.exists()) {
if (file.isDirectory()) {
for (File inDir : file.listFiles()) {
recursiveDelete(inDir);
}
}
if (!file.delete()) {
throw new IllegalStateException("Failed to delete " + file);
}
}
}
private List<File> recursiveList(File file) {
List<File> files = new ArrayList<File>();
if (file.isDirectory()) {
for (File inDir : file.listFiles()) {
files.addAll(recursiveList(inDir));
}
}
files.add(file);
return files;
}
}
}

View File

@ -35,7 +35,6 @@ import org.junit.runners.model.Statement;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.cli.command.AbstractCommand;
import org.springframework.boot.cli.command.OptionParsingCommand;
import org.springframework.boot.cli.command.grab.CleanCommand;
import org.springframework.boot.cli.command.grab.GrabCommand;
import org.springframework.boot.cli.command.run.RunCommand;
import org.springframework.boot.cli.command.test.TestCommand;
@ -173,7 +172,6 @@ public class CliTester implements TestRule {
public void evaluate() throws Throwable {
System.setProperty("disableSpringSnapshotRepos", "false");
try {
new CleanCommand().run("org.springframework");
try {
this.base.evaluate();
}

View File

@ -1,71 +0,0 @@
/*
* Copyright 2012-2014 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 javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.springframework.boot.cli.command.grab.CleanCommand;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* @author Dave Syer
*/
public class GrapesCleaner {
private static final String VERSION;
static {
try {
File pom = new File("pom.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(pom);
Element parent = (Element) document.getDocumentElement()
.getElementsByTagName("parent").item(0);
VERSION = parent.getElementsByTagName("version").item(0).getFirstChild()
.getTextContent();
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static void cleanIfNecessary() throws Exception {
File installedJar = new File(getMavenRepository(), String.format(
"org/springframework/boot/spring-boot/%s/spring-boot-%s.jar", VERSION,
VERSION));
File grapesJar = new File(getGrapesCache(), String.format(
"org.springframework.boot/spring-boot/jars/spring-boot-%s.jar", VERSION));
if (!VERSION.contains("SNAPSHOT") || installedJar.exists() && grapesJar.exists()
&& installedJar.lastModified() <= grapesJar.lastModified()) {
return;
}
new CleanCommand().run();
}
private static File getMavenRepository() {
return new File(System.getProperty("user.home"), ".m2/repository");
}
private static File getGrapesCache() {
return new File(System.getProperty("user.home"), ".groovy/grapes");
}
}

View File

@ -20,7 +20,6 @@ import java.io.File;
import java.net.URI;
import org.codehaus.plexus.util.FileUtils;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
@ -41,11 +40,6 @@ public class SampleIntegrationTests {
@Rule
public CliTester cli = new CliTester("samples/");
@BeforeClass
public static void cleanGrapes() throws Exception {
GrapesCleaner.cleanIfNecessary();
}
@Test
public void appSample() throws Exception {
String output = this.cli.run("app.groovy");

View File

@ -18,11 +18,9 @@ package org.springframework.boot.cli;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.cli.command.grab.CleanCommand;
import org.springframework.boot.cli.command.test.TestCommand;
import static org.hamcrest.Matchers.containsString;
@ -42,15 +40,9 @@ public class TestCommandIntegrationTests {
@Rule
public CliTester cli = new CliTester("test-samples/");
@BeforeClass
public static void cleanGrapes() throws Exception {
GrapesCleaner.cleanIfNecessary();
}
@Before
public void setup() throws Exception {
System.setProperty("disableSpringSnapshotRepos", "false");
new CleanCommand().run("org.springframework");
}
@After