summaryrefslogtreecommitdiff
path: root/scalalib/src/mill/scalalib/ScalaWorkerApi.scala
blob: 0b6a931bd1bed4ccaac55683213f80dfb9f7b287 (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
package mill.scalalib


import ammonite.ops.Path
import coursier.Cache
import coursier.maven.MavenRepository
import mill.Agg
import mill.T
import mill.define.{Discover, Worker}
import mill.scalalib.Lib.resolveDependencies
import mill.util.Loose
import mill.util.JsonFormatters._

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

  def classpath = T{
    val scalaWorkerJar = sys.props("MILL_SCALA_WORKER")
    if (scalaWorkerJar != null) {
      mill.eval.Result.Success(Loose.Agg.from(scalaWorkerJar.split(',').map(Path(_))))
    } else {
      resolveDependencies(
        repositories,
        Lib.depToDependency(_, "2.12.4", ""),
        Seq(ivy"com.lihaoyi::mill-scalaworker:${sys.props("MILL_VERSION")}")
      ).map(_.map(_.path))
    }
  }
  def worker: Worker[ScalaWorkerApi] = T.worker{
    val cl = mill.util.ClassLoader.create(
      classpath().map(_.toNIO.toUri.toURL).toVector,
      getClass.getClassLoader
    )
    val cls = cl.loadClass("mill.scalaworker.ScalaWorker")
    val instance = cls.getConstructor(classOf[mill.util.Ctx], classOf[Array[String]])
      .newInstance(T.ctx(), compilerInterfaceClasspath().map(_.path.toString).toArray[String])
    instance.asInstanceOf[ScalaWorkerApi]
  }

  def compilerInterfaceClasspath = T{
    resolveDependencies(
      repositories,
      Lib.depToDependency(_, "2.12.4", ""),
      Seq(ivy"org.scala-sbt:compiler-interface:1.1.0")
    )
  }

}

trait ScalaWorkerApi {

  def compileScala(scalaVersion: String,
                   sources: Agg[Path],
                   compilerBridgeSources: Path,
                   compileClasspath: Agg[Path],
                   compilerClasspath: Agg[Path],
                   scalacOptions: Seq[String],
                   scalacPluginClasspath: Agg[Path],
                   javacOptions: Seq[String],
                   upstreamCompileOutput: Seq[CompilationResult])
                  (implicit ctx: mill.util.Ctx): mill.eval.Result[CompilationResult]


  def discoverMainClasses(compilationResult: CompilationResult)
                         (implicit ctx: mill.util.Ctx): Seq[String]
}