diff options
author | Jakob Odersky <jakob@odersky.com> | 2018-04-10 21:37:07 -0700 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2018-04-10 21:37:07 -0700 |
commit | 358777fad2afbf7a7f3719367e1f4b0d73c2a42e (patch) | |
tree | 0d1d7a64a1b180b8e9beb87e15b12b111b7fc061 /src/main | |
download | sbt-gpg-358777fad2afbf7a7f3719367e1f4b0d73c2a42e.tar.gz sbt-gpg-358777fad2afbf7a7f3719367e1f4b0d73c2a42e.tar.bz2 sbt-gpg-358777fad2afbf7a7f3719367e1f4b0d73c2a42e.zip |
Initial commit
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/scala/Gpg.scala | 36 | ||||
-rw-r--r-- | src/main/scala/SbtGpg.scala | 58 |
2 files changed, 94 insertions, 0 deletions
diff --git a/src/main/scala/Gpg.scala b/src/main/scala/Gpg.scala new file mode 100644 index 0000000..cde4dba --- /dev/null +++ b/src/main/scala/Gpg.scala @@ -0,0 +1,36 @@ +package io.crashbox.gpg + +import java.io.File + +import scala.util.control.NonFatal +import sys.process._ + +class Gpg( + command: String, + options: Seq[String] = Seq.empty, + keyId: Option[String] = None)(log: String => Unit = System.err.println) { + + def run(params: String*): Int = + try { + val idOption = keyId.toSeq.flatMap(id => Seq("--local-user", id)) + val process = Process(command, options ++ idOption ++ params).run() + process.exitValue() + } catch { + case NonFatal(ex) => + log(ex.getMessage) + 127 + } + + def sign(file: File): Option[File] = { + val out = new File(file.getAbsolutePath + ".asc") + run("--armor", + "--output", + out.getAbsolutePath, + "--detach-sign", + file.getAbsolutePath) match { + case 0 => Some(out) + case _ => None + } + } + +} diff --git a/src/main/scala/SbtGpg.scala b/src/main/scala/SbtGpg.scala new file mode 100644 index 0000000..a015677 --- /dev/null +++ b/src/main/scala/SbtGpg.scala @@ -0,0 +1,58 @@ +package io.crashbox.gpg + +import sbt.{AutoPlugin, Def, _} +import sbt.Keys._ +import sbt.plugins.JvmPlugin + +object SbtGpg extends AutoPlugin { + + override def requires = JvmPlugin + override def trigger = allRequirements + + object autoImport { + val gpgCommand = settingKey[String]("Path to GnuPG executable.") + val gpgOptions = + settingKey[Seq[String]]("Additional global options to pass to gpg.") + val gpgKey = taskKey[Option[String]]( + "Key ID used to sign artifacts. Setting this to None will " + + "cause sbt-gpg to fall back to using gpg's default key. When set, " + + "it is equivalent to gpg's `--local-user` option.") + val gpg = + taskKey[Gpg]("Utility wrapper to the underlying gpg executable.") + } + import autoImport._ + + lazy val gpgSettings: Seq[Setting[_]] = Seq( + gpgCommand := "gpg", + gpgOptions := Seq("--yes"), + gpgKey := Credentials.forHost(credentials.value, "gpg").map(_.userName), + gpg := { + val log = streams.value.log + new Gpg(gpgCommand.value, gpgOptions.value, gpgKey.value)(log.warn(_)) + } + ) + + lazy val signingSettings: Seq[Setting[_]] = Seq( + packagedArtifacts := { + val log = streams.value.log + val arts: Map[Artifact, File] = packagedArtifacts.value + var failed = false + arts.map { + case (art, file) if !failed => + gpg.value.sign(file) match { + case Some(signed) => + art.withExtension(art.extension + ".asc") -> signed + case None => + log.warn("GPG reported an error. Artifacts won't be signed.") + failed = true + art -> file + } + case (art, file) => art -> file + } + } + ) + + override lazy val projectSettings + : Seq[Def.Setting[_]] = gpgSettings ++ signingSettings + +} |