org.crsh.lang.impl.groovy.command.GroovyScriptShellCommand Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2012 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.crsh.lang.impl.groovy.command;
import groovy.lang.Binding;
import groovy.lang.Closure;
import org.crsh.cli.descriptor.CommandDescriptor;
import org.crsh.cli.impl.descriptor.HelpDescriptor;
import org.crsh.cli.impl.descriptor.IntrospectionException;
import org.crsh.cli.impl.invocation.InvocationMatch;
import org.crsh.cli.impl.lang.CommandFactory;
import org.crsh.cli.impl.lang.Instance;
import org.crsh.cli.spi.Completer;
import org.crsh.command.CommandContext;
import org.crsh.groovy.GroovyCommand;
import org.crsh.shell.ErrorKind;
import org.crsh.shell.impl.command.spi.CommandException;
import org.crsh.lang.impl.groovy.ast.ScriptLastStatementTransformer;
import org.crsh.shell.impl.command.spi.CommandMatch;
import org.crsh.shell.impl.command.spi.CommandInvoker;
import org.crsh.shell.impl.command.InvocationContextImpl;
import org.crsh.command.RuntimeContext;
import org.crsh.shell.impl.command.spi.Command;
import org.crsh.util.Utils;
import java.io.IOException;
import java.util.List;
/** @author Julien Viet */
public class GroovyScriptShellCommand extends Command> {
/** . */
private final Class clazz;
/** . */
private final boolean hasExplicitReturn;
/** . */
private final CommandDescriptor> descriptor;
public GroovyScriptShellCommand(Class clazz) throws IntrospectionException {
//
CommandFactory factory = new CommandFactory(getClass().getClassLoader());
boolean hasExplicitReturn;
try {
clazz.getDeclaredField(ScriptLastStatementTransformer.FIELD_NAME);
hasExplicitReturn = true;
}
catch (NoSuchFieldException e) {
hasExplicitReturn = false;
}
//
this.clazz = clazz;
this.descriptor = HelpDescriptor.create(factory.create(clazz));
this.hasExplicitReturn = hasExplicitReturn;
}
@Override
public CommandDescriptor> getDescriptor() {
return descriptor;
}
@Override
protected CommandMatch, ?> resolve(final InvocationMatch> match) {
return new CommandMatch() {
@Override
public CommandInvoker getInvoker() throws CommandException {
List chunks = Utils.chunks(match.getRest());
String[] args = chunks.toArray(new String[chunks.size()]);
return GroovyScriptShellCommand.this.getInvoker(args);
}
@Override
public Class
© 2015 - 2024 Weber Informatics LLC | Privacy Policy