Skip to content

Commit

Permalink
SystemRegistry: added method initialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 17, 2020
1 parent af2d980 commit a526e18
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 47 deletions.
7 changes: 7 additions & 0 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.jline.builtins;

import java.io.File;
import java.util.Map;

import org.jline.builtins.CommandRegistry;
Expand All @@ -19,6 +20,12 @@ public interface ConsoleEngine extends CommandRegistry {

Object execute(ParsedLine parsedLine) throws Exception;

default Object execute(File script) throws Exception {
return execute(script, "", new String[0]);
}

Object execute(File script, String cmdLine, String[] args) throws Exception;

Object postProcess(String line, Object result);

void println(Object object);
Expand Down
117 changes: 77 additions & 40 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -209,10 +210,15 @@ private String quote(String var) {
private class ScriptFile {
private File script;
private String extension = "";
private String cmdLine;
private String[] args;
private Object result;

@SuppressWarnings("unchecked")
public ScriptFile(String command) {
public ScriptFile(String command, String cmdLine, String[] args) {
this.script = new File(command);
this.cmdLine = cmdLine;
this.args = args;
if (script.exists()) {
scriptExtension(command);
} else if (engine.hasVariable("PATH")) {
Expand All @@ -238,6 +244,16 @@ public ScriptFile(String command) {
}
}

public ScriptFile(File script, String cmdLine, String[] args) {
if (!script.exists()) {
throw new IllegalArgumentException();
}
this.script = script;
this.cmdLine = cmdLine;
this.args = args;
scriptExtension(script.getName());
}

private void scriptExtension(String command) {
String name = script.getName();
this.extension = name.contains(".") ? name.substring(name.lastIndexOf(".") + 1) : "";
Expand All @@ -246,18 +262,69 @@ private void scriptExtension(String command) {
}
}

public File getScript() {
return script;
}

public boolean isEngineScript() {
private boolean isEngineScript() {
return engine.getExtensions().contains(extension);
}

public boolean isConsoleScript() {
private boolean isConsoleScript() {
return scriptExtension.equals(extension);
}

private boolean isScript() {
return engine.getExtensions().contains(extension) || scriptExtension.equals(extension);
}

public boolean execute() throws Exception {
if (!isScript()) {
return false;
}
result = null;
if(isEngineScript()) {
result = engine.execute(script, expandParameters(args));
result = postProcess(cmdLine, result);
} else if (isConsoleScript()) {
boolean done = false;
String line = "";
try(BufferedReader br = new BufferedReader(new FileReader(script))) {
for(String l; (l = br.readLine()) != null; ) {
try {
line += l;
parser.parse(line, line.length() + 1, ParseContext.ACCEPT_LINE);
done = true;
for (int i = 0; i < args.length; i++) {
line = line.replaceAll("\\$\\d", args[i].startsWith("$") ? expandName(args[i]) : quote(args[i]));
}
systemRegistry.execute(parser.parse(line, 0, ParseContext.COMPLETE));
line = "";
} catch (EOFError e) {
done = false;
line += "\n";
} catch (SyntaxError e) {
throw e;
} catch (Exception e) {
throw new IllegalArgumentException(line + "\n" + e.getMessage());
}
}
if (!done) {
throw new IllegalArgumentException("Incompleted command: \n" + line);
}
result = engine.get("_return");
result = postProcess(cmdLine, result);
}
}
return true;
}

public Object getResult() {
return result;
}

}

public Object execute(File script, String cmdLine, String[] args) throws Exception {
ScriptFile file = new ScriptFile(script, cmdLine, args);
file.execute();
return file.getResult();
}

@Override
Expand All @@ -271,39 +338,9 @@ public Object execute(ParsedLine pl) throws Exception {
cmd = cmd.substring(1);
}
Object out = null;
ScriptFile file = new ScriptFile(cmd);
if(file.isEngineScript()) {
out = engine.execute(file.getScript(), expandParameters(args));
out = postProcess(pl.line(), out);
} else if (file.isConsoleScript()) {
boolean done = false;
String line = "";
try(BufferedReader br = new BufferedReader(new FileReader(file.getScript()))) {
for(String l; (l = br.readLine()) != null; ) {
try {
line += l;
parser.parse(line, line.length() + 1, ParseContext.ACCEPT_LINE);
done = true;
for (int i = 0; i < args.length; i++) {
line = line.replaceAll("\\$\\d", args[i].startsWith("$") ? expandName(args[i]) : quote(args[i]));
}
systemRegistry.execute(parser.parse(line, 0, ParseContext.COMPLETE));
line = "";
} catch (EOFError e) {
done = false;
line += "\n";
} catch (SyntaxError e) {
throw e;
} catch (Exception e) {
throw new IllegalArgumentException(line + "\n" + e.getMessage());
}
}
if (!done) {
throw new IllegalArgumentException("Incompleted command: \n" + line);
}
out = engine.get("_return");
out = postProcess(pl.line(), out);
}
ScriptFile file = new ScriptFile(cmd, pl.line(), args);
if (file.execute()) {
out = file.getResult();
} else {
String line = pl.line();
if (isCodeBlock(line)) {
Expand Down
3 changes: 3 additions & 0 deletions builtins/src/main/java/org/jline/builtins/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
*/
package org.jline.builtins;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.jline.reader.ParsedLine;

public interface SystemRegistry extends CommandRegistry {

public void initialize(File script);

Object execute(ParsedLine parsedLine) throws Exception;

static SystemRegistry get() {
Expand Down
24 changes: 20 additions & 4 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.jline.builtins;

import java.io.File;
import java.util.*;

import org.jline.builtins.CommandRegistry;
Expand Down Expand Up @@ -42,6 +43,18 @@ public SystemRegistryImpl(CommandRegistry... commandRegistries) {
SystemRegistry.add(this);
}

@Override
public void initialize(File script) {
if (consoleId != null) {
try {
consoleEngine().execute(script);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}

@Override
public Set<String> commandNames() {
Set<String> out = new HashSet<>();
for (CommandRegistry r : commandRegistries) {
Expand All @@ -50,6 +63,7 @@ public Set<String> commandNames() {
return out;
}

@Override
public Map<String, String> commandAliases() {
Map<String, String> out = new HashMap<>();
for (CommandRegistry r : commandRegistries) {
Expand All @@ -58,28 +72,29 @@ public Map<String, String> commandAliases() {
return out;
}

@Override
public List<String> commandInfo(String command) {
int id = registryId(command);
return id > -1 ? commandRegistries[id].commandInfo(command) : new ArrayList<>();
}

@Override
public boolean hasCommand(String command) {
return registryId(command) > -1;
}

@Override
public Completers.SystemCompleter compileCompleters() {
return CommandRegistry.compileCompleters(commandRegistries);
}

@Override
public Widgets.CmdDesc commandDescription(String command) {
int id = registryId(command);
return id > -1 ? commandRegistries[id].commandDescription(command) : new Widgets.CmdDesc(false);
}

public Object execute(String command, String[] args) throws Exception {
return null;
}

@Override
public Object invoke(String command, Object... args) throws Exception {
Object out = null;
if (command.startsWith(":")) {
Expand All @@ -92,6 +107,7 @@ public Object invoke(String command, Object... args) throws Exception {
return out;
}

@Override
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());
Expand Down
3 changes: 2 additions & 1 deletion builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static org.jline.builtins.Completers.TreeCompleter.node;

import java.io.FileNotFoundException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.LocalDate;
Expand Down Expand Up @@ -623,6 +623,7 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
ExampleCommands exampleCommands = new ExampleCommands();
ConsoleEngine consoleEngine = new ConsoleEngineImpl(scriptEngine, parser, terminal, null);
SystemRegistryImpl systemRegistry = new SystemRegistryImpl(consoleEngine, builtins, exampleCommands);
// systemRegistry.initialize(new File("./example/init.jline"));
//
// Command completers
//
Expand Down
4 changes: 2 additions & 2 deletions reader/src/main/java/org/jline/reader/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.regex.Pattern;

public interface Parser {
static final String REGEX_VARIABLE = "[a-zA-Z]{1,}[a-zA-Z0-9_-]*";
static final String REGEX_COMMAND = "[:]{0,1}" + REGEX_VARIABLE;
static final String REGEX_VARIABLE = "[a-zA-Z_]{1,}[a-zA-Z0-9_-]*";
static final String REGEX_COMMAND = "[:]{0,1}[a-zA-Z]{1,}[a-zA-Z0-9_-]*";

ParsedLine parse(String line, int cursor, ParseContext context) throws SyntaxError;

Expand Down

0 comments on commit a526e18

Please sign in to comment.