Skip to content

Commit

Permalink
Support saving and loading source history in config.
Browse files Browse the repository at this point in the history
  • Loading branch information
hptruong93 committed May 23, 2022
1 parent 82412ac commit d9c209f
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 113 deletions.
7 changes: 4 additions & 3 deletions src/core/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

public class Config implements ILoggable {

public static final String RELEASE_VERSION = "5.6.3";
public static final String RELEASE_VERSION = "5.6.4";
protected static final String CONFIG_FILE_NAME = "config.json";
public static final String EXPORTED_CONFIG_FILE_NAME = "exported_" + CONFIG_FILE_NAME;
protected static final String CURRENT_CONFIG_VERSION = "2.13";
protected static final String CURRENT_CONFIG_VERSION = "2.14";

private static final Level DEFAULT_NATIVE_HOOK_DEBUG_LEVEL = Level.WARNING;
private static final boolean DEFAULT_TRAY_ICON_USE = true;
Expand Down Expand Up @@ -89,7 +89,8 @@ public class Config implements ILoggable {
new Parser2_10(),
new Parser2_11(),
new Parser2_12(),
new Parser2_13()
new Parser2_13(),
new Parser2_14()
});
}

Expand Down
93 changes: 1 addition & 92 deletions src/core/config/Parser2_13.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package core.config;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import argo.jdom.JsonNode;
import argo.jdom.JsonNodeFactories;
import argo.jdom.JsonRootNode;
import core.cli.server.CliServer;
import core.controller.CoreConfig;
import core.ipc.IPCServiceManager;
import core.ipc.IPCServiceName;
import core.ipc.repeatClient.repeatPeerClient.RepeatsPeerServiceClientManager;
import core.keyChain.KeyChain;
import core.userDefinedTask.TaskGroup;
import core.userDefinedTask.internals.ToolsConfig;
import utilities.json.JSONUtility;

public class Parser2_13 extends ConfigParser {

private static final Logger LOGGER = Logger.getLogger(Parser2_13.class.getName());

@Override
protected String getVersion() {
return "2.13";
Expand All @@ -38,67 +25,6 @@ protected JsonRootNode internalConvertFromPreviousVersion(JsonRootNode previousV
return JSONUtility.replaceChild(previousVersion, "global_settings", replacedGlobalSettings).getRootNode();
}

@Override
protected boolean internalExtractData(Config config, JsonRootNode root) {
try {
JsonNode globalSettings = root.getNode("global_settings");
config.setUseTrayIcon(globalSettings.getBooleanValue("tray_icon_enabled"));
config.setEnabledHaltingKeyPressed(globalSettings.getBooleanValue("enabled_halt_by_key"));
config.setExecuteOnKeyReleased(globalSettings.getBooleanValue("execute_on_key_released"));
config.setUseClipboardToTypeString(globalSettings.getBooleanValue("use_clipboard_to_type_string"));
config.setRunTaskWithServerConfig(globalSettings.getBooleanValue("run_task_with_server_config"));
config.setUseJavaAwtToGetMousePosition(globalSettings.getBooleanValue("use_java_awt_for_mouse_position"));

config.setNativeHookDebugLevel(Level.parse(globalSettings.getNode("debug").getStringValue("level")));

JsonNode globalHotkey = globalSettings.getNode("global_hotkey");

String mouseGestureActivation = globalHotkey.getNumberValue("mouse_gesture_activation");
config.setMouseGestureActivationKey(Integer.parseInt(mouseGestureActivation));
config.setRECORD(KeyChain.parseJSON(globalHotkey.getArrayNode("record")));
config.setREPLAY(KeyChain.parseJSON(globalHotkey.getArrayNode("replay")));
config.setCOMPILED_REPLAY(KeyChain.parseJSON(globalHotkey.getArrayNode("replay_compiled")));

JsonNode toolsConfigNode = globalSettings.getNode("tools_config");
ToolsConfig toolsConfig = ToolsConfig.parseJSON(toolsConfigNode);
config.setToolsConfig(toolsConfig);

JsonNode coreConfigNode = globalSettings.getNode("core_config");
CoreConfig coreConfig = CoreConfig.parseJSON(coreConfigNode);
config.setCoreConfig(coreConfig);

JsonNode peerClients = root.getNode("remote_repeats_clients");
RepeatsPeerServiceClientManager repeatsPeerServiceClientManager = RepeatsPeerServiceClientManager.parseJSON(peerClients);
config.getBackEnd().getPeerServiceClientManager().updateClients(repeatsPeerServiceClientManager.getClients());

List<JsonNode> ipcSettings = root.getArrayNode("ipc_settings");
if (!IPCServiceManager.parseJSON(ipcSettings)) {
LOGGER.log(Level.WARNING, "IPC Service Manager failed to parse JSON metadata");
}

if (!config.getCompilerFactory().parseJSON(root.getNode("compilers"))) {
LOGGER.log(Level.WARNING, "Dynamic Compiler Manager failed to parse JSON metadata");
}

config.getBackEnd().clearTaskGroup();
for (JsonNode taskGroupNode : root.getArrayNode("task_groups")) {
TaskGroup taskGroup = TaskGroup.parseJSON(config.getCompilerFactory(), taskGroupNode);
if (taskGroup != null) {
config.getBackEnd().addTaskGroup(taskGroup);
}
}

if (config.getBackEnd().getTaskGroups().isEmpty()) {
config.getBackEnd().addTaskGroup(new TaskGroup("default"));
}
config.getBackEnd().setCurrentTaskGroup(config.getBackEnd().getTaskGroups().get(0));
return true;
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to parse json", e);
return false;
}
}

@Override
protected boolean internalImportData(Config config, JsonRootNode root) {
boolean result = true;
Expand All @@ -112,21 +38,4 @@ protected boolean internalImportData(Config config, JsonRootNode root) {
}
return result;
}

@Override
protected boolean internalExtractData(CliConfig config, JsonRootNode root) {
try {
List<JsonNode> ipcSettings = root.getArrayNode("ipc_settings");
if (!IPCServiceManager.parseJSON(ipcSettings)) {
LOGGER.log(Level.WARNING, "IPC Service Manager failed to parse JSON metadata");
}

CliServer cliServer = (CliServer) IPCServiceManager.getIPCService(IPCServiceName.CLI_SERVER);
config.setServerPort(cliServer.getPort());
return true;
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to parse json", e);
return false;
}
}
}
}
154 changes: 154 additions & 0 deletions src/core/config/Parser2_14.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package core.config;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import argo.jdom.JsonNode;
import argo.jdom.JsonNodeFactories;
import argo.jdom.JsonRootNode;
import core.cli.server.CliServer;
import core.controller.CoreConfig;
import core.ipc.IPCServiceManager;
import core.ipc.IPCServiceName;
import core.ipc.repeatClient.repeatPeerClient.RepeatsPeerServiceClientManager;
import core.keyChain.KeyChain;
import core.userDefinedTask.TaskGroup;
import core.userDefinedTask.internals.ToolsConfig;
import utilities.json.JSONUtility;

public class Parser2_14 extends ConfigParser {

private static final Logger LOGGER = Logger.getLogger(Parser2_14.class.getName());

@Override
protected String getVersion() {
return "2.14";
}

@Override
protected String getPreviousVersion() {
return "2.13";
}

@Override
protected JsonRootNode internalConvertFromPreviousVersion(JsonRootNode previousVersion) {
JsonNode taskGroups = previousVersion.getNode("task_groups");
return JSONUtility.replaceChild(previousVersion, "task_groups", convertTaskGroups(taskGroups)).getRootNode();
}

private JsonNode convertTaskGroups(JsonNode node) {
List<JsonNode> groups = node.getArrayNode();
List<JsonNode> convertedGroups = new ArrayList<>();
for (JsonNode group : groups) {
List<JsonNode> convertedTasks = group.getArrayNode("tasks").stream().map(task -> convertTask(task)).collect(Collectors.toList());
convertedGroups.add(JSONUtility.replaceChild(group, "tasks", JsonNodeFactories.array(convertedTasks)));
}

return JsonNodeFactories.array(convertedGroups);
}

private JsonNode convertTask(JsonNode node) {
String currentPath = node.getStringValue("source_path");
JsonNode currentSource = JsonNodeFactories.object(
JsonNodeFactories.field("path", JsonNodeFactories.string(currentPath)),
JsonNodeFactories.field("created_time", JsonNodeFactories.number(System.currentTimeMillis())));

return JSONUtility.addChild(node, "source_history", JsonNodeFactories.object(
JsonNodeFactories.field("entries", JsonNodeFactories.array(currentSource))));
}

@Override
protected boolean internalExtractData(Config config, JsonRootNode root) {
try {
JsonNode globalSettings = root.getNode("global_settings");
config.setUseTrayIcon(globalSettings.getBooleanValue("tray_icon_enabled"));
config.setEnabledHaltingKeyPressed(globalSettings.getBooleanValue("enabled_halt_by_key"));
config.setExecuteOnKeyReleased(globalSettings.getBooleanValue("execute_on_key_released"));
config.setUseClipboardToTypeString(globalSettings.getBooleanValue("use_clipboard_to_type_string"));
config.setRunTaskWithServerConfig(globalSettings.getBooleanValue("run_task_with_server_config"));
config.setUseJavaAwtToGetMousePosition(globalSettings.getBooleanValue("use_java_awt_for_mouse_position"));

config.setNativeHookDebugLevel(Level.parse(globalSettings.getNode("debug").getStringValue("level")));

JsonNode globalHotkey = globalSettings.getNode("global_hotkey");

String mouseGestureActivation = globalHotkey.getNumberValue("mouse_gesture_activation");
config.setMouseGestureActivationKey(Integer.parseInt(mouseGestureActivation));
config.setRECORD(KeyChain.parseJSON(globalHotkey.getArrayNode("record")));
config.setREPLAY(KeyChain.parseJSON(globalHotkey.getArrayNode("replay")));
config.setCOMPILED_REPLAY(KeyChain.parseJSON(globalHotkey.getArrayNode("replay_compiled")));

JsonNode toolsConfigNode = globalSettings.getNode("tools_config");
ToolsConfig toolsConfig = ToolsConfig.parseJSON(toolsConfigNode);
config.setToolsConfig(toolsConfig);

JsonNode coreConfigNode = globalSettings.getNode("core_config");
CoreConfig coreConfig = CoreConfig.parseJSON(coreConfigNode);
config.setCoreConfig(coreConfig);

JsonNode peerClients = root.getNode("remote_repeats_clients");
RepeatsPeerServiceClientManager repeatsPeerServiceClientManager = RepeatsPeerServiceClientManager.parseJSON(peerClients);
config.getBackEnd().getPeerServiceClientManager().updateClients(repeatsPeerServiceClientManager.getClients());

List<JsonNode> ipcSettings = root.getArrayNode("ipc_settings");
if (!IPCServiceManager.parseJSON(ipcSettings)) {
LOGGER.log(Level.WARNING, "IPC Service Manager failed to parse JSON metadata");
}

if (!config.getCompilerFactory().parseJSON(root.getNode("compilers"))) {
LOGGER.log(Level.WARNING, "Dynamic Compiler Manager failed to parse JSON metadata");
}

config.getBackEnd().clearTaskGroup();
for (JsonNode taskGroupNode : root.getArrayNode("task_groups")) {
TaskGroup taskGroup = TaskGroup.parseJSON(config.getCompilerFactory(), taskGroupNode);
if (taskGroup != null) {
config.getBackEnd().addTaskGroup(taskGroup);
}
}

if (config.getBackEnd().getTaskGroups().isEmpty()) {
config.getBackEnd().addTaskGroup(new TaskGroup("default"));
}
config.getBackEnd().setCurrentTaskGroup(config.getBackEnd().getTaskGroups().get(0));
return true;
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to parse json", e);
return false;
}
}

@Override
protected boolean internalImportData(Config config, JsonRootNode root) {
boolean result = true;

for (JsonNode taskGroupNode : root.getArrayNode("task_groups")) {
TaskGroup taskGroup = TaskGroup.parseJSON(config.getCompilerFactory(), taskGroupNode);
result &= taskGroup != null;
if (taskGroup != null) {
result &= config.getBackEnd().addPopulatedTaskGroup(taskGroup);
}
}
return result;
}

@Override
protected boolean internalExtractData(CliConfig config, JsonRootNode root) {
try {
List<JsonNode> ipcSettings = root.getArrayNode("ipc_settings");
if (!IPCServiceManager.parseJSON(ipcSettings)) {
LOGGER.log(Level.WARNING, "IPC Service Manager failed to parse JSON metadata");
}

CliServer cliServer = (CliServer) IPCServiceManager.getIPCService(IPCServiceName.CLI_SERVER);
config.setServerPort(cliServer.getPort());
return true;
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to parse json", e);
return false;
}
}
}
5 changes: 0 additions & 5 deletions src/core/userDefinedTask/TaskSourceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,5 @@ public static boolean submitTask(UserDefinedAction task, String source) {
return true;
}

public static boolean removeTask(UserDefinedAction task) {
File sourceFile = new File(task.getSourcePath());
return FileUtility.removeFile(sourceFile);
}

private TaskSourceManager() {}
}
10 changes: 9 additions & 1 deletion src/core/userDefinedTask/UserDefinedAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ public JsonRootNode jsonize() {
JsonNodeFactories.field("name", JsonNodeFactories.string(name)),
JsonNodeFactories.field("activation", activation.jsonize()),
JsonNodeFactories.field("enabled", JsonNodeFactories.booleanNode(enabled)),
JsonNodeFactories.field("statistics", statistics.jsonize())
JsonNodeFactories.field("statistics", statistics.jsonize()),
JsonNodeFactories.field("source_history", sourceHistory.jsonize())
);
}

Expand Down Expand Up @@ -301,6 +302,13 @@ protected static UserDefinedAction parsePureJSON(DynamicCompilerManager factory,
LOGGER.warning("Unable to retrieve statistics for task " + name);
}

TaskSourceHistory sourceHistory = TaskSourceHistory.parseJSON(node.getNode("source_history"));
if (sourceHistory == null) {
LOGGER.warning("Unable to retrieve task source history for task " + name);
} else {
output.sourceHistory = sourceHistory;
}

boolean enabled = node.getBooleanValue("enabled");

output.actionId = actionId;
Expand Down
Loading

0 comments on commit d9c209f

Please sign in to comment.