Skip to content

Commit

Permalink
Merge pull request #146 from quintesse/alias_args_and_props
Browse files Browse the repository at this point in the history
  • Loading branch information
maxandersen authored Jun 26, 2020
2 parents 0089bcb + 081582a commit e8f68ae
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 61 deletions.
26 changes: 23 additions & 3 deletions src/main/java/dk/xam/jbang/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,34 @@ public class Script {
// true if in this run a jar was build/created
private boolean createdJar;

public Script(File backingFile, String content) throws FileNotFoundException {
private List<String> arguments;

private Map<String, String> properties;

public Script(File backingFile, String content, List<String> arguments, Map<String, String> properties)
throws FileNotFoundException {
this.backingFile = backingFile;
this.script = content;
this.arguments = arguments;
this.properties = properties;
}

public Script(File backingFile) throws FileNotFoundException {
public Script(File backingFile, List<String> arguments, Map<String, String> properties)
throws FileNotFoundException {
this.backingFile = backingFile;
this.arguments = arguments;
this.properties = properties;
try (Scanner sc = new Scanner(this.backingFile)) {
sc.useDelimiter("\\Z");
this.script = sc.next();
}
}

public Script(String script) {
public Script(String script, List<String> arguments, Map<String, String> properties) {
this.backingFile = null;
this.script = script;
this.arguments = arguments;
this.properties = properties;
}

private List<String> getLines() {
Expand Down Expand Up @@ -306,4 +318,12 @@ public void createJarFile(File path, File output) throws IOException {
public boolean wasJarCreated() {
return createdJar;
}

public List<String> getArguments() {
return (arguments != null) ? arguments : Collections.emptyList();
}

public Map<String, String> getProperties() {
return (properties != null) ? properties : Collections.emptyMap();
}
}
38 changes: 30 additions & 8 deletions src/main/java/dk/xam/jbang/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -152,10 +150,14 @@ public static TemplateEngine getTemplateEngine() {
public static class Alias {
public final String scriptRef;
public final String description;
public final List<String> arguments;
public final Map<String, String> properties;

Alias(String scriptRef, String description) {
Alias(String scriptRef, String description, List<String> arguments, Map<String, String> properties) {
this.scriptRef = scriptRef;
this.description = description;
this.arguments = arguments;
this.properties = properties;
}
}

Expand Down Expand Up @@ -188,11 +190,31 @@ public static Map<String, Alias> getAliases() {
return getAliasInfo().aliases;
}

public static void addAlias(String name, String scriptRef, String description) {
if (getAliases().containsKey(scriptRef)) {
throw new ExitException(1, "Can't create alias to another alias.");
public static Alias getAlias(String ref, List<String> arguments, Map<String, String> properties) {
HashSet<String> names = new HashSet<>();
Alias alias = new Alias(null, null, arguments, properties);
return mergeAliases(alias, ref, names);
}

private static Alias mergeAliases(Alias a1, String ref2, HashSet<String> names) {
if (names.contains(ref2)) {
throw new RuntimeException("Encountered alias loop on '" + ref2 + "'");
}
Alias a2 = getAliases().get(ref2);
if (a2 != null) {
names.add(ref2);
a2 = mergeAliases(a2, a2.scriptRef, names);
List<String> args = a1.arguments != null ? a1.arguments : a2.arguments;
Map<String, String> props = a1.properties != null ? a1.properties : a2.properties;
return new Alias(a2.scriptRef, null, args, props);
} else {
return a1;
}
getAliases().put(name, new Alias(scriptRef, description));
}

public static void addAlias(String name, String scriptRef, String description, List<String> arguments,
Map<String, String> properties) {
getAliases().put(name, new Alias(scriptRef, description, arguments, properties));

try (Writer out = Files.newBufferedWriter(getAliasesFile())) {
Gson parser = new GsonBuilder().setPrettyPrinting().create();
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/dk/xam/jbang/cli/Alias.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dk.xam.jbang.cli;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import dk.xam.jbang.Settings;
import dk.xam.jbang.Util;
Expand All @@ -17,9 +19,12 @@ public class Alias {
public Integer add(
@CommandLine.Option(names = { "--description",
"-d" }, description = "A description for the alias") String description,
@CommandLine.Option(names = { "-D" }, description = "set a system property") Map<String, String> properties,
@CommandLine.Parameters(index = "0", description = "A name for the alias", arity = "1") String name,
@CommandLine.Parameters(index = "1", description = "A file or URL to a Java code file", arity = "1") String scriptOrFile) {
Settings.addAlias(name, scriptOrFile, description);
@CommandLine.Parameters(index = "1", description = "A file or URL to a Java code file", arity = "1") String scriptOrFile,
@CommandLine.Parameters(index = "2..*", arity = "0..*", description = "Parameters to pass on to the script") List<String> userParams) {

Settings.addAlias(name, scriptOrFile, description, userParams, properties);
return CommandLine.ExitCode.SOFTWARE;
}

Expand All @@ -38,6 +43,12 @@ public Integer list() {
} else {
out.println(a + " = " + ai.scriptRef);
}
if (ai.arguments != null) {
out.println(Util.repeat(" ", a.length()) + " Arguments: " + String.join(" ", ai.arguments));
}
if (ai.properties != null) {
out.println(Util.repeat(" ", a.length()) + " Properties: " + ai.properties);
}
});
return CommandLine.ExitCode.SOFTWARE;
}
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/dk/xam/jbang/cli/BaseScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import dk.xam.jbang.ExitException;
Expand All @@ -34,12 +35,16 @@ public abstract class BaseScriptCommand extends BaseCommand {

protected Script script;

public static Script prepareScript(String scriptResource) throws IOException {
public static Script prepareScript(String scriptResource, List<String> arguments, Map<String, String> properties)
throws IOException {
File scriptFile = getScriptFile(scriptResource);
if (scriptFile == null) {
// Not found as such, so let's check the aliases
if (Settings.getAliases().containsKey(scriptResource)) {
scriptFile = getScriptFile(Settings.getAliases().get(scriptResource).scriptRef);
Settings.Alias alias = Settings.getAlias(scriptResource, arguments, properties);
if (alias != null) {
scriptFile = getScriptFile(alias.scriptRef);
arguments = alias.arguments;
properties = alias.properties;
}
}

Expand Down Expand Up @@ -68,7 +73,7 @@ public static Script prepareScript(String scriptResource) throws IOException {

Script s = null;
try {
s = new Script(scriptFile);
s = new Script(scriptFile, arguments, properties);
s.setOriginal(new File(scriptResource));
} catch (FileNotFoundException e) {
throw new ExitException(1, e);
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/dk/xam/jbang/cli/Edit.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class Edit extends BaseScriptCommand {

@Override
public Integer doCall() throws IOException {
script = prepareScript(scriptOrFile);
File project = createProjectForEdit(script, userParams, false);
script = prepareScript(scriptOrFile, userParams, null);
File project = createProjectForEdit(script, false);
// err.println(project.getAbsolutePath());
if (liveeditor == null) {
out.println("echo " + project.getAbsolutePath()); // quit(project.getAbsolutePath());
Expand Down Expand Up @@ -66,8 +66,8 @@ public Integer doCall() throws IOException {
try {
// TODO only regenerate when dependencies changes.
info("Regenerating project.");
script = prepareScript(scriptOrFile);
createProjectForEdit(script, userParams, true);
script = prepareScript(scriptOrFile, userParams, null);
createProjectForEdit(script, true);
} catch (RuntimeException ee) {
warn("Error when re-generating project. Ignoring it, but state might be undefined: "
+ ee.getMessage());
Expand All @@ -88,7 +88,7 @@ public Integer doCall() throws IOException {
}

/** Create Project to use for editing **/
File createProjectForEdit(Script script, List<String> userParams, boolean reload) throws IOException {
File createProjectForEdit(Script script, boolean reload) throws IOException {

List<String> collectDependencies = script.collectDependencies();
String cp = script.resolveClassPath(offline);
Expand Down Expand Up @@ -120,45 +120,47 @@ File createProjectForEdit(Script script, List<String> userParams, boolean reload
Path destination = new File(tmpProjectDir, "build.gradle").toPath();
TemplateEngine engine = Settings.getTemplateEngine();

renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, userParams,
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, script.getArguments(),
destination);

// setup eclipse
templateName = ".qute.classpath";
destination = new File(tmpProjectDir, ".classpath").toPath();
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, userParams,
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, script.getArguments(),
destination);

templateName = ".qute.project";
destination = new File(tmpProjectDir, ".project").toPath();
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, userParams,
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, script.getArguments(),
destination);

templateName = "main.qute.launch";
destination = new File(tmpProjectDir, ".eclipse/" + baseName + ".launch").toPath();
destination.toFile().getParentFile().mkdirs();
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, userParams,
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, script.getArguments(),
destination);

templateName = "main-port-4004.qute.launch";
destination = new File(tmpProjectDir, ".eclipse/" + baseName + "-port-4004.launch").toPath();
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, userParams,
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, script.getArguments(),
destination);

// setup vscode
templateName = "launch.qute.json";
destination = new File(tmpProjectDir, ".vscode/launch.json").toPath();
if (isNeeded(reload, destination)) {
destination.toFile().getParentFile().mkdirs();
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, userParams,
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName,
script.getArguments(),
destination);
}

templateName = "settings.qute.json";
destination = new File(tmpProjectDir, ".vscode/settings.json").toPath();
if (isNeeded(reload, destination)) {
destination.toFile().getParentFile().mkdirs();
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName, userParams,
renderTemplate(engine, collectDependencies, baseName, resolvedDependencies, templateName,
script.getArguments(),
destination);
}

Expand All @@ -168,13 +170,13 @@ File createProjectForEdit(Script script, List<String> userParams, boolean reload
* File(tmpProjectDir, ".idea/runConfigurations/" + baseName +
* "-port-4004.xml").toPath(); destination.toFile().getParentFile().mkdirs();
* renderTemplate(engine, collectDependencies, baseName, resolvedDependencies,
* templateName, userParams, destination);
* templateName, script.getArguments(), destination);
*
* templateName = "idea.qute.xml"; destination = new File(tmpProjectDir,
* ".idea/runConfigurations/" + baseName + ".xml").toPath();
* destination.toFile().getParentFile().mkdirs(); renderTemplate(engine,
* collectDependencies, baseName, resolvedDependencies, templateName,
* userParams, destination);
* script.getArguments(), destination);
*/

return tmpProjectDir;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dk/xam/jbang/cli/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Integer doCall() throws IOException {
enableInsecure();
}

script = prepareScript(scriptOrFile);
script = prepareScript(scriptOrFile, userParams, properties);

if (script.needsJar()) {
build(script);
Expand Down Expand Up @@ -250,7 +250,7 @@ String generateCommandLine(Script script) throws IOException {
optionalArgs.add("--startup=DEFAULT");

File tempFile = File.createTempFile("jbang_arguments_", script.backingFile.getName());
Util.writeString(tempFile.toPath(), generateArgs(userParams, properties));
Util.writeString(tempFile.toPath(), generateArgs(script.getArguments(), script.getProperties()));

optionalArgs.add("--startup=" + tempFile.getAbsolutePath());

Expand Down Expand Up @@ -301,7 +301,7 @@ String generateCommandLine(Script script) throws IOException {
}

if (!script.forJShell()) {
fullArgs.addAll(userParams);
fullArgs.addAll(script.getArguments());
} else if (!interactive) {
File tempFile = File.createTempFile("jbang_exit_", script.backingFile.getName());
Util.writeString(tempFile.toPath(), "/exit");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/dk/xam/jbang/TestGrape.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void testFindGrabs() throws FileNotFoundException {
+
"})\n";

Script s = new Script(grabBlock);
Script s = new Script(grabBlock, null, null);

List<String> deps = s.collectDependencies();

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/dk/xam/jbang/TestScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TestScript {

@Test
void testFindDependencies() {
Script script = new Script(example);
Script script = new Script(example, null, null);

List<String> dependencies = script.collectDependencies();
assertEquals(2, dependencies.size());
Expand All @@ -58,8 +58,8 @@ void testFindDependencies() {

@Test
void testCDS() {
Script script = new Script("//CDS\nclass m { }");
Script script2 = new Script("class m { }");
Script script = new Script("//CDS\nclass m { }", null, null);
Script script2 = new Script("class m { }", null, null);

assertTrue(script.enableCDS());
assertFalse(script2.enableCDS());
Expand Down Expand Up @@ -105,7 +105,7 @@ void textExtractRepositoriesGrape() {

@Test
void testExtractOptions() {
Script s = new Script(example);
Script s = new Script(example, null, null);

assertEquals(s.collectCompileOptions(), Arrays.asList("--enable-preview", "--verbose"));

Expand All @@ -118,7 +118,7 @@ void testNonJavaExtension(@TempDir Path output) throws IOException {
Path p = output.resolve("kube-example");
writeString(p, example);

Script s = BaseScriptCommand.prepareScript(p.toAbsolutePath().toString());
Script s = BaseScriptCommand.prepareScript(p.toAbsolutePath().toString(), null, null);

}

Expand Down
Loading

0 comments on commit e8f68ae

Please sign in to comment.