Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "ShortcutReader" interface #2025

Merged
merged 3 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.phoenicis.containers.dto.WinePrefixContainerDTO;
import org.phoenicis.library.LibraryManager;
import org.phoenicis.library.ShortcutManager;
import org.phoenicis.library.ShortcutReader;
import org.phoenicis.library.dto.ShortcutCategoryDTO;
import org.phoenicis.library.dto.ShortcutDTO;
import org.phoenicis.scripts.interpreter.ScriptInterpreter;
Expand Down Expand Up @@ -135,17 +136,22 @@ public void deleteContainer(ContainerDTO container, Consumer<ContainerDTO> onSuc
final InteractiveScriptSession interactiveScriptSession = this.scriptInterpreter
.createInteractiveSession();

interactiveScriptSession.eval(
"const ShortcutReader = include(\"engines." + engineId + ".shortcuts.reader\");",
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
final org.graalvm.polyglot.Value shortcutReader = (org.graalvm.polyglot.Value) output;
shortcutReader.invokeMember("of", shortcut);
final String containerName = shortcutReader.invokeMember("getContainer")
.as(String.class);
interactiveScriptSession.eval("include(\"engines." + engineId + ".shortcuts.reader\");",
result -> {
final org.graalvm.polyglot.Value shortcutReaderClass = (org.graalvm.polyglot.Value) result;

final ShortcutReader shortcutReader = shortcutReaderClass.newInstance()
.as(ShortcutReader.class);

shortcutReader.of(shortcut);

final String containerName = shortcutReader.getContainer();

if (containerName.equals(container.getName())) {
this.shortcutManager.deleteShortcut(shortcut);
}
}, onError), onError);
},
onError);
});

onSuccess.accept(container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.phoenicis.configuration.security.Safe;
import org.phoenicis.library.dto.ShortcutDTO;
import org.phoenicis.library.dto.ShortcutInfoDTO;
import org.phoenicis.scripts.session.InteractiveScriptSession;
import org.phoenicis.scripts.interpreter.ScriptInterpreter;
import org.phoenicis.scripts.session.InteractiveScriptSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -120,12 +120,16 @@ public void createShortcut(ShortcutDTO shortcutDTO) {
public void uninstallFromShortcut(ShortcutDTO shortcutDTO, Consumer<Exception> errorCallback) {
final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();

interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");",
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
final Value shortcutReader = (Value) output;
shortcutReader.invokeMember("of", shortcutDTO);
shortcutReader.invokeMember("uninstall");
}, errorCallback), errorCallback);
interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");",
result -> {
Value shortcutReaderClass = (Value) result;

ShortcutReader shortcutReader = shortcutReaderClass.newInstance().as(ShortcutReader.class);

shortcutReader.of(shortcutDTO);
shortcutReader.uninstall();
},
errorCallback);
}

public void deleteShortcut(ShortcutDTO shortcutDTO) {
Expand Down Expand Up @@ -216,6 +220,7 @@ public void updateShortcut(ShortcutDTO shortcutDTO) {

/**
* creates a file with a resource fallback
*
* @param path file path
* @param resource resource which shall be used if file path cannot be accessed
* @return created file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.phoenicis.library;

import org.phoenicis.library.dto.ShortcutDTO;

import java.util.List;

/**
* Interface for all shortcut reader implementations
*/
public interface ShortcutReader {
/**
* Sets the shortcut
*
* @param shortcut The shortcut
*/
void of(ShortcutDTO shortcut);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still thinking whether it is better to add the functionality of this method to the constructor.

Changing the constructor to have the signature:

public ShortcutReader(ShortcutDTO shortcut)

has the benefit that the object usage is cleaner. In addition it has the disadvantage that we can't add the constructor requirement to the interface.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is part of the constructor, it cannot be defined in the interface, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly


/**
* Returns the name of the container belonging to the shortcut
*
* @return The container name
*/
String getContainer();

/**
* Runs a shortcut with the given user arguments
*
* @param arguments The user arguments
*/
void run(List<String> arguments);

/**
* Stops the running shortcut
*/
void stop();

/**
* Uninstalls the shortcut
*/
void uninstall();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import org.graalvm.polyglot.Value;
import org.phoenicis.library.dto.ShortcutDTO;
import org.phoenicis.scripts.session.InteractiveScriptSession;
import org.phoenicis.scripts.interpreter.ScriptInterpreter;
import org.phoenicis.scripts.session.InteractiveScriptSession;

import java.util.List;
import java.util.function.Consumer;
Expand All @@ -42,25 +42,31 @@ public void run(String shortcutName, List<String> arguments, Consumer<Exception>
public void run(ShortcutDTO shortcutDTO, List<String> arguments, Consumer<Exception> errorCallback) {
final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();

interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");",
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
final Value shortcutReader = (Value) output;
interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");",
result -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is the same everywhere. Can we extract it to a separate method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was to do this later on in a centralized method in PhoenicisScriptEngine:

public interface PhoenicisScriptEngine {
    <E> E evalAndReturn(String script, Class<E> type);
    
    <E> void eval(String script, Class<E> type, Consumer<E> onSuccess, Consumer<Exception> onError);
}

See also our documentation.

For now I would suggest to retain the redundancy and fix this later for all script types (i.e. engines, installers, plugins, shortcuts, verbs etc.) at once.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with that.

Value shortcutReaderClass = (Value) result;

shortcutReader.invokeMember("of", shortcutDTO);
shortcutReader.invokeMember("run", arguments);
}, errorCallback), errorCallback);
ShortcutReader shortcutReader = shortcutReaderClass.newInstance().as(ShortcutReader.class);

shortcutReader.of(shortcutDTO);
shortcutReader.run(arguments);
},
errorCallback);
}

public void stop(ShortcutDTO shortcutDTO, Consumer<Exception> errorCallback) {
final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();

interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");",
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
final Value shortcutReader = (Value) output;
interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");",
result -> {
Value shortcutReaderClass = (Value) result;

ShortcutReader shortcutReader = shortcutReaderClass.newInstance().as(ShortcutReader.class);

shortcutReader.invokeMember("of", shortcutDTO);
shortcutReader.invokeMember("stop");
}, errorCallback), errorCallback);
shortcutReader.of(shortcutDTO);
shortcutReader.stop();
},
errorCallback);
}

}