From 791366f5f020b19efb03c162fcb8547c8a0fe764 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Wed, 7 Jun 2017 00:37:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E7=B1=BB=20=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/YumCore/bukkit/Log.java | 20 ++- .../pw/yumc/YumCore/update/SubscribeTask.java | 31 +++- .../pw/yumc/YumCore/plugin/FakePlugin.java | 146 ++++++++++++++++++ .../YumCore/update/SubscribeTaskTest.java | 32 ++++ 4 files changed, 217 insertions(+), 12 deletions(-) create mode 100644 src/test/java/pw/yumc/YumCore/plugin/FakePlugin.java create mode 100644 src/test/java/pw/yumc/YumCore/update/SubscribeTaskTest.java diff --git a/src/main/java/pw/yumc/YumCore/bukkit/Log.java b/src/main/java/pw/yumc/YumCore/bukkit/Log.java index 8f624f5..afe850a 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/Log.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/Log.java @@ -23,10 +23,22 @@ import pw.yumc.YumCore.annotation.NotProguard; public class Log { private static boolean fullDebug = new File(String.format("plugins%1$sYumCore%1$sfulldebug", File.separatorChar)).exists(); private static boolean globalDebug = new File(String.format("plugins%1$sYumCore%1$sdebug", File.separatorChar)).exists(); - private static boolean debug = globalDebug || P.getDescription().getVersion().contains("DEV"); - private static Logger logger = P.instance.getLogger(); - private static CommandSender console = Bukkit.getConsoleSender(); - private static String prefix = String.format("§6[§b%s§6]§r ", P.instance.getName()); + private static boolean debug; + private static Logger logger; + private static CommandSender console; + private static String prefix; + static { + try { + debug = globalDebug || P.getDescription().getVersion().contains("DEV"); + logger = P.instance.getLogger(); + console = Bukkit.getConsoleSender(); + prefix = String.format("§6[§b%s§6]§r ", P.instance.getName()); + } catch (Throwable ex) { + logger = Logger.getLogger("YumCore"); + debug = true; + d(ex); + } + } private Log() { } diff --git a/src/main/java/pw/yumc/YumCore/update/SubscribeTask.java b/src/main/java/pw/yumc/YumCore/update/SubscribeTask.java index d21e7e8..233d2ea 100644 --- a/src/main/java/pw/yumc/YumCore/update/SubscribeTask.java +++ b/src/main/java/pw/yumc/YumCore/update/SubscribeTask.java @@ -44,7 +44,6 @@ public class SubscribeTask implements Runnable, Listener { * 插件实例 */ private static JavaPlugin instance; - private static String version; static { try { @@ -52,7 +51,6 @@ public class SubscribeTask implements Runnable, Listener { Field field = pluginClassLoader.getClass().getDeclaredField("plugin"); field.setAccessible(true); instance = (JavaPlugin) field.get(pluginClassLoader); - version = instance.getDescription().getVersion(); } catch (Exception e) { Log.d(e); } @@ -114,7 +112,7 @@ public class SubscribeTask implements Runnable, Listener { */ public SubscribeTask(String branch, boolean isSecret, UpdateType type) { updateFile = new UpdateFile(instance); - versionInfo = new VersionInfo(branch, isSecret); + versionInfo = new VersionInfo(instance, branch, isSecret); updateType = type; if (instance.isEnabled()) { Bukkit.getPluginManager().registerEvents(this, instance); @@ -150,7 +148,7 @@ public class SubscribeTask implements Runnable, Listener { } } updateFile.update(updateType.getDownloadUrl(instance, result)); - Log.d(Encrypt.decode("嘊⚲哀嘖⚶哅嘣⚩咖嗕♧哏嗕⚸咁嘨♢咰嘤♢哒嗚⚵呼嗣♰咊"), instance.getName(), version.split("-")[0], result); + Log.d(Encrypt.decode("嘊⚲哀嘖⚶哅嘣⚩咖嗕♧哏嗕⚸咁嘨♢咰嘤♢哒嗚⚵呼嗣♰咊"), instance.getName(), versionInfo.getVersion(), result); versionInfo.notify(Bukkit.getConsoleSender()); } } catch (Exception e) { @@ -245,6 +243,7 @@ public class SubscribeTask implements Runnable, Listener { } public static class VersionInfo { + /** * 直链POM */ @@ -256,18 +255,34 @@ public class SubscribeTask implements Runnable, Listener { private String pom = Encrypt.decode("嘝⚶哐嘥♼咋嗤⚥哅嗣⚻哑嘢⚥咊嘥⚹咋嘟⚱咾嗤♧哏嗤⚮咽嘨⚶咯嘪⚥咿嘚⚵哏嘛⚷哈嗷⚷哅嘡⚦咋嘖⚴哐嘞⚨咽嘘⚶咋嘥⚱哉嗣⚺哉嘡"); // private static String pom = "http://ci.yumc.pw/job/%s/lastSuccessfulBuild/artifact/pom.xml"; + /** + * 插件名称 + */ + private final String name; + /** + * 插件版本 + */ + private final String version; /** * 插件信息地址 */ private String info; - /** * POM文件文档 */ private Document document; - public VersionInfo(String branch, boolean isSecret) { - info = String.format(isSecret ? pom : url, instance.getName(), branch); + public VersionInfo(Plugin plugin, String branch, boolean isSecret) { + this.name = plugin.getName(); + this.version = plugin.getDescription().getVersion().split("-")[0]; + this.info = String.format(isSecret ? pom : url, name, branch); + } + + /** + * @return 插件版本 + */ + public String getVersion() { + return version; } /** @@ -331,7 +346,7 @@ public class SubscribeTask implements Runnable, Listener { * 命令接受者 */ public void notify(CommandSender sender) { - Log.sender(sender, "§a插件更新: §b" + instance.getName() + " §a已更新到最新版本 §bv" + getLastestVersion()); + Log.sender(sender, "§a插件更新: §b" + name + " §a已更新到最新版本 §bv" + getLastestVersion()); Log.sender(sender, "§e版本简介: §a" + getUpdateDescription()); final String[] changes = getUpdateChanges(); if (changes.length != 0) { diff --git a/src/test/java/pw/yumc/YumCore/plugin/FakePlugin.java b/src/test/java/pw/yumc/YumCore/plugin/FakePlugin.java new file mode 100644 index 0000000..23eb5cd --- /dev/null +++ b/src/test/java/pw/yumc/YumCore/plugin/FakePlugin.java @@ -0,0 +1,146 @@ +package pw.yumc.YumCore.plugin; + +import java.io.File; +import java.io.InputStream; +import java.util.List; +import java.util.logging.Logger; + +import org.bukkit.Server; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.generator.ChunkGenerator; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.PluginLoader; + +import com.avaje.ebean.EbeanServer; + +/** + * @author 喵♂呜 + * @since 2017/6/2 + */ +public class FakePlugin implements Plugin { + private String name; + private String version; + private String mainclass; + private PluginDescriptionFile descriptionFile; + + public FakePlugin(String name, String version) { + super(); + this.name = name; + this.version = version; + this.mainclass = getClass().getName(); + this.descriptionFile = new PluginDescriptionFile(name, version, mainclass); + } + + @Override + public File getDataFolder() { + return new File(name); + } + + @Override + public PluginDescriptionFile getDescription() { + return descriptionFile; + } + + @Override + public FileConfiguration getConfig() { + return null; + } + + @Override + public InputStream getResource(String s) { + return null; + } + + @Override + public void saveConfig() { + + } + + @Override + public void saveDefaultConfig() { + + } + + @Override + public void saveResource(String s, boolean b) { + + } + + @Override + public void reloadConfig() { + + } + + @Override + public PluginLoader getPluginLoader() { + return null; + } + + @Override + public Server getServer() { + return null; + } + + @Override + public boolean isEnabled() { + return false; + } + + @Override + public void onDisable() { + + } + + @Override + public void onLoad() { + + } + + @Override + public void onEnable() { + + } + + @Override + public boolean isNaggable() { + return false; + } + + @Override + public void setNaggable(boolean b) { + + } + + @Override + public EbeanServer getDatabase() { + return null; + } + + @Override + public ChunkGenerator getDefaultWorldGenerator(String s, String s1) { + return null; + } + + @Override + public Logger getLogger() { + return null; + } + + @Override + public String getName() { + return name; + } + + @Override + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + return false; + } + + @Override + public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { + return null; + } +} diff --git a/src/test/java/pw/yumc/YumCore/update/SubscribeTaskTest.java b/src/test/java/pw/yumc/YumCore/update/SubscribeTaskTest.java new file mode 100644 index 0000000..ee575f0 --- /dev/null +++ b/src/test/java/pw/yumc/YumCore/update/SubscribeTaskTest.java @@ -0,0 +1,32 @@ +package pw.yumc.YumCore.update; + +import java.io.IOException; +import java.util.logging.Logger; + +import javax.xml.parsers.ParserConfigurationException; + +import org.bukkit.plugin.Plugin; +import org.junit.Test; +import org.xml.sax.SAXException; + +import pw.yumc.YumCore.bukkit.Log; +import pw.yumc.YumCore.plugin.FakePlugin; + +/** + * @author 喵♂呜 + * @since 2017/6/1 + */ +public class SubscribeTaskTest { + private Plugin plugin = new FakePlugin("YumCore", "1.0"); + + @Test + public void test() throws IOException, SAXException, ParserConfigurationException { + SubscribeTask.VersionInfo info = new SubscribeTask.VersionInfo(plugin, "master", true); + info.update(); + Log.d("Currect Version: %s", info.getVersion()); + Log.d("New Version: %s", info.getNewVersion()); + boolean nu = info.needUpdate(info.getNewVersion(), plugin.getDescription().getVersion()); + Log.d("Need Update: %s", nu); + Log.d("File URL: %s", SubscribeTask.UpdateType.WS.getDownloadUrl(plugin, info.getNewVersion())); + } +} \ No newline at end of file