Skip to content

Commit

Permalink
Merge pull request #7 from TheProgramSrc/feat/update_checker
Browse files Browse the repository at this point in the history
Module Downloader & Update Checker:
* Added GitHub update checker
* Added method to download modules from SimpleCoreAPI.kt
  • Loading branch information
Im-Fran authored Dec 29, 2021
2 parents 1cfdfe1 + 3bdf518 commit 28fc7de
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)

}
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())
}
}

0 comments on commit 28fc7de

Please sign in to comment.