diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt index e736aab1..a64ecaef 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt @@ -5,6 +5,7 @@ import com.beust.kobalt.HostConfig import com.beust.kobalt.Plugins import com.google.inject.Injector import java.io.InputStream +import java.time.Duration import java.util.* public class Kobalt { @@ -34,8 +35,9 @@ public class Kobalt { if (repo.url.endsWith("/")) repo else repo.copy(url = (repo.url + "/"))) - private val PROPERTY_KOBALT_VERSION = "kobalt.version" 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 */ private val kobaltProperties: Properties by lazy { readProperties() } @@ -73,6 +75,7 @@ public class Kobalt { } 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) } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/build/VersionCheckTimestampFile.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/build/VersionCheckTimestampFile.kt new file mode 100644 index 00000000..ecb23bd6 --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/build/VersionCheckTimestampFile.kt @@ -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 + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 1b6aace6..5924f318 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -22,8 +22,6 @@ import com.google.inject.Guice import java.io.File import java.nio.file.Paths import java.util.* -import java.util.concurrent.TimeUnit -import java.util.concurrent.TimeoutException import javax.inject.Inject public fun main(argv: Array) { @@ -83,6 +81,7 @@ private class Main @Inject constructor( var result = 0 val latestVersionFuture = github.latestKobaltVersion + val seconds = benchmarkSeconds { try { result = runWithArgs(jc, args, argv) @@ -97,25 +96,7 @@ private class Main @Inject constructor( if (! args.update) { log(1, if (result != 0) "BUILD FAILED: $result" else "BUILD SUCCESSFUL ($seconds seconds)") - // Check for new version - 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") - } + updateKobalt.checkForNewVersion(latestVersionFuture) } return result } diff --git a/src/main/kotlin/com/beust/kobalt/app/UpdateKobalt.kt b/src/main/kotlin/com/beust/kobalt/app/UpdateKobalt.kt index 73d1a6d0..488c0d30 100644 --- a/src/main/kotlin/com/beust/kobalt/app/UpdateKobalt.kt +++ b/src/main/kotlin/com/beust/kobalt/app/UpdateKobalt.kt @@ -1,8 +1,14 @@ package com.beust.kobalt.app -import com.beust.kobalt.misc.GithubApi -import com.beust.kobalt.misc.KobaltWrapperProperties +import com.beust.kobalt.api.Kobalt +import com.beust.kobalt.internal.build.VersionCheckTimestampFile +import com.beust.kobalt.misc.* 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 /** @@ -12,6 +18,7 @@ public class UpdateKobalt @Inject constructor(val github: GithubApi, val wrapper fun updateKobalt() { val newVersion = github.latestKobaltVersion wrapperProperties.create(newVersion.get()) + VersionCheckTimestampFile.updateTimestamp(Instant.now()) Main.main(arrayOf()) } @@ -21,4 +28,33 @@ public class UpdateKobalt @Inject constructor(val github: GithubApi, val wrapper fun downloadKobalt() { Main.main(arrayOf("--download", "--no-launch")) } + + /** + * Accepts Future as `latestVersionFuture` to allow getting `latestVersion` in the background + * */ + fun checkForNewVersion(latestVersionFuture: Future) { + 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") + } + } } diff --git a/src/main/resources/kobalt.properties b/src/main/resources/kobalt.properties index 0f994c0b..182ef21c 100644 --- a/src/main/resources/kobalt.properties +++ b/src/main/resources/kobalt.properties @@ -1 +1,2 @@ kobalt.version=0.401 +kobalt.version.check_timeout=P1DT2H