-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
365 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
builtins/src/main/java/org/jline/builtins/ScriptCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package org.jline.builtins; | ||
|
||
import java.nio.file.Path; | ||
import java.util.*; | ||
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.CommandMethods; | ||
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 | ||
, DEL | ||
, ENGINES}; | ||
private final JLineEngine 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; | ||
|
||
public ScriptCommands(JLineEngine engine) { | ||
this.engine = engine; | ||
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)); | ||
} | ||
|
||
public Set<String> commandNames() { | ||
return nameCommand.keySet(); | ||
} | ||
|
||
public Map<String, String> commandAliases() { | ||
return aliasCommand; | ||
} | ||
|
||
public boolean hasCommand(String name) { | ||
if (nameCommand.containsKey(name) || aliasCommand.containsKey(name)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void doNameCommand() { | ||
nameCommand = commandName.entrySet() | ||
.stream() | ||
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); | ||
} | ||
|
||
private Command command(String name) { | ||
Command out = null; | ||
if (!hasCommand(name)) { | ||
throw new IllegalArgumentException("Command does not exists!"); | ||
} | ||
if (aliasCommand.containsKey(name)) { | ||
name = aliasCommand.get(name); | ||
} | ||
if (nameCommand.containsKey(name)) { | ||
out = nameCommand.get(name); | ||
} else { | ||
throw new IllegalArgumentException("Command does not exists!"); | ||
} | ||
return out; | ||
} | ||
|
||
public void rename(Command command, String newName) { | ||
if (nameCommand.containsKey(newName)) { | ||
throw new IllegalArgumentException("Duplicate command name!"); | ||
} else if (!commandName.containsKey(command)) { | ||
throw new IllegalArgumentException("Command does not exists!"); | ||
} | ||
commandName.put(command, newName); | ||
doNameCommand(); | ||
} | ||
|
||
public void alias(String alias, String command) { | ||
if (!nameCommand.keySet().contains(command)) { | ||
throw new IllegalArgumentException("Command does not exists!"); | ||
} | ||
aliasCommand.put(alias, command); | ||
} | ||
|
||
public List<String> commandInfo(String command) { | ||
return null; | ||
} | ||
|
||
public Completers.SystemCompleter compileCompleters() { | ||
return null; | ||
} | ||
|
||
public Widgets.CmdDesc commandDescription(String command) { | ||
return null; | ||
} | ||
|
||
public Object execute(String statement) throws Exception { | ||
return engine.execute(statement); | ||
} | ||
|
||
public Object execute(String command, String[] args) throws Exception { | ||
exception = null; | ||
Object out = commandExecute.get(command(command)).executeFunction().apply(new Builtins.CommandInput(args)); | ||
if (exception != null) { | ||
throw exception; | ||
} | ||
return out; | ||
} | ||
|
||
public Object engines(Builtins.CommandInput input) { | ||
return JLineEngine.listEngines(); | ||
} | ||
|
||
public Object set(Builtins.CommandInput input) { | ||
return engine.get(); | ||
} | ||
|
||
private List<Completer> defaultCompleter(String command) { | ||
return Arrays.asList(NullCompleter.INSTANCE); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0"?> | ||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jline</groupId> | ||
<artifactId>jline-parent</artifactId> | ||
<version>3.13.3-SNAPSHOT</version> | ||
</parent> | ||
<groupId>org.jline</groupId> | ||
<artifactId>jline-script</artifactId> | ||
<version>3.13.3-SNAPSHOT</version> | ||
<name>jline-script</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.codehaus.groovy</groupId> | ||
<artifactId>groovy-all</artifactId> | ||
<version>${groovy.version}</version> | ||
<type>pom</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
<configuration> | ||
<showWarnings>true</showWarnings> | ||
<compilerArgs> | ||
<arg>-Xlint:all,-options</arg> | ||
<!-- | ||
<arg>-Werror</arg> | ||
--> | ||
<arg>-profile</arg> | ||
<arg>compact1</arg> | ||
</compilerArgs> | ||
<fork>true</fork> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.jline.script; | ||
|
||
import java.io.File; | ||
import java.util.*; | ||
|
||
import javax.script.ScriptEngineFactory; | ||
import javax.script.ScriptEngineManager; | ||
|
||
public interface JLineEngine { | ||
|
||
public String getEngineName(); | ||
|
||
public void put(String name, Object value); | ||
|
||
public Object get(String name); | ||
|
||
public Map<String,Object> get(); | ||
|
||
public void del(String... vars); | ||
|
||
public Object execute(String statement) throws Exception; | ||
|
||
public Object execute(File script) throws Exception; | ||
|
||
public static List<Map<String, Object>> listEngines() { | ||
List<Map<String, Object>> out = new ArrayList<>(); | ||
ScriptEngineManager f = new ScriptEngineManager(); | ||
List<ScriptEngineFactory> engines = f.getEngineFactories(); | ||
for (ScriptEngineFactory engine : engines) { | ||
Map<String,Object> e = new HashMap<>(); | ||
e.put("name", engine.getEngineName()); | ||
e.put("version", engine.getEngineVersion()); | ||
e.put("language", engine.getLanguageName()); | ||
e.put("extensions", engine.getExtensions()); | ||
e.put("nick-names", engine.getNames()); | ||
out.add(e); | ||
} | ||
return out; | ||
} | ||
} |
Oops, something went wrong.