feat: jbang line magic by maxandersen · Pull Request #76 · dflib/jjava

This is a PR to discuss how/if support for jbang styled code would be of your interest in jjava.

This PR adds two jbang related magics.

%jbang <scriptRef>

Line magic that takes an argument and will build and add its resulting jar + classpath.

The argument can be any meaningfull jbang reference, i.e. local or remote (https://..) file (.java,.kt,.groovy), maven artifact (g:a:v:), gist or git repo url etc.

Examples:

%jbang myfile.java
%jbang https://acme.org/my.jar
%jbang a.b:artifact:1.0

One caveat due to jshell - scripts must define a package as the default/empty package are not accessible to jshell (annoying :)

But otherwise it lets you use almost every feature of jbang.

In future could add support for command line arguments beyond just scriptRef (ie. --java to use different java versions for building)

%%jbang cell magic

Basically same setup as %%loadfrompom, where we take the content of the cell and pass to jbang and the resulting classpath added to the running shell.

Example:

%%jbang
//DEPS org.dflib:dflib-jupyter:1.0.0
...

Ultimately I would have preferred I could also eval the code via the kernel and show the result but afaics magics cannot affect the display. There is DisplayMagics but does not seem to be enabled afaics.

Proper JBang integration

This pr made me realize that a more sensible form of jbang integration would be to let jbang hook into eval and thus not require additional magic lines. I can open PR for that too but wanted to start with more "traditional" integration.

Also, this PR currently require jbang on the PATH - a proper one would go look for it and possibly even fetch it if not installed (jbang-maven and jbang-gradle does similar)

Question

  1. would you be interested in these jbang based magics?

  2. would you be interested in a PR that would make it so if a cell has jbang directives (so only activated if such are present - without requiring use of %magics. ?