Skip to content

Commit

Permalink
[patch] add --init
Browse files Browse the repository at this point in the history
  • Loading branch information
maxandersen committed Dec 29, 2019
1 parent 8557ce5 commit 0e7e02f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
implementation 'com.offbytwo:docopt:0.6.0.20150202'

implementation 'info.picocli:picocli:4.1.4'
//implementation 'io.quarkus.qute:qute-core:1.1.0.Final'

implementation("com.jcabi:jcabi-aether:0.10.1") {
exclude group: "org.hibernate", module: "hibernate-validator"
Expand Down
4 changes: 4 additions & 0 deletions readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ chmod +x helloworld.java
There are experimental support to run `.jsh` via `jshell`. The advantage of `jshell` is that you do not need to have a class or static main method, downside is that
at least for now you have no way to read the command line arguments.

## Getting started

To get started you can run `jbang --init helloworld.java` and a simple java class with a static main is generated.

## Declare dependencies with `//DEPS`

If you want to write real scripts you will want to use some java libraries.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/dk/xam/jbang/ExitException.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public ExitException(int status) {
this.status = status;
}

public ExitException(int status, Exception cause) {
super(cause);
this.status = status;
}

public int getStatus() {
return status;
}
Expand Down
47 changes: 41 additions & 6 deletions src/main/java/dk/xam/jbang/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import static java.lang.System.*;
import static picocli.CommandLine.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -34,6 +33,9 @@ public class Main implements Callable<Integer> {
@Parameters(index = "1..*", arity = "0..*", description = "Parameters to pass on to the script")
List<String> params = new ArrayList<String>();

@Option(names = {"--init"}, description = "Init script with a java class useful for scripting.")
boolean initScript;

void info(String msg) {
spec.commandLine().getErr().println(msg);
}
Expand Down Expand Up @@ -65,7 +67,7 @@ static CommandLine getCommandLine() {
}

@Override
public Integer call() throws FileNotFoundException {
public Integer call() throws IOException {

if (helpRequested) {
spec.commandLine().usage(err);
Expand All @@ -75,13 +77,46 @@ public Integer call() throws FileNotFoundException {
quit(0);
}

String cmdline = generateCommandLine();
if (initScript) {
var f = new File(scriptOrFile);
if (f.exists()) {
warn("File " + f + " already exists. Will not initialize.");
} else {
// Use try-with-resource to get auto-closeable writer instance
try (BufferedWriter writer = Files.newBufferedWriter(f.toPath())) {
String result = renderInitClass(f);
writer.write(result);
}
}

out.println(cmdline);
} else {
String cmdline = generateCommandLine();

out.println(cmdline);
}
return 0;
}

static String initTemplate = "//usr/bin/env jbang \"$0\" \"$@\" ; exit $?\n"
+ "// //DEPS <dependency1> <dependency2>\n" + "\n" + "import static java.lang.System.*;\n" + "\n"
+ "public class {className} {\n" + "\n" + " public static void main(String... args) {\n"
+ " out.println(\"Hello World\");\n" + " }\n" + "}";

String renderInitClass(File f) {

return initTemplate.replace("{className}", getBaseName(f.getName()));

}

public static String getBaseName(String fileName) {
int index = fileName.lastIndexOf('.');
if (index == -1) {
return fileName;
} else {
return fileName.substring(0, index);
}
}

String generateCommandLine() throws FileNotFoundException {
prepareScript = prepareScript(scriptOrFile);

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/dk/xam/jbang/TestInit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dk.xam.jbang;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import java.io.File;

import org.junit.jupiter.api.Test;

public class TestInit {

@Test
void testInit() {

var m = new Main();

String s = m.renderInitClass(new File("test.java"));

assertThat(s, not(containsString("NOT_FOUND")));

}
}
2 changes: 1 addition & 1 deletion src/test/java/dk/xam/jbang/TestMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ void testDependencies() throws FileNotFoundException {
assertThat(result, not(containsString(" ")));
assertThat(result, containsString("classpath"));
assertThat(result, containsString("log4j"));
}
}

}

0 comments on commit 0e7e02f

Please sign in to comment.