Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Java API for running JBang #1814

Open
dsyer opened this issue Jun 26, 2024 · 3 comments
Open

feat: Java API for running JBang #1814

dsyer opened this issue Jun 26, 2024 · 3 comments
Labels
ideas Some idea/suggestion around jbang behavior/feature set

Comments

@dsyer
Copy link
Contributor

dsyer commented Jun 26, 2024

I would like to be able to use JBang to run another JBang command (so I can do some processing of user input and delegate the result). It's quite hard right now as you have to try and scrape the generated command line from stderr, unless I'm missing something. E.g. this works for me:

public class runner {

	private static final String RUN_PREFIX = "] run: ";

	public static void main(String[] args) throws IOException {
		CommandLine cli = JBang.getCommandLine();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		var err = System.err;
		System.setErr(new PrintStream(out));
		boolean verbose = Util.isVerbose();
		Util.setVerbose(true);
		int result = cli.execute(args);
		System.setErr(err);
		Util.setVerbose(verbose);
		String cmd = out.toString().trim();
		if (result == 255 && cmd.length() > 0 && cmd.indexOf(RUN_PREFIX) > 0) {
			cmd = cmd.substring(cmd.lastIndexOf(RUN_PREFIX) + 7);
			// System.out.println("Run: " + cmd);
			Process process = new ProcessBuilder(cmd.toString().split("\\s+")).inheritIO().start();
			try {
				process.waitFor();
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		}
	}

}

but it's kind of scary, and you can't avoid the command also being printed to stdout.

@dsyer dsyer added the ideas Some idea/suggestion around jbang behavior/feature set label Jun 26, 2024
@nnsns
Copy link

nnsns commented Jun 27, 2024

Also it will be nice to have an option to pass to another JBang command an object from main JBang/Java app.

For example, the main application can prepare an object for further processing and logger and pass these 2 objects to the spawned JBang command.

Ideally, it would be great to have full communication between the two processes, including callback capability.

@dsyer
Copy link
Contributor Author

dsyer commented Jun 28, 2024

If #1811 is merged we could implement a JBang launcher API by referring to System.getenv("JBANG_LAUNCH_CMD"). That might be cleaner. Would be good to have access to the original command line, or the options at least, but I'm not sure it would allow communication between the two processes.

@maxandersen
Copy link
Collaborator

There can't be two way communication - jbang exits after printing the command to execute.

Having an API to find and get access to jbang and it's features makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ideas Some idea/suggestion around jbang behavior/feature set
Projects
None yet
Development

No branches or pull requests

3 participants