From da33cbb2293c50269a9f66c46216ea17e8cd27bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B6=85=E7=A5=9E=E7=9A=84=E5=86=B0=E5=87=89?= <2801621420@qq.com> Date: Fri, 5 May 2023 10:06:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9PNX-CLI=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=B9=B6=E4=BF=AE=E5=A4=8D=E9=81=97=E7=95=99?= =?UTF-8?q?=E7=9A=84=E6=97=A7libs=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/cn/powernukkitx/cli/App.java | 9 ++-- .../cn/powernukkitx/cli/cmd/AboutCommand.java | 9 ++-- .../cli/cmd/ComponentsCommand.java | 12 ++--- .../cn/powernukkitx/cli/cmd/JVMCommand.java | 22 ++++----- .../cn/powernukkitx/cli/cmd/LibsCommand.java | 39 +++++++-------- .../powernukkitx/cli/cmd/ServerCommand.java | 18 +++---- .../powernukkitx/cli/cmd/SponsorCommand.java | 5 +- .../cn/powernukkitx/cli/cmd/StartCommand.java | 24 +++++----- .../cli/cmd/SysInstallCommand.java | 35 +++++++------- .../cn/powernukkitx/cli/util/HttpUtils.java | 8 ++-- .../cn/powernukkitx/cli/util/InputUtils.java | 6 +-- .../cn/powernukkitx/cli/util/LibsUtils.java | 32 ------------- .../java/cn/powernukkitx/cli/util/Logger.java | 4 ++ .../cn/powernukkitx/cli/util/OSUtils.java | 48 ------------------- 14 files changed, 99 insertions(+), 172 deletions(-) delete mode 100644 src/main/java/cn/powernukkitx/cli/util/LibsUtils.java diff --git a/src/main/java/cn/powernukkitx/cli/App.java b/src/main/java/cn/powernukkitx/cli/App.java index 6fa6584..e97df65 100644 --- a/src/main/java/cn/powernukkitx/cli/App.java +++ b/src/main/java/cn/powernukkitx/cli/App.java @@ -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; @@ -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; } diff --git a/src/main/java/cn/powernukkitx/cli/cmd/AboutCommand.java b/src/main/java/cn/powernukkitx/cli/cmd/AboutCommand.java index 9bf0f8a..b56f026 100644 --- a/src/main/java/cn/powernukkitx/cli/cmd/AboutCommand.java +++ b/src/main/java/cn/powernukkitx/cli/cmd/AboutCommand.java @@ -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; @@ -14,10 +15,10 @@ public final class AboutCommand implements Callable { @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; } } diff --git a/src/main/java/cn/powernukkitx/cli/cmd/ComponentsCommand.java b/src/main/java/cn/powernukkitx/cli/cmd/ComponentsCommand.java index e590036..dd7fcbc 100644 --- a/src/main/java/cn/powernukkitx/cli/cmd/ComponentsCommand.java +++ b/src/main/java/cn/powernukkitx/cli/cmd/ComponentsCommand.java @@ -4,6 +4,7 @@ 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; @@ -11,7 +12,6 @@ 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; @@ -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()) { @@ -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()) { @@ -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); diff --git a/src/main/java/cn/powernukkitx/cli/cmd/JVMCommand.java b/src/main/java/cn/powernukkitx/cli/cmd/JVMCommand.java index 195407a..819547e 100644 --- a/src/main/java/cn/powernukkitx/cli/cmd/JVMCommand.java +++ b/src/main/java/cn/powernukkitx/cli/cmd/JVMCommand.java @@ -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(); @@ -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 { @@ -141,7 +141,7 @@ 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")) { @@ -149,36 +149,36 @@ public Integer call() { 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; } } @@ -221,7 +221,7 @@ private List> 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; } } diff --git a/src/main/java/cn/powernukkitx/cli/cmd/LibsCommand.java b/src/main/java/cn/powernukkitx/cli/cmd/LibsCommand.java index 1a2b606..6470783 100644 --- a/src/main/java/cn/powernukkitx/cli/cmd/LibsCommand.java +++ b/src/main/java/cn/powernukkitx/cli/cmd/LibsCommand.java @@ -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; @@ -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(); @@ -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(); @@ -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(); @@ -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; } @@ -198,7 +199,7 @@ 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; } @@ -206,14 +207,14 @@ public Integer call() { 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()); } } @@ -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; } } diff --git a/src/main/java/cn/powernukkitx/cli/cmd/ServerCommand.java b/src/main/java/cn/powernukkitx/cli/cmd/ServerCommand.java index 3a33c57..a0b7289 100644 --- a/src/main/java/cn/powernukkitx/cli/cmd/ServerCommand.java +++ b/src/main/java/cn/powernukkitx/cli/cmd/ServerCommand.java @@ -8,6 +8,7 @@ import cn.powernukkitx.cli.share.CLIConstant; 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.Command; @@ -16,7 +17,6 @@ import java.io.File; import java.io.IOException; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Formatter; import java.util.ResourceBundle; @@ -62,7 +62,7 @@ public Integer call() throws ParseException { displayName = "PowerNukkitX-Core v" + release.tagName(); return downloadCore(displayName, release); } catch (IOException | InterruptedException e) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), displayName)).fgDefault()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), displayName)).fgDefault()); e.printStackTrace(); return 1; } @@ -73,11 +73,11 @@ public Integer call() throws ParseException { try { // 获取所有版本 var allReleases = VersionListHelperV2.getAllReleases(); - 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; @@ -90,14 +90,14 @@ public Integer call() throws ParseException { displayName = "PowerNukkitX-Core v" + release.tagName(); return downloadCore(displayName, release); } catch (IOException | InterruptedException e) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), displayName)).fgDefault()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), displayName)).fgDefault()); e.printStackTrace(); return 1; } } public @NotNull Integer updateDev() { - System.out.println(ansi().fgBrightYellow().a(bundle.getString("dev-warning")).fgDefault()); + Logger.warn(ansi().fgBrightYellow().a(bundle.getString("dev-warning")).fgDefault()); String displayName = "PowerNukkitX-Core Unknown"; try { // 获取最新构建 @@ -106,7 +106,7 @@ public Integer call() throws ParseException { displayName = "PowerNukkitX-Core dev (" + commonTimeFormat.format(coreArtifact.createAt()) + ")"; return downloadCore(displayName, "PowerNukkitX-dev.jar", coreArtifact); } catch (IOException | InterruptedException e) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), displayName)).fgDefault()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), displayName)).fgDefault()); e.printStackTrace(); return 1; } @@ -120,10 +120,10 @@ public Integer call() throws ParseException { new File(CLIConstant.userDir, fileName), displayName, coreArtifact.sizeInBytes(), Main.getTimer()); if (result) { - System.out.println(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("successfully-installed"), displayName)).fgDefault()); + Logger.info(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("successfully-installed"), displayName)).fgDefault()); return 0; } else { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), displayName)).fgDefault()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail-to-install"), displayName)).fgDefault()); return 1; } } diff --git a/src/main/java/cn/powernukkitx/cli/cmd/SponsorCommand.java b/src/main/java/cn/powernukkitx/cli/cmd/SponsorCommand.java index 464856f..c020957 100644 --- a/src/main/java/cn/powernukkitx/cli/cmd/SponsorCommand.java +++ b/src/main/java/cn/powernukkitx/cli/cmd/SponsorCommand.java @@ -1,5 +1,6 @@ package cn.powernukkitx.cli.cmd; +import cn.powernukkitx.cli.util.Logger; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import picocli.CommandLine.Command; @@ -38,9 +39,9 @@ public Integer call() { } var names = namesBuilder.toString(); names = names.substring(0, names.length() - 2); - System.out.println(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("thank"), names)).fgDefault()); + Logger.info(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("thank"), names)).fgDefault()); } catch (IOException | InterruptedException e) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail"), request.uri().toString())).fgDefault()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail"), request.uri().toString())).fgDefault()); if (debug()) { e.printStackTrace(); } diff --git a/src/main/java/cn/powernukkitx/cli/cmd/StartCommand.java b/src/main/java/cn/powernukkitx/cli/cmd/StartCommand.java index ad93c7e..06002f2 100644 --- a/src/main/java/cn/powernukkitx/cli/cmd/StartCommand.java +++ b/src/main/java/cn/powernukkitx/cli/cmd/StartCommand.java @@ -7,10 +7,7 @@ import cn.powernukkitx.cli.data.locator.JarLocator; import cn.powernukkitx.cli.data.locator.JavaLocator; import cn.powernukkitx.cli.share.CLIConstant; -import cn.powernukkitx.cli.util.ConfigUtils; -import cn.powernukkitx.cli.util.InputUtils; -import cn.powernukkitx.cli.util.LibsUtils; -import cn.powernukkitx.cli.util.OSUtils; +import cn.powernukkitx.cli.util.*; import picocli.CommandLine.Command; import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; @@ -52,18 +49,18 @@ public Integer call() { } var javaList = new JavaLocator("17", true).locate(); if (javaList.size() == 0) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("no-java17"), OSUtils.getProgramName())).fgDefault()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("no-java17"), OSUtils.getProgramName())).fgDefault()); return 1; } var java = javaList.get(0); cmdBuilder.setJvmExecutable(java.getFile()); - System.out.println(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("using-jvm"), java.getInfo().getVendor())).fgDefault()); + Logger.info(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("using-jvm"), java.getInfo().getVendor())).fgDefault()); var pnxList = new JarLocator(CLIConstant.userDir, "cn.nukkit.api.PowerNukkitXOnly").locate(); //auto install if (pnxList.size() == 0) { try { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("no-pnx"), OSUtils.getProgramName())).fgDefault()); + Logger.warn(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("no-pnx"), OSUtils.getProgramName())).fgDefault()); var download = new ServerCommand(); download.update = true; download.latest = true; @@ -81,13 +78,18 @@ public Integer call() { } var oldLibFiles = new LinkedList<>(Arrays.asList(Objects.requireNonNull(libDir.listFiles((dir, name) -> name.endsWith(".jar"))))); if (oldLibFiles.size() < 32) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("no-libs"), OSUtils.getProgramName())).fgDefault()); - LibsUtils.checkAndUpdate(); + Logger.warn(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("no-libs"), OSUtils.getProgramName())).fgDefault()); + var libs = new LibsCommand(); + libs.options = new LibsCommand.AllOptions(); + libs.options.update = true; + libs.versionOptions = new LibsCommand.VersionOptions(); + libs.versionOptions.latest = true; + libs.call(); } var pnx = pnxList.get(0); cmdBuilder.addClassPath(pnx.getFile().getAbsolutePath()); - System.out.println(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("using-pnx"), Ok(pnx.getInfo().getGitInfo().orElse(null), info -> info.getMainVersion() + " - " + info.getCommitID(), "unknown"))).fgDefault()); + Logger.info(ansi().fgBrightYellow().a(new Formatter().format(bundle.getString("using-pnx"), Ok(pnx.getInfo().getGitInfo().orElse(null), info -> info.getMainVersion() + " - " + info.getCommitID(), "unknown"))).fgDefault()); cmdBuilder.setStartTarget("cn.nukkit.Nukkit"); cmdBuilder.addClassPath(new File(CLIConstant.userDir, "libs").getAbsolutePath() + File.separator + "*"); cmdBuilder.addAddOpen("java.base/java.lang"); @@ -124,7 +126,7 @@ public Integer call() { cmdBuilder.addXxOption(each); } if (generateOnly) { - System.out.println(cmdBuilder.build()); + Logger.raw(cmdBuilder.build()); return 0; } cmdBuilder.addProperty("pnx.cli.path", OSUtils.getProgramPath()); diff --git a/src/main/java/cn/powernukkitx/cli/cmd/SysInstallCommand.java b/src/main/java/cn/powernukkitx/cli/cmd/SysInstallCommand.java index f40ebee..7052344 100644 --- a/src/main/java/cn/powernukkitx/cli/cmd/SysInstallCommand.java +++ b/src/main/java/cn/powernukkitx/cli/cmd/SysInstallCommand.java @@ -1,9 +1,10 @@ package cn.powernukkitx.cli.cmd; import cn.powernukkitx.cli.util.EnumOS; +import cn.powernukkitx.cli.util.Logger; import cn.powernukkitx.cli.util.OSUtils; -import org.fusesource.jansi.Ansi; -import picocli.CommandLine.*; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; import java.io.IOException; import java.nio.file.Files; @@ -25,11 +26,11 @@ public final class SysInstallCommand implements Callable { public Integer call() { var os = OSUtils.getOS(); if (!"Substrate VM".equals(System.getProperty("java.vm.name"))) { - System.out.println(ansi().fgBrightRed().a(bundle.getString("executable-only")).fgDefault().toString()); + Logger.error(ansi().fgBrightRed().a(bundle.getString("executable-only")).fgDefault().toString()); return 1; } if (os == EnumOS.UNKNOWN || os == EnumOS.MACOS) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("unsupportedOS"), System.getProperty("os.name"))).fgDefault().toString()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("unsupportedOS"), System.getProperty("os.name"))).fgDefault().toString()); return 1; } if (uninstall) { @@ -40,17 +41,17 @@ public Integer call() { try { boolean ok = OSUtils.removeWindowsPath(programDir); if (ok) { - System.out.println(ansi().fgBrightGreen().a(bundle.getString("success-uninstall")).fgDefault().toString()); - System.out.println(ansi().fgBrightGreen().a(bundle.getString("windows-cmd")).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(bundle.getString("success-uninstall")).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(bundle.getString("windows-cmd")).fgDefault().toString()); return 0; } } catch (IOException | InterruptedException e) { e.printStackTrace(); } - System.out.println(ansi().fgBrightRed().a(bundle.getString("fail-uninstall")).fgDefault().toString()); + Logger.error(ansi().fgBrightRed().a(bundle.getString("fail-uninstall")).fgDefault().toString()); return 1; } else { - System.out.println(ansi().fgBrightGreen().a(bundle.getString("have-not")).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(bundle.getString("have-not")).fgDefault().toString()); return 0; } } @@ -59,27 +60,27 @@ public Integer call() { var sysPath = System.getenv("PATH"); var programDir = OSUtils.getProgramDir(); if (sysPath.contains(programDir)) { - System.out.println(ansi().fgBrightGreen().a(bundle.getString("already")).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(bundle.getString("already")).fgDefault().toString()); return 0; } else { try { boolean ok = OSUtils.addWindowsPath(programDir); if (ok) { - System.out.println(ansi().fgBrightGreen().a(bundle.getString("success")).fgDefault().toString()); - System.out.println(ansi().fgBrightGreen().a(bundle.getString("windows-cmd")).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(bundle.getString("success")).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(bundle.getString("windows-cmd")).fgDefault().toString()); return 0; } } catch (IOException | InterruptedException e) { e.printStackTrace(); } - System.out.println(ansi().fgBrightRed().a(bundle.getString("fail")).fgDefault().toString()); + Logger.error(ansi().fgBrightRed().a(bundle.getString("fail")).fgDefault().toString()); return 1; } } else if (os == EnumOS.LINUX) { var sysPath = System.getenv("PATH"); var programDir = OSUtils.getProgramDir(); if (sysPath.contains(programDir)) { - System.out.println(ansi().fgBrightGreen().a(bundle.getString("already")).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(bundle.getString("already")).fgDefault().toString()); return 0; } else { try { @@ -89,7 +90,7 @@ public Integer call() { profilePath = homeDir + "/.bash_profile"; } if (!Files.exists(Path.of(profilePath))) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("unsupportedOS"), System.getProperty("os.name"))).fgDefault().toString()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("unsupportedOS"), System.getProperty("os.name"))).fgDefault().toString()); return 1; } var profile = Files.readAllLines(Path.of(profilePath)); @@ -106,13 +107,13 @@ public Integer call() { profile.add("export PATH=" + programDir + ":$PATH"); } Files.write(Path.of(profilePath), profile); - System.out.println(ansi().fgBrightGreen().a(bundle.getString("success")).fgDefault().toString()); - System.out.println(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("linux-cmd"), profilePath)).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(bundle.getString("success")).fgDefault().toString()); + Logger.info(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("linux-cmd"), profilePath)).fgDefault().toString()); return 0; } catch (IOException e) { e.printStackTrace(); } - System.out.println(ansi().fgBrightRed().a(bundle.getString("fail")).fgDefault().toString()); + Logger.error(ansi().fgBrightRed().a(bundle.getString("fail")).fgDefault().toString()); return 1; } } diff --git a/src/main/java/cn/powernukkitx/cli/util/HttpUtils.java b/src/main/java/cn/powernukkitx/cli/util/HttpUtils.java index 2a7aad4..d8be4b5 100644 --- a/src/main/java/cn/powernukkitx/cli/util/HttpUtils.java +++ b/src/main/java/cn/powernukkitx/cli/util/HttpUtils.java @@ -139,8 +139,8 @@ public static boolean downloadWithBar(String downloadURL, File target, String di try { var client = getClient(); var request = HttpRequest.newBuilder(URI.create(downloadURL)).GET().build(); - System.out.println(ansi().fgBrightDefault().a(new Formatter().format(bundle.getString("connecting"), downloadURL)).fgDefault().toString()); - System.out.println(); + Logger.info(ansi().fgBrightDefault().a(new Formatter().format(bundle.getString("connecting"), downloadURL)).fgDefault().toString()); + Logger.raw(""); var contentLength = new AtomicLong(); var atomicLong = new AtomicLong(0); var task = new TimerTask() { @@ -181,10 +181,10 @@ public void run() { } AnsiConsole.out().println(ansi().saveCursorPosition().cursorUpLine().eraseLine()); task.cancel(); - System.out.println(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("success"), displayName)).fgDefault()); + Logger.info(ansi().fgBrightGreen().a(new Formatter().format(bundle.getString("success"), displayName)).fgDefault()); return true; } catch (Exception e) { - System.out.println(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail"), displayName)).fgDefault()); + Logger.error(ansi().fgBrightRed().a(new Formatter().format(bundle.getString("fail"), displayName)).fgDefault()); if (debug()) { e.printStackTrace(); } diff --git a/src/main/java/cn/powernukkitx/cli/util/InputUtils.java b/src/main/java/cn/powernukkitx/cli/util/InputUtils.java index be74380..e7b988d 100644 --- a/src/main/java/cn/powernukkitx/cli/util/InputUtils.java +++ b/src/main/java/cn/powernukkitx/cli/util/InputUtils.java @@ -13,7 +13,7 @@ private InputUtils() { } public static int readIndex(String prompt) { - System.out.print(prompt + " "); + Logger.info(prompt + " "); var scanner = new Scanner(System.in); return scanner.nextInt(); } @@ -27,7 +27,7 @@ public static String readLine() { } public static void pressEnterToContinue() { - System.out.println(bundle.getString("press-enter-to-continue")); + Logger.info(bundle.getString("press-enter-to-continue")); try { //noinspection ResultOfMethodCallIgnored System.in.read(); @@ -37,7 +37,7 @@ public static void pressEnterToContinue() { } public static boolean pressEnterToStopWithTimeLimit(long timeLimit) { - System.out.println(ansi().fgBrightDefault().bold().a(bundle.getString("press-enter-to-stop-with-time-limit")).fgDefault().boldOff()); + Logger.info(ansi().fgBrightDefault().bold().a(bundle.getString("press-enter-to-stop-with-time-limit")).fgDefault().boldOff()); try { var startTime = System.currentTimeMillis(); while (System.currentTimeMillis() - startTime < timeLimit) { diff --git a/src/main/java/cn/powernukkitx/cli/util/LibsUtils.java b/src/main/java/cn/powernukkitx/cli/util/LibsUtils.java deleted file mode 100644 index b346473..0000000 --- a/src/main/java/cn/powernukkitx/cli/util/LibsUtils.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.powernukkitx.cli.util; - -import cn.powernukkitx.cli.Main; -import cn.powernukkitx.cli.data.locator.LibsLocator; -import cn.powernukkitx.cli.share.CLIConstant; - -import java.io.File; -import java.util.*; - -import static org.fusesource.jansi.Ansi.ansi; - -public class LibsUtils { - public static boolean checkAndUpdate() { - var libs = new LibsLocator().locate(); - var libDir = new File(CLIConstant.userDir, "libs"); - var oldLibFiles = new LinkedList<>(Arrays.asList(Objects.requireNonNull(libDir.listFiles((dir, name) -> name.endsWith(".jar"))))); - var allSuccess = true; - for (var each : libs) { - oldLibFiles.removeIf(file -> file.getName().equals(each.getFile().getName())); - if (each.getInfo().isNeedsUpdate()) { - var result = HttpUtils.downloadWithBar("https://assets.powernukkitx.cn/libs/" + each.getInfo().getName(), each.getFile(), each.getInfo().getName(), Main.getTimer()); - if (!result) { - allSuccess = false; - System.out.println(ansi().fgBrightRed().a(new Formatter().format(ResourceBundle.getBundle("cn.powernukkitx.cli.cmd.Libs").getString("fail-to-update"), each.getInfo().getName())).fgDefault()); - } - } - } - //noinspection ResultOfMethodCallIgnored - oldLibFiles.forEach(File::delete); - return allSuccess; - } -} diff --git a/src/main/java/cn/powernukkitx/cli/util/Logger.java b/src/main/java/cn/powernukkitx/cli/util/Logger.java index ad02824..cc511f7 100644 --- a/src/main/java/cn/powernukkitx/cli/util/Logger.java +++ b/src/main/java/cn/powernukkitx/cli/util/Logger.java @@ -43,4 +43,8 @@ public static void error(Ansi msg) { System.out.println(ansi().fgBrightBlue().a(LocalDateTime.now().format(formatter)) .fgDefault().a(" [").fgBrightRed().a("ERROR").fgDefault().a("] ").fgDefault().a(msg).reset()); } + + public static void raw(String msg) { + System.out.println(msg); + } } diff --git a/src/main/java/cn/powernukkitx/cli/util/OSUtils.java b/src/main/java/cn/powernukkitx/cli/util/OSUtils.java index e405b5a..917eccc 100644 --- a/src/main/java/cn/powernukkitx/cli/util/OSUtils.java +++ b/src/main/java/cn/powernukkitx/cli/util/OSUtils.java @@ -93,54 +93,6 @@ public static boolean editWindowsPath(String op, String path) throws IOException } } - public static boolean setWindowsEnv(EnvEntry... entries) throws IOException, InterruptedException { - var batBuilder = new StringBuilder(""" - @echo off - - :: BatchGotAdmin - :------------------------------------- - REM --> Check for permissions - >nul 2>&1 "%SYSTEMROOT%\\system32\\cacls.exe" "%SYSTEMROOT%\\system32\\config\\system" - - REM --> If error flag set, we do not have admin. - if '%errorlevel%' NEQ '0' ( - echo Requesting administrative privileges... - goto UACPrompt - ) else ( goto gotAdmin ) - - :UACPrompt - echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\\getadmin.vbs" - echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\\getadmin.vbs" - - "%temp%\\getadmin.vbs" - exit /B - - :gotAdmin - if exist "%temp%\\getadmin.vbs" ( del "%temp%\\getadmin.vbs" ) - pushd "%CD%" - CD /D "%~dp0" - :-------------------------------------- - - @echo on - """); - for (var each : entries) { - batBuilder.append("setx \"").append(each.key()).append("\" \"").append(each.value()).append("\"\n"); - } - batBuilder.append("echo pnxEnvOK\n"); - var batFile = File.createTempFile("pnxBat", ".bat"); - batFile.deleteOnExit(); - Files.writeString(batFile.toPath(), batBuilder.toString()); - System.out.println(batBuilder); - var resultFile = File.createTempFile("pnxBuffer", null); - resultFile.deleteOnExit(); - var process = new ProcessBuilder("\"" + batFile.getAbsolutePath() + "\"") - .redirectOutput(resultFile).start(); - process.waitFor(30, TimeUnit.SECONDS); - var result = Files.readString(resultFile.toPath()); - System.out.println(result); - return result.contains("pnxEnvOK"); - } - public static Locale getWindowsLocale() { try { var process = new ProcessBuilder().command("C:\\Windows\\System32\\reg.exe", "query",