Improved UpdatePlugin

- Added branch checking
- Added IsTestServer()
- Restricted production updating to the master branch
This commit is contained in:
Norbi Peti 2016-12-22 16:41:50 +01:00
parent bceb57c522
commit 8ad5504527
2 changed files with 75 additions and 43 deletions

View file

@ -16,6 +16,7 @@ import net.milkbowl.vault.permission.Permission;
public class MainPlugin extends JavaPlugin {
public static MainPlugin Instance;
public static Permission permission;
public static boolean Test;
private PluginDescriptionFile pdfFile;
private Logger logger;
@ -27,6 +28,8 @@ public class MainPlugin extends JavaPlugin {
pdfFile = getDescription();
logger = getLogger();
setupPermissions();
Test = getConfig().getBoolean("test", true);
saveConfig();
TBMCChatAPI.AddCommand(this, UpdatePluginCommand.class);
TBMCChatAPI.AddCommand(this, ScheduledRestartCommand.class);
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);

View file

@ -1,17 +1,10 @@
package buttondevteam.lib;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import java.util.Map.Entry;
import org.apache.commons.io.FileUtils;
@ -22,10 +15,9 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import buttondevteam.core.MainPlugin;
public final class TBMCCoreAPI {
static List<String> coders = new ArrayList<String>() {
@ -36,7 +28,9 @@ public final class TBMCCoreAPI {
add("iie");
add("thewindmillman");
add("mayskam1995");
}};
}
};
/**
* Updates or installs the specified plugin. The plugin must use Maven.
*
@ -73,30 +67,41 @@ public final class TBMCCoreAPI {
error(sender, "Can't find plugin: " + name);
return;
}
info(sender, "Updating TBMC plugin: " + correctname + " from " + branch);
info(sender, "Checking branch name...");
if (TBMCCoreAPI.IsTestServer() && !branch.equalsIgnoreCase("master")) {
error(sender, "The server is in production mode, updating only allowed from master!");
return;
}
Optional<String> correctbranch = GetPluginBranches(correctname).stream().filter(b -> b.equalsIgnoreCase(branch))
.findAny();
if (!correctbranch.isPresent()) {
error(sender, "Can't find branch \"" + branch + "\" for plugin \"" + correctname + "\"");
return;
}
info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get());
URL url;
File result = new File("plugins/" + correctname + ".jar_tmp");
File finalresult = new File("plugins/" + correctname + ".jar");
try {
url = new URL("https://jitpack.io/com/github/TBMCPlugins/"
+ (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/" + branch
+ "-SNAPSHOT/" + correctname + "-" + branch + "-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well
+ (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/"
+ correctbranch.get() + "-SNAPSHOT/" + correctname + "-" + correctbranch.get() + "-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well
FileUtils.copyURLToFile(url, result);
if (!result.exists() || result.length() < 25) {
result.delete();
error(sender, "The downloaded JAR for " + correctname + " from " + branch
error(sender, "The downloaded JAR for " + correctname + " from " + correctbranch.get()
+ " is too small (smnaller than 25 bytes). Am I downloading from the right place?");
return;
} else {
finalresult.delete(); // Attempt to support Windows
Files.move(result.toPath(), finalresult.toPath(), StandardCopyOption.REPLACE_EXISTING);
info(sender, "Updating plugin " + correctname + " from " + branch + " done!");
info(sender, "Updating plugin " + correctname + " from " + correctbranch.get() + " done!");
}
} catch (FileNotFoundException e) {
error(sender,
"Can't find JAR for " + correctname + " from " + branch
"Can't find JAR for " + correctname + " from " + correctbranch.get()
+ ", the build probably failed. Build log (scroll to bottom):" + "\n"
+ "https://jitpack.io/com/github/TBMCPlugins/" + correctname + "/" + branch
+ "https://jitpack.io/com/github/TBMCPlugins/" + correctname + "/" + correctbranch.get()
+ "-SNAPSHOT/build.log\nIf you'd like to rebuild the same commit, go to:\nhttps://jitpack.io/#TBMCPlugins/"
+ correctname + "\nAnd delete the newest build.");
} catch (IOException e) {
@ -139,6 +144,26 @@ public final class TBMCCoreAPI {
return ret;
}
/**
* Retrieves all the branches from the plugin repository.
*
* @return A list of names
*/
public static List<String> GetPluginBranches(String plugin) {
List<String> ret = new ArrayList<>();
try {
String resp = DownloadString("https://api.github.com/repos/TBMCPlugins/" + plugin + "/branches");
JsonArray arr = new JsonParser().parse(resp).getAsJsonArray();
for (JsonElement obj : arr) {
JsonObject jobj = obj.getAsJsonObject();
ret.add(jobj.get("name").getAsString());
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static String DownloadString(String urlstr) throws MalformedURLException, IOException {
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
@ -154,7 +179,6 @@ public final class TBMCCoreAPI {
private static HashMap<String, Throwable> exceptionsToSend = new HashMap<>();
private static List<String> debugMessagesToSend = new ArrayList<>();
/**
* Send exception to the {@link TBMCExceptionEvent}.
*
@ -166,6 +190,7 @@ public final class TBMCCoreAPI {
public static void SendException(String sourcemsg, Throwable e) {
SendException(sourcemsg, e, false);
}
public static void SendException(String sourcemsg, Throwable e, boolean debugPotato) {
SendUnsentExceptions();
TBMCExceptionEvent event = new TBMCExceptionEvent(sourcemsg, e);
@ -180,7 +205,8 @@ public final class TBMCCoreAPI {
if (coders.contains(player.getName())) {
devsOnline.add(player);
}
};
}
;
if (!devsOnline.isEmpty()) {
DebugPotato potato = new DebugPotato()
.setMessage(new String[] { //
@ -190,8 +216,7 @@ public final class TBMCCoreAPI {
.setType(e instanceof IOException ? "Throwable Potato"
: e instanceof ClassCastException ? "Squished Potato"
: e instanceof NullPointerException ? "Plain Potato"
: e instanceof StackOverflowError ? "Chips"
: "Error Potato");
: e instanceof StackOverflowError ? "Chips" : "Error Potato");
for (Player dev : devsOnline) {
potato.Send(dev);
}
@ -247,4 +272,8 @@ public final class TBMCCoreAPI {
debugMessagesToSend.remove(message);
}
}
public static boolean IsTestServer() {
return MainPlugin.Test;
}
}