Skip to content

Commit

Permalink
Fixing Jbang CommandLineRunner (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmadHanif01 authored Aug 5, 2023
1 parent a2e0a15 commit 22cf749
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package com.edgechain;

import com.edgechain.lib.flyfly.commands.jbang.JbangCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
import picocli.CommandLine;

import java.net.URL;
import java.nio.file.Paths;

@SpringBootApplication
@EnableScheduling
public class EdgeChainApplication {
public class EdgeChainApplication {

private static final Logger logger = LoggerFactory.getLogger(EdgeChainApplication.class);

public static void main(String[] args) {

logger.info("Please avoid special symbols such as hypen(-) in naming the directory.");

System.setProperty("jar.name", getJarFileName(EdgeChainApplication.class));
logger.info("Executed jar file: " + System.getProperty("jar.name"));

Expand Down Expand Up @@ -56,4 +59,5 @@ private static String getJarFileName(Class<?> clazz) {
}
throw new RuntimeException("Invalid jar file");
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package com.edgechain.lib.flyfly;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import picocli.CommandLine;
import picocli.CommandLine.IFactory;

import java.util.Map;

@Component
public class CustomApplicationRunner implements CommandLineRunner, ExitCodeGenerator {

@Autowired private FlyflyCommand runCommand;
@Autowired private IFactory factory; // auto-configured to inject PicocliSpringFactory
@Autowired private CommandLine.IFactory factory; // auto-configured to inject PicocliSpringFactory

@Autowired private ApplicationContext context;

private int exitCode;

@Override
public void run(String... args) throws Exception {
exitCode = new CommandLine(runCommand, factory).execute(args);
if (getBootLoaderClass().contains("com.edgechain.EdgeChainApplication")) {
exitCode = new CommandLine(runCommand, factory).execute(args);
}
}

public String getBootLoaderClass() {
Map<String, Object> annotatedBeans =
context.getBeansWithAnnotation(SpringBootApplication.class);
return annotatedBeans.isEmpty()
? null
: annotatedBeans.values().toArray()[0].getClass().getName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
@Command(
name = "edgechain",
subcommands = {
RunCommand.class,
FormatCommand.class,
// RunCommand.class,
// FormatCommand.class,
JbangCommand.class,
CommandLine.HelpCommand.class
// CommandLine.HelpCommand.class
})
public class FlyflyCommand {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

@Component
@Command(
name = "jbang",
description =
"Activate jbang through the jar placed in resources. Ignore if your application is"
+ " executed.")
name = "jbang",
description =
"Activate jbang through the jar placed in resources.")
public class JbangCommand implements Runnable {

@Parameters(description = "Java file to be executed with jbang")
@Parameters(description = "Java file to be executed with jbang;")
private String javaFile;

// @Parameters(description = "ClassPath Jar to be used")
Expand Down Expand Up @@ -54,14 +53,14 @@ private void runJbang(File jarFile, String javaFile, String classPathJar) {
try {
// Step One: Execute the initial command to get the classpath
ProcessBuilder pb =
new ProcessBuilder(
"java",
"-cp",
jarFile.getAbsolutePath(),
"dev.jbang.Main",
"--cp",
classPathJar,
javaFile);
new ProcessBuilder(
"java",
"-cp",
jarFile.getAbsolutePath(),
"dev.jbang.Main",
"--cp",
classPathJar,
javaFile);
pb.redirectErrorStream(true);
Process process = pb.start();

Expand Down Expand Up @@ -146,12 +145,11 @@ private void runJavaWithClassPath(String classPath, String mainClass) {
try {
ProcessBuilder pb = new ProcessBuilder("java", "-classpath", classPath, mainClass);
pb.inheritIO();
pb.start();

System.exit(0);
Process process = pb.start();
process.waitFor();

} catch (IOException e) {
e.printStackTrace();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Command(
name = "run",
description =
"Run a JAR or Gradle Spring Boot Application. Ignore if your application is executed.")
"Run a JAR or Gradle Spring Boot Application. Ignore if your application is executed.", hidden = true)
public class RunCommand implements Runnable {

private final Logger log = LoggerFactory.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public static Retrofit getInstance() {
.message(errorMessage)
.build();
})
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.connectTimeout(15, TimeUnit.MINUTES)
.readTimeout(20, TimeUnit.MINUTES)
.build())
.build();
}
Expand Down

0 comments on commit 22cf749

Please sign in to comment.