Skip to content

Commit

Permalink
Merge pull request #105 from web3j/web3jCliFix
Browse files Browse the repository at this point in the history
Removed JCDP and updated with Jansi
  • Loading branch information
NickSneo authored Mar 28, 2024
2 parents 819e1e9 + 6ffa700 commit d54dc27
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 130 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ext {
log4jVersion = '2.15.0'
semverVersion = '0.9.0'
commonsLangVersion = '3.9'
jcdpVersion = '4.0.1'
jansiVersion = '2.4.1'
}


Expand Down Expand Up @@ -103,7 +103,8 @@ dependencies {
"org.apache.logging.log4j:log4j-core:$log4jVersion",
"io.github.microutils:kotlin-logging:$kotlinLoggin",
"com.github.docker-java:docker-java:$dockerJavaVersion",
"com.diogonunes:JCDP:$jcdpVersion"
"org.fusesource.jansi:jansi:$jansiVersion"


runtimeOnly "org.slf4j:slf4j-nop:$slf4jVersion"

Expand Down
48 changes: 15 additions & 33 deletions src/main/java/org/web3j/console/project/ProjectRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

import java.io.IOException;

import com.diogonunes.jcdp.color.ColoredPrinter;
import com.diogonunes.jcdp.color.api.Ansi;
import org.fusesource.jansi.Ansi;

import org.web3j.console.project.java.JavaProject;
import org.web3j.console.project.java.JavaProjectRunner;
Expand All @@ -41,44 +40,27 @@ public ProjectRunner(final ProjectCreatorConfig projectCreatorConfig) {
}

public static void onSuccess(Project project) {
System.out.print(System.lineSeparator());
ColoredPrinter cp =
new ColoredPrinter.Builder(0, false)
.foreground(Ansi.FColor.WHITE)
.background(Ansi.BColor.GREEN)
.attribute(Ansi.Attribute.BOLD)
.build();
ColoredPrinter instructionPrinter =
new ColoredPrinter.Builder(0, false).foreground(Ansi.FColor.CYAN).build();
ColoredPrinter commandPrinter =
new ColoredPrinter.Builder(0, false).foreground(Ansi.FColor.GREEN).build();
System.out.print(System.lineSeparator());
cp.println("Project Created Successfully");
System.out.print(System.lineSeparator());
System.out.println(Ansi.ansi().reset().newline());

System.out.println(
Ansi.ansi().fgGreen().bold().a("Project Created Successfully").reset().newline());

if (project.getProjectWallet() != null) {
instructionPrinter.println(
"Project information",
Ansi.Attribute.LIGHT,
Ansi.FColor.WHITE,
Ansi.BColor.BLACK);
instructionPrinter.print(
String.format("%-20s", "Wallet Address"),
Ansi.Attribute.CLEAR,
Ansi.FColor.WHITE,
Ansi.BColor.BLACK);
instructionPrinter.println(
project.getProjectWallet().getWalletAddress(),
Ansi.Attribute.BOLD,
Ansi.FColor.GREEN,
Ansi.BColor.BLACK);
System.out.print(System.lineSeparator());
System.out.println(Ansi.ansi().fgCyan().a("Project information").reset());
System.out.println(
Ansi.ansi()
.a(String.format("%-20s", "Wallet Address") + " ")
.fgGreen()
.bold()
.a(project.getProjectWallet().getWalletAddress())
.reset()
.newline());
}

InstructionsPrinter.initContextPrinter(null);
InstructionsPrinter.getContextPrinterInstance()
.getContextPrinter()
.printInstructionsOnSuccess(instructionPrinter, commandPrinter);
.printInstructionsOnSuccess();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
*/
package org.web3j.console.project.utils.printer;

import com.diogonunes.jcdp.color.ColoredPrinter;

public abstract class Printer {

public abstract void printInstructionsOnSuccess(
ColoredPrinter instructionPrinter, ColoredPrinter commandPrinter);
// Remove the parameters since JColor directly uses System.out for printing
public abstract void printInstructionsOnSuccess();

public abstract void printInstructionsOnSuccessOpenApi(
ColoredPrinter instructionPrinter, ColoredPrinter commandPrinter);
public abstract void printInstructionsOnSuccessOpenApi();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,38 @@
*/
package org.web3j.console.project.utils.printer;

import com.diogonunes.jcdp.color.ColoredPrinter;
import com.diogonunes.jcdp.color.api.Ansi;
import org.fusesource.jansi.Ansi;

public class Web3jPrinter extends Printer {

@Override
public void printInstructionsOnSuccess(
ColoredPrinter instructionPrinter, ColoredPrinter commandPrinter) {
public void printInstructionsOnSuccess() {
String gradleCommand =
System.getProperty("os.name").toLowerCase().startsWith("windows")
? "./gradlew.bat"
: "./gradlew";
instructionPrinter.println(
"Commands", Ansi.Attribute.LIGHT, Ansi.FColor.YELLOW, Ansi.BColor.BLACK);
instructionPrinter.print(String.format("%-40s", gradleCommand + " test"));
commandPrinter.println("Test your application");
instructionPrinter.print(String.format("%-40s", "web3j run"));
commandPrinter.print("Runs your application\n");
instructionPrinter.print(String.format("%-40s", "web3j docker run"));
commandPrinter.print("Runs your application in docker");

System.out.println(Ansi.ansi().fgYellow().bold().a("Commands").reset());
System.out.println(
String.format("%-40s", gradleCommand + " test") + "Test your application");
System.out.println(String.format("%-40s", "web3j run") + "Runs your application");
System.out.println(
String.format("%-40s", "web3j docker run") + "Runs your application in docker");
}

@Override
public void printInstructionsOnSuccessOpenApi(
ColoredPrinter instructionPrinter, ColoredPrinter commandPrinter) {
public void printInstructionsOnSuccessOpenApi() {
String gradleCommand =
System.getProperty("os.name").toLowerCase().startsWith("windows")
? "./gradlew.bat"
: "./gradlew";
System.out.println(System.lineSeparator());
commandPrinter.println("Project Created Successfully");
System.out.println(System.lineSeparator());

instructionPrinter.println(
"Commands", Ansi.Attribute.LIGHT, Ansi.FColor.YELLOW, Ansi.BColor.BLACK);
instructionPrinter.print(String.format("%-40s", gradleCommand + " run"));
commandPrinter.println("Run your application manually");
System.out.println();
System.out.println(Ansi.ansi().fgGreen().a("Project Created Successfully").reset());
System.out.println();

System.out.println(Ansi.ansi().fgYellow().bold().a("Commands").reset());
System.out.println(
String.format("%-40s", gradleCommand + " run") + "Run your application manually");
}
}
19 changes: 14 additions & 5 deletions src/main/java/org/web3j/console/run/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import java.nio.file.Paths;

import com.google.common.annotations.VisibleForTesting;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;

import org.web3j.codegen.Console;
import org.web3j.console.Web3jVersionProvider;

import static org.web3j.console.EnvironmentVariablesProperties.WEB3J_OPENAPI_VAR_PREFIX;
import static org.web3j.console.EnvironmentVariablesProperties.WEB3J_VAR_PREFIX;
import static org.web3j.console.utils.PrinterUtilities.coloredPrinter;
import static org.web3j.console.utils.PrinterUtilities.printErrorAndExit;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.Parameters;
Expand Down Expand Up @@ -64,6 +65,10 @@ public class RunCommand implements Runnable {
defaultValue = "")
String walletPassword;

static {
AnsiConsole.systemInstall();
}

private Path workingDirectory = Paths.get(System.getProperty("user.dir"));

@VisibleForTesting
Expand All @@ -79,18 +84,22 @@ public void run() {
try {
deploy();
} catch (Exception e) {
throw new RuntimeException(e);
e.printStackTrace();
System.exit(1);
}
}

public void deploy() throws Exception {
coloredPrinter.println("Preparing to run your Web3App");
System.out.print(System.lineSeparator());
coloredPrinter.println("Running your Web3App");
printlnWithColor("Preparing to run your Web3App", Ansi.Color.GREEN);
printlnWithColor("Running your Web3App", Ansi.Color.GREEN);
System.out.print(System.lineSeparator());
runGradle(workingDirectory);
}

private void printlnWithColor(String message, Ansi.Color color) {
System.out.println(Ansi.ansi().fg(color).a(message).reset());
}

private void runGradle(Path runLocation) throws Exception {
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
executeProcess(
Expand Down
52 changes: 25 additions & 27 deletions src/main/java/org/web3j/console/utils/PrinterUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,41 @@
*/
package org.web3j.console.utils;

import com.diogonunes.jcdp.color.ColoredPrinter;
import com.diogonunes.jcdp.color.api.Ansi;
import org.fusesource.jansi.Ansi;

public class PrinterUtilities {
private static final String CARRIAGE_RETURN = "\r";
public static ColoredPrinter coloredPrinter =
new ColoredPrinter.Builder(0, false)
.foreground(Ansi.FColor.WHITE)
.background(Ansi.BColor.GREEN)
.attribute(Ansi.Attribute.BOLD)
.build();

public static void printErrorAndExit(String errorMessage) {
coloredPrinter.println(
errorMessage, Ansi.Attribute.BOLD, Ansi.FColor.RED, Ansi.BColor.NONE);
System.out.println(Ansi.ansi().fgBrightRed().bold().a(errorMessage).reset());
System.exit(1);
}

public static void printInformationPair(
String firstText, int leftJustify, String secondText, Ansi.FColor informationColor) {
coloredPrinter.print(
String.format("%-" + leftJustify + "s", firstText),
Ansi.Attribute.CLEAR,
Ansi.FColor.WHITE,
Ansi.BColor.BLACK);
coloredPrinter.println(
secondText, Ansi.Attribute.CLEAR, informationColor, Ansi.BColor.BLACK);
String firstText, int leftJustify, String secondText, Ansi.Color informationColor) {
System.out.print(
Ansi.ansi()
.fgBrightDefault()
.a(String.format("%-" + leftJustify + "s", firstText))
.reset());
System.out.println(Ansi.ansi().fg(informationColor).a(secondText).reset());
}

public static void printInformationPairWithStatus(
String firstText, int leftJustify, String secondText, Ansi.FColor statusTextColor) {
System.out.print(CARRIAGE_RETURN);
coloredPrinter.print(
String.format("%-" + leftJustify + "s", firstText),
Ansi.Attribute.CLEAR,
Ansi.FColor.WHITE,
Ansi.BColor.BLACK);
coloredPrinter.print(secondText, Ansi.Attribute.CLEAR, statusTextColor, Ansi.BColor.BLACK);
String firstText, int leftJustify, String secondText, Ansi.Color statusTextColor) {
System.out.print(
Ansi.ansi()
.eraseLine()
.fgBrightDefault()
.a(String.format("%-" + leftJustify + "s", firstText))
.reset());
System.out.println(Ansi.ansi().fg(statusTextColor).a(secondText).reset());
}

public static void main(String[] args) {
// Example usage
printErrorAndExit("Error message with exit.");
printInformationPair("Info:", 10, "This is an information message.", Ansi.Color.CYAN);
printInformationPairWithStatus(
"Status:", 10, "This is a status message.", Ansi.Color.GREEN);
}
}
54 changes: 18 additions & 36 deletions src/main/kotlin/org/web3j/console/openapi/utils/PrettyPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,42 @@
*/
package org.web3j.console.openapi.utils

import com.diogonunes.jcdp.color.ColoredPrinter
import com.diogonunes.jcdp.color.api.Ansi
import org.web3j.console.project.utils.InstructionsPrinter

import org.fusesource.jansi.Ansi

object PrettyPrinter {
private val cp = ColoredPrinter.Builder(0, false)
.foreground(Ansi.FColor.WHITE)
.background(Ansi.BColor.GREEN)
.attribute(Ansi.Attribute.BOLD)
.build()
private val cpf = ColoredPrinter.Builder(0, false)
.foreground(Ansi.FColor.RED)
.background(Ansi.BColor.YELLOW)
.attribute(Ansi.Attribute.BOLD)
.build()
private val instructionPrinter = ColoredPrinter.Builder(0, false).foreground(Ansi.FColor.CYAN).build()
private val commandPrinter = ColoredPrinter.Builder(0, false).foreground(Ansi.FColor.GREEN).build()

fun onOpenApiProjectSuccess() {
InstructionsPrinter.initContextPrinter(null)
InstructionsPrinter.getContextPrinterInstance().contextPrinter.printInstructionsOnSuccessOpenApi(
instructionPrinter, commandPrinter
)
InstructionsPrinter.getContextPrinterInstance().contextPrinter.printInstructionsOnSuccessOpenApi()
}

fun onJarSuccess() {
print(System.lineSeparator())
cp.println("JAR generated Successfully")
print(System.lineSeparator())
println(System.lineSeparator())
println(Ansi.ansi().fgGreen().bold().a("JAR generated Successfully").reset())
println(System.lineSeparator())

instructionPrinter.println(
"Commands", Ansi.Attribute.LIGHT, Ansi.FColor.YELLOW, Ansi.BColor.BLACK
)
instructionPrinter.print(String.format("%-45s", "java -jar <jar_name> <args>"))
commandPrinter.println("Run your Jar")
instructionPrinter.print(String.format("%-45s", "java -jar <jar_name> --help"))
commandPrinter.println("See the available options")
println(Ansi.ansi().fgCyan().a("Commands").reset())
println(String.format("%-45s", "java -jar <jar_name> <args>") + Ansi.ansi().fgGreen().a(" Run your Jar").reset())
println(String.format("%-45s", "java -jar <jar_name> --help") + Ansi.ansi().fgGreen().a(" See the available options").reset())
}

fun onSuccess() {
print(System.lineSeparator())
cp.println("Project generated Successfully")
print(System.lineSeparator())
println(System.lineSeparator())
println(Ansi.ansi().fgGreen().bold().a("Project generated Successfully").reset())
println(System.lineSeparator())
}

fun onFailed() {
print(System.lineSeparator())
cpf.println("Project generation Failed. Check log file for more information.")
print(System.lineSeparator())
println(System.lineSeparator())
println(Ansi.ansi().fgRed().bold().a("Project generation Failed. Check log file for more information.").reset())
println(System.lineSeparator())
}

fun onWrongPath() {
print(System.lineSeparator())
cpf.println("No Solidity smart contracts found! Please enter a correct path containing Solidity code.")
print(System.lineSeparator())
println(System.lineSeparator())
println(Ansi.ansi().fgRed().bold().a("No Solidity smart contracts found! Please enter a correct path containing Solidity code.").reset())
println(System.lineSeparator())
}
}

0 comments on commit d54dc27

Please sign in to comment.