Search in sources :

Example 1 with GroovyVariableManager

use of net.robinfriedli.aiode.scripting.GroovyVariableManager in project aiode by robinfriedli.

the class AbstractScriptCommand method saveNewScript.

private void saveNewScript(QueryBuilderFactory queryBuilderFactory, Session session, CommandContext context) {
    String identifier = getArgumentValue("identifier");
    String script = getCommandInput();
    Optional<StoredScript> existingScript = SearchEngine.searchScript(identifier, scriptUsageId, session);
    if (existingScript.isPresent()) {
        throw new InvalidCommandException(String.format("%s with identifier '%s' already exists", scriptUsageId, identifier));
    }
    Aiode aiode = Aiode.get();
    GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
    GroovyShell groovyShell;
    if (argumentSet("privileged")) {
        groovyShell = new GroovyShell();
    } else {
        groovyShell = new GroovyShell(aiode.getGroovySandboxComponent().getCompilerConfiguration());
    }
    groovyVariableManager.prepareShell(groovyShell);
    try {
        groovyShell.parse(script);
    } catch (MultipleCompilationErrorsException e) {
        throw new InvalidCommandException(String.format("Could not compile provided script:%s%s", System.lineSeparator(), ExceptionUtils.formatScriptCompilationError(e)));
    }
    StoredScript.ScriptUsage scriptUsage = queryBuilderFactory.find(StoredScript.ScriptUsage.class).where((cb, root) -> cb.equal(root.get("uniqueId"), scriptUsageId)).build(session).uniqueResult();
    StoredScript storedScript = new StoredScript();
    storedScript.setGuildId(context.getGuild().getIdLong());
    storedScript.setIdentifier(identifier);
    storedScript.setScript(script);
    storedScript.setScriptUsage(scriptUsage);
    storedScript.setActive(!argumentSet("deactivate"));
    invoke(() -> session.persist(storedScript));
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StoredScript(net.robinfriedli.aiode.entities.StoredScript) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) Aiode(net.robinfriedli.aiode.Aiode) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) GroovyShell(groovy.lang.GroovyShell)

Example 2 with GroovyVariableManager

use of net.robinfriedli.aiode.scripting.GroovyVariableManager in project aiode by robinfriedli.

the class ScriptCommand method executeScript.

private void executeScript(Aiode aiode, CommandContext context, Session session) {
    GroovySandboxComponent groovySandboxComponent = aiode.getGroovySandboxComponent();
    GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
    SafeGroovyScriptRunner groovyScriptRunner = new SafeGroovyScriptRunner(context, groovySandboxComponent, groovyVariableManager, aiode.getSecurityManager(), argumentSet("privileged"));
    StoredScript script;
    if (this.script != null) {
        script = this.script;
    } else {
        String identifier = getArgumentValue("invoke");
        Optional<StoredScript> storedScript = SearchEngine.searchScript(identifier, "script", session);
        if (storedScript.isPresent()) {
            script = storedScript.get();
        } else {
            throw new InvalidCommandException(String.format("No such script '%s'", identifier));
        }
    }
    if (!script.isActive()) {
        throw new InvalidCommandException(String.format("Script '%s' has been deactivated. Use the active argument to reactivate the script.", script.getIdentifier()));
    }
    groovyScriptRunner.runAndSendResult(script.getScript(), 10, TimeUnit.SECONDS);
}
Also used : SafeGroovyScriptRunner(net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StoredScript(net.robinfriedli.aiode.entities.StoredScript) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) GroovySandboxComponent(net.robinfriedli.aiode.boot.configurations.GroovySandboxComponent)

Example 3 with GroovyVariableManager

use of net.robinfriedli.aiode.scripting.GroovyVariableManager in project aiode by robinfriedli.

the class EvalCommand method doRun.

@Override
public void doRun() {
    CommandContext context = getContext();
    Aiode aiode = Aiode.get();
    SecurityManager securityManager = aiode.getSecurityManager();
    GroovySandboxComponent groovySandboxComponent = aiode.getGroovySandboxComponent();
    GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
    SafeGroovyScriptRunner groovyScriptRunner = new SafeGroovyScriptRunner(context, groovySandboxComponent, groovyVariableManager, securityManager, argumentSet("privileged"));
    groovyScriptRunner.runAndSendResult(getCommandInput(), 10, TimeUnit.SECONDS);
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) SafeGroovyScriptRunner(net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner) Aiode(net.robinfriedli.aiode.Aiode) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) GroovySandboxComponent(net.robinfriedli.aiode.boot.configurations.GroovySandboxComponent)

Aggregations

GroovyVariableManager (net.robinfriedli.aiode.scripting.GroovyVariableManager)3 Aiode (net.robinfriedli.aiode.Aiode)2 GroovySandboxComponent (net.robinfriedli.aiode.boot.configurations.GroovySandboxComponent)2 StoredScript (net.robinfriedli.aiode.entities.StoredScript)2 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)2 SafeGroovyScriptRunner (net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner)2 GroovyShell (groovy.lang.GroovyShell)1 CommandContext (net.robinfriedli.aiode.command.CommandContext)1 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)1 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)1