Search in sources :

Example 31 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project phoenicis by PhoenicisOrg.

the class GenericContainersManager method deleteContainer.

/**
 * {@inheritDoc}
 * @param container
 * @param errorCallback
 */
@Override
public void deleteContainer(ContainerDTO container, Consumer<Exception> errorCallback) {
    try {
        this.fileUtilities.remove(new File(container.getPath()));
    } catch (IOException e) {
        LOGGER.error("Cannot delete container (" + container.getPath() + ")! Exception: " + e.toString());
        errorCallback.accept(e);
    }
    List<ShortcutCategoryDTO> categories = this.libraryManager.fetchShortcuts();
    categories.stream().flatMap(shortcutCategoryDTO -> shortcutCategoryDTO.getShortcuts().stream()).forEach(shortcutDTO -> {
        final InteractiveScriptSession interactiveScriptSession = this.scriptInterpreter.createInteractiveSession();
        interactiveScriptSession.eval("include([\"Engines\", \"" + container.getEngine() + "\", \"Shortcuts\", \"Reader\"]);", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
            final ScriptObjectMirror shortcutReader = (ScriptObjectMirror) output;
            shortcutReader.callMember("of", shortcutDTO);
            final String containerName = (String) shortcutReader.callMember("container");
            if (containerName.equals(container.getName())) {
                this.shortcutManager.deleteShortcut(shortcutDTO);
            }
        }, errorCallback), errorCallback);
    });
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) WinePrefixContainerInputConfiguration(org.phoenicis.containers.wine.configurations.WinePrefixContainerInputConfiguration) ContainerCategoryDTO(org.phoenicis.containers.dto.ContainerCategoryDTO) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Map(java.util.Map) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ContainerDTO(org.phoenicis.containers.dto.ContainerDTO) WinePrefixContainerDisplayConfiguration(org.phoenicis.containers.wine.configurations.WinePrefixContainerDisplayConfiguration) Logger(org.slf4j.Logger) FileUtilities(org.phoenicis.tools.files.FileUtilities) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Consumer(java.util.function.Consumer) List(java.util.List) ShortcutManager(org.phoenicis.library.ShortcutManager) ConfigFile(org.phoenicis.tools.config.ConfigFile) Safe(org.phoenicis.configuration.security.Safe) CompatibleConfigFileFormatFactory(org.phoenicis.tools.config.CompatibleConfigFileFormatFactory) CollectionUtils(org.springframework.util.CollectionUtils) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) LibraryManager(org.phoenicis.library.LibraryManager) Collections(java.util.Collections) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) IOException(java.io.IOException) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) File(java.io.File) ConfigFile(org.phoenicis.tools.config.ConfigFile)

Example 32 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project phoenicis by PhoenicisOrg.

the class LibraryController method createShortcut.

/**
 * creates a new shortcut
 * @param shortcutCreationDTO DTO describing the new shortcut
 */
private void createShortcut(ShortcutCreationDTO shortcutCreationDTO) {
    // get container
    // TODO: smarter way using container manager
    final String executablePath = shortcutCreationDTO.getExecutable().getAbsolutePath();
    final String pathInContainers = executablePath.replace(containersPath, "");
    final String[] split = pathInContainers.split("/");
    final String engineContainer = split[0];
    final String engine = (Character.toUpperCase(engineContainer.charAt(0)) + engineContainer.substring(1)).replace("prefix", "");
    final String container = split[1];
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"" + engine + "\", \"Shortcuts\", \"" + engine + "\"]);", ignored -> interactiveScriptSession.eval("new " + engine + "Shortcut()", output -> {
        final ScriptObjectMirror shortcutObject = (ScriptObjectMirror) output;
        shortcutObject.callMember("name", shortcutCreationDTO.getName());
        shortcutObject.callMember("category", shortcutCreationDTO.getCategory());
        shortcutObject.callMember("description", shortcutCreationDTO.getDescription());
        shortcutObject.callMember("miniature", shortcutCreationDTO.getMiniature());
        shortcutObject.callMember("search", shortcutCreationDTO.getExecutable().getName());
        shortcutObject.callMember("prefix", container);
        shortcutObject.callMember("create");
    }, e -> this.showErrorMessage(e, tr("Error while creating shortcut"))), e -> this.showErrorMessage(e, tr("Error while creating shortcut")));
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) LibraryView(org.phoenicis.javafx.views.mainwindow.library.LibraryView) RepositoryDTO(org.phoenicis.repository.dto.RepositoryDTO) ErrorMessage(org.phoenicis.javafx.views.common.ErrorMessage) ShortcutCreationDTO(org.phoenicis.library.dto.ShortcutCreationDTO) RepositoryManager(org.phoenicis.repository.RepositoryManager) ConsoleController(org.phoenicis.javafx.controller.library.console.ConsoleController) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) Localisation.tr(org.phoenicis.configuration.localisation.Localisation.tr) Platform(javafx.application.Platform) Value(org.springframework.beans.factory.annotation.Value) List(java.util.List) ShortcutManager(org.phoenicis.library.ShortcutManager) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) ShortcutRunner(org.phoenicis.library.ShortcutRunner) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ConfirmMessage(org.phoenicis.javafx.views.common.ConfirmMessage) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) LibraryManager(org.phoenicis.library.LibraryManager) Collections(java.util.Collections) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 33 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project phoenicis by PhoenicisOrg.

the class ShortcutRunner method run.

public void run(ShortcutDTO shortcutDTO, List<String> arguments, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"Wine\", \"Shortcuts\", \"Reader\"]);", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
        final ScriptObjectMirror shortcutReader = (ScriptObjectMirror) output;
        shortcutReader.callMember("of", shortcutDTO);
        shortcutReader.callMember("run", arguments);
    }, errorCallback), errorCallback);
}
Also used : Consumer(java.util.function.Consumer) List(java.util.List) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 34 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project phoenicis by PhoenicisOrg.

the class ShortcutRunner method stop.

public void stop(ShortcutDTO shortcutDTO, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"Wine\", \"Shortcuts\", \"Reader\"]);", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
        final ScriptObjectMirror shortcutReader = (ScriptObjectMirror) output;
        shortcutReader.callMember("of", shortcutDTO);
        shortcutReader.callMember("stop");
    }, errorCallback), errorCallback);
}
Also used : Consumer(java.util.function.Consumer) List(java.util.List) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 35 with ScriptObjectMirror

use of jdk.nashorn.api.scripting.ScriptObjectMirror in project phoenicis by PhoenicisOrg.

the class EngineToolsManager method runTool.

/**
 * runs a tool in a given container
 * @param engineId ID of the engine which provides the tool (e.g. "Wine")
 * @param container name of the container
 * @param toolId ID of the tool
 * @param doneCallback callback executed after the script ran
 * @param errorCallback callback executed in case of an error
 */
public void runTool(String engineId, String container, String toolId, Runnable doneCallback, Consumer<Exception> errorCallback) {
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Engines\", \"" + engineId + "\", \"Tools\", \"" + toolId + "\"]);", ignored -> interactiveScriptSession.eval("new " + toolId + "()", output -> {
        final ScriptObjectMirror toolObject = (ScriptObjectMirror) output;
        toolObject.callMember("run", container);
        doneCallback.run();
    }, errorCallback), errorCallback);
}
Also used : Consumer(java.util.function.Consumer) CategoryDTO(org.phoenicis.repository.dto.CategoryDTO) TypeDTO(org.phoenicis.repository.dto.TypeDTO) List(java.util.List) RepositoryDTO(org.phoenicis.repository.dto.RepositoryDTO) Map(java.util.Map) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) HashMap(java.util.HashMap) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) ApplicationDTO(org.phoenicis.repository.dto.ApplicationDTO) ArrayList(java.util.ArrayList) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Aggregations

ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)76 Test (org.junit.Test)34 InteractiveScriptSession (org.phoenicis.scripts.interpreter.InteractiveScriptSession)28 ScriptInterpreter (org.phoenicis.scripts.interpreter.ScriptInterpreter)28 Consumer (java.util.function.Consumer)27 List (java.util.List)23 LoggerFactory (org.slf4j.LoggerFactory)20 File (java.io.File)17 IOException (java.io.IOException)17 Logger (org.slf4j.Logger)14 WinePrefixContainerDTO (org.phoenicis.containers.dto.WinePrefixContainerDTO)12 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 RegistryParameter (org.phoenicis.containers.wine.parameters.RegistryParameter)10 LibraryManager (org.phoenicis.library.LibraryManager)10 ShortcutManager (org.phoenicis.library.ShortcutManager)10 ShortcutCategoryDTO (org.phoenicis.library.dto.ShortcutCategoryDTO)10 ShortcutDTO (org.phoenicis.library.dto.ShortcutDTO)10 RegistryWriter (org.phoenicis.win32.registry.RegistryWriter)10 URI (java.net.URI)8