summaryrefslogtreecommitdiff
path: root/scalalib/src/ZincWorkerModule.scala
blob: 4c94102cdcf0b15a368c63d9389c5214c46a0bb8 (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
104
105
106
107
108
109
110
111
package mill.scalalib


import coursier.maven.MavenRepository
import mill.Agg
import mill.T
import mill.api.KeyedLockedCache
import mill.define.{Discover, Worker}
import mill.scalalib.Lib.resolveDependencies
import mill.scalalib.api.Util.isDotty
import mill.scalalib.api.ZincWorkerApi
import mill.api.Loose
import mill.util.JsonFormatters._

object ZincWorkerModule extends mill.define.ExternalModule with ZincWorkerModule{
  lazy val millDiscover = Discover[this.type]
}
trait ZincWorkerModule extends mill.Module{
  def repositories = Seq(
    coursier.LocalRepositories.ivy2Local,
    MavenRepository("https://repo1.maven.org/maven2"),
    MavenRepository("https://oss.sonatype.org/content/repositories/releases")
  )

  def classpath = T{
    mill.modules.Util.millProjectModule("MILL_SCALA_WORKER", "mill-scalalib-worker", repositories)
  }

  def scalalibClasspath = T{
    mill.modules.Util.millProjectModule("MILL_SCALA_LIB", "mill-scalalib", repositories)
  }

  def backgroundWrapperClasspath = T{
    mill.modules.Util.millProjectModule(
      "MILL_BACKGROUNDWRAPPER", "mill-scalalib-backgroundwrapper",
      repositories, artifactSuffix = ""
    )
  }

  def worker: Worker[mill.scalalib.api.ZincWorkerApi] = T.worker{
    val cl = mill.api.ClassLoader.create(
      classpath().map(_.path.toNIO.toUri.toURL).toVector,
      getClass.getClassLoader
    )
    val cls = cl.loadClass("mill.scalalib.worker.ZincWorkerImpl")
    val instance = cls.getConstructor(
      classOf[
        Either[
          (ZincWorkerApi.Ctx, Array[os.Path], (String, String) => os.Path),
          String => os.Path
        ]
      ],
      classOf[(Agg[os.Path], String) => os.Path],
      classOf[(Agg[os.Path], String) => os.Path],
      classOf[KeyedLockedCache[_]],
      classOf[Boolean]
    )
      .newInstance(
        Left((
          T.ctx(),
          compilerInterfaceClasspath().map(_.path).toArray,
          (x: String, y: String) => scalaCompilerBridgeSourceJar(x, y).asSuccess.get.value
        )),
        mill.scalalib.api.Util.grepJar(_, "scala-library", _, sources = false),
        mill.scalalib.api.Util.grepJar(_, "scala-compiler", _, sources = false),
        new KeyedLockedCache.RandomBoundedCache(1, 1),
        false.asInstanceOf[AnyRef]
      )
    instance.asInstanceOf[mill.scalalib.api.ZincWorkerApi]
  }

  def scalaCompilerBridgeSourceJar(scalaVersion: String,
                                   scalaOrganization: String) = {
    val (scalaVersion0, scalaBinaryVersion0) = scalaVersion match {
      case s if s.startsWith("2.13.") => ("2.13.0-M2", "2.13.0-M2")
      case _ => (scalaVersion, mill.scalalib.api.Util.scalaBinaryVersion(scalaVersion))
    }

    val (bridgeDep, bridgeName, bridgeVersion) =
      if (isDotty(scalaVersion0)) {
        val org = scalaOrganization
        val name = "dotty-sbt-bridge"
        val version = scalaVersion
        (ivy"$org:$name:$version", name, version)
      } else {
        val org = "org.scala-sbt"
        val name = "compiler-bridge"
        val version = Versions.zinc
        (ivy"$org::$name:$version", s"${name}_$scalaBinaryVersion0", version)
      }

    resolveDependencies(
      repositories,
      Lib.depToDependency(_, scalaVersion0, ""),
      Seq(bridgeDep),
      sources = true
    ).map(deps =>
      mill.scalalib.api.Util.grepJar(deps.map(_.path), bridgeName, bridgeVersion, sources = true)
    )
  }

  def compilerInterfaceClasspath = T{
    resolveDependencies(
      repositories,
      Lib.depToDependency(_, "2.12.4", ""),
      Seq(ivy"org.scala-sbt:compiler-interface:${Versions.zinc}"),
      ctx = Some(implicitly[mill.util.Ctx.Log])
    )
  }

}