Skip to content

Commit

Permalink
feat: export an additional env var pointing to the location of jbang (#…
Browse files Browse the repository at this point in the history
…1811)

* feat: export an env var with the location of jbang

It's *really* hard (impossible?) to find the location of the script that
launched the JVM from within jbang. It would be really useful to be able
to do that so you can run jbang again with some logic to precompute the
command line. This works really simply:

```
$ jbang --interactive
WARNING: Using incubator modules: jdk.incubator.vector, jdk.incubator.foreign
|  Welcome to JShell -- Version 17.0.7
|  For an introduction type: /help intro

jshell> System.getenv("JBANG")
$1 ==> "/home/dsyer/.sdkman/candidates/jbang/current/bin/jbang"
```

* Add JBANG_LAUNCH_CMD + tests for other env variables

* fix: check out instead of err for env results

---------

Co-authored-by: Max Rydahl Andersen <[email protected]>
  • Loading branch information
dsyer and maxandersen authored Jul 6, 2024
1 parent d863c33 commit a2ef5a7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions itests/shellenv.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: env variables

Scenario: JBANG_RUNTIME_SHELL available
When command('jbang env@jbangdev JBANG')
Then match out contains "JBANG_RUNTIME_SHELL"

Scenario: JBANG_STDIN_NOTTY available
When command('jbang env@jbangdev JBANG')
Then match out contains "JBANG_STDIN_NOTTY"

Scenario: JBANG_LAUNCH_CMD available
When command('jbang env@jbangdev JBANG')
Then match out contains "JBANG_LAUNCH_CMD"
1 change: 1 addition & 0 deletions src/main/scripts/jbang
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ fi
## run it using command substitution to have just the user process once jbang is done
export JBANG_RUNTIME_SHELL=bash
export JBANG_STDIN_NOTTY=$([ -t 0 ] && echo "false" || echo "true")
export JBANG_LAUNCH_CMD=$0
output=$(CLICOLOR_FORCE=1 "${JAVA_EXEC}" ${JBANG_JAVA_OPTIONS} -classpath "${jarPath}" dev.jbang.Main "$@")
err=$?
if [ $err -eq 255 ]; then
Expand Down
1 change: 1 addition & 0 deletions src/main/scripts/jbang.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ if not exist "%TDIR%" ( mkdir "%TDIR%" )
set tmpfile=%TDIR%\%RANDOM%.jbang.tmp
rem execute jbang and pipe to temporary random file
set JBANG_RUNTIME_SHELL=cmd
set JBANG_LAUNCH_CMD=%~nx0
2>nul >nul timeout /t 0 && (set JBANG_STDIN_NOTTY=false) || (set JBANG_STDIN_NOTTY=true)
set "CMD=!JAVA_EXEC!"
SETLOCAL DISABLEDELAYEDEXPANSION
Expand Down
1 change: 1 addition & 0 deletions src/main/scripts/jbang.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ if ($JAVA_EXEC -eq "") {

$env:JBANG_RUNTIME_SHELL="powershell"
$env:JBANG_STDIN_NOTTY=$MyInvocation.ExpectingInput
$env:JBANG_LAUNCH_CMD = $PSCommandPath
$output = & "$JAVA_EXEC" $env:JBANG_JAVA_OPTIONS -classpath "$jarPath" dev.jbang.Main @args
$err=$LASTEXITCODE

Expand Down

0 comments on commit a2ef5a7

Please sign in to comment.