Skip to content

Commit 5bcd4f7

Browse files
authored
feat: Inform user if cli is outdated (#81)
1 parent ad53aa9 commit 5bcd4f7

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/main/kotlin/app/morphe/cli/command/PatchCommand.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import app.morphe.cli.command.model.toPatchBundle
2020
import app.morphe.cli.command.model.toSerializablePatch
2121
import app.morphe.cli.command.model.withUpdatedBundle
2222
import app.morphe.engine.ApkLibraryStripper
23+
import app.morphe.engine.UpdateChecker
2324
import app.morphe.patcher.apk.ApkUtils
2425
import app.morphe.patcher.apk.ApkUtils.applyTo
2526
import app.morphe.library.installation.installer.*
@@ -318,6 +319,9 @@ internal object PatchCommand : Callable<Int> {
318319
private var updateOptions: Boolean = false
319320

320321
override fun call(): Int {
322+
// Check for any newer version
323+
UpdateChecker.check()?.let { logger.info(it) }
324+
321325
// region Setup
322326

323327
val outputFilePath =
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package app.morphe.engine
2+
3+
import java.net.HttpURLConnection
4+
import java.net.URL
5+
import java.util.Properties
6+
7+
8+
object UpdateChecker {
9+
fun check(): String? {
10+
try {
11+
// Try to get the latest version. (TTL IS SET TO 3000)
12+
val currentVersion = javaClass.getResourceAsStream("/app/morphe/cli/version.properties")
13+
?.use { stream ->
14+
Properties().apply { load(stream) }.getProperty("version")
15+
}
16+
?: return null
17+
18+
val connection = URL("https://api.github.com/repos/MorpheApp/morphe-cli/releases/latest")
19+
.openConnection() as HttpURLConnection
20+
21+
connection.connectTimeout = 3000
22+
connection.readTimeout = 3000
23+
connection.setRequestProperty("Accept", "application/vnd.github.v3+json")
24+
25+
//
26+
val response = connection.getInputStream().bufferedReader().use { it.readText() }
27+
28+
val latestVersion = Regex(""""tag_name"\s*:\s*"v?([^"]+)"""").find(response)
29+
?.groupValues?.get(1) ?: return null
30+
31+
if (latestVersion != currentVersion) {
32+
return "Update available: v$latestVersion (current: v$currentVersion). Download from https://github.com/MorpheApp/morphe-cli/releases/latest"
33+
}
34+
return null
35+
36+
}catch (e: Exception) {
37+
// In case we fail anything, we silently return.
38+
return null
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)