Skip to content

Commit

Permalink
PNX-CLI v0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperIceCN committed Jun 18, 2023
1 parent 158e39e commit 0d19f6f
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/main/java/cn/powernukkitx/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@
})
public final class App implements Callable<Integer> {

@SuppressWarnings("unused")
@Option(names = {"-l", "--lang", "--language"}, paramLabel = "<lang>", descriptionKey = "lang")
private String ignoredLocale;

@Option(names = "--config-path", paramLabel = "<config-path>", descriptionKey = "config-path")
public String configFilePath;

@SuppressWarnings("unused")
@Option(names = {"-u", "--update"}, descriptionKey = "update", negatable = true, defaultValue = "true")
public boolean ignoredCheckUpdate;

@Parameters(index = "0..*", hidden = true)
public String[] args;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/powernukkitx/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public final class Main {
static Timer timer = null;
public static boolean pnxRunning = false;

public static void main(String[] args) {
AnsiConsole.systemInstall();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/cn/powernukkitx/cli/Preprocessor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.powernukkitx.cli;

import cn.powernukkitx.cli.util.CheckUpdateUtil;
import picocli.CommandLine.*;

import java.util.List;
Expand All @@ -12,6 +13,13 @@ public void setLocale(String locale) {
Locale.setDefault(Locale.forLanguageTag(locale));
}

@Option(names = {"-u", "--update"}, descriptionKey = "update", negatable = true, defaultValue = "true")
public void setCheckUpdate(boolean update) {
if (update) {
CheckUpdateUtil.scheduleCheckUpdate(Main.getTimer());
}
}

@Unmatched
public List<String> remainder;
}
19 changes: 16 additions & 3 deletions src/main/java/cn/powernukkitx/cli/cmd/StartCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ public Integer call() {
cmdBuilder.addXxOption("UseG1GC", true);
cmdBuilder.addXxOption("UseStringDeduplication", true);
cmdBuilder.addXxOption("EnableJVMCI", true);
if (isLowVersionGraalVM(java.getInfo()) != GraalStatus.NotFound) {
var graalStatus = getGraalStatus(java.getInfo());
if (graalStatus != GraalStatus.NotFound) {
cmdBuilder.addXxOption("UseJVMCICompiler", true);
if (graalStatus != GraalStatus.LowVersion && graalStatus != GraalStatus.Standard) {
cmdBuilder.addXxOption("UseJVMCINativeLibrary", true);
}
} else {
var graalJIT = new GraalJITLocator().locate();
if (graalJIT.size() > 1) {
Expand Down Expand Up @@ -149,14 +153,18 @@ public Integer call() {
enum GraalStatus {
NotFound,
Standard,
Oracle,
LowVersion
}

private GraalStatus isLowVersionGraalVM(JavaLocator.JavaInfo javaInfo) {
private GraalStatus getGraalStatus(JavaLocator.JavaInfo javaInfo) {
var vendor = javaInfo.getVendor().toLowerCase();
if (!vendor.contains("graal")) {
return GraalStatus.NotFound;
}
if (vendor.contains("oracle graalvm")) {
return GraalStatus.Oracle;
}
var index = vendor.indexOf("2", vendor.indexOf("graalvm"));
if (index == -1) {
return GraalStatus.NotFound;
Expand All @@ -177,9 +185,11 @@ private int start() {
builder.inheritIO();
}
var process = builder.start();
Main.pnxRunning = true;
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (process.isAlive()) {
process.destroy();
Main.pnxRunning = false;
}
}));
if (useStdinFile) {
Expand Down Expand Up @@ -211,9 +221,12 @@ public void run() {
}, 1000, 1000);
}
}
return process.waitFor();
int exitValue = process.waitFor();
Main.pnxRunning = false;
return exitValue;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
Main.pnxRunning = false;
return 1;
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/cn/powernukkitx/cli/data/locator/JavaLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,19 @@ public List<Location<JavaInfo>> locate() {
final List<File> binDirs = new ArrayList<>();
{ // 探测当前运行环境
var javaHome = System.getProperty("java.home");
if (javaHome != null)
binDirs.add(new File(javaHome));
if (javaHome != null) {
var binDir = new File(javaHome);
if (binDir.exists() && binDir.isDirectory()) {
if (isJavaDir(binDir)) {
binDirs.add(binDir);
} else {
final File innerBinDir = new File(binDir, "bin");
if (isJavaDir(innerBinDir)) {
binDirs.add(innerBinDir);
}
}
}
}
}
{ // 当前文件夹下缓存探测
if (localJavaDir.exists()) {
Expand Down Expand Up @@ -90,7 +101,7 @@ public List<Location<JavaInfo>> locate() {
if (each == null || "".equals(each)) {
continue;
}
final File binDir = new File(CLIConstant.userDir, each);
final File binDir = new File(each);
if (binDir.exists() && binDir.isDirectory()) {
if (isJavaDir(binDir)) {
binDirs.add(binDir);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/powernukkitx/cli/share/CLIConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;

public interface CLIConstant {
String version = "0.1.5";
String version = "0.1.6";
List<String> authors = List.of("超神的冰凉");
File userDir = new File(System.getProperty("user.dir"));
File programDir = new File(OSUtils.getProgramDir());
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/cn/powernukkitx/cli/util/CheckUpdateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cn.powernukkitx.cli.util;

import cn.powernukkitx.cli.share.CLIConstant;
import com.google.gson.JsonParser;
import org.fusesource.jansi.Ansi;
import org.jetbrains.annotations.NotNull;

import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

public final class CheckUpdateUtil {
private CheckUpdateUtil() {

}

public static void scheduleCheckUpdate(@NotNull Timer timer) {
timer.schedule(new TimerTask() {
@Override
public void run() {
var apiURL = HttpUtils.getAPIUrl("/git/latest-release/PowerNukkitX/PNX-CLI");
var client = HttpUtils.getClient();
try {
var result = client.send(HttpRequest.newBuilder(URI.create(apiURL)).GET()
.timeout(Duration.ofSeconds(10)).build(), HttpResponse.BodyHandlers.ofString()).body();
var jsonObject = JsonParser.parseString(result).getAsJsonObject();
var tagName = jsonObject.get("tagName").getAsString();
if (compareVersion(tagName, CLIConstant.version) > 0) {
var str = ResourceBundle.getBundle("cn.powernukkitx.cli.util.CheckUpdate")
.getString("new-version-detected");
Logger.info(Ansi.ansi().fgBrightYellow().a(str.formatted(tagName)).fgDefault());
}
} catch (Exception ignored) {
// ignored
}
}
}, 1000 * 30, 1000 * 60 * 60 * 24);
}

/**
* 比较版本号(如0.1.2)
*
* @param a 版本号a
* @param b 版本号b
* @return a > b 返回1,a = b 返回0,a < b 返回-1
*/
public static int compareVersion(@NotNull String a, @NotNull String b) {
var aSplit = a.split("\\.");
var bSplit = b.split("\\.");
var length = Math.min(aSplit.length, bSplit.length);
for (var i = 0; i < length; i++) {
var ai = Integer.parseInt(aSplit[i]);
var bi = Integer.parseInt(bSplit[i]);
if (ai > bi) {
return 1;
} else if (ai < bi) {
return -1;
}
}
if (aSplit.length > bSplit.length) {
return 1;
} else if (aSplit.length < bSplit.length) {
return -1;
}
return 0;
}
}
14 changes: 14 additions & 0 deletions src/main/java/cn/powernukkitx/cli/util/Logger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.powernukkitx.cli.util;

import cn.powernukkitx.cli.Main;
import org.fusesource.jansi.Ansi;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -28,44 +29,57 @@ public static void clearProgress() {
}
}

public static void ensureRunningFormatBefore() {
if (Main.pnxRunning) {
System.out.print(ansi().eraseLine().cursorLeft(2));
}
}

public static void info(String msg) {
clearProgress();
ensureRunningFormatBefore();
System.out.println(ansi().fgCyan().a(LocalDateTime.now().format(formatter))
.fgDefault().a(" [").fgBlue().a("INFO ").fgDefault().a("] ").fgDefault().a(msg).reset());
}

public static void info(Ansi msg) {
clearProgress();
ensureRunningFormatBefore();
System.out.println(ansi().fgCyan().a(LocalDateTime.now().format(formatter))
.fgDefault().a(" [").fgBlue().a("INFO ").fgDefault().a("] ").fgDefault().a(msg).reset());
}

public static void warn(String msg) {
clearProgress();
ensureRunningFormatBefore();
System.out.println(ansi().fgCyan().a(LocalDateTime.now().format(formatter))
.fgDefault().a(" [").fgRed().a("WARN ").fgDefault().a("] ").fgDefault().a(msg).reset());
}

public static void warn(Ansi msg) {
clearProgress();
ensureRunningFormatBefore();
System.out.println(ansi().fgCyan().a(LocalDateTime.now().format(formatter))
.fgDefault().a(" [").fgRed().a("WARN ").fgDefault().a("] ").fgDefault().a(msg).reset());
}

public static void error(String msg) {
clearProgress();
ensureRunningFormatBefore();
System.out.println(ansi().fgCyan().a(LocalDateTime.now().format(formatter))
.fgDefault().a(" [").fgBrightRed().a("ERROR").fgDefault().a("] ").fgDefault().a(msg).reset());
}

public static void error(Ansi msg) {
clearProgress();
ensureRunningFormatBefore();
System.out.println(ansi().fgCyan().a(LocalDateTime.now().format(formatter))
.fgDefault().a(" [").fgBrightRed().a("ERROR").fgDefault().a("] ").fgDefault().a(msg).reset());
}

public static void raw(String msg) {
clearProgress();
ensureRunningFormatBefore();
System.out.print(msg);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/cn/powernukkitx/cli/App.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lang = Select the language to use temporarily to execute this command.
args = If you want to start pnx with some arguments, you should use `%1s start YOUR_ARGS` instead.
config-path = Select which config file will be used. (Default pnx-cli-config.ini)
invalid-file = Cannot find or no IO permission of config file: %1s.
invalid-file = Cannot find or no IO permission of config file: %1s.
update=Check for PNX-CLI updates.
9 changes: 5 additions & 4 deletions src/main/resources/cn/powernukkitx/cli/App_zh.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lang = \u9009\u62e9\u6267\u884c\u6b64\u6b21\u547d\u4ee4\u6240\u7528\u7684\u8bed\u8a00\u4ee3\u7801\u3002
args = \u82e5\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u53c2\u6570\u542f\u52a8\u0050\u004e\u0058\uff0c\u0020\u8bf7\u4f7f\u7528`%1s start YOUR_ARGS`\u547d\u4ee4\u3002
config-path = \u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\uff08\u9ed8\u8ba4\u4e3apnx-cli-config.ini\uff09
invalid-file = \u627e\u4e0d\u5230\u6216\u6ca1\u6709\u914d\u7f6e\u6587\u4ef6 %1s \u7684\u0049\u004f\u6743\u9650
lang = \u9009\u62E9\u6267\u884C\u6B64\u6B21\u547D\u4EE4\u6240\u7528\u7684\u8BED\u8A00\u4EE3\u7801\u3002
args = \u82E5\u8981\u4F7F\u7528\u81EA\u5B9A\u4E49\u53C2\u6570\u542F\u52A8PNX\uFF0C\u0020\u8BF7\u4F7F\u7528`%1s start YOUR_ARGS`\u547D\u4EE4\u3002
config-path = \u6307\u5B9A\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84\uFF08\u9ED8\u8BA4\u4E3Apnx-cli-config.ini\uFF09
invalid-file = \u627E\u4E0D\u5230\u6216\u6CA1\u6709\u914D\u7F6E\u6587\u4EF6 %1s \u7684IO\u6743\u9650
update=\u662F\u5426\u68C0\u67E5PNX-CLI\u66F4\u65B0\u3002
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lang = Select the language to use temporarily to execute this command.
lang = Select the language to use temporarily to execute this command.
update=Select the language to use temporarily to execute this command.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lang = \u9009\u62e9\u6267\u884c\u6b64\u6b21\u547d\u4ee4\u6240\u7528\u7684\u8bed\u8a00\u4ee3\u7801\u3002
lang = \u9009\u62E9\u6267\u884C\u6B64\u6B21\u547D\u4EE4\u6240\u7528\u7684\u8BED\u8A00\u4EE3\u7801\u3002
update=\u662F\u5426\u68C0\u67E5PNX-CLI\u66F4\u65B0\u3002
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new-version-detected=New version %1s of PNX-CLI has been released, you can get the new version on GitHub.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new-version-detected=\u68C0\u6D4B\u5230PNX-CLI\u6709\u65B0\u7248\u672C%1s\uFF0C\u8BF7\u60A8\u5C3D\u5FEB\u66F4\u65B0\u3002

0 comments on commit 0d19f6f

Please sign in to comment.