|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Lightbend Inc. <https://www.lightbend.com> |
| 3 | + */ |
| 4 | + |
| 5 | +import java.util.concurrent.atomic.AtomicBoolean |
| 6 | + |
| 7 | +import scala.language.postfixOps |
| 8 | + |
| 9 | +import sbt.{Def, _} |
| 10 | +import Keys._ |
| 11 | +import com.geirsson.CiReleasePlugin |
| 12 | +import com.jsuereth.sbtpgp.PgpKeys.publishSigned |
| 13 | +import xerial.sbt.Sonatype.autoImport.sonatypeProfileName |
| 14 | + |
| 15 | +/** |
| 16 | + * For projects that are not published. |
| 17 | + */ |
| 18 | +object NoPublish extends AutoPlugin { |
| 19 | + override def requires = plugins.JvmPlugin |
| 20 | + |
| 21 | + override def projectSettings = |
| 22 | + Seq(publish / skip := true, publishArtifact := false, publish := {}, publishLocal := {}) |
| 23 | +} |
| 24 | + |
| 25 | +object Publish extends AutoPlugin { |
| 26 | + override def requires = plugins.JvmPlugin && Common |
| 27 | + override def trigger = AllRequirements |
| 28 | + |
| 29 | + lazy val beforePublishTask = taskKey[Unit]("setup before publish") |
| 30 | + |
| 31 | + lazy val beforePublishDone = new AtomicBoolean(false) |
| 32 | + |
| 33 | + def beforePublish(snapshot: Boolean) = { |
| 34 | + if (beforePublishDone.compareAndSet(false, true)) { |
| 35 | + CiReleasePlugin.setupGpg() |
| 36 | + if (!snapshot) |
| 37 | + cloudsmithCredentials(validate = true) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + override def projectSettings: Seq[Def.Setting[_]] = |
| 42 | + Seq( |
| 43 | + sonatypeProfileName := "com.lightbend", |
| 44 | + beforePublishTask := beforePublish(isSnapshot.value), |
| 45 | + publishSigned := publishSigned.dependsOn(beforePublishTask).value, |
| 46 | + publishTo := (if (isSnapshot.value) |
| 47 | + Some(Resolver.file("file", target.value / "repository")) // FIXME snapshot repo |
| 48 | + else |
| 49 | + Some("Cloudsmith API".at("https://maven.cloudsmith.io/lightbend/akka/"))), |
| 50 | + credentials ++= (if (isSnapshot.value) Seq[Credentials]() else cloudsmithCredentials(validate = false)) |
| 51 | + ) |
| 52 | + |
| 53 | + def cloudsmithCredentials(validate: Boolean): Seq[Credentials] = { |
| 54 | + (sys.env.get("PUBLISH_USER"), sys.env.get("PUBLISH_PASSWORD")) match { |
| 55 | + case (Some(user), Some(password)) => |
| 56 | + Seq(Credentials("Cloudsmith API", "maven.cloudsmith.io", user, password)) |
| 57 | + case _ => |
| 58 | + if (validate) |
| 59 | + throw new Exception("Publishing credentials expected in `PUBLISH_USER` and `PUBLISH_PASSWORD`.") |
| 60 | + else |
| 61 | + Nil |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments