diff --git a/src/main/kotlin/com/beust/kobalt/maven/Gpg.kt b/src/main/kotlin/com/beust/kobalt/maven/Gpg.kt new file mode 100644 index 00000000..54a021fb --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/maven/Gpg.kt @@ -0,0 +1,57 @@ +package com.beust.kobalt.maven + +import com.beust.kobalt.misc.log +import com.google.inject.Singleton +import java.io.File + +@Singleton +public class Gpg { + val COMMANDS = listOf("gpg", "gpg2") + + fun findGpgCommand() : String? { + val path = System.getenv("PATH") + if (path != null) { + path.split(File.pathSeparator).forEach { dir -> + COMMANDS.map { File(dir, it) }.firstOrNull { it.exists() }?.let { + return it.absolutePath + } + } + } + return null + } + + /** + * @return the .asc files + */ + fun runGpg(files: List) : List { + val result = arrayListOf() + val gpg = findGpgCommand() + if (gpg != null) { + val directory = files.get(0).parentFile.absoluteFile + files.forEach { file -> + with(File(directory, file.absolutePath + ".asc")) { + delete() + result.add(this) + } + val allArgs = arrayListOf() + allArgs.add(gpg) + allArgs.add("-ab") + allArgs.add(file.absolutePath) + + val pb = ProcessBuilder(allArgs) + pb.directory(directory) + pb.inheritIO() + log(1, "Signing files: " + allArgs.join(" ")) + val process = pb.start() + val errorCode = process.waitFor() + if (errorCode != 0) { + throw KobaltException("Couldn't sign file $file") + } + } + + return files.map { File(it.absolutePath + ".asc") } + } else { + throw KobaltException("Couldn't find the command, is it installed and in your PATH?") + } + } +} diff --git a/src/main/kotlin/com/beust/kobalt/plugin/publish/JCenterApi.kt b/src/main/kotlin/com/beust/kobalt/plugin/publish/JCenterApi.kt index fb4b1b31..6a26f8b0 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/publish/JCenterApi.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/publish/JCenterApi.kt @@ -3,6 +3,7 @@ package com.beust.kobalt.plugin.publish import com.beust.klaxon.* import com.beust.kobalt.api.Project import com.beust.kobalt.internal.TaskResult +import com.beust.kobalt.maven.Gpg import com.beust.kobalt.maven.Http import com.beust.kobalt.maven.KobaltException import com.beust.kobalt.maven.Md5 @@ -43,7 +44,7 @@ open public class UnauthenticatedJCenterApi @Inject constructor(open val http: H public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val username: String?, @Nullable @Assisted("password") val password: String?, - override val http: Http) : UnauthenticatedJCenterApi(http) { + override val http: Http, val gpg: Gpg) : UnauthenticatedJCenterApi(http) { interface IFactory { fun create(@Nullable @Assisted("username") username: String?, @@ -88,24 +89,29 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val .join("/") } - return upload(files, configuration, fileToPath, true) + return upload(files, configuration, fileToPath, generateMd5 = true, generateAsc = true) } - fun uploadFile(file: File, url: String, configuration: JCenterConfiguration, generateMd5: Boolean = false) = - upload(arrayListOf(file), configuration, { - f: File -> "${UnauthenticatedJCenterApi.BINTRAY_URL_API_CONTENT}/$username/generic/$url" - }, generateMd5) + fun uploadFile(file: File, url: String, configuration: JCenterConfiguration, generateMd5: Boolean = false, + generateAsc: Boolean = false) = + upload(arrayListOf(file), configuration, { + f: File -> "${UnauthenticatedJCenterApi.BINTRAY_URL_API_CONTENT}/$username/generic/$url"}, + generateMd5, generateAsc) private fun upload(files: List, configuration : JCenterConfiguration?, fileToPath: (File) -> String, - generateMd5: Boolean = false) : TaskResult { + generateMd5: Boolean = false, generateAsc: Boolean) : TaskResult { val successes = arrayListOf() val failures = hashMapOf() val filesToUpload = arrayListOf() - files.forEach { - // Create the md5 for this file + if (generateAsc) { + // Create the .asc files + filesToUpload.addAll(gpg.runGpg(files)) + } + files.forEach { filesToUpload.add(it) if (generateMd5) { + // Create and upload the md5 for this file with(File(it.absolutePath)) { val md5: String = Md5.toMd5(this) val md5File = File(absolutePath + ".md5")