diff --git a/kobalt/src/Build.kt b/kobalt/src/Build.kt index f41e3124..51f55985 100644 --- a/kobalt/src/Build.kt +++ b/kobalt/src/Build.kt @@ -12,7 +12,7 @@ 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.github -import com.beust.kobalt.plugin.publish.jcenter +import com.beust.kobalt.plugin.publish.bintray import com.beust.kobalt.repos import com.beust.kobalt.test import java.io.File @@ -98,7 +98,7 @@ val kobaltPluginApi = kotlinProject { args("-nowarn") } - jcenter { + bintray { publish = true } } @@ -151,7 +151,7 @@ val kobaltApp = kotlinProject(kobaltPluginApi, wrapper) { args("-nowarn") } - jcenter { + bintray { publish = true } diff --git a/src/main/kotlin/com/beust/kobalt/app/MainModule.kt b/src/main/kotlin/com/beust/kobalt/app/MainModule.kt index eb8d4d3d..325d8864 100644 --- a/src/main/kotlin/com/beust/kobalt/app/MainModule.kt +++ b/src/main/kotlin/com/beust/kobalt/app/MainModule.kt @@ -8,7 +8,7 @@ import com.beust.kobalt.maven.Pom import com.beust.kobalt.maven.PomGenerator import com.beust.kobalt.misc.DependencyExecutor import com.beust.kobalt.misc.KobaltExecutors -import com.beust.kobalt.plugin.publish.JCenterApi +import com.beust.kobalt.plugin.publish.BintrayApi import com.google.inject.AbstractModule import com.google.inject.Provider import com.google.inject.Singleton @@ -24,11 +24,12 @@ public open class MainModule(val args: Args) : AbstractModule() { } override fun configure() { + configureTest() val builder = FactoryModuleBuilder() arrayListOf( PomGenerator.IFactory::class.java, - JCenterApi.IFactory::class.java, + BintrayApi.IFactory::class.java, Pom.IFactory::class.java, BuildFileCompiler.IFactory::class.java, ArtifactFetcher.IFactory::class.java) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/publish/JCenterApi.kt b/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt similarity index 76% rename from src/main/kotlin/com/beust/kobalt/plugin/publish/JCenterApi.kt rename to src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt index 7ad6d9e8..71ad66b4 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/publish/JCenterApi.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt @@ -20,72 +20,57 @@ import java.io.File import javax.annotation.Nullable import javax.inject.Inject -data class JCenterPackage(val jo: JsonObject) { +data class BintrayPackage(val jo: JsonObject) { // @Suppress("UNCHECKED_CAST") // val latestPublishedVersion = (jo.get("versions") as JsonArray).get(0) as JsonObject). } -open public class UnauthenticatedJCenterApi @Inject constructor(open val http: Http){ +open public class UnauthenticatedBintrayApi @Inject constructor(open val http: Http) { companion object { const val BINTRAY_URL_API = "https://api.bintray.com" const val BINTRAY_URL_API_CONTENT = BINTRAY_URL_API + "/content" } - class JCenterResponse(val jo: JsonObject?, val errorMessage: String?) + class BintrayResponse(val jo: JsonObject?, val errorMessage: String?) - fun parseResponse(r: Response) : JCenterResponse { + fun parseResponse(r: Response): BintrayResponse { val networkResponse = r.networkResponse() if (networkResponse.code() != 200) { val message = networkResponse.message() val errorObject = JsonParser().parse(r.body().string()).asJsonObject - return JCenterResponse(null, message + ": " + errorObject.get("message").asString) + return BintrayResponse(null, message + ": " + errorObject.get("message").asString) } else { - return JCenterResponse(JsonParser().parse(r.body().string()).asJsonObject, null) + return BintrayResponse(JsonParser().parse(r.body().string()).asJsonObject, null) } } - -// fun getPackage() : JCenterPackage { -// val url = arrayListOf(BINTRAY_URL_API, "packages", "cbeust", "maven", "kobalt").joinToString("/") -// val response = http.get(url).getAsString() -// val result = parseResponse(response) -// return JCenterPackage(result) -// } } -public class JCenterApi @Inject constructor ( +class BintrayApi @Inject constructor ( @Nullable @Assisted("username") val username: String?, @Nullable @Assisted("password") val password: String?, @Nullable @Assisted("org") val org: String?, - override val http: Http, val gpg: Gpg, val executors: KobaltExecutors) : UnauthenticatedJCenterApi(http) { + override val http: Http, val gpg: Gpg, val executors: KobaltExecutors) : UnauthenticatedBintrayApi(http) { interface IFactory { fun create(@Nullable @Assisted("username") username: String?, @Nullable @Assisted("password") password: String?, - @Nullable @Assisted("org") org: String?) : JCenterApi + @Nullable @Assisted("org") org: String?) : BintrayApi } fun packageExists(packageName: String) : Boolean { - val url = arrayListOf(UnauthenticatedJCenterApi.BINTRAY_URL_API, "packages", org ?: username!!, + val url = arrayListOf(UnauthenticatedBintrayApi.BINTRAY_URL_API, "packages", org ?: username!!, "maven", packageName) .joinToString("/") val jcResponse = parseResponse(http.get(username, password, url)) if (jcResponse.errorMessage != null) { - throw KobaltException("Error from JCenter: ${jcResponse.errorMessage}") + throw KobaltException("Error from Bintray: ${jcResponse.errorMessage}") } return jcResponse.jo!!.get("name").asString == packageName } -// class ForPost(val name: String, val license: Array) -// -// fun createPackage(packageName: String) : String { -// val url = arrayListOf(UnauthenticatedJCenterApi.BINTRAY_URL_API, "packages", username!!, "maven").join("/") -// val jsonString = Gson().toJson(ForPost(packageName, arrayOf("Apache 2.0"))) -// return http.post(username, password, url, jsonString) -// } - - fun uploadMaven(project: Project, files: List, config: JCenterConfig?) : TaskResult { + fun uploadMaven(project: Project, files: List, config: BintrayConfig?) : TaskResult { if (! packageExists(project.name)) { throw KobaltException("Couldn't find a package called ${project.name} on bintray, please create one first" + " as explained at https://bintray.com/docs/usermanual/uploads/uploads_creatinganewpackage.html") @@ -93,7 +78,7 @@ public class JCenterApi @Inject constructor ( val fileToPath: (File) -> String = { f: File -> arrayListOf( - UnauthenticatedJCenterApi.BINTRAY_URL_API_CONTENT, + UnauthenticatedBintrayApi.BINTRAY_URL_API_CONTENT, org ?: username!!, "maven", project.name, @@ -108,12 +93,12 @@ public class JCenterApi @Inject constructor ( return upload(files, config, fileToPath, generateMd5 = true) } - fun uploadFile(file: File, url: String, config: JCenterConfig, generateMd5: Boolean = false) = + fun uploadFile(file: File, url: String, config: BintrayConfig?, generateMd5: Boolean = false) = upload(arrayListOf(file), config, { - f: File -> "${UnauthenticatedJCenterApi.BINTRAY_URL_API_CONTENT}/${org ?: username}/generic/$url"}, + f: File -> "${UnauthenticatedBintrayApi.BINTRAY_URL_API_CONTENT}/${org ?: username}/generic/$url"}, generateMd5) - private fun upload(files: List, config: JCenterConfig?, fileToPath: (File) -> String, + private fun upload(files: List, config: BintrayConfig?, fileToPath: (File) -> String, generateMd5: Boolean = false) : TaskResult { val filesToUpload = arrayListOf() @@ -146,7 +131,7 @@ public class JCenterApi @Inject constructor ( } // - // Uploads can'be done in parallel or JCenter rejects them + // Uploads can'be done in parallel or Bintray rejects them // val fileCount = filesToUpload.size if (fileCount > 0) { @@ -165,7 +150,7 @@ public class JCenterApi @Inject constructor ( filesToUpload.forEach { file -> http.uploadFile(username, password, fileToPath(file) + optionPath, TypedFile(MediaType.ANY_APPLICATION_TYPE.toString(), file), - post = false, // JCenter requires PUT + post = false, // Bintray requires PUT success = { r: Response -> results.add(true) }, error = { r: Response -> results.add(false) 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 38d2080e..bd892d88 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt @@ -16,14 +16,14 @@ import javax.inject.Singleton @Suppress("VARIABLE_WITH_REDUNDANT_INITIALIZER") @Singleton public class PublishPlugin @Inject constructor(val files: KFiles, val factory: PomGenerator.IFactory, - val jcenterFactory: JCenterApi.IFactory, val github: GithubApi, val localProperties: LocalProperties) + val bintrayFactory: BintrayApi.IFactory, val github: GithubApi, val localProperties: LocalProperties) : BasePlugin() { override val name = PLUGIN_NAME companion object { const val PLUGIN_NAME = "Publish" - private const val TASK_UPLOAD_JCENTER = "uploadJcenter" + private const val TASK_UPLOAD_BINTRAY = "uploadBintray" private const val TASK_UPLOAD_GITHUB = "uploadGithub" private const val TASK_GENERATE_POM = "generatePom" @@ -55,47 +55,22 @@ public class PublishPlugin @Inject constructor(val files: KFiles, val factory: P return result } - @Task(name = TASK_UPLOAD_JCENTER, description = "Upload files to JCenter", + @Task(name = TASK_UPLOAD_BINTRAY, description = "Upload files to Bintray", runAfter = arrayOf(TASK_GENERATE_POM)) - fun taskUploadJcenter(project: Project): TaskResult { + fun taskUploadBintray(project: Project): TaskResult { validateProject(project) - return uploadJcenter(project) + return uploadBintray(project) } - @Task(name = TASK_UPLOAD_GITHUB, description = "Upload files to Github", - runAfter = arrayOf(TASK_GENERATE_POM)) - fun taskUploadGithub(project: Project): TaskResult { - validateProject(project) - return uploadGithub(project) - } - - private fun uploadGithub(project: Project) : TaskResult { - val configuration = githubConfigurations[project.name] - - // - // Upload individual files, if applicable - // - if (configuration != null) { - configuration.files.forEach { - log(2, "Uploading $it tag: ${project.version}") - github.uploadRelease(project.name, project.version!!, it) - } - } else { - warn("Couldn't find any github{} configuration, not uploading anything") - } - - return TaskResult() - } - - private fun uploadJcenter(project: Project) : TaskResult { + private fun uploadBintray(project: Project) : TaskResult { val docUrl = DocUrl.PUBLISH_PLUGIN_URL val user = localProperties.get(PROPERTY_BINTRAY_USER, docUrl) val password = localProperties.get(PROPERTY_BINTRAY_PASSWORD, docUrl) val org = localProperties.getNoThrows(PROPERTY_BINTRAY_ORG, docUrl) - val jcenter = jcenterFactory.create(user, password, org) + val jcenter = bintrayFactory.create(user, password, org) var success = false - val configuration = jcenterConfigurations[project.name] + val configuration = bintrayConfigurations[project.name] val messages = arrayListOf() if (configuration != null) { @@ -125,12 +100,37 @@ public class PublishPlugin @Inject constructor(val files: KFiles, val factory: P return TaskResult(success, messages.joinToString("\n ")) } + @Task(name = TASK_UPLOAD_GITHUB, description = "Upload files to Github", + runAfter = arrayOf(TASK_GENERATE_POM)) + fun taskUploadGithub(project: Project): TaskResult { + validateProject(project) + return uploadGithub(project) + } + + private fun uploadGithub(project: Project) : TaskResult { + val configuration = githubConfigurations[project.name] + + // + // Upload individual files, if applicable + // + if (configuration != null) { + configuration.files.forEach { + log(2, "Uploading $it tag: ${project.version}") + github.uploadRelease(project.name, project.version!!, it) + } + } else { + warn("Couldn't find any github{} configuration, not uploading anything") + } + + return TaskResult() + } + /** - * Map of project name -> JCenterConfiguration + * Map of project name -> BintrayConfig */ - private val jcenterConfigurations = hashMapOf() - fun addJCenterConfiguration(projectName: String, config: JCenterConfig) { - jcenterConfigurations.put(projectName, config) + private val bintrayConfigurations = hashMapOf() + fun addBintrayConfiguration(projectName: String, config: BintrayConfig) { + bintrayConfigurations.put(projectName, config) } /** @@ -159,18 +159,18 @@ public fun Project.github(init: GithubConfig.() -> Unit) { } } -data class JCenterConfig(val project: Project) { +data class BintrayConfig(val project: Project) { /** * If true, the uploaded file will be published in your personal space (e.g. https://dl.bintray.com/cbeust/maven). * Once the file is uploaded there, it can be automatically synchronized to JCenter by linking your project to - * JCenter on the bintray web site. By default, files are not published. + * Bintray. By default, files are not published. */ @Directive var publish: Boolean = false /** * If true, sign the files with GPG. This is only required if you plan to later synchronize these files - * from JCenter to Maven Central. Keep this to false if you are only interested in uploading to JCenter. + * from Bintray to Maven Central. Keep this to false if you are only interested in uploading to JCenter. */ @Directive var sign: Boolean = false @@ -184,9 +184,9 @@ data class JCenterConfig(val project: Project) { } @Directive -public fun Project.jcenter(init: JCenterConfig.() -> Unit) { - with(JCenterConfig(this)) { +public fun Project.bintray(init: BintrayConfig.() -> Unit) { + with(BintrayConfig(this)) { init() - (Kobalt.findPlugin(PublishPlugin.PLUGIN_NAME) as PublishPlugin).addJCenterConfiguration(name, this) + (Kobalt.findPlugin(PublishPlugin.PLUGIN_NAME) as PublishPlugin).addBintrayConfiguration(name, this) } }