Skip to content

Commit 2ea8552

Browse files
authored
Add a "ShortcutReader" interface (#2025)
- add a ShortcutReader interface for type safety reasons - use ShortcutReader interface in GenericContainersManager
1 parent 964ad59 commit 2ea8552

File tree

4 files changed

+86
-28
lines changed

4 files changed

+86
-28
lines changed

phoenicis-containers/src/main/java/org/phoenicis/containers/GenericContainersManager.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.phoenicis.containers.dto.WinePrefixContainerDTO;
2828
import org.phoenicis.library.LibraryManager;
2929
import org.phoenicis.library.ShortcutManager;
30+
import org.phoenicis.library.ShortcutReader;
3031
import org.phoenicis.library.dto.ShortcutCategoryDTO;
3132
import org.phoenicis.library.dto.ShortcutDTO;
3233
import org.phoenicis.scripts.interpreter.ScriptInterpreter;
@@ -135,17 +136,22 @@ public void deleteContainer(ContainerDTO container, Consumer<ContainerDTO> onSuc
135136
final InteractiveScriptSession interactiveScriptSession = this.scriptInterpreter
136137
.createInteractiveSession();
137138

138-
interactiveScriptSession.eval(
139-
"const ShortcutReader = include(\"engines." + engineId + ".shortcuts.reader\");",
140-
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
141-
final org.graalvm.polyglot.Value shortcutReader = (org.graalvm.polyglot.Value) output;
142-
shortcutReader.invokeMember("of", shortcut);
143-
final String containerName = shortcutReader.invokeMember("getContainer")
144-
.as(String.class);
139+
interactiveScriptSession.eval("include(\"engines." + engineId + ".shortcuts.reader\");",
140+
result -> {
141+
final org.graalvm.polyglot.Value shortcutReaderClass = (org.graalvm.polyglot.Value) result;
142+
143+
final ShortcutReader shortcutReader = shortcutReaderClass.newInstance()
144+
.as(ShortcutReader.class);
145+
146+
shortcutReader.of(shortcut);
147+
148+
final String containerName = shortcutReader.getContainer();
149+
145150
if (containerName.equals(container.getName())) {
146151
this.shortcutManager.deleteShortcut(shortcut);
147152
}
148-
}, onError), onError);
153+
},
154+
onError);
149155
});
150156

151157
onSuccess.accept(container);

phoenicis-library/src/main/java/org/phoenicis/library/ShortcutManager.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.phoenicis.configuration.security.Safe;
2525
import org.phoenicis.library.dto.ShortcutDTO;
2626
import org.phoenicis.library.dto.ShortcutInfoDTO;
27-
import org.phoenicis.scripts.session.InteractiveScriptSession;
2827
import org.phoenicis.scripts.interpreter.ScriptInterpreter;
28+
import org.phoenicis.scripts.session.InteractiveScriptSession;
2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
3131

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

123-
interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");",
124-
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
125-
final Value shortcutReader = (Value) output;
126-
shortcutReader.invokeMember("of", shortcutDTO);
127-
shortcutReader.invokeMember("uninstall");
128-
}, errorCallback), errorCallback);
123+
interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");",
124+
result -> {
125+
Value shortcutReaderClass = (Value) result;
126+
127+
ShortcutReader shortcutReader = shortcutReaderClass.newInstance().as(ShortcutReader.class);
128+
129+
shortcutReader.of(shortcutDTO);
130+
shortcutReader.uninstall();
131+
},
132+
errorCallback);
129133
}
130134

131135
public void deleteShortcut(ShortcutDTO shortcutDTO) {
@@ -216,6 +220,7 @@ public void updateShortcut(ShortcutDTO shortcutDTO) {
216220

217221
/**
218222
* creates a file with a resource fallback
223+
*
219224
* @param path file path
220225
* @param resource resource which shall be used if file path cannot be accessed
221226
* @return created file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.phoenicis.library;
2+
3+
import org.phoenicis.library.dto.ShortcutDTO;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Interface for all shortcut reader implementations
9+
*/
10+
public interface ShortcutReader {
11+
/**
12+
* Sets the shortcut
13+
*
14+
* @param shortcut The shortcut
15+
*/
16+
void of(ShortcutDTO shortcut);
17+
18+
/**
19+
* Returns the name of the container belonging to the shortcut
20+
*
21+
* @return The container name
22+
*/
23+
String getContainer();
24+
25+
/**
26+
* Runs a shortcut with the given user arguments
27+
*
28+
* @param arguments The user arguments
29+
*/
30+
void run(List<String> arguments);
31+
32+
/**
33+
* Stops the running shortcut
34+
*/
35+
void stop();
36+
37+
/**
38+
* Uninstalls the shortcut
39+
*/
40+
void uninstall();
41+
}

phoenicis-library/src/main/java/org/phoenicis/library/ShortcutRunner.java

+19-13
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import org.graalvm.polyglot.Value;
2222
import org.phoenicis.library.dto.ShortcutDTO;
23-
import org.phoenicis.scripts.session.InteractiveScriptSession;
2423
import org.phoenicis.scripts.interpreter.ScriptInterpreter;
24+
import org.phoenicis.scripts.session.InteractiveScriptSession;
2525

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

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

49-
shortcutReader.invokeMember("of", shortcutDTO);
50-
shortcutReader.invokeMember("run", arguments);
51-
}, errorCallback), errorCallback);
49+
ShortcutReader shortcutReader = shortcutReaderClass.newInstance().as(ShortcutReader.class);
50+
51+
shortcutReader.of(shortcutDTO);
52+
shortcutReader.run(arguments);
53+
},
54+
errorCallback);
5255
}
5356

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

57-
interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");",
58-
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
59-
final Value shortcutReader = (Value) output;
60+
interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");",
61+
result -> {
62+
Value shortcutReaderClass = (Value) result;
63+
64+
ShortcutReader shortcutReader = shortcutReaderClass.newInstance().as(ShortcutReader.class);
6065

61-
shortcutReader.invokeMember("of", shortcutDTO);
62-
shortcutReader.invokeMember("stop");
63-
}, errorCallback), errorCallback);
66+
shortcutReader.of(shortcutDTO);
67+
shortcutReader.stop();
68+
},
69+
errorCallback);
6470
}
6571

6672
}

0 commit comments

Comments
 (0)