Skip to content

Commit 2aa6263

Browse files
authored
fix: Use GitHub raw links for CLI update check (#87)
1 parent 91da5fd commit 2aa6263

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ internal object PatchCommand : Callable<Int> {
320320

321321
override fun call(): Int {
322322
// Check for any newer version
323-
UpdateChecker.check()?.let { logger.info(it) }
323+
UpdateChecker.check(logger)?.let { logger.info(it) }
324324

325325
// region Setup
326326

src/main/kotlin/app/morphe/engine/UpdateChecker.kt

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,59 @@ package app.morphe.engine
33
import java.net.HttpURLConnection
44
import java.net.URL
55
import java.util.Properties
6-
6+
import java.util.logging.Logger
77

88
object UpdateChecker {
9-
fun check(): String? {
9+
fun check(logger: Logger): String? {
1010
try {
11-
// Try to get the latest version. (TTL IS SET TO 3000)
11+
// Current version of this CLI.
1212
val currentVersion = javaClass.getResourceAsStream("/app/morphe/cli/version.properties")
1313
?.use { stream ->
1414
Properties().apply { load(stream) }.getProperty("version")
15-
}
16-
?: return null
15+
} ?: return null
16+
17+
// Check if the user is using dev or stable release.
18+
val isDev = currentVersion.contains("dev")
1719

18-
val connection = URL("https://api.github.com/repos/MorpheApp/morphe-cli/releases/latest")
19-
.openConnection() as HttpURLConnection
20+
val url = if (isDev) {
21+
// If on dev and a new stable release is available, then this
22+
// ref still is correct because after a stable release dev branch is same as main.
23+
"https://raw.githubusercontent.com/MorpheApp/morphe-cli/refs/heads/dev/gradle.properties"
24+
} else {
25+
"https://raw.githubusercontent.com/MorpheApp/morphe-cli/refs/heads/main/gradle.properties"
26+
}
2027

28+
val connection = URL(url).openConnection() as HttpURLConnection
2129
connection.connectTimeout = 3000
2230
connection.readTimeout = 3000
23-
connection.setRequestProperty("Accept", "application/vnd.github.v3+json")
2431

25-
//
2632
val response = connection.getInputStream().bufferedReader().use { it.readText() }
2733

28-
val latestVersion = Regex(""""tag_name"\s*:\s*"v?([^"]+)"""").find(response)
29-
?.groupValues?.get(1) ?: return null
34+
val latestVersion = Properties().apply {
35+
load(response.byteInputStream())
36+
}.getProperty("version") ?: return null
3037

3138
if (latestVersion != currentVersion) {
32-
return "Update available: v$latestVersion (current: v$currentVersion). Download from https://github.com/MorpheApp/morphe-cli/releases/latest"
39+
// Warning message for when the user is to about to move from dev -> stable edge case.
40+
val trackChangesMessage = if (isDev && !latestVersion.contains("dev")){
41+
"\nNotice: The latest CLI is a stable release. Updating to that will stop dev " +
42+
"update notifications. To keep receiving dev updates, skip stable update " +
43+
"and wait for the next dev release."
44+
} else ""
45+
46+
val downloadLink = if (isDev) {
47+
"https://github.com/MorpheApp/morphe-cli/releases/"
48+
} else {
49+
"https://github.com/MorpheApp/morphe-cli/releases/latest"
50+
}
51+
52+
return "Update available: v$latestVersion (current: v$currentVersion)" +
53+
"$trackChangesMessage\nDownload from $downloadLink"
3354
}
3455
return null
3556

36-
}catch (e: Exception) {
37-
// In case we fail anything, we silently return.
57+
} catch (ex: Exception) {
58+
logger.fine("Could not check for CLI update: $ex")
3859
return null
3960
}
4061
}

0 commit comments

Comments
 (0)