mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 16:28:12 -07:00
Merge pull request #109 from voddan/ADD_newVersionCheck_timeout
ADD_newVersionCheck_timeout
This commit is contained in:
commit
efd12733ee
5 changed files with 68 additions and 24 deletions
|
@ -5,6 +5,7 @@ import com.beust.kobalt.HostConfig
|
||||||
import com.beust.kobalt.Plugins
|
import com.beust.kobalt.Plugins
|
||||||
import com.google.inject.Injector
|
import com.google.inject.Injector
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
import java.time.Duration
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
public class Kobalt {
|
public class Kobalt {
|
||||||
|
@ -34,8 +35,9 @@ public class Kobalt {
|
||||||
if (repo.url.endsWith("/")) repo
|
if (repo.url.endsWith("/")) repo
|
||||||
else repo.copy(url = (repo.url + "/")))
|
else repo.copy(url = (repo.url + "/")))
|
||||||
|
|
||||||
private val PROPERTY_KOBALT_VERSION = "kobalt.version"
|
|
||||||
private val KOBALT_PROPERTIES = "kobalt.properties"
|
private val KOBALT_PROPERTIES = "kobalt.properties"
|
||||||
|
private val PROPERTY_KOBALT_VERSION = "kobalt.version"
|
||||||
|
private val PROPERTY_KOBALT_VERSION_CHECK_TIMEOUT = "kobalt.version.check_timeout" // ISO-8601
|
||||||
|
|
||||||
/** kobalt.properties */
|
/** kobalt.properties */
|
||||||
private val kobaltProperties: Properties by lazy { readProperties() }
|
private val kobaltProperties: Properties by lazy { readProperties() }
|
||||||
|
@ -73,6 +75,7 @@ public class Kobalt {
|
||||||
}
|
}
|
||||||
|
|
||||||
val version = kobaltProperties.getProperty(PROPERTY_KOBALT_VERSION)
|
val version = kobaltProperties.getProperty(PROPERTY_KOBALT_VERSION)
|
||||||
|
val versionCheckTimeout = Duration.parse(kobaltProperties.getProperty(PROPERTY_KOBALT_VERSION_CHECK_TIMEOUT))
|
||||||
|
|
||||||
fun findPlugin(name: String) = Plugins.findPlugin(name)
|
fun findPlugin(name: String) = Plugins.findPlugin(name)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.beust.kobalt.internal.build
|
||||||
|
|
||||||
|
import com.beust.kobalt.misc.KFiles
|
||||||
|
import java.io.File
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
|
class VersionCheckTimestampFile {
|
||||||
|
companion object {
|
||||||
|
private val KOBALT_VERSION_CHECK_TIMESTAMP_FILE = "versionCheckTimestamp.txt"
|
||||||
|
private val checkTimestampFile = File(KFiles.KOBALT_DOT_DIR, KOBALT_VERSION_CHECK_TIMESTAMP_FILE)
|
||||||
|
|
||||||
|
fun updateTimestamp(timestamp: Instant) = KFiles.saveFile(checkTimestampFile, timestamp.toString())
|
||||||
|
|
||||||
|
fun getTimestamp(): Instant {
|
||||||
|
return if(checkTimestampFile.exists())
|
||||||
|
Instant.parse(checkTimestampFile.readText())
|
||||||
|
else {
|
||||||
|
updateTimestamp(Instant.MIN)
|
||||||
|
Instant.MIN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,8 +22,6 @@ import com.google.inject.Guice
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import java.util.concurrent.TimeoutException
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
public fun main(argv: Array<String>) {
|
public fun main(argv: Array<String>) {
|
||||||
|
@ -83,6 +81,7 @@ private class Main @Inject constructor(
|
||||||
|
|
||||||
var result = 0
|
var result = 0
|
||||||
val latestVersionFuture = github.latestKobaltVersion
|
val latestVersionFuture = github.latestKobaltVersion
|
||||||
|
|
||||||
val seconds = benchmarkSeconds {
|
val seconds = benchmarkSeconds {
|
||||||
try {
|
try {
|
||||||
result = runWithArgs(jc, args, argv)
|
result = runWithArgs(jc, args, argv)
|
||||||
|
@ -97,25 +96,7 @@ private class Main @Inject constructor(
|
||||||
if (! args.update) {
|
if (! args.update) {
|
||||||
log(1, if (result != 0) "BUILD FAILED: $result" else "BUILD SUCCESSFUL ($seconds seconds)")
|
log(1, if (result != 0) "BUILD FAILED: $result" else "BUILD SUCCESSFUL ($seconds seconds)")
|
||||||
|
|
||||||
// Check for new version
|
updateKobalt.checkForNewVersion(latestVersionFuture)
|
||||||
try {
|
|
||||||
val latestVersionString = latestVersionFuture.get(1, TimeUnit.SECONDS)
|
|
||||||
val latestVersion = Versions.toLongVersion(latestVersionString)
|
|
||||||
val current = Versions.toLongVersion(Kobalt.version)
|
|
||||||
val distFile = File(KFiles.joinDir(KFiles.distributionsDir, latestVersionString))
|
|
||||||
if (latestVersion > current) {
|
|
||||||
if (distFile.exists()) {
|
|
||||||
log(1, "**** Version $latestVersionString is installed")
|
|
||||||
} else {
|
|
||||||
listOf("", "New Kobalt version available: $latestVersionString",
|
|
||||||
"To update, run ./kobaltw --update", "").forEach {
|
|
||||||
log(1, "**** $it")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(ex: TimeoutException) {
|
|
||||||
log(2, "Didn't get the new version in time, skipping it")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
package com.beust.kobalt.app
|
package com.beust.kobalt.app
|
||||||
|
|
||||||
import com.beust.kobalt.misc.GithubApi
|
import com.beust.kobalt.api.Kobalt
|
||||||
import com.beust.kobalt.misc.KobaltWrapperProperties
|
import com.beust.kobalt.internal.build.VersionCheckTimestampFile
|
||||||
|
import com.beust.kobalt.misc.*
|
||||||
import com.beust.kobalt.wrapper.Main
|
import com.beust.kobalt.wrapper.Main
|
||||||
|
import java.io.File
|
||||||
|
import java.time.Duration
|
||||||
|
import java.time.Instant
|
||||||
|
import java.util.concurrent.Future
|
||||||
|
import java.util.concurrent.TimeoutException
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,6 +18,7 @@ public class UpdateKobalt @Inject constructor(val github: GithubApi, val wrapper
|
||||||
fun updateKobalt() {
|
fun updateKobalt() {
|
||||||
val newVersion = github.latestKobaltVersion
|
val newVersion = github.latestKobaltVersion
|
||||||
wrapperProperties.create(newVersion.get())
|
wrapperProperties.create(newVersion.get())
|
||||||
|
VersionCheckTimestampFile.updateTimestamp(Instant.now())
|
||||||
Main.main(arrayOf())
|
Main.main(arrayOf())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,4 +28,33 @@ public class UpdateKobalt @Inject constructor(val github: GithubApi, val wrapper
|
||||||
fun downloadKobalt() {
|
fun downloadKobalt() {
|
||||||
Main.main(arrayOf("--download", "--no-launch"))
|
Main.main(arrayOf("--download", "--no-launch"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts Future<String> as `latestVersionFuture` to allow getting `latestVersion` in the background
|
||||||
|
* */
|
||||||
|
fun checkForNewVersion(latestVersionFuture: Future<String>) {
|
||||||
|
if(Kobalt.versionCheckTimeout
|
||||||
|
> Duration.between(VersionCheckTimestampFile.getTimestamp(), Instant.now()))
|
||||||
|
return // waits `Kobalt.versionCheckTimeout` before the next check
|
||||||
|
|
||||||
|
try {
|
||||||
|
val latestVersionString = latestVersionFuture.get()
|
||||||
|
val latestVersion = Versions.toLongVersion(latestVersionString)
|
||||||
|
val current = Versions.toLongVersion(Kobalt.version)
|
||||||
|
val distFile = File(KFiles.joinDir(KFiles.distributionsDir, latestVersionString))
|
||||||
|
if (latestVersion > current) {
|
||||||
|
if (distFile.exists()) {
|
||||||
|
log(1, "**** Version $latestVersionString is installed")
|
||||||
|
} else {
|
||||||
|
listOf("", "New Kobalt version available: $latestVersionString",
|
||||||
|
"To update, run ./kobaltw --update", "").forEach {
|
||||||
|
log(1, "**** $it")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VersionCheckTimestampFile.updateTimestamp(Instant.now())
|
||||||
|
} catch(ex: TimeoutException) {
|
||||||
|
log(2, "Didn't get the new version in time, skipping it")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
kobalt.version=0.401
|
kobalt.version=0.401
|
||||||
|
kobalt.version.check_timeout=P1DT2H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue