Skip to content

Commit

Permalink
Implemented simple script execution
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 9, 2020
1 parent 939c1d7 commit 55c1624
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 40 deletions.
9 changes: 8 additions & 1 deletion builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void alias(String alias, String command) {
aliasCommand.put(alias, command);
}

@Override
public boolean hasCommand(String name) {
if (nameCommand.containsKey(name) || aliasCommand.containsKey(name)) {
return true;
Expand Down Expand Up @@ -169,8 +170,14 @@ private Command command(String name) {
return out;
}

private void execute(String command, List<String> args) throws Exception {
@Override
public Object execute(String command, String[] args) throws Exception {
return execute(command, Arrays.asList(args));
}

private Object execute(String command, List<String> args) throws Exception {
execute(command, args, System.in, System.out, System.err);
return null;
}

public void execute(String command, List<String> args, InputStream in, PrintStream out, PrintStream err) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.jline.builtins.Completers;
import org.jline.builtins.Widgets;
import org.jline.reader.*;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -79,4 +80,12 @@ static Completers.SystemCompleter compileCompleters(CommandRegistry ... commandR
* in the terminal status bar.
*/
Widgets.CmdDesc commandDescription(String command);

default Object execute(String command, String[] args) throws Exception {
throw new IllegalAccessError("Not implemented!");
}

default Object execute(ParsedLine parsedLine) throws Exception {
throw new IllegalAccessError("Not implemented!");
}
}
70 changes: 57 additions & 13 deletions builtins/src/main/java/org/jline/builtins/ConsoleCommands.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.jline.builtins;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -13,12 +17,11 @@
import org.jline.builtins.Builtins.CommandInput;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.reader.Completer;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.LineReader;
import org.jline.reader.Widget;
import org.jline.reader.*;
import org.jline.reader.Parser.ParseContext;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.ScriptEngine;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;

public class ConsoleCommands implements CommandRegistry {
public enum Command {SHOW
Expand All @@ -31,11 +34,13 @@ public enum Command {SHOW
private final Map<Command,CommandMethods> commandExecute = new HashMap<>();
private Map<Command,List<String>> commandInfo = new HashMap<>();
private Exception exception;
private Consumer<String> execute;
private CommandRegistry masterRegistry;
private String scriptExtension = "jline";
private Parser parser;

public ConsoleCommands(ScriptEngine engine) {
public ConsoleCommands(ScriptEngine engine, Parser parser) {
this.engine = engine;
// this.commandExecute = execute;
this.parser = parser;
Set<Command> cmds = new HashSet<>(EnumSet.allOf(Command.class));
for (Command c: cmds) {
commandName.put(c, c.name().toLowerCase());
Expand All @@ -46,6 +51,15 @@ public ConsoleCommands(ScriptEngine engine) {
commandExecute.put(Command.SHOW, new CommandMethods(this::show, this::defaultCompleter));
}

public void setMasterRegistry(CommandRegistry masterRegistry) {
this.masterRegistry = masterRegistry;
}

public ConsoleCommands scriptExtension(String extension) {
this.scriptExtension = extension;
return this;
}

public Set<String> commandNames() {
return nameCommand.keySet();
}
Expand Down Expand Up @@ -116,22 +130,52 @@ public Completers.SystemCompleter compileCompleters() {
public Widgets.CmdDesc commandDescription(String command) {
return null;
}

private List<String> slurp(String path) throws IOException{
List<String> out = new ArrayList<>();
byte[] encoded = Files.readAllBytes(Paths.get(path));
out.addAll(Arrays.asList(new String(encoded, StandardCharsets.UTF_8).split("\n|\n\r")));
return out;
}

public Object execute(String cmd, String[] args, String statement) throws Exception {
public Object execute(ParsedLine pl) throws Exception {
String[] args = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
String cmd = Parser.getCommand(pl.word());
Object out = null;
if (new File(cmd).exists()) {
File file = new File(cmd);
String name = file.getName();
String ext = name.contains(".") ? name.substring(name.lastIndexOf(".") + 1) : "";
if(engine.getExtensions().contains(ext)) {
out = engine.execute(file, args);
out = engine.execute(file, args);
} else if (scriptExtension.equals(ext)) {
List<String> commandsBuffer = slurp(cmd);
String line = "";
boolean done = false;
while (!commandsBuffer.isEmpty()) {
try {
line += commandsBuffer.remove(0);
parser.parse(line, line.length() + 1, ParseContext.ACCEPT_LINE);
masterRegistry.execute(parser.parse(line, 0, ParseContext.ACCEPT_LINE));
line = "";
} catch (EOFError e) {
if (commandsBuffer.isEmpty()) {
throw new IllegalArgumentException("Incompleted command: \n" + line);
}
line += "\n";
} catch (SyntaxError e) {
commandsBuffer.clear();
throw e;
} catch (Exception e) {
commandsBuffer.clear();
throw new IllegalArgumentException(e.getMessage());
}
}
} else {
throw new IllegalArgumentException("Command not found: " + cmd);
}
// execute.accept(statement);

} else {
out = engine.execute(statement);
out = engine.execute(pl.line());
}
return out;
}
Expand Down
86 changes: 61 additions & 25 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.jline.reader.*;
import org.jline.reader.LineReader.Option;
import org.jline.reader.LineReader.SuggestionType;
import org.jline.reader.Parser.ParseContext;
import org.jline.reader.impl.DefaultParser;
import org.jline.reader.impl.DefaultParser.Bracket;
import org.jline.reader.impl.LineReaderImpl;
Expand Down Expand Up @@ -135,13 +136,42 @@ private static Map<String,CmdDesc> compileTailTips() {
return tailTips;
}

private static class MasterRegistry {
public static class MasterRegistry implements CommandRegistry {
private final CommandRegistry[] commandRegistries;
private Parser parser;

public MasterRegistry(CommandRegistry... commandRegistries) {
this.commandRegistries = commandRegistries;
}

public void setParser(Parser parser) {
this.parser = parser;
}

public Object execute(ParsedLine pl) throws Exception {
String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
String cmd = Parser.getCommand(pl.word());
Object out = null;
boolean done = false;
if ("help".equals(cmd) || "?".equals(cmd)) {
help();
done = true;
}
else {
for (CommandRegistry r : commandRegistries) {
if (r.hasCommand(cmd)) {
out = r.execute(cmd, argv);
done = true;
break;
}
}
}
if (!done) {
out = commandRegistries[0].execute(pl);
}
return out;
}

public void help() {
System.out.println("List of available commands:");
for (CommandRegistry r : commandRegistries) {
Expand All @@ -163,6 +193,29 @@ public void help() {
System.out.println(" Additional help:");
System.out.println(" <command> --help");
}
public Set<String> commandNames(){
return null;
}

public Map<String, String> commandAliases(){
return null;
}

public List<String> commandInfo(String command) {
return null;
}

public boolean hasCommand(String command) {
return true;
}

public Completers.SystemCompleter compileCompleters() {
return null;
}

public CmdDesc commandDescription(String command) {
return null;
}

public CmdDesc commandDescription(CmdLine line) {
CmdDesc out = null;
Expand Down Expand Up @@ -297,12 +350,13 @@ public Completers.SystemCompleter compileCompleters() {
return out;
}

public void execute(String command, String[] args) throws Exception {
public Object execute(String command, String[] args) throws Exception {
exception = null;
commandExecute.get(command(command)).execute().accept(new Builtins.CommandInput(args));
if (exception != null) {
throw exception;
}
return null;
}

public CmdDesc commandDescription(String command) {
Expand Down Expand Up @@ -643,8 +697,10 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
builtins.alias("zle", "widget");
builtins.alias("bindkey", "keymap");
ExampleCommands exampleCommands = new ExampleCommands();
ConsoleCommands consoleCommands = new ConsoleCommands(new Engine());
MasterRegistry masterRegistry = new MasterRegistry(builtins, consoleCommands, exampleCommands);
ConsoleCommands consoleCommands = new ConsoleCommands(new Engine(), parser);
MasterRegistry masterRegistry = new MasterRegistry(consoleCommands, builtins, exampleCommands);
masterRegistry.setParser(parser);
consoleCommands.setMasterRegistry(masterRegistry);
//
// Command completers
//
Expand Down Expand Up @@ -748,30 +804,10 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
break;
}

ParsedLine pl = reader.getParser().parse(line, 0);
String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
String cmd = Parser.getCommand(pl.word());
Object result = null;
if ("help".equals(cmd) || "?".equals(cmd)) {
masterRegistry.help();
}
else if (builtins.hasCommand(cmd)) {
builtins.execute(cmd, argv, System.in, System.out, System.err);
}
else if (exampleCommands.hasCommand(cmd)) {
exampleCommands.execute(cmd, argv);
}
else if (consoleCommands.hasCommand(cmd)) {
result = consoleCommands.execute(cmd, argv);
}
else {
result = consoleCommands.execute(cmd, argv, line);
}
Object result = masterRegistry.execute(reader.getParser().parse(line, 0, ParseContext.ACCEPT_LINE));
if (result != null) {
System.out.println(result);
}


}
catch (Options.HelpException e) {
Options.HelpException.highlight(e.getMessage(), Options.HelpException.defaultStyle()).print(terminal);
Expand Down
9 changes: 9 additions & 0 deletions builtins/src/test/script/init.jline
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import org.jline.utils.*

println "Is Windows: " + OSUtils.IS_WINDOWS

array=[0,1,2,3,4,5,6,7]

array.each {
println it
}
2 changes: 1 addition & 1 deletion groovy/src/test/script/hello.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class HelloWorld {
}

def static main(args){
HelloWorld.hello(!args || args.size() == 0 ? 'world' : args[0])
HelloWorld.hello(!args ? 'world' : args[0])
}

0 comments on commit 55c1624

Please sign in to comment.