-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from TheProgramSrc/feat/update_checker
Module Downloader & Update Checker: * Added GitHub update checker * Added method to download modules from SimpleCoreAPI.kt
- Loading branch information
Showing
5 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
74 changes: 74 additions & 0 deletions
74
src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateChecker.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package xyz.theprogramsrc.simplecoreapi.global | ||
|
||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParser | ||
import java.net.URL | ||
import java.time.Instant | ||
import java.time.format.DateTimeFormatter | ||
import java.util.logging.Logger | ||
|
||
/** | ||
* Representation of the GitHub Update Checker | ||
* @param logger The logger to use | ||
* @param repo The repository to check | ||
* @param currentVersion the current version (tag name) of the product | ||
*/ | ||
class GitHubUpdateChecker(val logger: Logger, val repo: String, val currentVersion: String, val latestReleaseTag: String = "latest") { | ||
|
||
private var lastCheck = 0L | ||
private var lastCheckResult = false | ||
private var lastRequest = 0L | ||
private var latestData = JsonObject() | ||
private val current = if(currentVersion.startsWith("v")) currentVersion else "v$currentVersion" | ||
|
||
/** | ||
* Checks if there is an update available and prints | ||
* a message if there is one asking the end user to | ||
* update the product. | ||
*/ | ||
fun checkWithPrint() { | ||
val latestData = getLatestReleaseData() | ||
val latestVersion = latestData.get("tag_name").asString | ||
if(checkForUpdates()){ | ||
logger.info("Please update (from $current to $latestVersion)! Download it now from here: https://github.com/$repo/releases/tag/$latestVersion") | ||
} | ||
} | ||
|
||
/** | ||
* Checks if there is an update available | ||
* @return true if there is an update available, false otherwise | ||
*/ | ||
fun checkForUpdates(): Boolean { | ||
val difference = System.currentTimeMillis() - lastCheck | ||
if(difference > 60000 || lastCheck == 0L){ | ||
lastCheckResult = try { | ||
val parser = DateTimeFormatter.ISO_INSTANT | ||
val currentData = JsonParser().parse(URL("https://api.github.com/repos/$repo/releases/tags/$current").readText()).asJsonObject | ||
val currentReleasedAt = Instant.from(parser.parse(currentData.get("published_at").asString)) | ||
val latestData = getLatestReleaseData() | ||
val latestReleasedAt = Instant.from(parser.parse(latestData.get("published_at").asString)) | ||
Instant.from(currentReleasedAt).isBefore(latestReleasedAt) | ||
}catch (e: Exception){ | ||
e.printStackTrace() | ||
false | ||
} | ||
|
||
lastCheck = System.currentTimeMillis() | ||
} | ||
return lastCheckResult | ||
} | ||
|
||
/** | ||
* Gets the information of the latest release | ||
* @return The information of the latest release | ||
*/ | ||
fun getLatestReleaseData(): JsonObject { | ||
val difference = System.currentTimeMillis() - lastRequest | ||
if(difference > 60000 || lastRequest == 0L){ | ||
latestData = JsonParser().parse(URL(if(latestReleaseTag != "latest") "https://api.github.com/repos/$repo/releases/tags/$latestReleaseTag" else "https://api.github.com/repos/$repo/releases/latest").readText()).asJsonObject | ||
lastRequest = System.currentTimeMillis() | ||
} | ||
return latestData | ||
} | ||
|
||
} |
This file contains 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
21 changes: 21 additions & 0 deletions
21
src/test/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateCheckerTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package xyz.theprogramsrc.simplecoreapi.global | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import java.util.logging.Logger | ||
|
||
internal class GitHubUpdateCheckerTest { | ||
|
||
private val check1 = GitHubUpdateChecker(Logger.getLogger("GitHubUpdateCheckerTest - 1"), "TheProgramSrc/SimpleCoreAPI", "0.1.9-SNAPSHOT", "v0.1.9-SNAPSHOT") | ||
private val check2 = GitHubUpdateChecker(Logger.getLogger("GitHubUpdateCheckerTest - 2"), "TheProgramSrc/SimpleCoreAPI", "0.1.6-SNAPSHOT") | ||
|
||
@Test | ||
fun noUpdatesAvailableTest() { | ||
assertEquals(false, check1.checkForUpdates()) | ||
} | ||
|
||
@Test | ||
fun updatesAvailableTest() { | ||
assertEquals(true, check2.checkForUpdates()) | ||
} | ||
} |