Skip to content

Commit

Permalink
Google Java Format
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Aug 8, 2023
1 parent 3c00157 commit 264343b
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,152 +11,147 @@

@Component
@Command(
name = "jbang",
description = "Activate jbang i.e., java -jar edgechain.jar jbang Hello.java")
name = "jbang",
description = "Activate jbang i.e., java -jar edgechain.jar jbang Hello.java")
public class JbangCommand implements Runnable {

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

@Override
public void run() {

@Override
public void run() {
InputStream inputStream = this.getClass().getResourceAsStream("/jbang.jar");

InputStream inputStream = this.getClass().getResourceAsStream("/jbang.jar");
if (Objects.isNull(inputStream))
throw new RuntimeException("Unable to find jbang.jar in resource directory");

if (Objects.isNull(inputStream))
throw new RuntimeException("Unable to find jbang.jar in resource directory");
File jbangJar = new File(System.getProperty("java.io.tmpdir") + File.separator + "jbang.jar");

File jbangJar = new File(System.getProperty("java.io.tmpdir") + File.separator + "jbang.jar");

try {
FileUtils.copyInputStreamToFile(inputStream, jbangJar);
} catch (IOException e) {
throw new RuntimeException(e);
}

jbangJar.deleteOnExit();

// Clear Jbang Cache
clearCache(jbangJar);

// Build Jar using Jbang
buildJar(jbangJar, this.javaFile);

// Get Information
JbangResponse jbangResponse = info(jbangJar, this.javaFile);
try {
FileUtils.copyInputStreamToFile(inputStream, jbangJar);
} catch (IOException e) {
throw new RuntimeException(e);
}

String classPath;
if (jbangResponse.getResolvedDependencies().isEmpty()) {
classPath = jbangResponse
.getApplicationJar()
.concat(File.pathSeparator)
.concat(System.getProperty("jar.name"));
} else {
classPath = jbangResponse
.getApplicationJar()
.concat(File.pathSeparator)
.concat(String.join(File.pathSeparator, jbangResponse.getResolvedDependencies()))
.concat(File.pathSeparator)
.concat(System.getProperty("jar.name"));
}
jbangJar.deleteOnExit();

// Clear Jbang Cache
clearCache(jbangJar);

// Build Jar using Jbang
buildJar(jbangJar, this.javaFile);

// Get Information
JbangResponse jbangResponse = info(jbangJar, this.javaFile);

String classPath;
if (jbangResponse.getResolvedDependencies().isEmpty()) {
classPath =
jbangResponse
.getApplicationJar()
.concat(File.pathSeparator)
.concat(System.getProperty("jar.name"));
} else {
classPath =
jbangResponse
.getApplicationJar()
.concat(File.pathSeparator)
.concat(String.join(File.pathSeparator, jbangResponse.getResolvedDependencies()))
.concat(File.pathSeparator)
.concat(System.getProperty("jar.name"));
}

String mainClass;
if (Objects.isNull(jbangResponse.getMainClass())) {
mainClass = "com.edgechain." + jbangResponse.getOriginalResource();
} else {
mainClass = jbangResponse.getMainClass();
}
String mainClass;
if (Objects.isNull(jbangResponse.getMainClass())) {
mainClass = "com.edgechain." + jbangResponse.getOriginalResource();
} else {
mainClass = jbangResponse.getMainClass();
}

// System.out.println("Classpath ==========: "+classPath);
// System.out.println("Main Class =========: "+mainClass);
// System.out.println("Classpath ==========: "+classPath);
// System.out.println("Main Class =========: "+mainClass);

// Execute
execute(classPath, mainClass);
}
// Execute
execute(classPath, mainClass);
}

private void clearCache(File jbangJar) {
private void clearCache(File jbangJar) {

try {
ProcessBuilder pb =
new ProcessBuilder("java", "-jar", jbangJar.getAbsolutePath(), "cache", "clear")
.inheritIO();
try {
ProcessBuilder pb =
new ProcessBuilder("java", "-jar", jbangJar.getAbsolutePath(), "cache", "clear")
.inheritIO();

Process process = pb.start();
process.waitFor();
Process process = pb.start();
process.waitFor();

} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}

private void buildJar(File jbangJar, String javaFile) {

try {
ProcessBuilder pb =
new ProcessBuilder(
"java",
"-jar",
jbangJar.getAbsolutePath(),
"--cp",
System.getProperty("jar.name"),
javaFile).inheritIO().redirectErrorStream(true);
Process process = pb.start();
process.waitFor();
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
}

private void buildJar(File jbangJar, String javaFile) {

try {
ProcessBuilder pb =
new ProcessBuilder(
"java",
"-jar",
jbangJar.getAbsolutePath(),
"--cp",
System.getProperty("jar.name"),
javaFile)
.inheritIO()
.redirectErrorStream(true);
Process process = pb.start();
process.waitFor();
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
}

private JbangResponse info(File jbangJar, String javaFile) {

try {
ProcessBuilder pb =
new ProcessBuilder(
"java",
"-jar",
jbangJar.getAbsolutePath(),
"info",
"tools",
javaFile);

private JbangResponse info(File jbangJar, String javaFile) {

Process process = pb.start();
try {
ProcessBuilder pb =
new ProcessBuilder("java", "-jar", jbangJar.getAbsolutePath(), "info", "tools", javaFile);

BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
Process process = pb.start();

StringBuilder appender = new StringBuilder();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;
while ((line = bufferedReader.readLine()) != null) {
appender.append(line);
}
StringBuilder appender = new StringBuilder();

process.waitFor();
bufferedReader.close();
String line;
while ((line = bufferedReader.readLine()) != null) {
appender.append(line);
}

return JsonUtils.convertToObject(appender.toString(), JbangResponse.class);
process.waitFor();
bufferedReader.close();

return JsonUtils.convertToObject(appender.toString(), JbangResponse.class);

} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

private void execute(String classPath, String mainClass) {
private void execute(String classPath, String mainClass) {

try {
try {

ProcessBuilder pb =
new ProcessBuilder("java", "-classpath", classPath, mainClass).inheritIO();
ProcessBuilder pb =
new ProcessBuilder("java", "-classpath", classPath, mainClass).inheritIO();

Process process = pb.start();
Process process = pb.start();

process.waitFor();
process.waitFor();

} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.edgechain.lib.flyfly.commands.jbang;

public class JbangFile {
private String originalResource;
private String backingResource;
public String target;

}
private String originalResource;
private String backingResource;
public String target;
}
Loading

0 comments on commit 264343b

Please sign in to comment.