From a44b15d6d17b6ea48fb84188333a775e130557d3 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Wed, 7 Oct 2015 02:09:11 -0700 Subject: [PATCH] Better version check. --- src/main/kotlin/com/beust/kobalt/Main.kt | 12 +++--- .../kotlin/com/beust/kobalt/misc/GithubApi.kt | 43 +++++++++++-------- .../com/beust/kobalt/misc/KobaltExecutors.kt | 8 ++-- .../com/beust/kobalt/misc/UpdateKobalt.kt | 2 +- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 5687b4e8..484b33ef 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -50,6 +50,7 @@ private class Main @Inject constructor( data class RunInfo(val jc: JCommander, val args: Args) public fun run(jc: JCommander, args: Args) { + val latestVersionFuture = github.latestKobaltVersion benchmark("Build", { println(Banner.get() + Kobalt.version + "\n") // runTest() @@ -59,15 +60,14 @@ private class Main @Inject constructor( }) // Check for new version - // TODO(cbeust): Start the network call at the beginning of the build and - // get the future here, at the end of the build - val remote = Versions.toLongVersion(github.latestKobaltRelease) + val latestVersionString = latestVersionFuture.get() + val latestVersion = Versions.toLongVersion(latestVersionString) val current = Versions.toLongVersion(Kobalt.version) - if (remote > current) { + if (latestVersion > current) { "***** ".let { log(1, it) - log(1, "$it New Kobalt version available: ${github.latestKobaltRelease}") - log(1, "$it To update, run ./kobaltw --update") + log(1, "$it New Kobalt version available: $latestVersionString") + log(1, "$it To update, run ./kobaltw --update") log(1, it ) } } diff --git a/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt b/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt index 509f64f6..07ef97f5 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt @@ -4,34 +4,43 @@ import com.beust.klaxon.JsonArray import com.beust.klaxon.JsonObject import com.beust.klaxon.Parser import com.beust.klaxon.string -import com.beust.kobalt.maven.Http import com.google.inject.Inject import java.io.IOException import java.net.URL +import java.util.concurrent.Callable +import java.util.concurrent.Future -public class GithubApi @Inject constructor(val http: Http) : KobaltLogger { +/** + * Retrieve Kobalt's latest release version from github. + */ +public class GithubApi @Inject constructor(val executors: KobaltExecutors) : KobaltLogger { companion object { const val HOST = "https://api.github.com/" } - val latestKobaltRelease: String + val latestKobaltVersion: Future get() { - val url = HOST + "repos/cbeust/kobalt/releases" - try { - val ins = URL(url).openConnection().inputStream - val jo = Parser().parse(ins) as JsonArray - if (jo.size() > 0) { - var result = jo.get(0).string("name") - if (result == null) { - result = jo.get(0).string("tag_name") - } - if (result != null) { - return result + val callable = Callable { + var result = "0" + val url = HOST + "repos/cbeust/kobalt/releases" + try { + val ins = URL(url).openConnection().inputStream + @Suppress("UNCHECKED_CAST") + val jo = Parser().parse(ins) as JsonArray + if (jo.size() > 0) { + var versionName = jo.get(0).string("name") + if (versionName == null) { + versionName = jo.get(0).string("tag_name") + } + if (versionName != null) { + result = versionName + } } + } catch(ex: IOException) { + warn("Couldn't load the release URL: $url") } - } catch(ex: IOException) { - warn("Couldn't load the release URL: ${url}") + result } - return "0" + return executors.miscExecutor.submit(callable) } } \ No newline at end of file diff --git a/src/main/kotlin/com/beust/kobalt/misc/KobaltExecutors.kt b/src/main/kotlin/com/beust/kobalt/misc/KobaltExecutors.kt index 3f2976f0..14efc038 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/KobaltExecutors.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/KobaltExecutors.kt @@ -48,13 +48,15 @@ public class KobaltExecutors : KobaltLogger { public fun newExecutor(name: String, threadCount: Int) : ExecutorService = KobaltExecutor(name, threadCount) - var dependencyExecutor = newExecutor("Dependency", 5) + val dependencyExecutor = newExecutor("Dependency", 5) + val miscExecutor = newExecutor("Misc", 2) - public fun shutdown() { + fun shutdown() { dependencyExecutor.shutdown() + miscExecutor.shutdown() } - public fun completionService(name: String, threadCount: Int, + fun completionService(name: String, threadCount: Int, maxMs: Long, tasks: List>) : List { val result = arrayListOf() val executor = newExecutor(name, threadCount) diff --git a/src/main/kotlin/com/beust/kobalt/misc/UpdateKobalt.kt b/src/main/kotlin/com/beust/kobalt/misc/UpdateKobalt.kt index 300f8fe4..70329323 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/UpdateKobalt.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/UpdateKobalt.kt @@ -9,7 +9,7 @@ import java.io.File */ public class UpdateKobalt @Inject constructor(val http: Http, val github: GithubApi) { fun updateKobalt() { - val newVersion = github.latestKobaltRelease + val newVersion = github.latestKobaltVersion KFiles.saveFile(File("kobalt/wrapper/kobalt-wrapper.properties"), "kobalt.version=$newVersion") com.beust.kobalt.wrapper.main(arrayOf()) }