diff --git a/kobalt/src/Build.kt b/kobalt/src/Build.kt index 5139b526..06e43965 100644 --- a/kobalt/src/Build.kt +++ b/kobalt/src/Build.kt @@ -1,7 +1,9 @@ import com.beust.kobalt.api.License +import com.beust.kobalt.api.Project import com.beust.kobalt.api.Scm import com.beust.kobalt.homeDir +import com.beust.kobalt.internal.TaskResult import com.beust.kobalt.internal.test import com.beust.kobalt.plugin.application.application import com.beust.kobalt.plugin.java.javaCompiler @@ -9,8 +11,10 @@ import com.beust.kobalt.plugin.java.javaProject import com.beust.kobalt.plugin.kotlin.kotlinCompiler import com.beust.kobalt.plugin.kotlin.kotlinProject import com.beust.kobalt.plugin.packaging.assemble -import com.beust.kobalt.plugin.publish.jcenter import java.io.File +import java.nio.file.Files +import java.nio.file.Paths +import java.nio.file.StandardCopyOption val wrapper = javaProject { name = "kobalt-wrapper" @@ -87,18 +91,22 @@ val kobalt = kotlinProject(wrapper) { } } +// install { +// libDir = "lib-test" +// } + test { - args("-log", "2", "src/test/resources/testng.xml") + args("-log", "1", "src/test/resources/testng.xml") } kotlinCompiler { args("-nowarn") } - jcenter { - publish = true - file("$buildDirectory/libs/$name-$version.zip", "$name/$version/$name-$version.zip") - } +// jcenter { +// publish = true +// file("$buildDirectory/libs/$name-$version.zip", "$name/$version/$name-$version.zip") +// } } fun readVersion() : String { @@ -111,6 +119,15 @@ fun readVersion() : String { return p.getProperty("kobalt.version") } +@com.beust.kobalt.api.annotation.Task(name = "copyVersionForWrapper", runBefore = arrayOf("compile"), description = "") +fun taskCopyVersionForWrapper(project: Project) : TaskResult { + if (project.name == "kobalt-wrapper") { + Files.copy(Paths.get("src/main/resources/kobalt.properties"), + Paths.get("modules/wrapper/kobaltBuild/classes/kobalt.properties"), + StandardCopyOption.REPLACE_EXISTING) + } + return TaskResult() +} //import com.beust.kobalt.plugin.linecount.lineCount //val plugins = plugins( // "com.beust.kobalt:kobalt-line-count:0.15" diff --git a/kobalt/wrapper/kobalt-wrapper.jar b/kobalt/wrapper/kobalt-wrapper.jar index c71f7c07..5ae9002f 100644 Binary files a/kobalt/wrapper/kobalt-wrapper.jar and b/kobalt/wrapper/kobalt-wrapper.jar differ diff --git a/kobalt/wrapper/kobalt-wrapper.properties b/kobalt/wrapper/kobalt-wrapper.properties index eee113f8..fde79fa5 100644 --- a/kobalt/wrapper/kobalt-wrapper.properties +++ b/kobalt/wrapper/kobalt-wrapper.properties @@ -1 +1,2 @@ -kobalt.version=0.234 \ No newline at end of file +kobalt.version=0.238 +kobalt.downloadUrl=https://github.com/cbeust/kobalt/releases/download/0.238/kobalt-0.238.zip diff --git a/modules/wrapper/src/main/java/com/beust/kobalt/wrapper/Main.java b/modules/wrapper/src/main/java/com/beust/kobalt/wrapper/Main.java index 9b9201b5..e74ab74b 100644 --- a/modules/wrapper/src/main/java/com/beust/kobalt/wrapper/Main.java +++ b/modules/wrapper/src/main/java/com/beust/kobalt/wrapper/Main.java @@ -24,7 +24,7 @@ public class Main { private static final String KOBALTW = "kobaltw"; private static final String KOBALT_WRAPPER_PROPERTIES = "kobalt-wrapper.properties"; private static final String PROPERTY_VERSION = "kobalt.version"; - private static final String URL = "https://dl.bintray.com/cbeust/generic/"; + private static final String PROPERTY_DOWNLOAD_URL = "kobalt.downloadUrl"; private static final String FILE_NAME = "kobalt"; private static final String DISTRIBUTIONS_DIR = System.getProperty("user.home") + "/.kobalt/wrapper/dist"; @@ -66,10 +66,16 @@ public class Main { return new File("kobalt", "wrapper"); } + private static final String downloadUrl(String version) { + return "https://github.com/cbeust/kobalt/releases/download/" + version + "/kobalt-" + version + ".zip"; + } + private void initWrapperFile(String version) throws IOException { File config = new File(getWrapperDir(), KOBALT_WRAPPER_PROPERTIES); if (! config.exists()) { - saveFile(config, PROPERTY_VERSION + "=" + version); + saveFile(config, + PROPERTY_VERSION + "=" + version + "\n" + + PROPERTY_DOWNLOAD_URL + "=" + downloadUrl(version) + "\n"); } wrapperProperties.load(new FileReader(config)); } @@ -78,6 +84,10 @@ public class Main { return wrapperProperties.getProperty(PROPERTY_VERSION); } + private String getWrapperDownloadUrl() { + return wrapperProperties.getProperty(PROPERTY_DOWNLOAD_URL); + } + private boolean isWindows() { return System.getProperty("os.name").contains("Windows"); } @@ -97,7 +107,7 @@ public class Main { getWrapperDir().getPath() + "/" + FILE_NAME + "-" + getWrapperVersion() + ".jar"); if (! Files.exists(localZipFile) || ! Files.exists(kobaltJarFile)) { if (!Files.exists(localZipFile)) { - download(fileName, localZipFile.toFile()); + download(localZipFile.toFile()); } // @@ -129,7 +139,7 @@ public class Main { error("Couldn't open zip file " + localZipFile + ": " + e.getMessage()); error("The file is probably corrupt, downloading it again"); Files.delete(localZipFile); - download(fileName, localZipFile.toFile()); + download(localZipFile.toFile()); } } } @@ -161,8 +171,8 @@ public class Main { private static final String[] FILES = new String[] { KOBALTW, "kobalt/wrapper/" + FILE_NAME + "-wrapper.jar" }; - private void download(String fn, File file) throws IOException { - String fileUrl = URL + fn; + private void download(File file) throws IOException { + String fileUrl = getWrapperDownloadUrl(); URL url = new URL(fileUrl); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); @@ -238,11 +248,11 @@ public class Main { Files.write(Paths.get(file.toURI()), text.getBytes()); } - private static void log2(int level, String s) { + static void log2(int level, String s) { p(level, s, false); } - private static void log(int level, String s) { + static void log(int level, String s) { p(level, "[Wrapper] " + s, true); } diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 85de5c1d..a16867dc 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -59,7 +59,8 @@ private class Main @Inject constructor( val server: KobaltServer, val pluginInfo: PluginInfo, val projectGenerator: ProjectGenerator, - val resolveDependency: ResolveDependency) { + val resolveDependency: ResolveDependency, + val wrapper: Wrapper) { data class RunInfo(val jc: JCommander, val args: Args) @@ -137,7 +138,7 @@ private class Main @Inject constructor( // // --init: create a new build project and install the wrapper // - Wrapper().install() + wrapper.install() projectGenerator.run(args) } else if (args.usage) { jc.usage() diff --git a/src/main/kotlin/com/beust/kobalt/misc/KobaltWrapperProperties.kt b/src/main/kotlin/com/beust/kobalt/misc/KobaltWrapperProperties.kt index c3fff544..f18ecece 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/KobaltWrapperProperties.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/KobaltWrapperProperties.kt @@ -1,21 +1,64 @@ package com.beust.kobalt.misc +import com.beust.kobalt.api.Kobalt import com.google.inject.Inject import java.io.File +import java.io.FileReader +import java.util.* +/** + * Manage kobalt-wrapper.properties. + */ class KobaltWrapperProperties @Inject constructor() { private val WRAPPER_DIR = KFiles.KOBALT_DIR + "/wrapper" private val KOBALT_WRAPPER_PROPERTIES = "kobalt-wrapper.properties" private val PROPERTY_VERSION = "kobalt.version" private val PROPERTY_DOWNLOAD_URL = "kobalt.downloadUrl" + fun defaultUrlFor(version: String) = + "bogushttps://github.com/cbeust/kobalt/releases/download/$version/kobalt-$version.zip" + val file: File get() = File("$WRAPPER_DIR/$KOBALT_WRAPPER_PROPERTIES") fun create(version: String) { + log(2, "Creating $file with $version and ${defaultUrlFor(version)}") KFiles.saveFile(file, listOf( "$PROPERTY_VERSION=$version", - "$PROPERTY_DOWNLOAD_URL" + "$PROPERTY_DOWNLOAD_URL=${defaultUrlFor(version)}" ).joinToString("\n")) } + + val properties : Properties + get() { + val config = file + if (!config.exists()) { + create(Kobalt.version) + } + + val result = Properties() + result.load(FileReader(config)) + return result + } + + val version : String + get() = properties.getProperty(PROPERTY_VERSION) + + val downloadUrl : String + get() { + return properties.getProperty(PROPERTY_DOWNLOAD_URL) + ?: defaultUrlFor(version) + } + + fun maybeCreate(version: String) { + if (! file.exists()) { + create(version) + } + } + +// fun urlFor(version: String) : String { +// // val URL = "https://dl.bintray.com/cbeust/generic/" +// // return "https://dl.bintray.com/cbeust/generic/$fileName-$version.zip" +// return downloadUrl ?: defaultUrlFor(version) +// } } diff --git a/src/main/kotlin/com/beust/kobalt/misc/UpdateKobalt.kt b/src/main/kotlin/com/beust/kobalt/misc/UpdateKobalt.kt index 965f8436..5040d86b 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/UpdateKobalt.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/UpdateKobalt.kt @@ -1,15 +1,14 @@ package com.beust.kobalt.misc -import java.io.File import javax.inject.Inject /** * Update Kobalt to the latest version. */ -public class UpdateKobalt @Inject constructor(val github: GithubApi) { +public class UpdateKobalt @Inject constructor(val github: GithubApi, val kobaltWrapperProperties: KobaltWrapperProperties) { fun updateKobalt() { val newVersion = github.latestKobaltVersion - KFiles.saveFile(File("kobalt/wrapper/kobalt-wrapper.properties"), "kobalt.version=${newVersion.get()}") + kobaltWrapperProperties.maybeCreate(newVersion.get()) com.beust.kobalt.wrapper.main(arrayOf()) } } diff --git a/src/main/kotlin/com/beust/kobalt/wrapper/Wrapper.kt b/src/main/kotlin/com/beust/kobalt/wrapper/Wrapper.kt index 400789f7..743ea432 100644 --- a/src/main/kotlin/com/beust/kobalt/wrapper/Wrapper.kt +++ b/src/main/kotlin/com/beust/kobalt/wrapper/Wrapper.kt @@ -1,37 +1,34 @@ package com.beust.kobalt.wrapper -import com.beust.kobalt.maven.Http -import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.benchmark import com.beust.kobalt.JavaInfo import com.beust.kobalt.SystemProperties +import com.beust.kobalt.api.Kobalt +import com.beust.kobalt.maven.Http +import com.beust.kobalt.misc.KFiles +import com.beust.kobalt.misc.KobaltWrapperProperties +import com.beust.kobalt.misc.benchmark import com.beust.kobalt.misc.log +import com.google.inject.Inject import java.io.File -import java.io.FileReader import java.io.InputStream import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths -import java.util.Properties +import java.util.* import java.util.zip.ZipFile public fun main(argv: Array) { - Wrapper().installAndLaunchMain(argv) + Kobalt.INJECTOR.getInstance(Wrapper::class.java).installAndLaunchMain(argv) } /** * Download and install a new wrapper if requested. */ -public class Wrapper { +public class Wrapper @Inject constructor(val wrapperProperties: KobaltWrapperProperties){ // kobalt.properties private val KOBALT_PROPERTIES = "kobalt.properties" private val KOBALTW = "kobaltw" - private val WRAPPER_DIR = KFiles.KOBALT_DIR + "/wrapper" - private val KOBALT_WRAPPER_PROPERTIES = "kobalt-wrapper.properties" - private val PROPERTY_VERSION = "kobalt.version" - - val URL = "https://dl.bintray.com/cbeust/generic/" val FILE_NAME = "kobalt" private val properties = Properties() @@ -47,7 +44,10 @@ public class Wrapper { properties.forEach { es -> System.setProperty(es.key.toString(), es.value.toString()) } } - private fun maybeCreateProperties() : Properties { + /** + * Attemps to read kobalt.properties (which should always exist). + */ + private fun maybeCreateKobaltProperties() : Properties { val result = Properties() // kobalt.properties is internal to Kobalt @@ -61,18 +61,29 @@ public class Wrapper { return result } - private fun initWrapperFile(version: String) { - val config = File(WRAPPER_DIR, KOBALT_WRAPPER_PROPERTIES) - if (! config.exists()) { - KFiles.saveFile(config, "$PROPERTY_VERSION=$version") - } - properties.load(FileReader(config)) - } - - private val wrapperVersion : String - get() { - return properties.getProperty(PROPERTY_VERSION) - } +// private fun initWrapperFile(version: String) { +// val config = wrapperProperties.file +// if (! config.exists()) { +// wrapperProperties.create(version) +// } +// properties.load(FileReader(config)) +// } +// +// private val wrapperVersion : String +// get() { +// return properties.getProperty(PROPERTY_VERSION) +// } +// +// private val wrapperDownloadUrl : String +// get() { +// return properties.getProperty(PROPERTY_DOWNLOAD_URL) +// } +// +// fun urlFor(version: String) : String { +// // val URL = "https://dl.bintray.com/cbeust/generic/" +// // return "https://dl.bintray.com/cbeust/generic/$fileName-$version.zip" +// return wrapperDownloadUrl ?: defaultUrlFor(version) +// } /** * Install a new version if requested in .kobalt/wrapper/kobalt-wrapper.properties @@ -80,12 +91,12 @@ public class Wrapper { * @return the path to the Kobalt jar file */ public fun install() : Path { - val properties = maybeCreateProperties() - val version = properties.getProperty(PROPERTY_VERSION) - initWrapperFile(version) - - log(2, "Wrapper version: $wrapperVersion") + val properties = maybeCreateKobaltProperties() + val version = properties.getProperty("kobalt.version") + wrapperProperties.maybeCreate(version) + log(2, "Wrapper version: ${wrapperProperties.version}") + val wrapperVersion = wrapperProperties.version val fileName = "$FILE_NAME-$wrapperVersion.zip" File(KFiles.distributionsDir).mkdirs() val localZipFile = Paths.get(KFiles.distributionsDir, fileName) @@ -93,7 +104,7 @@ public class Wrapper { val kobaltJarFile = Paths.get(zipOutputDir, "kobalt/wrapper/$FILE_NAME-$wrapperVersion.jar") if (!Files.exists(localZipFile) || !Files.exists(kobaltJarFile)) { log(1, "Downloading $fileName") - val fullUrl = "$URL/$fileName" + val fullUrl = wrapperProperties.downloadUrl val body = Http().get(fullUrl) if (body.code == 200) { if (!Files.exists(localZipFile)) { @@ -133,7 +144,7 @@ public class Wrapper { } log(2, "$localZipFile extracted") } else { - error("Couldn't download $URL") + error("Couldn't download $fullUrl") } } diff --git a/src/main/resources/kobalt.properties b/src/main/resources/kobalt.properties index 120b1444..b27d957c 100644 --- a/src/main/resources/kobalt.properties +++ b/src/main/resources/kobalt.properties @@ -1 +1,2 @@ -kobalt.version=0.234 +kobalt.version=0.238 +