From 97a5f54c9a4a36e132f23a1079ed154e38237341 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Thu, 12 Nov 2015 13:45:56 -0800 Subject: [PATCH] New Publishing plugin task: "uploadGithub". --- kobalt/src/Build.kt | 6 +- .../kotlin/com/beust/kobalt/misc/GithubApi.kt | 7 +- .../kobalt/plugin/publish/PublishPlugin.kt | 88 ++++++++++++++++--- 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/kobalt/src/Build.kt b/kobalt/src/Build.kt index ce87a0fa..66fde415 100644 --- a/kobalt/src/Build.kt +++ b/kobalt/src/Build.kt @@ -12,7 +12,7 @@ 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 com.beust.kobalt.plugin.publish.* import java.io.File import java.nio.file.Files import java.nio.file.Paths @@ -109,6 +109,10 @@ val kobalt = kotlinProject(wrapper) { args("-nowarn") } + github { + 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") diff --git a/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt b/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt index ae75c776..e3d905a1 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt @@ -70,7 +70,7 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors) { // private val service = RestAdapter.Builder() - .setLogLevel(RestAdapter.LogLevel.FULL) +// .setLogLevel(RestAdapter.LogLevel.FULL) .setClient(OkClient(OkHttpClient())) .setEndpoint("https://api.github.com") .build() @@ -81,7 +81,8 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors) { var prerelease: Boolean? = null } - class CreateRelease(@SerializedName("tag_name") var tag_name: String? = null) + class CreateRelease(@SerializedName("tag_name") var tagName: String? = null, + var name: String? = tagName) class CreateReleaseResponse(var id: String? = null) class GetReleaseResponse(var id: String? = null, @SerializedName("upload_url") var uploadUrl: String? = null) @@ -109,7 +110,7 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors) { val uploadService = RestAdapter.Builder() .setEndpoint("https://uploads.github.com/") - .setLogLevel(RestAdapter.LogLevel.FULL) +// .setLogLevel(RestAdapter.LogLevel.FULL) .setClient(OkClient(OkHttpClient())) .build() .create(UploadApi::class.java) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt index 9dc9f2ae..9e0671ba 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt @@ -7,7 +7,9 @@ import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.internal.TaskResult import com.beust.kobalt.maven.PomGenerator +import com.beust.kobalt.misc.GithubApi import com.beust.kobalt.misc.KFiles +import com.beust.kobalt.misc.log import com.google.common.base.Preconditions import java.io.File import javax.inject.Inject @@ -15,13 +17,14 @@ import javax.inject.Singleton @Singleton public class PublishPlugin @Inject constructor(val files: KFiles, val factory: PomGenerator.IFactory, - val jcenterFactory: JCenterApi.IFactory) + val jcenterFactory: JCenterApi.IFactory, val github: GithubApi) : BasePlugin() { override val name = "publish" companion object { private const val TASK_UPLOAD_JCENTER = "uploadJcenter" + private const val TASK_UPLOAD_GITHUB = "uploadGithub" private const val TASK_GENERATE_POM = "generatePom" private const val PROPERTY_BINTRAY_USER = "bintray.user" @@ -56,19 +59,53 @@ public class PublishPlugin @Inject constructor(val files: KFiles, val factory: P "your credentials in local.properties") } - @Task(name = TASK_UPLOAD_JCENTER, description = "Upload the artifacts to JCenter", - runAfter = arrayOf(TASK_GENERATE_POM)) - fun taskUploadJcenter(project: Project): TaskResult { + data class UserData(val user: String, val password: String) + + private fun checkCredentials(project: Project) : UserData { val user = System.getProperty(PROPERTY_BINTRAY_USER) val password = System.getProperty(PROPERTY_BINTRAY_PASSWORD) checkAuthentication(user, PROPERTY_BINTRAY_USER) checkAuthentication(password, PROPERTY_BINTRAY_PASSWORD) validateProject(project) + return UserData(user, password) + } + @Task(name = TASK_UPLOAD_JCENTER, description = "Upload the artifacts to JCenter", + runAfter = arrayOf(TASK_GENERATE_POM)) + fun taskUploadJcenter(project: Project): TaskResult { + checkCredentials(project).let { + return uploadJcenter(project, it.user, it.password) + } + } + + @Task(name = TASK_UPLOAD_GITHUB, description = "Upload the release to Github", + runAfter = arrayOf(TASK_GENERATE_POM)) + fun taskUploadGithub(project: Project): TaskResult { + checkCredentials(project).let { + return uploadGithub(project) + } + } + + private fun uploadGithub(project: Project) : TaskResult { + val configuration = githubConfigurations.getRaw(project.name) + + // + // Upload individual files, if applicable + // + configuration?.let { conf : GithubConfig -> + conf.files.forEach { + log(2, "Uploading $it tag: ${project.version}") + github.uploadRelease(project.name, project.version!!, it) + } + } + return TaskResult() + } + + private fun uploadJcenter(project: Project, user: String?, password: String?) : TaskResult { val jcenter = jcenterFactory.create(user, password) - val configuration = configurations.getRaw(project.name) + val configuration = jcenterConfigurations.getRaw(project.name) // // Upload to Maven @@ -96,11 +133,36 @@ public class PublishPlugin @Inject constructor(val files: KFiles, val factory: P /** * Map of project name -> JCenterConfiguration */ - private val configurations = hashMapOf() - fun addConfiguration(projectName: String, config: JCenterConfig) { - configurations.put(projectName, config) + private val jcenterConfigurations = hashMapOf() + fun addJCenterConfiguration(projectName: String, config: JCenterConfig) { + jcenterConfigurations.put(projectName, config) } + /** + * Map of project name -> GithubConfiguration + */ + private val githubConfigurations = hashMapOf() + fun addGithubConfiguration(projectName: String, config: GithubConfig) { + githubConfigurations.put(projectName, config) + } +} + +data class GithubConfig(val project: Project) { + var publish: Boolean = false + val files = arrayListOf() + + @Directive + public fun file(filePath: String, url: String) { + files.add(File(filePath)) + } +} + +@Directive +public fun Project.github(init: GithubConfig.() -> Unit) { + with(GithubConfig(this)) { + init() + (Kobalt.findPlugin("publish") as PublishPlugin).addGithubConfiguration(name, this) + } } data class JCenterConfig(val project: Project) { @@ -114,9 +176,9 @@ data class JCenterConfig(val project: Project) { } @Directive -public fun Project.jcenter(ini: JCenterConfig.() -> Unit) : JCenterConfig { - val pd = JCenterConfig(this) - pd.ini() - (Kobalt.findPlugin("publish") as PublishPlugin).addConfiguration(name, pd) - return pd +public fun Project.jcenter(init: JCenterConfig.() -> Unit) { + with(JCenterConfig(this)) { + init() + (Kobalt.findPlugin("publish") as PublishPlugin).addJCenterConfiguration(name, this) + } }