Updater now uses GitHub Releases and API

This commit is contained in:
Matt 2016-02-29 11:15:51 -05:00
parent 43b7a7aba8
commit b0a3f70cbd
5 changed files with 288 additions and 140 deletions

View File

@ -1,5 +1,7 @@
dependencies { dependencies {
testCompile 'junit:junit:4.12'
compile 'org.yaml:snakeyaml:1.16' compile 'org.yaml:snakeyaml:1.16'
compile 'com.google.code.gson:gson:2.2.4'
} }
sourceCompatibility = 1.7 sourceCompatibility = 1.7

View File

@ -8,7 +8,11 @@ import com.intellectualcrafters.plot.commands.WE_Anywhere;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Configuration; import com.intellectualcrafters.plot.config.Configuration;
import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.*; import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.database.Database;
import com.intellectualcrafters.plot.database.MySQL;
import com.intellectualcrafters.plot.database.SQLManager;
import com.intellectualcrafters.plot.database.SQLite;
import com.intellectualcrafters.plot.flag.AbstractFlag; import com.intellectualcrafters.plot.flag.AbstractFlag;
import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.flag.FlagValue; import com.intellectualcrafters.plot.flag.FlagValue;
@ -16,22 +20,67 @@ import com.intellectualcrafters.plot.generator.GeneratorWrapper;
import com.intellectualcrafters.plot.generator.HybridPlotWorld; import com.intellectualcrafters.plot.generator.HybridPlotWorld;
import com.intellectualcrafters.plot.generator.HybridUtils; import com.intellectualcrafters.plot.generator.HybridUtils;
import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; import com.intellectualcrafters.plot.generator.IndependentPlotGenerator;
import com.intellectualcrafters.plot.object.*; import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.util.*; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotAnalysis;
import com.intellectualcrafters.plot.object.PlotArea;
import com.intellectualcrafters.plot.object.PlotCluster;
import com.intellectualcrafters.plot.object.PlotFilter;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.RegionWrapper;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.AbstractTitle;
import com.intellectualcrafters.plot.util.ChatManager;
import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.CommentManager;
import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.InventoryUtil;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.PlotGamemode;
import com.intellectualcrafters.plot.util.PlotWeather;
import com.intellectualcrafters.plot.util.ReflectionUtils;
import com.intellectualcrafters.plot.util.SchematicHandler;
import com.intellectualcrafters.plot.util.SetQueue;
import com.intellectualcrafters.plot.util.SetupUtils;
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.intellectualcrafters.plot.util.WorldUtil;
import com.intellectualcrafters.plot.util.area.QuadMap; import com.intellectualcrafters.plot.util.area.QuadMap;
import com.plotsquared.listener.WESubscriber; import com.plotsquared.listener.WESubscriber;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import java.io.*; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.file.Files; import java.nio.file.Files;
import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
@ -47,7 +96,7 @@ public class PS {
// protected static: // protected static:
private static PS instance; private static PS instance;
private HashSet<Integer> plotareaHashCheck = new HashSet<Integer>(); private HashSet<Integer> plotareaHashCheck = new HashSet<>();
private boolean plotareaHasCollision = false; private boolean plotareaHasCollision = false;
/** /**
* All plot areas (quick global access) * All plot areas (quick global access)
@ -81,7 +130,6 @@ public class PS {
private String PLATFORM = null; private String PLATFORM = null;
private String LAST_VERSION; private String LAST_VERSION;
private Database database; private Database database;
private Connection connection;
private Thread thread; private Thread thread;
/** /**
@ -162,7 +210,7 @@ public class PS {
@Override @Override
public void run() { public void run() {
for (final Plot plot : getPlots()) { for (final Plot plot : getPlots()) {
if ((plot.hasOwner()) && (plot.temp != -1)) { if (plot.hasOwner() && (plot.temp != -1)) {
if (UUIDHandler.getName(plot.owner) == null) { if (UUIDHandler.getName(plot.owner) == null) {
UUIDHandler.implementation.unknown.add(plot.owner); UUIDHandler.implementation.unknown.add(plot.owner);
} }
@ -219,7 +267,7 @@ public class PS {
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(new Runnable() {
@Override @Override
public void run() { public void run() {
final URL url = getUpdate(); final URL url = Updater.getUpdate();
if (url != null) { if (url != null) {
update = url; update = url;
} else if ((LAST_VERSION != null) && !StringMan.join(VERSION, ".").equals(LAST_VERSION)) { } else if ((LAST_VERSION != null) && !StringMan.join(VERSION, ".").equals(LAST_VERSION)) {
@ -332,8 +380,9 @@ public class PS {
} }
/** /**
* Get the platform this is running on (Bukkit, Sponge) * <p>Get the server platform this plugin is running on this is running on.
* @return * This will be either <b>Bukkit</b> or <b>Sponge</b></p>
* @return The server platform
*/ */
public String getPlatform() { public String getPlatform() {
return PLATFORM; return PLATFORM;
@ -343,7 +392,7 @@ public class PS {
* Get the database object * Get the database object
* *
* @return Database object * @return Database object
* @see #getConnection() Get the database connection * @see Database#getConnection() To get the database connection
*/ */
public Database getDatabase() { public Database getDatabase() {
return database; return database;
@ -1660,77 +1709,33 @@ public class PS {
} }
public String normalisedVersion(final String version) { public String normalisedVersion(final String version) {
return normalisedVersion(version, ".", 4); final String[] split = Pattern.compile(".", Pattern.LITERAL).split(version);
}
public String normalisedVersion(final String version, final String sep, final int maxWidth) {
final String[] split = Pattern.compile(sep, Pattern.LITERAL).split(version);
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
for (final String s : split) { for (final String s : split) {
sb.append(String.format("%" + maxWidth + 's', s)); sb.append(String.format("%" + 4 + 's', s));
} }
return sb.toString(); return sb.toString();
} }
/**
* Gets the default update URL, or null if the plugin is up to date
* @return
*/
public URL getUpdate() {
final String pom = "https://raw.githubusercontent.com/IntellectualSites/PlotSquared/master/pom.xml";
try {
final URL page = new URL(pom);
final URLConnection con = page.openConnection();
final String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
con.addRequestProperty("User-Agent", agent);
String line;
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
while ((line = in.readLine()) != null) {
line = line.trim();
if (line.startsWith("<version>")) {
line = line.replaceAll("[^\\d.]", "");
break;
}
}
}
if (!canUpdate(config.getString("version"), line)) {
PS.debug("&7PlotSquared is already up to date!");
return null;
}
String dl = "https://raw.githubusercontent.com/IntellectualSites/PlotSquared/master/target/PlotSquared-${PLATFORM}.jar";
dl = dl.replaceAll(Pattern.quote("${PLATFORM}"), getPlatform());
log("&6PlotSquared v" + line + " is available:");
log("&8 - &3Use: &7/plot update");
log("&8 - &3Or: &7" + dl);
return new URL(dl);
} catch (IOException e) {
e.printStackTrace();
log("&dCould not check for updates (0)");
log("&7 - Manually check for updates: " + pom);
}
return null;
}
public boolean update(final PlotPlayer sender, final URL url) { public boolean update(final PlotPlayer sender, final URL url) {
if (url == null) {
return false;
}
try { try {
final String name = FILE.getName(); final String name = FILE.getName();
final File newJar = new File("plugins/update/" + name); final File newJar = new File("plugins/update/" + name);
MainUtil.sendMessage(sender, "$1Downloading from provided URL: &7" + url); MainUtil.sendMessage(sender, "$1Downloading from provided URL: &7" + url);
MainUtil.sendMessage(sender, "$2 - User-Agent: " + "Mozilla/4.0");
final URLConnection con = url.openConnection(); final URLConnection con = url.openConnection();
con.addRequestProperty("User-Agent", "Mozilla/4.0"); try (InputStream stream = con.getInputStream()) {
final InputStream stream = con.getInputStream();
final File parent = newJar.getParentFile(); final File parent = newJar.getParentFile();
if (!parent.exists()) { if (!parent.exists()) {
parent.mkdirs(); parent.mkdirs();
} }
MainUtil.sendMessage(sender, "$2 - Output: " + newJar); MainUtil.sendMessage(sender, "$2 - Output: " + newJar);
newJar.delete(); if (newJar.delete()) {
MainUtil.sendMessage(sender, "Failed to update PlotSquared");
MainUtil.sendMessage(sender, "Jar file failed to delete.");
MainUtil.sendMessage(sender, " - Please update manually");
}
Files.copy(stream, newJar.toPath()); Files.copy(stream, newJar.toPath());
stream.close(); }
MainUtil.sendMessage(sender, "$1The update will take effect when the server is restarted next"); MainUtil.sendMessage(sender, "$1The update will take effect when the server is restarted next");
return true; return true;
} catch (IOException e) { } catch (IOException e) {
@ -1743,14 +1748,6 @@ public class PS {
return false; return false;
} }
/**
* Get the database connection
* @return The database connection
*/
public Connection getConnection() {
return connection;
}
/** /**
* Copy a file from inside the jar to a location * Copy a file from inside the jar to a location
* @param file Name of the file inside PlotSquared.jar * @param file Name of the file inside PlotSquared.jar
@ -2350,7 +2347,7 @@ public class PS {
/** /**
* Show startup debug information * Show startup debug information
*/ */
public void showDebug() { private void showDebug() {
if (Settings.DEBUG) { if (Settings.DEBUG) {
final Map<String, String> settings = new HashMap<>(); final Map<String, String> settings = new HashMap<>();
settings.put("Kill Road Mobs", "" + Settings.KILL_ROAD_MOBS); settings.put("Kill Road Mobs", "" + Settings.KILL_ROAD_MOBS);

View File

@ -0,0 +1,85 @@
package com.intellectualcrafters.plot;
import static com.intellectualcrafters.plot.PS.log;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class Updater {
private static String readUrl(String urlString) {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder buffer = new StringBuilder();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1) {
buffer.append(chars, 0, read);
}
return buffer.toString();
} catch (IOException e) {
log("&dCould not check for updates (0)");
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static URL getUpdate() {
String str = readUrl("https://api.github.com/repos/IntellectualSites/PlotSquared/releases/latest");
Gson gson = new Gson();
URL url = null;
Release release = gson.fromJson(str, Release.class);
String downloadURL = String.format("PlotSquared-%s%n.jar", PS.get().getPlatform());
List<Release.Asset> assets = release.assets;
for (Release.Asset asset : assets) {
if (asset.name.equals(downloadURL)) {
try {
url = new URL(asset.downloadUrl);
break;
} catch (MalformedURLException e) {
e.printStackTrace();
log("&dCould not check for updates (0)");
log("&7 - Manually check for updates: https://github.com/IntellectualSites/PlotSquared/releases");
}
}
}
if (!PS.get().canUpdate(PS.get().config.getString("version"), release.name)) {
PS.debug("&7PlotSquared is already up to date!");
return null;
}
log("&6PlotSquared " + release.tagName + " is available:");
log("&8 - &3Use: &7/plot update");
log("&8 - &3Or: &7" + downloadURL);
return url;
}
private static class Release {
public String name;
@SerializedName("tag_name") String tagName;
List<Asset> assets;
static class Asset {
public String name;
@SerializedName("browser_download_url") String downloadUrl;
}
}
}

View File

@ -20,15 +20,15 @@
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.commands; package com.intellectualcrafters.plot.commands;
import java.net.MalformedURLException;
import java.net.URL;
import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.plotsquared.general.commands.CommandDeclaration; import com.plotsquared.general.commands.CommandDeclaration;
import java.net.MalformedURLException;
import java.net.URL;
@CommandDeclaration( @CommandDeclaration(
command = "update", command = "update",
permission = "plots.admin.command.update", permission = "plots.admin.command.update",

View File

@ -0,0 +1,64 @@
package com.intellectualcrafters.plot;
import static com.intellectualcrafters.plot.PS.log;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
public class UpdaterTest {
@org.junit.Test
public void getUpdate() throws Exception {
String str = null;
BufferedReader reader = null;
try {
URL url = new URL("https://api.github.com/repos/IntellectualSites/PlotSquared/releases/latest");
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder buffer = new StringBuilder();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1) {
buffer.append(chars, 0, read);
}
str = buffer.toString();
} catch (IOException e) {
log("&dCould not check for updates (0)");
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (str == null) {
return;
}
Gson gson = new Gson();
Release release = gson.fromJson(str, Release.class);
System.out.println(release.name);
for (Release.Assets asset : release.assets) {
System.out.println(asset.name);
System.out.println(asset.downloadUrl);
}
}
private static class Release {
String name;
List<Assets> assets;
private static class Assets {
String name;
@SerializedName("browser_download_url") String downloadUrl;
}
}
}