Skip to content

Commit

Permalink
修改PNX-CLI输出格式并修复遗留的旧libs下载
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperIceCN committed May 5, 2023
1 parent 9a02d7b commit da33cbb
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 172 deletions.
9 changes: 3 additions & 6 deletions src/main/java/cn/powernukkitx/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import cn.powernukkitx.cli.cmd.*;
import cn.powernukkitx.cli.share.CLIConstant;
import cn.powernukkitx.cli.util.ConfigUtils;
import cn.powernukkitx.cli.util.InputUtils;
import cn.powernukkitx.cli.util.OSUtils;
import cn.powernukkitx.cli.util.StringUtils;
import cn.powernukkitx.cli.util.*;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -51,13 +48,13 @@ public Integer call() {
ConfigUtils.globalConfigFile = file;
ConfigUtils.parseConfigFile(file);
} else {
System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("invalid-file"), configFilePath)).fgDefault());
Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("invalid-file"), configFilePath)).fgDefault());
}
}
var start = new StartCommand();
start.args = args;
if (args != null && args.length != 0) {
System.out.println(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("args"), OSUtils.getProgramName())).fgDefault());
Logger.error(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("args"), OSUtils.getProgramName())).fgDefault());
CommandLine.usage(this, System.out);
return 1;
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/cn/powernukkitx/cli/cmd/AboutCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.powernukkitx.cli.cmd;

import cn.powernukkitx.cli.share.CLIConstant;
import cn.powernukkitx.cli.util.Logger;
import picocli.CommandLine.Command;

import java.util.ResourceBundle;
Expand All @@ -14,10 +15,10 @@ public final class AboutCommand implements Callable<Integer> {

@Override
public Integer call() {
System.out.println(ansi().fgBrightDefault().bold().a("PowerNukkitX").fgDefault().boldOff());
System.out.println(ansi().fgBrightDefault().bold().a("------------------------------").fgDefault().boldOff());
System.out.println(ansi().a(bundle.getString("cli-version")).fgBrightYellow().a(CLIConstant.version).fgDefault());
System.out.println(ansi().a(bundle.getString("author")).fgBrightCyan().a(String.join(", ", CLIConstant.authors)).fgDefault());
Logger.info(ansi().fgBrightDefault().bold().a("PowerNukkitX").fgDefault().boldOff());
Logger.info(ansi().fgBrightDefault().bold().a("------------------------------").fgDefault().boldOff());
Logger.info(ansi().a(bundle.getString("cli-version")).fgBrightYellow().a(CLIConstant.version).fgDefault());
Logger.info(ansi().a(bundle.getString("author")).fgBrightCyan().a(String.join(", ", CLIConstant.authors)).fgDefault());
return 0;
}
}
12 changes: 6 additions & 6 deletions src/main/java/cn/powernukkitx/cli/cmd/ComponentsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import cn.powernukkitx.cli.data.locator.ComponentsLocator;
import cn.powernukkitx.cli.share.CLIConstant;
import cn.powernukkitx.cli.util.HttpUtils;
import cn.powernukkitx.cli.util.Logger;
import cn.powernukkitx.cli.util.OSUtils;
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Objects;
Expand Down Expand Up @@ -48,7 +48,7 @@ public Integer call() {
ansi.fgBrightRed().a("- ");
}
ansi.a(each.getInfo().getName()).fgDefault().a(" - ").bold().a(each.getInfo().getVersion()).fgDefault().boldOff().a(" ").a(each.getInfo().getDescription());
System.out.println(ansi);
Logger.info(ansi);
}
return 0;
} else if (options.update != null && !options.update.isBlank()) {
Expand All @@ -61,7 +61,7 @@ public Integer call() {
for (var each : componentList) {
var componentInfo = each.getInfo();
if (componentInfo.getName().equalsIgnoreCase(options.update)) {
System.out.println(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("installing"), componentInfo.getName() + " - " + componentInfo.getVersion())).fgDefault());
Logger.info(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("installing"), componentInfo.getName() + " - " + componentInfo.getVersion())).fgDefault());
for (var clearFileName : componentInfo.getClearFiles()) {
var file = new File(CLIConstant.userDir, clearFileName);
if (file.exists()) {
Expand All @@ -77,15 +77,15 @@ public Integer call() {
ok = ok && HttpUtils.downloadWithBar(compFile.getDownloadPath(), new File(CLIConstant.userDir, compFile.getFileName()), componentInfo.getName() + " - " + componentInfo.getVersion(), Main.getTimer());
}
if (ok) {
System.out.println(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("successfully-update"), componentInfo.getName() + " - " + componentInfo.getVersion())).fgDefault());
Logger.info(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("successfully-update"), componentInfo.getName() + " - " + componentInfo.getVersion())).fgDefault());
return 0;
} else {
System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-update"), componentInfo.getName() + " - " + componentInfo.getVersion())).fgDefault());
Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-update"), componentInfo.getName() + " - " + componentInfo.getVersion())).fgDefault());
return 1;
}
}
}
System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("component-not-found"), OSUtils.getProgramName())).fgDefault());
Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("component-not-found"), OSUtils.getProgramName())).fgDefault());
return 1;
} else {
CommandLine.usage(this, System.out);
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/cn/powernukkitx/cli/cmd/JVMCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Integer call() {
ansi.a("(").a(size).a(")\n");
}
}
System.out.println(ansi);
Logger.info(ansi);
return 0;
} else if (options.install != null && !options.install.isBlank()) {
var vms = getVMList();
Expand Down Expand Up @@ -122,7 +122,7 @@ public Integer call() {
size = obj.get("size").getAsString();
}
if ("".equals(url) || "unknown".equals(size)) {
System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("no-this-arch"),
Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("no-this-arch"),
options.install, os.name().toLowerCase(), arch)).fgDefault().toString());
return 1;
} else {
Expand All @@ -141,44 +141,44 @@ public Integer call() {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-uncompress")).fgDefault().toString());
Logger.error(ansi().fgBrightRed().a(bundle.getString("fail-to-uncompress")).fgDefault().toString());
return 1;
}
} else if (suffix.endsWith("tar.gz")) {
try {
GzipUtils.uncompressTGzipFile(tmpFile, localJavaDir);
} catch (IOException e) {
e.printStackTrace();
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-uncompress")).fgDefault().toString());
Logger.error(ansi().fgBrightRed().a(bundle.getString("fail-to-uncompress")).fgDefault().toString());
return 1;
}
}
System.out.println(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("successfully-install"), options.install)).fgDefault().toString());
Logger.info(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("successfully-install"), options.install)).fgDefault().toString());
return 0;
} else {
System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), options.install)).fgDefault().toString());
Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), options.install)).fgDefault().toString());
return 1;
}
}
} else {
System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("vm-not-found"), OSUtils.getProgramName())).fgDefault().toString());
Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("vm-not-found"), OSUtils.getProgramName())).fgDefault().toString());
return 1;
}
} else if (options.uninstall) {
var JVMs = printJVMs();
var index = InputUtils.readIndex(bundle.getString("uninstall-index")) - 1;
if (index >= JVMs.size()) {
System.out.println(ansi().fgBrightRed().a(bundle.getString("index-out-of-range")).fgDefault().toString());
Logger.error(ansi().fgBrightRed().a(bundle.getString("index-out-of-range")).fgDefault().toString());
return 1;
} else {
var vmInfo = JVMs.get(index);
var vmFolder = vmInfo.getFile().getParentFile().getParentFile();
FileUtils.deleteDir(vmFolder);
if (!vmInfo.getFile().exists()) {
System.out.println(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("successfully-uninstall"), vmInfo.getInfo().getVendor())).fgDefault().toString());
Logger.info(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("successfully-uninstall"), vmInfo.getInfo().getVendor())).fgDefault().toString());
return 0;
} else {
System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-uninstall"), vmInfo.getInfo().getVendor())).fgDefault().toString());
Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-uninstall"), vmInfo.getInfo().getVendor())).fgDefault().toString());
return 1;
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ private List<Location<JavaLocator.JavaInfo>> printJVMs() {
.a(" - ").fgBrightCyan().a("java ").a(each.getInfo().getMajorVersion()).fgDefault().a("\n ")
.a(each.getFile().getAbsolutePath()).a("\n").toString());
}
System.out.println(out);
Logger.info(out.toString());
return javaLocations;
}
}
39 changes: 20 additions & 19 deletions src/main/java/cn/powernukkitx/cli/cmd/LibsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cn.powernukkitx.cli.util.FileUtils;
import cn.powernukkitx.cli.util.HttpUtils;
import cn.powernukkitx.cli.util.InputUtils;
import cn.powernukkitx.cli.util.Logger;
import org.jetbrains.annotations.NotNull;
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
Expand Down Expand Up @@ -76,10 +77,10 @@ public Integer call() {
}

public @NotNull Integer updateLatest(boolean check) {
System.out.println(ansi().fgBrightYellow().a(bundle.getString("fetching-manifest")).fgDefault());
Logger.info(ansi().fgBrightYellow().a(bundle.getString("fetching-manifest")).fgDefault());
var future = VersionListHelperV2.getLatestReleaseLibs();
var remoteFileBeanMap = future.exceptionally(throwable -> {
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-get-manifest")).fgDefault());
Logger.error(ansi().fgBrightRed().a(bundle.getString("fail-to-get-manifest")).fgDefault());
throwable.printStackTrace();
return null;
}).join();
Expand All @@ -95,10 +96,10 @@ public Integer call() {
}

public @NotNull Integer updateDev(boolean check) {
System.out.println(ansi().fgBrightYellow().a(bundle.getString("fetching-manifest")).fgDefault());
Logger.info(ansi().fgBrightYellow().a(bundle.getString("fetching-manifest")).fgDefault());
var future = VersionListHelperV2.getLatestBuildLibs();
var remoteFileBeanMap = future.exceptionally(throwable -> {
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-get-manifest")).fgDefault());
Logger.error(ansi().fgBrightRed().a(bundle.getString("fail-to-get-manifest")).fgDefault());
throwable.printStackTrace();
return null;
}).join();
Expand All @@ -119,28 +120,28 @@ public Integer call() {
try {
allReleases = VersionListHelperV2.getAllReleases();
} catch (IOException | InterruptedException e) {
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-get-version-list")).fgDefault());
Logger.error(ansi().fgBrightRed().a(bundle.getString("fail-to-get-version-list")).fgDefault());
e.printStackTrace();
return -1;
}
System.out.println(ansi().fgBrightDefault().a(bundle.getString("available-version")).fgDefault());
Logger.info(ansi().fgBrightDefault().a(bundle.getString("available-version")).fgDefault());
for (int i = 0, len = allReleases.length; i < len; i++) {
var entry = allReleases[i];
var time = commonTimeFormat.format(entry.publishedAt());
System.out.println((i + 1) + ". " + entry.tagName() + " (" + time + ")");
Logger.info((i + 1) + ". " + entry.tagName() + " (" + time + ")");
}
// 等待用户选择
var index = InputUtils.readIndex(bundle.getString("choose-version")) - 1;
if (index < 0 || index >= allReleases.length) {
System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("invalid-index"), allReleases.length)).fgDefault());
Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("invalid-index"), allReleases.length)).fgDefault());
return 1;
}
var release = allReleases[index];
System.out.println(ansi().fgBrightYellow().a(bundle.getString("fetching-manifest")).fgDefault());
Logger.info(ansi().fgBrightYellow().a(bundle.getString("fetching-manifest")).fgDefault());
var future = VersionListHelperV2.getReleaseLibsFromArtifact(Arrays.stream(release.artifacts())
.filter(artifact -> artifact.name().equalsIgnoreCase("libs.tar.gz")).findFirst().orElseThrow());
var remoteFileBeanMap = future.exceptionally(throwable -> {
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-get-manifest")).fgDefault());
Logger.error(ansi().fgBrightRed().a(bundle.getString("fail-to-get-manifest")).fgDefault());
throwable.printStackTrace();
return null;
}).join();
Expand All @@ -167,19 +168,19 @@ public Integer call() {
var remoteFileBean = remoteFileBeanMapCopy.remove(childFile.getName());
try {
if (FileUtils.getMD5(childFile).equals(remoteFileBean.md5())) {
System.out.println(ansi().fgBrightGreen().a("+ ").a(childFile.getName()).fgDefault());
Logger.info(ansi().fgBrightGreen().a("+ ").a(childFile.getName()).fgDefault());
} else {
System.out.println(ansi().fgBrightYellow().a("~ ").a(childFile.getName()).fgDefault());
Logger.info(ansi().fgBrightYellow().a("~ ").a(childFile.getName()).fgDefault());
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println(ansi().fgBrightRed().a("- ").a(childFile.getName()).fgDefault());
Logger.info(ansi().fgBrightRed().a("- ").a(childFile.getName()).fgDefault());
}
}
for (var remoteFileBean : remoteFileBeanMapCopy.values()) {
System.out.println(ansi().fgBrightRed().a("+ ").a(remoteFileBean.fileName()).fgDefault());
Logger.info(ansi().fgBrightRed().a("+ ").a(remoteFileBean.fileName()).fgDefault());
}
return 0;
}
Expand All @@ -198,22 +199,22 @@ public Integer call() {
if (!FileUtils.getMD5(childFile).equals(remoteFileBean.md5())) {
var result = childFile.delete();
if (!result) {
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-update")
Logger.warn(ansi().fgBrightRed().a(bundle.getString("fail-to-update")
.formatted(remoteFileBean.fileName())).fgDefault());
continue;
}
HttpUtils.downloadWithBar(HttpUtils.getAPIUrl("/download/") + remoteFileBean.downloadID(),
childFile, remoteFileBean.fileName(), Main.getTimer());
}
} catch (IOException e) {
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-update")
Logger.warn(ansi().fgBrightRed().a(bundle.getString("fail-to-update")
.formatted(remoteFileBean.fileName())).fgDefault());
e.printStackTrace();
}
} else {
var result = childFile.delete();
if (!result) {
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-update")
Logger.warn(ansi().fgBrightRed().a(bundle.getString("fail-to-update")
.formatted(childFile.getName())).fgDefault());
}
}
Expand All @@ -224,12 +225,12 @@ public Integer call() {
HttpUtils.downloadWithBar(HttpUtils.getAPIUrl("/download/") + remoteFileBean.downloadID(),
file, remoteFileBean.fileName(), Main.getTimer());
} catch (Exception e) {
System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-to-update")
Logger.warn(ansi().fgBrightRed().a(bundle.getString("fail-to-update")
.formatted(remoteFileBean.fileName())).fgDefault());
e.printStackTrace();
}
}
System.out.println(ansi().fgBrightGreen().a(bundle.getString("successfully-update")).fgDefault());
Logger.info(ansi().fgBrightGreen().a(bundle.getString("successfully-update")).fgDefault());
return 0;
}
}
Loading

0 comments on commit da33cbb

Please sign in to comment.