support shebang

This commit is contained in:
Peter Hormanns 2014-09-30 18:08:58 +02:00
parent 713099eda5
commit 26a849dd26
3 changed files with 51 additions and 7 deletions

View File

@ -1,6 +1,6 @@
package de.hsadmin.jscli; package de.hsadmin.jscli;
import java.io.File; import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -19,19 +19,37 @@ public class Main {
final CommandlineParser cmdParser = new CommandlineParser(args); final CommandlineParser cmdParser = new CommandlineParser(args);
final String runAs = cmdParser.getRunAs(); final String runAs = cmdParser.getRunAs();
console.open(runAs + "@hsadmin> "); console.open(runAs + "@hsadmin> ");
final ScriptClient scriptClient = new ScriptClient(console, cmdParser.getUser(), runAs); final ScriptClient scriptClient = new ScriptClient(console, cmdParser.getUser(), runAs, cmdParser.getArgs());
final String file = cmdParser.getFile(); final String file = cmdParser.getFile();
if (file != null && file.length() > 0) { if (file != null && file.length() > 0) {
if ("-".equals(file)) { if ("-".equals(file)) {
scriptClient.execute(new InputStreamReader(System.in)); scriptClient.execute(new InputStreamReader(System.in));
console.println(formatter.format(scriptClient.getLastRpcResult())); console.println(formatter.format(scriptClient.getLastRpcResult()));
} else { } else {
File fileHandle = new File(file); BufferedReader bufferedReader = null;
try { try {
scriptClient.execute(new FileReader(fileHandle)); bufferedReader = new BufferedReader(new FileReader(file));
console.println(formatter.format(scriptClient.getLastRpcResult())); String inputLine = bufferedReader.readLine();
boolean isFirstLine = true;
final StringBuffer scriptFromFile = new StringBuffer();
while (inputLine != null) {
if (isFirstLine && inputLine.startsWith("#!")) {
scriptFromFile.append("//");
scriptFromFile.append(inputLine.substring(2));
} else {
scriptFromFile.append(inputLine);
}
scriptFromFile.append("\n");
isFirstLine = false;
inputLine = bufferedReader.readLine();
}
scriptClient.execute(scriptFromFile.toString());
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.err.println("File not found: " + file); System.err.println("File not found: " + file);
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
} }
} }
} }

View File

@ -20,7 +20,7 @@ public class ScriptClient {
final private ScriptEngine engine; final private ScriptEngine engine;
final private Set<String> completionStrings; final private Set<String> completionStrings;
public ScriptClient(final ConsoleWrapper console, final String user, final String runAs) throws JSCliException { public ScriptClient(final ConsoleWrapper console, final String user, final String runAs, final String... arguments) throws JSCliException {
final CASTicketProvider ticketProvider = new CASTicketProvider(console, user, runAs); final CASTicketProvider ticketProvider = new CASTicketProvider(console, user, runAs);
final RpcClient rpcClient = new RpcClient(ticketProvider); final RpcClient rpcClient = new RpcClient(ticketProvider);
final ScriptEngineManager engineManager = new ScriptEngineManager(); final ScriptEngineManager engineManager = new ScriptEngineManager();
@ -31,6 +31,7 @@ public class ScriptClient {
completionStrings = new HashSet<String>(); completionStrings = new HashSet<String>();
completionStrings.add("set"); completionStrings.add("set");
completionStrings.add("where"); completionStrings.add("where");
considerArguments(arguments);
try { try {
final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js"); final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js");
engine.eval(new InputStreamReader(inputResource)); engine.eval(new InputStreamReader(inputResource));
@ -69,7 +70,7 @@ public class ScriptClient {
} }
console.codeCompletion(getCodeCompletionStrings()); console.codeCompletion(getCodeCompletionStrings());
} }
public String[] getCodeCompletionStrings() { public String[] getCodeCompletionStrings() {
final String[] codeCompletionStrings = new String[completionStrings.size()]; final String[] codeCompletionStrings = new String[completionStrings.size()];
int idx = 0; int idx = 0;
@ -101,5 +102,26 @@ public class ScriptClient {
public Object getLastRpcResult() { public Object getLastRpcResult() {
return engine.get("xmlrpcLastResult"); return engine.get("xmlrpcLastResult");
} }
private void considerArguments(final String... arguments)
throws JSCliException {
final StringBuilder argsBuilder = new StringBuilder("var arguments = [ ");
boolean isFirstArg = true;
for (final String arg : arguments) {
if (!isFirstArg) {
argsBuilder.append(", ");
}
argsBuilder.append('\'');
argsBuilder.append(arg);
argsBuilder.append('\'');
isFirstArg = false;
}
argsBuilder.append(" ];");
try {
engine.eval(argsBuilder.toString());
} catch (ScriptException e) {
throw new JSCliException(e);
}
}
} }

View File

@ -63,4 +63,8 @@ public class CommandlineParser {
formatter.printHelp("hsscript", opts); formatter.printHelp("hsscript", opts);
} }
public String[] getArgs() {
return cmd.getArgs();
}
} }