Fix help and extend clean command

Examples:

    $ spring clean --all org.springframework commons-logging
    $ spring clean --all org.springframework:spring-tx
pull/1/merge
Dave Syer 12 years ago
parent ca99e4d7b9
commit 737886e4da

@ -16,16 +16,25 @@
package org.springframework.bootstrap.cli;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.apache.ivy.util.FileUtil;
/**
* {@link Command} to 'clean' up grapes.
* {@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 AbstractCommand {
public class CleanCommand extends OptionParsingCommand {
private OptionSpec<Void> allOption;
public CleanCommand() {
super("clean",
@ -33,7 +42,19 @@ public class CleanCommand extends AbstractCommand {
}
@Override
public void run(String... args) throws Exception {
public String getUsageHelp() {
return "[options] <dependencies>";
}
@Override
protected OptionParser createOptionParser() {
OptionParser parser = new OptionParser();
this.allOption = parser.accepts("all", "Clean all files (not just snapshots)");
return parser;
}
@Override
protected void run(OptionSet options) throws Exception {
String dir = System.getenv("GROOVY_HOME");
String userdir = System.getProperty("user.home");
@ -54,12 +75,31 @@ public class CleanCommand extends AbstractCommand {
}
File grapes = new File(home, "grapes");
// TODO: add support for other packages as args
String[] packages = new String[] { "org.springframework.bootstrap" };
for (String pkg : packages) {
File file = new File(grapes, pkg);
ArrayList<String> specs = new ArrayList<String>(options.nonOptionArguments());
if (!specs.contains("org.springframework.bootstrap")) {
specs.add(0, "org.springframework.bootstrap");
}
for (String spec : specs) {
String group = spec;
String module = null;
if (spec.contains(":")) {
group = spec.substring(0, spec.indexOf(":"));
module = spec.substring(spec.indexOf(":") + 1);
}
File file = module == null ? new File(grapes, group) : new File(new File(
grapes, group), module);
if (file.exists()) {
FileUtil.forceDelete(file);
if (options.has(this.allOption)
|| group.equals("org.springframework.bootstrap")) {
FileUtil.forceDelete(file);
} else {
for (Object obj : FileUtil.listAll(file, Collections.emptyList())) {
File candidate = (File) obj;
if (candidate.getName().contains("SNAPSHOT")) {
FileUtil.forceDelete(candidate);
}
}
}
}
}

@ -138,7 +138,8 @@ public class SpringBootstrapCli {
System.out.println("Available commands are:");
for (Command command : this.commands) {
if (!command.isOptionCommand()) {
System.out.println(String.format(" %1$-15s %2$s", command.getName(),
System.out.println(String.format("\n %1$s %2$-15s\n %3$s",
command.getName(), command.getUsageHelp(),
command.getDescription()));
}
}

Loading…
Cancel
Save