package net.sourceforge.javautil.groovy.cli;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sourceforge.javautil.classloader.boot.EntryPoint;
import net.sourceforge.javautil.classloader.boot.EntryPointConsole;
import net.sourceforge.javautil.common.CollectionUtil;
import net.sourceforge.javautil.common.ReflectionUtil;
import net.sourceforge.javautil.common.StringUtil;
import net.sourceforge.javautil.common.io.console.IOConsole;
import net.sourceforge.javautil.ui.cli.CommandLineArgumentsStandard;
import net.sourceforge.javautil.ui.cli.CommandLineCommand;
import net.sourceforge.javautil.ui.cli.CommandLineContext;
import net.sourceforge.javautil.ui.cli.CommandLineExitException;
import net.sourceforge.javautil.ui.cli.CommandLineInterfaceAbstract;
import net.sourceforge.javautil.ui.command.UICommand;
import net.sourceforge.javautil.ui.command.UICommandContext;
import net.sourceforge.javautil.ui.command.UICommandSet;
/**
* A simple command line that can be provided domain objects and takes commands as scripts to be ran.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class GroovyCLI extends CommandLineInterfaceAbstract {
protected final Binding domain = new Binding();
protected final GroovyCLICommandSet set;
protected final SimpleDateFormat format = new SimpleDateFormat("MM/dd hh:mm:ss");
protected final List<String> imports = new ArrayList<String>();
protected final GroovyClassLoader compiler;
public GroovyCLI() { this(new GroovyClassLoader(), new EntryPointConsole(), null); }
public GroovyCLI(GroovyClassLoader compiler) { this(compiler, new EntryPointConsole(), null); }
public GroovyCLI(GroovyClassLoader compiler, IOConsole console, GroovyCLICommandSet set) {
super(console);
this.compiler = compiler;
this.domain.setVariable("console", console);
this.domain.setVariable("input", console.getReader());
this.domain.setVariable("output", console.getPrintStream());
this.domain.setVariable("cli", this);
this.domain.setVariable("cl", Thread.currentThread().getContextClassLoader());
this.set = set == null ? new GroovyCLICommandSet(compiler) : set;
}
/**
* @return The compiler this CLI is using
*/
public GroovyClassLoader getCompiler() { return compiler; }
/**
* @return The packages imported into the scripts
*/
public List<String> getImports() { return imports; }
/**
* @return The container domain objects for scripts
*/
public Binding getDomain() { return domain; }
@Override protected UICommandSet<? extends UICommand, ? extends GroovyCLIContext> createCommandSet() { return set; }
@Override protected CommandLineContext createContext(LineNumberReader input, PrintStream output) { return new GroovyCLIContext(this); }
public GroovyCLICommandSet getSet() { return set; }
@Override public String getPrompt() {
return "[" + format.format(new Date()) + "] GROOVY> ";
}
@Override public Object execute(String... commandInfo) {
Object results = super.execute(commandInfo);
if (results != null && results != domain.getVariables()) domain.setVariable("results", results);
return results;
}
}