Last active
June 20, 2017 22:26
-
-
Save Skyost/e3389e6f40642785fccc to your computer and use it in GitHub Desktop.
An update checker based on Github.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An update checker based on Github. | |
* <br>The file "version.update" must contain a valid version. | |
* | |
* @author Skyost. | |
* @see <a href="https://gist.github.com/Skyost/e3389e6f40642785fccc">The gist's link</a> or the <a href="http://paste.skyost.eu/skyost-5554ebf124a9c">paste's link</a>. | |
*/ | |
public class GithubUpdater extends Thread { | |
public static final String UPDATER_NAME = "GithubUpdater"; | |
public static final String UPDATER_VERSION = "0.1"; | |
public static final String[] UPDATER_AUTHORS = new String[]{"Skyost"}; | |
private static final List<GithubUpdaterResultListener> listeners = new ArrayList<GithubUpdaterResultListener>(); | |
private final String githubAuthor; | |
private final String githubRepo; | |
private final String localVersion; | |
/** | |
* Creates a new <b>GithubUpdater</b> instance. | |
* | |
* @param githubAuthor The author of the Github repository. | |
* @param githubRepo The Github repository. | |
* @param localVersion The local version. | |
*/ | |
public GithubUpdater(final String githubAuthor, final String githubRepo, final String localVersion) { | |
this(githubAuthor, githubRepo, localVersion, null); | |
} | |
/** | |
* Creates a new <b>GithubUpdater</b> instance. | |
* | |
* @param githubAuthor The author of the Github repository. | |
* @param githubRepo The Github repository. | |
* @param localVersion The local version. | |
* @param listener A listener. | |
*/ | |
public GithubUpdater(final String githubAuthor, final String githubRepo, final String localVersion, final GithubUpdaterResultListener listener) { | |
this.githubAuthor = githubAuthor; | |
this.githubRepo = githubRepo; | |
this.localVersion = localVersion; | |
if(listener != null) { | |
listeners.add(listener); | |
} | |
} | |
@Override | |
public final void run() { | |
for(final GithubUpdaterResultListener listener : listeners) { | |
listener.updaterStarted(); | |
} | |
try { | |
final HttpURLConnection connection = (HttpURLConnection)new URL("https://api.github.com/repos/" + githubAuthor + "/" + githubRepo + "/contents/version.update").openConnection(); | |
connection.addRequestProperty("User-Agent", UPDATER_NAME + " by " + Utils.join(' ', UPDATER_AUTHORS) + " v" + UPDATER_VERSION); | |
final String response = connection.getResponseCode() + " " + connection.getResponseMessage(); | |
for(final GithubUpdaterResultListener listener : listeners) { | |
listener.updaterResponse(response); | |
} | |
if(!response.startsWith("2")) { | |
return; | |
} | |
final InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream()); | |
final BufferedReader bufferedReader = new BufferedReader(inputStreamReader); | |
final String remoteVersion = new String(Base64.decode(JsonObject.readFrom(bufferedReader.readLine()).get("content").asString())); | |
if(compareVersions(remoteVersion, localVersion)) { | |
for(final GithubUpdaterResultListener listener : listeners) { | |
listener.updaterUpdateAvailable(localVersion, remoteVersion); | |
} | |
} | |
else { | |
for(final GithubUpdaterResultListener listener : listeners) { | |
listener.updaterNoUpdate(localVersion, remoteVersion); | |
} | |
} | |
} | |
catch(final Exception ex) { | |
for(final GithubUpdaterResultListener listener : listeners) { | |
listener.updaterException(ex); | |
} | |
} | |
} | |
/** | |
* Compares two versions. | |
* | |
* @param version1 The version you want to compare to. | |
* @param version2 The version you want to compare with. | |
* | |
* @return <b>true</b> If <b>versionTo</b> is inferior than <b>versionWith</b>. | |
* <br><b>false</b> If <b>versionTo</b> is superior or equals to <b>versionWith</b>. | |
*/ | |
private static final boolean compareVersions(final String versionTo, final String versionWith) { | |
return normalisedVersion(versionTo, ".", 4).compareTo(normalisedVersion(versionWith, ".", 4)) > 0; | |
} | |
/** | |
* Gets the formatted name of a version. | |
* <br>Used for the method <b>compareVersions(...)</b> of this class. | |
* | |
* @param version The version you want to format. | |
* @param separator The separator between the numbers of this version. | |
* @param maxWidth The max width of the formatted version. | |
* | |
* @return A string which the formatted version of your version. | |
* | |
* @author Peter Lawrey. | |
*/ | |
private static final String normalisedVersion(final String version, final String separator, final int maxWidth) { | |
final StringBuilder stringBuilder = new StringBuilder(); | |
for(final String normalised : Pattern.compile(separator, Pattern.LITERAL).split(version)) { | |
stringBuilder.append(String.format("%" + maxWidth + 's', normalised)); | |
} | |
return stringBuilder.toString(); | |
} | |
/** | |
* Add a listener to the updater. | |
* | |
* @param listener The listener. | |
*/ | |
public static final void addListener(final GithubUpdaterResultListener listener) { | |
listeners.add(listener); | |
} | |
/** | |
* Remove a listener from the updater. | |
* | |
* @param listener The listener. | |
*/ | |
public static final void removeListener(final GithubUpdaterResultListener listener) { | |
listeners.remove(listener); | |
} | |
public interface GithubUpdaterResultListener { | |
/** | |
* When the updater starts. | |
*/ | |
public void updaterStarted(); | |
/** | |
* When an Exception occurs. | |
* | |
* @param ex | |
*/ | |
public void updaterException(final Exception ex); | |
/** | |
* The response of the request. | |
* | |
* @param response The response. | |
*/ | |
public void updaterResponse(final String response); | |
/** | |
* If an update is available. | |
* | |
* @param localVersion The local version (used to create the updater). | |
* @param remoteVersion The remove version. | |
*/ | |
public void updaterUpdateAvailable(final String localVersion, final String remoteVersion); | |
/** | |
* If there is no update. | |
* | |
* @param localVersion The local version (used to create the updater). | |
* @param remoteVersion The remove version. | |
*/ | |
public void updaterNoUpdate(final String localVersion, final String remoteVersion); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment