Search in sources :

Example 1 with ScriptException

use of org.phoenicis.scripts.exceptions.ScriptException in project POL-POM-5 by PlayOnLinux.

the class IncludeInjector method injectInto.

@Override
public void injectInto(PhoenicisScriptEngine phoenicisScriptEngine) {
    // store scripts that are currently in progress
    final Stack<String> includeStack = new Stack<>();
    // store included scripts (include path -> JS object)
    final Map<String, Object> includedScripts = new HashMap<>();
    phoenicisScriptEngine.put("include", (Function<String, Object>) argument -> {
        if (includeStack.contains(argument)) {
            throw new CircularIncludeException(argument, includeStack);
        }
        includeStack.push(argument);
        if (!includedScripts.containsKey(argument)) {
            final String script = scriptFetcher.getScript(argument);
            if (script == null) {
                throw new ScriptNotFoundException(argument);
            }
            try {
                String extendedString = String.format("(module) => { %s }", script);
                Value includeFunction = (Value) phoenicisScriptEngine.evalAndReturn(extendedString, this::throwException);
                Value module = (Value) phoenicisScriptEngine.evalAndReturn("({})", this::throwException);
                includeFunction.execute(module);
                if (module.hasMember("default")) {
                    includedScripts.put(argument, module.getMember("default"));
                } else {
                    includedScripts.put(argument, module);
                }
            } catch (ScriptException se) {
                throw new IncludeException(argument, se);
            }
        }
        includeStack.pop();
        return includedScripts.get(argument);
    }, this::throwException);
}
Also used : IncludeException(org.phoenicis.scripts.exceptions.IncludeException) ScriptException(org.phoenicis.scripts.exceptions.ScriptException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) Value(org.graalvm.polyglot.Value) Map(java.util.Map) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine) ScriptNotFoundException(org.phoenicis.scripts.exceptions.ScriptNotFoundException) ScriptFetcher(org.phoenicis.scripts.interpreter.ScriptFetcher) HashMap(java.util.HashMap) Function(java.util.function.Function) Stack(java.util.Stack) ScriptException(org.phoenicis.scripts.exceptions.ScriptException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) HashMap(java.util.HashMap) Value(org.graalvm.polyglot.Value) IncludeException(org.phoenicis.scripts.exceptions.IncludeException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) ScriptNotFoundException(org.phoenicis.scripts.exceptions.ScriptNotFoundException) Stack(java.util.Stack)

Example 2 with ScriptException

use of org.phoenicis.scripts.exceptions.ScriptException in project POL-POM-5 by PlayOnLinux.

the class UiSetupWizardImplementation method licenceFile.

/**
 * Show the content of a licence file
 *
 * @param textToShow a message above the licence
 * @param licenceFile the licence file to display (with 'from java.io import File')
 */
@Override
public Void licenceFile(String textToShow, File licenceFile) {
    try {
        try (final FileInputStream content = new FileInputStream(licenceFile)) {
            final StringWriter writer = new StringWriter();
            IOUtils.copy(content, writer, "UTF-8");
            return licence(textToShow, writer.toString());
        }
    } catch (IOException e) {
        throw new ScriptException("Cannot acces the licence file", e);
    }
}
Also used : ScriptException(org.phoenicis.scripts.exceptions.ScriptException) StringWriter(java.io.StringWriter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 3 with ScriptException

use of org.phoenicis.scripts.exceptions.ScriptException in project POL-POM-5 by PhoenicisOrg.

the class UiSetupWizardImplementation method licenceFile.

/**
 * Show the content of a licence file
 *
 * @param textToShow a message above the licence
 * @param licenceFile the licence file to display (with 'from java.io import File')
 */
@Override
public Void licenceFile(String textToShow, File licenceFile) {
    try {
        try (final FileInputStream content = new FileInputStream(licenceFile)) {
            final StringWriter writer = new StringWriter();
            IOUtils.copy(content, writer, "UTF-8");
            return licence(textToShow, writer.toString());
        }
    } catch (IOException e) {
        throw new ScriptException("Cannot acces the licence file", e);
    }
}
Also used : ScriptException(org.phoenicis.scripts.exceptions.ScriptException) StringWriter(java.io.StringWriter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 4 with ScriptException

use of org.phoenicis.scripts.exceptions.ScriptException in project POL-POM-5 by PlayOnLinux.

the class VerbsManager method installVerb.

/**
 * Installs a Verb in a given container
 *
 * @param engineId ID of the engine which provides the Verb (e.g. "Wine")
 * @param container name of the container
 * @param verbId ID of the Verb
 * @param doneCallback callback executed after the script ran
 * @param errorCallback callback executed in case of an error
 */
public void installVerb(String engineId, String container, String verbId, Runnable doneCallback, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    final String script = String.format("include(\"%s\");", verbId);
    interactiveScriptSession.eval(script, output -> {
        final Value verbClass = (Value) output;
        try {
            verbClass.invokeMember("install", container);
        } catch (ScriptException se) {
            errorCallback.accept(se);
        }
        doneCallback.run();
    }, errorCallback);
}
Also used : ScriptException(org.phoenicis.scripts.exceptions.ScriptException) Value(org.graalvm.polyglot.Value) InteractiveScriptSession(org.phoenicis.scripts.session.InteractiveScriptSession)

Example 5 with ScriptException

use of org.phoenicis.scripts.exceptions.ScriptException in project POL-POM-5 by PhoenicisOrg.

the class VerbsManager method installVerb.

/**
 * Installs a Verb in a given container
 *
 * @param engineId ID of the engine which provides the Verb (e.g. "Wine")
 * @param container name of the container
 * @param verbId ID of the Verb
 * @param doneCallback callback executed after the script ran
 * @param errorCallback callback executed in case of an error
 */
public void installVerb(String engineId, String container, String verbId, Runnable doneCallback, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    final String script = String.format("include(\"%s\");", verbId);
    interactiveScriptSession.eval(script, output -> {
        final Value verbClass = (Value) output;
        try {
            verbClass.invokeMember("install", container);
        } catch (ScriptException se) {
            errorCallback.accept(se);
        }
        doneCallback.run();
    }, errorCallback);
}
Also used : ScriptException(org.phoenicis.scripts.exceptions.ScriptException) Value(org.graalvm.polyglot.Value) InteractiveScriptSession(org.phoenicis.scripts.session.InteractiveScriptSession)

Aggregations

ScriptException (org.phoenicis.scripts.exceptions.ScriptException)6 Value (org.graalvm.polyglot.Value)4 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Stack (java.util.Stack)2 Function (java.util.function.Function)2 PhoenicisScriptEngine (org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine)2 CircularIncludeException (org.phoenicis.scripts.exceptions.CircularIncludeException)2 IncludeException (org.phoenicis.scripts.exceptions.IncludeException)2 ScriptNotFoundException (org.phoenicis.scripts.exceptions.ScriptNotFoundException)2 ScriptFetcher (org.phoenicis.scripts.interpreter.ScriptFetcher)2 InteractiveScriptSession (org.phoenicis.scripts.session.InteractiveScriptSession)2