Skip to content

Commit

Permalink
mspsim: search for scripts in resources
Browse files Browse the repository at this point in the history
  • Loading branch information
nfi committed Nov 5, 2024
1 parent c3042b5 commit 4e054da
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions java/se/sics/mspsim/cli/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.sics.mspsim.cli;
import java.io.File;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -128,15 +129,45 @@ public int executeCommand(String commandLine, CommandContext context) {
return -1;
}

private File resolveScript(String script) {
// Only search for script files
if (!script.endsWith(".sc")) {
return null;
}
var scriptFile = new File(scriptDirectory, script);
if (scriptFile.exists()) {
return scriptFile;
}
scriptFile = new File("config/scripts", script);
if (scriptFile.exists()) {
return scriptFile;
}
File parent;
try {
parent = new File(CommandHandler.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.getParentFile().getParentFile();
} catch (URISyntaxException e) {
parent = null;
}
if (parent != null) {
var scriptPath = "resources/main/scripts/" + script;
scriptFile = new File(parent, scriptPath);
if (!scriptFile.exists()) { // Running from gradle
scriptFile= new File(parent.getParentFile(), scriptPath);
}
}
return scriptFile.exists() ? scriptFile : null;
}

// This will return an instance that can be configured -
// which is basically not OK... TODO - fix this!!!
private Command getCommand(String cmd) {
Command command = commands.get(cmd);
if (command != null) {
return (Command) command.getInstance();
}
File scriptFile = new File(scriptDirectory, cmd);
if (scriptFile.isFile() && scriptFile.canRead()) {
File scriptFile = resolveScript(cmd);
if (scriptFile != null && scriptFile.isFile() && scriptFile.canRead()) {
return new ScriptCommand(scriptFile);
}
return null;
Expand Down

0 comments on commit 4e054da

Please sign in to comment.