From 1af54ec0eb14bf77399d1393c6e434a8bedcccf3 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Mon, 5 Oct 2015 22:46:37 -0700 Subject: [PATCH] Check new version. --- src/main/kotlin/com/beust/kobalt/Main.kt | 27 +++++++------- .../kotlin/com/beust/kobalt/api/Project.kt | 3 +- .../kotlin/com/beust/kobalt/misc/GithubApi.kt | 37 +++++++++++++++++++ .../kotlin/com/beust/kobalt/misc/KFiles.kt | 13 ++++++- 4 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 9decf8aa..9b033365 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -34,25 +34,13 @@ private class Main @Inject constructor( val localRepo: LocalRepo, val depFactory: DepFactory, val checkVersions: CheckVersions, - val jcenter: UnauthenticatedJCenterApi) + val jcenter: UnauthenticatedJCenterApi, + val github: GithubApi) : KobaltLogger { data class RunInfo(val jc: JCommander, val args: Args) public fun run(argv: Array) { - // Check for new version - // Commented out until I can find a way to get the latest available download - // from bintray. Right now, it always returns all the versions uploaded, not - // just the one I mark -// val p = jcenter.kobaltPackage -// val current = Versions.toLongVersion(Kobalt.version) -// val remote = Versions.toLongVersion(p.latestPublishedVersion) -// if (remote > current) { -// log(1, "*****") -// log(1, "***** New Kobalt version available: ${p.latestPublishedVersion}") -// log(1, "*****") -// } - benchmark("Build", { println(Banner.get() + Kobalt.version + "\n") // runTest() @@ -61,6 +49,17 @@ private class Main @Inject constructor( executors.shutdown() debug("All done") }) + + // 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 current = Versions.toLongVersion(Kobalt.version) + if (remote > current) { + log(1, "*****") + log(1, "***** New Kobalt version available: ${github.latestKobaltRelease}") + log(1, "*****") + } } public class Worker(val runNodes: ArrayList, val n: T) : IWorker, KobaltLogger { diff --git a/src/main/kotlin/com/beust/kobalt/api/Project.kt b/src/main/kotlin/com/beust/kobalt/api/Project.kt index a9914542..caea1dfb 100644 --- a/src/main/kotlin/com/beust/kobalt/api/Project.kt +++ b/src/main/kotlin/com/beust/kobalt/api/Project.kt @@ -3,6 +3,7 @@ package com.beust.kobalt.api import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.maven.MavenDependency import com.beust.kobalt.maven.IClasspathDependency +import com.beust.kobalt.misc.KFiles import com.google.common.base.Preconditions import java.util.ArrayList @@ -10,7 +11,7 @@ open public class Project( open var name: String? = null, open var version: String? = null, open var directory: String = ".", - open var buildDirectory: String? = "kobaltBuild", + open var buildDirectory: String? = KFiles.KOBALT_BUILD_DIR, open var group: String? = null, open var artifactId: String? = null, open var dependencies: Dependencies? = null, diff --git a/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt b/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt new file mode 100644 index 00000000..509f64f6 --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt @@ -0,0 +1,37 @@ +package com.beust.kobalt.misc + +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 + +public class GithubApi @Inject constructor(val http: Http) : KobaltLogger { + companion object { + const val HOST = "https://api.github.com/" + } + + val latestKobaltRelease: String + 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 + } + } + } catch(ex: IOException) { + warn("Couldn't load the release URL: ${url}") + } + return "0" + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt b/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt index 33cd310a..fc6b3001 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt @@ -15,7 +15,17 @@ import java.nio.file.StandardCopyOption public class KFiles { val kobaltJar : String - get() = joinDir(distributionsDir, Kobalt.version, "kobalt/wrapper/kobalt-" + Kobalt.version + ".jar") + get() { + val jar = joinDir(distributionsDir, Kobalt.version, "kobalt/wrapper/kobalt-" + Kobalt.version + ".jar") + val jarFile = File(jar) + if (! jarFile.exists()) { + // Will only happen when building kobalt itself: the jar file might not be in the dist/ directory + // yet since we're currently building it. Instead, use the classes directly + return File(joinDir("build", "classes", "main")).absolutePath + } else { + return jar + } + } init { File(KOBALT_DOT_DIR).mkdirs() @@ -24,6 +34,7 @@ public class KFiles { companion object { private const val KOBALT_DOT_DIR : String = ".kobalt" const val KOBALT_DIR : String = "kobalt" + const val KOBALT_BUILD_DIR = "kobaltBuild" // Directories under ~/.kobalt public val localRepo = homeDir(KOBALT_DOT_DIR, "repository")