Skip to content

Commit

Permalink
GroovyCommand: added grap command
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 26, 2020
1 parent 51b8bcf commit 8be9242
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 7 deletions.
1 change: 1 addition & 0 deletions demo/jline-repl.bat
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pushd %TARGETDIR%\lib
for %%G in (jline-*.jar) do call:APPEND_TO_CLASSPATH %%G
rem Groovy
for %%G in (groovy-*.jar) do call:APPEND_TO_CLASSPATH %%G
for %%G in (ivy-*.jar) do call:APPEND_TO_CLASSPATH %%G

set "opts=%JLINE_OPTS%"
set "logconf=%DIRNAME%etc\logging.properties"
Expand Down
1 change: 1 addition & 0 deletions demo/jline-repl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cp=${TARGETDIR}/classes
# JLINE
cp=${cp}$(find ${TARGETDIR}/lib -name "jline-*.jar" -exec printf :{} ';')
cp=${cp}$(find ${TARGETDIR}/lib -name "groovy-*.jar" -exec printf :{} ';')
cp=${cp}$(find ${TARGETDIR}/lib -name "ivy-*.jar" -exec printf :{} ';')

opts="${JLINE_OPTS}"
logconf="${DIRNAME}/etc/logging.properties"
Expand Down
5 changes: 5 additions & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
<artifactId>groovy-console</artifactId>
</dependency>

<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>groovy-console</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
90 changes: 85 additions & 5 deletions groovy/src/main/java/org/jline/script/GroovyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
import groovy.console.ui.ObjectBrowser;

public class GroovyCommand extends AbstractCommandRegistry implements CommandRegistry {
public enum Command {INSPECT, CONSOLE}
public enum Command {INSPECT, CONSOLE, GRAP}
private GroovyEngine engine;
private Printer printer;
private final Map<Command,CmdDesc> commandDescs = new HashMap<>();
private final Map<Command,List<String>> commandInfos = new HashMap<>();
private boolean consoleUi;
private boolean ivy;

public GroovyCommand(GroovyEngine engine, Printer printer) {
this(null, engine, printer);
Expand All @@ -52,6 +53,12 @@ public GroovyCommand(Set<Command> commands, GroovyEngine engine, Printer printer
consoleUi = true;
} catch (Exception e) {
}
try {
Class.forName("org.apache.ivy.util.Message");
System.setProperty("groovy.grape.report.downloads","true");
ivy = true;
} catch (Exception e) {
}
Set<Command> cmds = new HashSet<>();
Map<Command,String> commandName = new HashMap<>();
Map<Command,CommandMethods> commandExecute = new HashMap<>();
Expand All @@ -63,14 +70,19 @@ public GroovyCommand(Set<Command> commands, GroovyEngine engine, Printer printer
if (!consoleUi) {
cmds.remove(Command.CONSOLE);
}
if (!ivy) {
cmds.remove(Command.GRAP);
}
for (Command c: cmds) {
commandName.put(c, c.name().toLowerCase());
}
commandExecute.put(Command.INSPECT, new CommandMethods(this::inspect, this::inspectCompleter));
commandExecute.put(Command.CONSOLE, new CommandMethods(this::console, this::consoleCompleter));
commandExecute.put(Command.CONSOLE, new CommandMethods(this::console, this::defaultCompleter));
commandExecute.put(Command.GRAP, new CommandMethods(this::grap, this::grapCompleter));
registerCommands(commandName, commandExecute);
commandDescs.put(Command.INSPECT, inspectCmdDesc());
commandDescs.put(Command.CONSOLE, consoleCmdDesc());
commandDescs.put(Command.GRAP, grapCmdDesc());
}

@Override
Expand All @@ -85,6 +97,49 @@ public CmdDesc commandDescription(String command) {
return commandDescs.get(cmd);
}

@SuppressWarnings("unchecked")
public Object grap(CommandInput input) {
if (input.args().length != 1) {
throw new IllegalArgumentException("Wrong number of command parameters: " + input.args().length);
}
try {
String arg = input.args()[0];
if (arg.equals("-?") || arg.equals("--help")) {
printer.println(commandDescs.get(Command.GRAP));
} else if (arg.equals("-l") || arg.equals("--list")) {
Object resp = engine.execute("groovy.grape.Grape.getInstance().enumerateGrapes()");
Map<String, Object> options = new HashMap<>();
options.put(Printer.SKIP_DEFAULT_OPTIONS, true);
options.put(Printer.MAX_DEPTH, 1);
options.put(Printer.INDENTION, 4);
printer.println(options, resp);
} else if (arg.startsWith("-")) {
throw new IllegalArgumentException("Unknown command option: " + arg);
} else {
Map<String, String> artifact = new HashMap<>();
Object xarg = input.xargs()[0];
if (xarg instanceof String) {
String[] vals = input.args()[0].split(":");
if (vals.length != 3) {
throw new IllegalArgumentException("Invalid command parameter: " + input.args()[0]);
}
artifact.put("group", vals[0]);
artifact.put("module", vals[1]);
artifact.put("version", vals[2]);
} else if (xarg instanceof Map) {
artifact = (Map<String, String>) xarg;
} else {
throw new IllegalArgumentException("Unknown command parameter: " + xarg);
}
engine.put("_artifact", artifact);
engine.execute("groovy.grape.Grape.grab(_artifact)");
}
} catch (Exception e) {
saveException(e);
}
return null;
}

public void console(CommandInput input) {
if (input.args().length > 1) {
throw new IllegalArgumentException("Wrong number of command parameters: " + input.args().length);
Expand All @@ -98,7 +153,7 @@ public void console(CommandInput input) {
throw new IllegalArgumentException("Unknown command parameter: " + input.args()[0]);
}
}
Console c = new Console();
Console c = new Console(engine.sharedData);
c.run();
}

Expand Down Expand Up @@ -149,6 +204,22 @@ public Object inspect(CommandInput input) {
return null;
}

private CmdDesc grapCmdDesc() {
Map<String,List<AttributedString>> optDescs = new HashMap<>();
optDescs.put("-? --help", doDescription ("Displays command help"));
optDescs.put("-l --list", doDescription ("List the modules in the cache"));
CmdDesc out = new CmdDesc(new ArrayList<>(), optDescs);
List<AttributedString> mainDesc = new ArrayList<>();
List<String> info = new ArrayList<>();
info.add("Add maven repository dependencies to classpath");
commandInfos.put(Command.GRAP, info);
mainDesc.add(new AttributedString("grap - " + info.get(0)));
mainDesc.add(new AttributedString("Usage: grap <group>:<artifact>:<version>"));
mainDesc.add(new AttributedString(" grap --list"));
out.setMainDesc(mainDesc);
return out;
}

private CmdDesc consoleCmdDesc() {
Map<String,List<AttributedString>> optDescs = new HashMap<>();
optDescs.put("-? --help", doDescription ("Displays command help"));
Expand All @@ -171,7 +242,7 @@ private CmdDesc inspectCmdDesc() {
}
optDescs.put("-i --info", doDescription ("Object class info"));
optDescs.put("-m --methods", doDescription ("List object methods"));
optDescs.put("-n --metaMethods", doDescription ("List object metaMethads"));
optDescs.put("-n --metaMethods", doDescription ("List object metaMethods"));
CmdDesc out = new CmdDesc(new ArrayList<>(), optDescs);
List<AttributedString> mainDesc = new ArrayList<>();
List<String> info = new ArrayList<>();
Expand Down Expand Up @@ -219,7 +290,16 @@ public List<Completer> inspectCompleter(String command) {
return out;
}

public List<Completer> consoleCompleter(String command) {
public List<Completer> grapCompleter(String command) {
List<Completer> out = new ArrayList<>();
ArgumentCompleter ac = new ArgumentCompleter(NullCompleter.INSTANCE
, new StringsCompleter("--help", "-?", "--list", "-l")
, NullCompleter.INSTANCE);
out.add(ac);
return out;
}

public List<Completer> defaultCompleter(String command) {
List<Completer> out = new ArrayList<>();
ArgumentCompleter ac = new ArgumentCompleter(NullCompleter.INSTANCE
, new StringsCompleter("--help", "-?")
Expand Down
2 changes: 1 addition & 1 deletion groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public enum Format {JSON, GROOVY, NONE};
private static final Pattern PATTERN_CLASS_DEF=Pattern.compile("^class\\s+(" + REGEX_VAR + ")\\ .*?\\{.*?\\}(|\n)$"
, Pattern.DOTALL);
private GroovyShell shell;
private Binding sharedData;
protected Binding sharedData;
private Map<String,String> imports = new HashMap<>();
private Map<String,String> methods = new HashMap<>();

Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<slf4j.version>1.7.21</slf4j.version>
<findbugs.version>3.0.2</findbugs.version>
<groovy.version>3.0.3</groovy.version>

<ivy.version>2.5.0</ivy.version>
<surefire.argLine />
</properties>

Expand Down Expand Up @@ -245,6 +245,12 @@
<version>${groovy.version}</version>
</dependency>

<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
<version>${ivy.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit 8be9242

Please sign in to comment.