Skip to content

Commit

Permalink
tip to write jshell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Aug 19, 2022
1 parent 7bb7146 commit e20b537
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import io.yupiik.kubernetes.bindings.v1_24_3.v1.*;

class Generator {
public void generate() {
final var output = Files.createDirectories(Path.of("target/k8s-descriptors"));

final var deployment = new Deployment()
.spec(new DeploymentSpec()
/* ... */);

Files.writeString(
output.resolve("my-deployment.json"),
deployment.asJson());
}
}

class Runner { // enables to code in an IDE even if not needed by JShell
public static void main(final String... args) throws Exception {
new Generator().generate();
}
}
8 changes: 8 additions & 0 deletions documentation/src/main/minisite/content/ci-setup.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ include::{partialsdir}/ci.jshell.java[]
<.> A small trick to write a readable script is to enable multiline snippet by wrapping the code in a block (rest of the code is similar to previous ones),
<.> Finally when finished we exit JShell.

TIP: a way to write the script as a standard Java code with classes and a final standard main class (`Runner`). This will enable you to setup the dependencies in your IDE and run the main as a standard class (ensure to name your file `<scriptname>.java`).
Then to execute it through JShell - where `/exit` is needed - use this command: `echo 'Runner.main();/exit' | jshell --class-path .... --startup <scriptname>.java`.
This will automatically load your script and exeute the main before exiting the shell.
[source,java]
----
include::{partialsdir}/ci.jshell.main.java[]
----

== JBang Setup

link:https://www.jbang.dev/[JBang] can also be used to generate your descriptors.
Expand Down

0 comments on commit e20b537

Please sign in to comment.