1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

New Publishing plugin task: "uploadGithub".

This commit is contained in:
Cedric Beust 2015-11-12 13:45:56 -08:00
parent 5a7d730ac5
commit 97a5f54c9a
3 changed files with 84 additions and 17 deletions

View file

@ -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")

View file

@ -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)

View file

@ -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<String, JCenterConfig>()
fun addConfiguration(projectName: String, config: JCenterConfig) {
configurations.put(projectName, config)
private val jcenterConfigurations = hashMapOf<String, JCenterConfig>()
fun addJCenterConfiguration(projectName: String, config: JCenterConfig) {
jcenterConfigurations.put(projectName, config)
}
/**
* Map of project name -> GithubConfiguration
*/
private val githubConfigurations = hashMapOf<String, GithubConfig>()
fun addGithubConfiguration(projectName: String, config: GithubConfig) {
githubConfigurations.put(projectName, config)
}
}
data class GithubConfig(val project: Project) {
var publish: Boolean = false
val files = arrayListOf<File>()
@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)
}
}