Skip to content

Commit

Permalink
refactoring...
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 8, 2020
1 parent 0136022 commit 939c1d7
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 49 deletions.
2 changes: 1 addition & 1 deletion builtins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-script</artifactId>
<artifactId>jline-groovy</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
package org.jline.builtins;

import java.io.File;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.jline.builtins.Builtins;
import org.jline.builtins.Builtins.Command;
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.impl.completer.NullCompleter;
import org.jline.script.JLineEngine;

public class ScriptCommands implements CommandRegistry {
public enum Command {SET
import org.jline.reader.ScriptEngine;
public class ConsoleCommands implements CommandRegistry {
public enum Command {SHOW
, DEL
, ENGINES};
private final JLineEngine engine;
private final ScriptEngine engine;
private Map<Command,String> commandName = new HashMap<>();
private Map<String,Command> nameCommand = new HashMap<>();
private Map<String,String> aliasCommand = new HashMap<>();
private final Map<Command,CommandMethods> commandExecute = new HashMap<>();
private Map<Command,List<String>> commandInfo = new HashMap<>();
private Exception exception;
private Consumer<String> execute;

public ScriptCommands(JLineEngine engine) {
public ConsoleCommands(ScriptEngine engine) {
this.engine = engine;
// this.commandExecute = execute;
Set<Command> cmds = new HashSet<>(EnumSet.allOf(Command.class));
for (Command c: cmds) {
commandName.put(c, c.name().toLowerCase());
}
doNameCommand();
commandExecute.put(Command.ENGINES, new CommandMethods(this::engines, this::defaultCompleter));
commandExecute.put(Command.SET, new CommandMethods(this::set, this::defaultCompleter));
commandExecute.put(Command.DEL, new CommandMethods(this::del, this::defaultCompleter));
commandExecute.put(Command.SHOW, new CommandMethods(this::show, this::defaultCompleter));
}

public Set<String> commandNames() {
Expand Down Expand Up @@ -98,15 +105,35 @@ public List<String> commandInfo(String command) {
}

public Completers.SystemCompleter compileCompleters() {
return null;
SystemCompleter out = new SystemCompleter();
for (Map.Entry<Command, String> entry: commandName.entrySet()) {
out.add(entry.getValue(), commandExecute.get(entry.getKey()).compileCompleter().apply(entry.getValue()));
}
out.addAliases(aliasCommand);
return out;
}

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

public Object execute(String statement) throws Exception {
return engine.execute(statement);
public Object execute(String cmd, String[] args, String statement) throws Exception {
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);
} else {
throw new IllegalArgumentException("Command not found: " + cmd);
}
// execute.accept(statement);

} else {
out = engine.execute(statement);
}
return out;
}

public Object execute(String command, String[] args) throws Exception {
Expand All @@ -119,15 +146,19 @@ public Object execute(String command, String[] args) throws Exception {
}

public Object engines(Builtins.CommandInput input) {
return JLineEngine.listEngines();
return ScriptEngine.listEngines();
}

public Object set(Builtins.CommandInput input) {
public Object show(Builtins.CommandInput input) {
return engine.get();
}

public Object del(Builtins.CommandInput input) {
engine.del(input.args());
return null;
}

private List<Completer> defaultCompleter(String command) {
return Arrays.asList(NullCompleter.INSTANCE);
}

}
}
25 changes: 14 additions & 11 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
import org.jline.builtins.Completers;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.Completers.TreeCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.ScriptCommands;
import org.jline.builtins.Options;
import org.jline.builtins.ConsoleCommands;
import org.jline.builtins.Widgets.ArgDesc;
import org.jline.builtins.Widgets.AutopairWidgets;
import org.jline.builtins.Widgets.AutosuggestionWidgets;
import org.jline.builtins.Widgets.CmdDesc;
import org.jline.builtins.Widgets.CmdLine;
import org.jline.builtins.Widgets.TailTipWidgets;
import org.jline.builtins.Widgets.TailTipWidgets.TipType;
import org.jline.script.impl.Groovy;
import org.jline.groovy.Engine;
import org.jline.keymap.KeyMap;
import org.jline.reader.*;
import org.jline.reader.LineReader.Option;
Expand Down Expand Up @@ -643,12 +643,12 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
builtins.alias("zle", "widget");
builtins.alias("bindkey", "keymap");
ExampleCommands exampleCommands = new ExampleCommands();
ScriptCommands scriptCommands = new ScriptCommands(new Groovy());
MasterRegistry masterRegistry = new MasterRegistry(builtins, exampleCommands);
ConsoleCommands consoleCommands = new ConsoleCommands(new Engine());
MasterRegistry masterRegistry = new MasterRegistry(builtins, consoleCommands, exampleCommands);
//
// Command completers
//
AggregateCompleter finalCompleter = new AggregateCompleter(CommandRegistry.compileCompleters(builtins, exampleCommands)
AggregateCompleter finalCompleter = new AggregateCompleter(CommandRegistry.compileCompleters(builtins, consoleCommands, exampleCommands)
, completer != null ? completer : NullCompleter.INSTANCE);
//
// Terminal & LineReader
Expand Down Expand Up @@ -747,6 +747,7 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
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());
Expand All @@ -760,18 +761,20 @@ else if (builtins.hasCommand(cmd)) {
else if (exampleCommands.hasCommand(cmd)) {
exampleCommands.execute(cmd, argv);
}
else if (scriptCommands.hasCommand(cmd)) {
result = scriptCommands.execute(cmd, argv);
else if (consoleCommands.hasCommand(cmd)) {
result = consoleCommands.execute(cmd, argv);
}
else {
result = scriptCommands.execute(line);
result = consoleCommands.execute(cmd, argv, line);
}
if (result != null) {
System.out.println(result);
}


}
catch (HelpException e) {
HelpException.highlight(e.getMessage(), HelpException.defaultStyle()).print(terminal);
catch (Options.HelpException e) {
Options.HelpException.highlight(e.getMessage(), Options.HelpException.defaultStyle()).print(terminal);
}
catch (IllegalArgumentException|FileNotFoundException e) {
System.out.println(e.getMessage());
Expand Down
8 changes: 6 additions & 2 deletions script/pom.xml → groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
<version>3.13.3-SNAPSHOT</version>
</parent>
<groupId>org.jline</groupId>
<artifactId>jline-script</artifactId>
<artifactId>jline-groovy</artifactId>
<version>3.13.3-SNAPSHOT</version>
<name>jline-script</name>
<name>JLine Groovy</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<groovy.version>2.5.8</groovy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-reader</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package org.jline.script.impl;
package org.jline.groovy;

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

import org.jline.script.JLineEngine;
import org.jline.reader.ScriptEngine;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;

public class Groovy implements JLineEngine {
public class Engine implements ScriptEngine {
private GroovyShell shell;
private Binding sharedData;
private Map<String,String> imports = new HashMap<String,String>();

public Groovy() {
public Engine() {
this.sharedData = new Binding();
shell = new GroovyShell(sharedData);
}
Expand All @@ -37,7 +35,8 @@ public Map<String,Object> get() {
}

@Override
public Object execute(File script) throws Exception {
public Object execute(File script, Object[] args) throws Exception {
sharedData.setProperty("args", args);
Script s = shell.parse(script);
return s.run();
}
Expand All @@ -52,7 +51,7 @@ public Object execute(String statement) throws Exception {
} else if (statement.equals("import")) {
out = new ArrayList<>(imports.keySet());
} else {
String e="";
String e = "";
for (Map.Entry<String, String> entry : imports.entrySet()) {
e += entry.getValue()+"\n";
}
Expand All @@ -67,6 +66,11 @@ public String getEngineName() {
return this.getClass().getSimpleName();
}

@Override
public List<String> getExtensions() {
return Arrays.asList("groovy");
}

private void del(String var){
if (var==null) {
return;
Expand Down
9 changes: 9 additions & 0 deletions groovy/src/test/script/hello.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class HelloWorld {
static void hello(def who) {
println "hello $who!"
}
}

def static main(args){
HelloWorld.hello(!args || args.size() == 0 ? 'world' : args[0])
}
35 changes: 35 additions & 0 deletions jline/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
<artifactId>jline-reader</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-groovy</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-builtins</artifactId>
Expand Down Expand Up @@ -145,6 +150,14 @@
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.jline</groupId>
<artifactId>jline-groovy</artifactId>
<classifier>sources</classifier>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.jline</groupId>
<artifactId>jline-builtins</artifactId>
Expand Down Expand Up @@ -211,6 +224,14 @@
<outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
<excludes>**/*.class</excludes>
</artifactItem>
<artifactItem>
<groupId>org.jline</groupId>
<artifactId>jline-groovy</artifactId>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
<excludes>**/*.class</excludes>
</artifactItem>
<artifactItem>
<groupId>org.jline</groupId>
<artifactId>jline-builtins</artifactId>
Expand Down Expand Up @@ -294,6 +315,7 @@
<configuration>
<excludes>
<exclude>**/TTop.java</exclude>
<exclude>**/groovy/Engine.java</exclude>
</excludes>
<compilerArgs>
<arg>-Xlint:all,-options</arg>
Expand All @@ -303,6 +325,19 @@
</compilerArgs>
</configuration>
</execution>
<execution>
<id>compile-accept-warn</id>
<configuration>
<includes>
<include>**/groovy/Engine.java</include>
</includes>
<compilerArgs>
<arg>-Xlint:all,-options</arg>
<arg>-profile</arg>
<arg>compact1</arg>
</compilerArgs>
</configuration>
</execution>
<execution>
<id>noncompact</id>
<goals>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@

<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-script</artifactId>
<artifactId>jline-groovy</artifactId>
<version>${project.version}</version>
</dependency>

Expand Down Expand Up @@ -505,7 +505,7 @@
<module>terminal-jna</module>
<module>terminal-jansi</module>
<module>reader</module>
<module>script</module>
<module>groovy</module>
<module>builtins</module>
<module>remote-ssh</module>
<module>remote-telnet</module>
Expand Down
Loading

0 comments on commit 939c1d7

Please sign in to comment.