-
Notifications
You must be signed in to change notification settings - Fork 3
/
SbtGpg.scala
92 lines (71 loc) · 2.8 KB
/
SbtGpg.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 gpgWarnOnFailure = settingKey[Boolean](
"If true, only issue a warning when signing fails. If false, error " +
"and fail the build. Defaults to true in publishLocal, false in publish.")
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.")
}
def packagedArtifactsImpl(
arts: Map[Artifact, File],
gpg: Gpg,
warnOnFailure: Boolean)(warn: String => Unit): Map[Artifact, File] = {
val (signatures, failure) = arts.foldLeft((Map[Artifact, File](), false)) {
case ((acc, false), (art, file)) =>
gpg.sign(file) match {
case Some(signed) =>
(acc + (art.withExtension(art.extension + ".asc") -> signed), false)
case None =>
val report: String => Unit =
if (warnOnFailure) warn else sys.error(_)
report("GPG reported an error. Artifacts won't be signed.")
(acc, true)
}
case (pair @ (_, true), _) => pair
}
// if we fail the signing part-way through, we throw out *all* the signatures
if (failure) arts else signatures ++ arts
}
import autoImport._
lazy val gpgSettings: Seq[Setting[_]] = Seq(
gpgWarnOnFailure := false,
publishLocal / gpgWarnOnFailure := true,
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.info(_),
log.warn(_))
}
)
lazy val signingSettings: Seq[Setting[_]] = Seq(
publish / packagedArtifacts := {
packagedArtifactsImpl(
(publish / packagedArtifacts).value,
gpg.value,
(publish / gpgWarnOnFailure).value)(streams.value.log.warn(_))
},
publishLocal / packagedArtifacts := {
packagedArtifactsImpl(
(publishLocal / packagedArtifacts).value,
gpg.value,
(publishLocal / gpgWarnOnFailure).value)(streams.value.log.warn(_))
}
)
override lazy val projectSettings
: Seq[Def.Setting[_]] = gpgSettings ++ signingSettings
}