summaryrefslogtreecommitdiff
path: root/scalalib/src/mill/scalalib/PublishModule.scala
blob: 5c8b35e60fc13dce3a9c315678e660cdff827050 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
package mill
package scalalib

import ammonite.ops._
import mill.define.{ExternalModule, Task}
import mill.eval.{PathRef, Result}
import mill.scalalib.publish.SonatypePublisher
import mill.util.Loose.Agg
/**
  * Configuration necessary for publishing a Scala module to Maven Central or similar
  */
trait PublishModule extends ScalaModule { outer =>
  import mill.scalalib.publish._

  override def moduleDeps = Seq.empty[PublishModule]

  def pomSettings: T[PomSettings]
  def publishVersion: T[String]
  def artifactId: T[String] = T { s"${artifactName()}${artifactSuffix()}" }
  def publishSelfDependency = T{
    Artifact(pomSettings().organization, artifactId(), publishVersion()),
  }

  def publishXmlDeps = T.task{
    val ivyPomDeps = ivyDeps().map(
      Artifact.fromDep(_, scalaVersion(), Lib.scalaBinaryVersion(scalaVersion()))
    )
    val modulePomDeps = Task.sequence(moduleDeps.map(_.publishSelfDependency))()
    ivyPomDeps ++ modulePomDeps.map(Dependency(_, Scope.Compile))
  }
  def pom = T {
    val pom = Pom(artifact(), publishXmlDeps(), artifactId(), pomSettings())
    val pomPath = T.ctx().dest / s"${artifactId()}-${publishVersion()}.pom"
    write.over(pomPath, pom)
    PathRef(pomPath)
  }

  def ivy = T {
    val ivy = Ivy(artifact(), publishXmlDeps())
    val ivyPath = T.ctx().dest / "ivy.xml"
    write.over(ivyPath, ivy)
    PathRef(ivyPath)
  }

  def artifact: T[Artifact] = T {
    Artifact(pomSettings().organization, artifactId(), publishVersion())
  }

  def publishLocal(): define.Command[Unit] = T.command {
    LocalPublisher.publish(
      jar = jar().path,
      sourcesJar = sourcesJar().path,
      docsJar = docsJar().path,
      pom = pom().path,
      ivy = ivy().path,
      artifact = artifact()
    )
  }

  def sonatypeUri: String = "https://oss.sonatype.org/service/local"

  def sonatypeSnapshotUri: String = "https://oss.sonatype.org/content/repositories/snapshots"

  def publishArtifacts = T{
    val baseName = s"${artifactId()}-${publishVersion()}"
    Seq(
      jar().path -> s"$baseName.jar",
      sourcesJar().path -> s"$baseName-sources.jar",
      docsJar().path -> s"$baseName-javadoc.jar",
      pom().path -> s"$baseName.pom"
    )
  }

  def publish(sonatypeCreds: String, gpgPassphrase: String): define.Command[Unit] = T.command {
    new SonatypePublisher(
      sonatypeUri,
      sonatypeSnapshotUri,
      sonatypeCreds,
      gpgPassphrase,
      T.ctx().log
    ).publish(publishArtifacts(), artifact())
  }
}
object PublishModule extends ExternalModule{
  def publishAll(sonatypeCreds: String,
                 gpgPassphrase: String,
                 modules: Seq[PublishModule],
                 sonatypeUri: String = "https://oss.sonatype.org/service/local",
                 sonatypeSnapshotUri: String = "https://oss.sonatype.org/content/repositories/snapshots") = T.task{
    new SonatypePublisher(
      sonatypeUri,
      sonatypeSnapshotUri,
      sonatypeCreds,
      gpgPassphrase,
      T.ctx().log
    ).publishAll(
      Task.traverse(modules)(
        m => T.task{(m.publishArtifacts(), m.artifact())}
      )():_*
    )
  }
  def millDiscover = mill.define.Discover[this.type]
}