Added support for branches in UpdatePlugin

https://github.com/TBMCPlugins/ButtonCore/issues/23
This commit is contained in:
Norbi Peti 2016-12-10 23:58:34 +01:00
parent 2cf12399ec
commit 1c1d42df4b

View file

@ -39,6 +39,20 @@ public final class TBMCCoreAPI {
* The command sender (if not console, messages will be printed to console as well). * The command sender (if not console, messages will be printed to console as well).
*/ */
public static void UpdatePlugin(String name, CommandSender sender) { public static void UpdatePlugin(String name, CommandSender sender) {
UpdatePlugin(name, sender, "master");
}
/**
* Updates or installs the specified plugin from the specified branch. The plugin must use Maven.
*
* @param name
* The plugin's repository name.
* @param sender
* The command sender (if not console, messages will be printed to console as well).
* @param branch
* The branch to download the plugin from.
*/
public static void UpdatePlugin(String name, CommandSender sender, String branch) {
info(sender, "Checking plugin name..."); info(sender, "Checking plugin name...");
List<String> plugins = GetPluginNames(); List<String> plugins = GetPluginNames();
String correctname = null; String correctname = null;
@ -51,28 +65,30 @@ public final class TBMCCoreAPI {
if (correctname == null) { if (correctname == null) {
error(sender, "Can't find plugin: " + name); error(sender, "Can't find plugin: " + name);
} }
info(sender, "Updating TBMC plugin: " + correctname); info(sender, "Updating TBMC plugin: " + correctname + " from " + branch);
URL url; URL url;
File result = new File("plugins/" + correctname + ".jar_tmp"); File result = new File("plugins/" + correctname + ".jar_tmp");
File finalresult = new File("plugins/" + correctname + ".jar"); File finalresult = new File("plugins/" + correctname + ".jar");
try { try {
url = new URL("https://jitpack.io/com/github/TBMCPlugins/" url = new URL("https://jitpack.io/com/github/TBMCPlugins/"
+ (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/master-SNAPSHOT/" + (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/" + branch
+ correctname + "-master-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well + "-SNAPSHOT/" + correctname + "-" + branch + "-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well
FileUtils.copyURLToFile(url, result); FileUtils.copyURLToFile(url, result);
if (!result.exists() || result.length() < 25) { if (!result.exists() || result.length() < 25) {
result.delete(); result.delete();
error(sender, "The downloaded JAR for " + correctname error(sender, "The downloaded JAR for " + correctname + " from " + branch
+ " is too small (smnaller than 25 bytes). Am I downloading from the right place?"); + " is too small (smnaller than 25 bytes). Am I downloading from the right place?");
} else { } else {
finalresult.delete(); // Attempt to support Windows
Files.move(result.toPath(), finalresult.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.move(result.toPath(), finalresult.toPath(), StandardCopyOption.REPLACE_EXISTING);
info(sender, "Updating plugin " + correctname + " done!"); info(sender, "Updating plugin " + correctname + " from " + branch + " done!");
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
error(sender, error(sender,
"Can't find JAR for " + correctname + ", the build probably failed. Build log (scroll to bottom):" "Can't find JAR for " + correctname + " from " + branch
+ "\n" + "https://jitpack.io/com/github/TBMCPlugins/" + correctname + ", the build probably failed. Build log (scroll to bottom):" + "\n"
+ "/master-SNAPSHOT/build.log\nIf you'd like to rebuild the same commit, go to:\nhttps://jitpack.io/#TBMCPlugins/" + "https://jitpack.io/com/github/TBMCPlugins/" + correctname + "/" + branch
+ "-SNAPSHOT/build.log\nIf you'd like to rebuild the same commit, go to:\nhttps://jitpack.io/#TBMCPlugins/"
+ correctname + "\nAnd delete the newest build."); + correctname + "\nAnd delete the newest build.");
} catch (IOException e) { } catch (IOException e) {
error(sender, "IO error while updating " + correctname + "\n" + e.getMessage()); error(sender, "IO error while updating " + correctname + "\n" + e.getMessage());
@ -153,18 +169,19 @@ public final class TBMCCoreAPI {
Bukkit.getLogger().warning(sourcemsg); Bukkit.getLogger().warning(sourcemsg);
e.printStackTrace(); e.printStackTrace();
Optional<? extends Player> randomPlayer = Bukkit.getOnlinePlayers().stream().findAny(); Optional<? extends Player> randomPlayer = Bukkit.getOnlinePlayers().stream().findAny();
if (randomPlayer.isPresent()){ if (randomPlayer.isPresent()) {
DebugPotato potato = new DebugPotato() DebugPotato potato = new DebugPotato()
.setMessage(new String[] { // .setMessage(new String[] { //
"§b§o" + potatoMessages[new Random().nextInt(potatoMessages.length)], // "§b§o" + potatoMessages[new Random().nextInt(potatoMessages.length)], //
"§c§o" + sourcemsg, // "§c§o" + sourcemsg, //
"§a§oFind a dev to fix this issue" }) "§a§oFind a dev to fix this issue" })
.setType(e instanceof IOException ? "Potato on a Stick" .setType(e instanceof IOException ? "Potato on a Stick"
: e instanceof ClassCastException ? "Square Potato" : "Plain Potato"); : e instanceof ClassCastException ? "Square Potato" : "Plain Potato");
potato.Send(randomPlayer.get()); potato.Send(randomPlayer.get());
} }
} }
public static void sendDebugMessage(String debugMessage){
public static void sendDebugMessage(String debugMessage) {
SendUnsentDebugMessages(); SendUnsentDebugMessages();
TBMCDebugMessageEvent event = new TBMCDebugMessageEvent(debugMessage); TBMCDebugMessageEvent event = new TBMCDebugMessageEvent(debugMessage);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);