Skip to content

Commit

Permalink
feat: --jsh to allow forcing .jsh (quarkusio#658)
Browse files Browse the repository at this point in the history
Main usecase is that tools like bach uses suffix less files.

for example to allow building https://github.com/jbee/purejin
using `jbang --jsh --java 16 https://bit.ly/bach-main-build`
  • Loading branch information
maxandersen committed Jan 9, 2021
1 parent 9d8b614 commit bbab86e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions itests/hellojsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// example of jsh without suffix.
// use jbang --jsh to force it.
System.out.println(args[0]);
6 changes: 5 additions & 1 deletion itests/jsh.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ Scenario: jsh quoted system property
When command('jbang -Dvalue="a quoted" hello.jsh')
Then match out == "a quoted\n"


Scenario: jsh fail on --native
When command('jbang --native hello.jsh')
Then match err contains ".jsh cannot be used with --native"

Scenario: force jsh
When command('jbang --jsh hellojsh hello')
Then match err == ""
Then match out == "hello\n"
11 changes: 10 additions & 1 deletion src/main/java/dev/jbang/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public class Script {
**/
private String javaAgentOption;

/**
* if true, interpret any input as for jshell
*/
private boolean forcejsh = false;

public Script(ScriptResource resource, List<String> arguments, Map<String, String> properties)
throws FileNotFoundException {
this(resource, getBackingFileContent(resource.getFile()), arguments, properties);
Expand Down Expand Up @@ -437,7 +442,7 @@ public boolean needsJar() {
}

public boolean forJShell() {
return getBackingFile().getName().endsWith(".jsh");
return forcejsh || getBackingFile().getName().endsWith(".jsh");
}

public void setOriginal(String ref) {
Expand Down Expand Up @@ -658,4 +663,8 @@ public List<KeyValue> getAgentOptions() {
return agentOptions;
}

public void setForcejsh(boolean forcejsh) {
this.forcejsh = forcejsh;
}

}
3 changes: 2 additions & 1 deletion src/main/java/dev/jbang/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ public static Path downloadFileSwizzled(String fileURL, File saveDir) throws IOE
// to handle if kubectl-style name (i.e. extension less)
File f = path.toFile();
String nonkebabname = f.getName();
if (!f.getName().endsWith(".jar")) { // avoid directly downloaded jar files getting renamed to .java
if (!f.getName().endsWith(".jar") && !f.getName().endsWith(".jsh")) { // avoid directly downloaded jar files
// getting renamed to .java
nonkebabname = unkebabify(f.getName());
}
if (nonkebabname.equals(f.getName())) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/dev/jbang/cli/BaseScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public abstract class BaseScriptCommand extends BaseCommand {
"--insecure" }, description = "Enable insecure trust of all SSL certificates.", defaultValue = "false")
boolean insecure;

@CommandLine.Option(names = { "--jsh" }, description = "Force input to be interpreted with jsh/jshell")
boolean forcejsh = false;

@CommandLine.Parameters(index = "0", arity = "1", description = "A file with java code or if named .jsh will be run with jshell")
String scriptOrFile;

Expand Down Expand Up @@ -106,11 +109,11 @@ public static Script prepareScript(String scriptResource, List<String> arguments

public static Script prepareScript(String scriptResource, List<String> arguments, Map<String, String> properties,
List<String> dependencies, List<String> classpaths) throws IOException {
return prepareScript(scriptResource, arguments, properties, dependencies, classpaths, false);
return prepareScript(scriptResource, arguments, properties, dependencies, classpaths, false, false);
}

public static Script prepareScript(String scriptResource, List<String> arguments, Map<String, String> properties,
List<String> dependencies, List<String> classpaths, boolean fresh)
List<String> dependencies, List<String> classpaths, boolean fresh, boolean forcejsh)
throws IOException {
ScriptResource scriptFile = getScriptFile(scriptResource);

Expand Down Expand Up @@ -156,6 +159,7 @@ public static Script prepareScript(String scriptResource, List<String> arguments
Script s = null;
try {
s = new Script(scriptFile, arguments, properties);
s.setForcejsh(forcejsh);
s.setOriginal(scriptResource);
s.setAlias(alias);
s.setAdditionalDependencies(dependencies);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/dev/jbang/cli/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public Integer doCall() throws IOException {
enableInsecure();
}

script = prepareArtifacts(prepareScript(scriptOrFile, userParams, properties, dependencies, classpaths, fresh));
script = prepareArtifacts(
prepareScript(scriptOrFile, userParams, properties, dependencies, classpaths, fresh, forcejsh));

String cmdline = generateCommandLine(script);
debug("run: " + cmdline);
Expand Down Expand Up @@ -125,7 +126,7 @@ String generateCommandLine(Script script) throws IOException {

String requestedJavaVersion = javaVersion != null ? javaVersion : script.javaVersion();
String javacmd = resolveInJavaHome("java", requestedJavaVersion);
if (script.getBackingFile().getName().endsWith(".jsh")) {
if (script.forJShell()) {

javacmd = resolveInJavaHome("jshell", requestedJavaVersion);
if (!classpath.trim().isEmpty()) {
Expand Down

0 comments on commit bbab86e

Please sign in to comment.