From 3bdf5188038d8791b4e64d3de4435414d1959da4 Mon Sep 17 00:00:00 2001 From: Francisco Solis Date: Wed, 29 Dec 2021 15:43:25 -0300 Subject: [PATCH] Module Downloader & Update Checker * Added GitHub update checker * Added method to download modules from SimpleCoreAPI.kt --- CHANGELOG.md | 4 + build.gradle | 2 +- .../global/GitHubUpdateChecker.kt | 74 +++++++++++++++++++ .../simplecoreapi/global/SimpleCoreAPI.kt | 14 +++- .../global/GitHubUpdateCheckerTest.kt | 21 ++++++ 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateChecker.kt create mode 100644 src/test/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateCheckerTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index dad65cc9..8ce4e0cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v0.1.10 - Snapshot +* Added GitHub update checker +* Added method to download modules from SimpleCoreAPI.kt + ## v0.1.9 - Snapshot * Fixed when trying to get simplecoreapi.properties resource returning null diff --git a/build.gradle b/build.gradle index ad6d25dd..3797466b 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { id 'org.jetbrains.dokka' version '1.6.0' } -def projectVersion = (System.getenv("VERSION") ?: '0.1.9-SNAPSHOT').replaceFirst("v", "").replace('/', '') +def projectVersion = (System.getenv("VERSION") ?: '0.1.10-SNAPSHOT').replaceFirst("v", "").replace('/', '') group 'xyz.theprogramsrc' version projectVersion diff --git a/src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateChecker.kt b/src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateChecker.kt new file mode 100644 index 00000000..7ae33502 --- /dev/null +++ b/src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateChecker.kt @@ -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 + } + +} \ No newline at end of file diff --git a/src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/SimpleCoreAPI.kt b/src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/SimpleCoreAPI.kt index 5fbae0a3..a5fc9eed 100644 --- a/src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/SimpleCoreAPI.kt +++ b/src/main/kotlin/xyz/theprogramsrc/simplecoreapi/global/SimpleCoreAPI.kt @@ -1,5 +1,6 @@ package xyz.theprogramsrc.simplecoreapi.global +import xyz.theprogramsrc.simplecoreapi.global.module.ModuleHelper import xyz.theprogramsrc.simplecoreapi.global.module.ModuleManager import java.util.* import java.util.logging.Logger @@ -33,10 +34,14 @@ class SimpleCoreAPI(logger: Logger) { init { instance = this val resource = SimpleCoreAPI::class.java.getResource("/simplecoreapi.properties") - if(resource != null){ + if (resource != null) { props.load(resource.openStream()) } + logger.info("SimpleCoreAPI v${getVersion()} - Git Commit: ${getShortHash()}") + if (getVersion() != "unknown") { + GitHubUpdateChecker(logger, "TheProgramSrc/SimpleCoreAPI", getVersion()) + } moduleManager = ModuleManager.init(logger) } @@ -58,4 +63,11 @@ class SimpleCoreAPI(logger: Logger) { */ fun getVersion(): String = props.getProperty("version", "unknown") + /** + * Downloads a Module from the database + * @param repositoryId Identifier of the artifact inside the repository + * @return true if the module was downloaded, false otherwise + */ + fun downloadModule(repositoryId: String): Boolean = ModuleHelper.downloadModule(repositoryId) + } \ No newline at end of file diff --git a/src/test/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateCheckerTest.kt b/src/test/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateCheckerTest.kt new file mode 100644 index 00000000..105f965b --- /dev/null +++ b/src/test/kotlin/xyz/theprogramsrc/simplecoreapi/global/GitHubUpdateCheckerTest.kt @@ -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()) + } +} \ No newline at end of file