summaryrefslogtreecommitdiff
path: root/contrib/playlib/src/mill/playlib/RouteCompilerWorkerApi.scala
blob: 6be0f3f6c9ba3f0a3cbd07e226d7490de9f2f122 (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
package mill.playlib

import ammonite.ops.Path
import mill._
import mill.api.{Ctx, Result}
import mill.define.{Discover, ExternalModule, Worker}
import mill.playlib.api.RouteCompilerType
import mill.scalalib.api.CompilationResult

class RouteCompilerWorker {
  private var routeCompilerInstanceCache = Option.empty[(Long, mill.playlib.api.RouteCompilerWorkerApi)]

  private def bridge(toolsClasspath: Agg[os.Path])
                    (implicit ctx: Ctx) = {
    val classloaderSig =
      toolsClasspath.map(p => p.toString().hashCode + os.mtime(p)).sum
    routeCompilerInstanceCache match {
      case Some((sig, bridge)) if sig == classloaderSig => bridge
      case _ =>
        val toolsClassPath = toolsClasspath.map(_.toIO.toURI.toURL).toVector
        ctx.log.debug("Loading classes from\n"+toolsClassPath.mkString("\n"))
        val cl = mill.api.ClassLoader.create(
          toolsClassPath,
          getClass.getClassLoader
        )
        val bridge = cl
          .loadClass("mill.playlib.worker.RouteCompilerWorker")
          .getDeclaredConstructor()
          .newInstance()
          .asInstanceOf[mill.playlib.api.RouteCompilerWorkerApi]
        routeCompilerInstanceCache = Some((classloaderSig, bridge))
        bridge
    }
  }


  def compile(routerClasspath: Agg[Path],
              files: Seq[Path],
              additionalImports: Seq[String],
              forwardsRouter: Boolean,
              reverseRouter: Boolean,
              namespaceReverseRouter: Boolean,
              generatorType: RouteCompilerType,
              dest: Path)(implicit ctx: Ctx)
  : Result[CompilationResult] = {
    //the routes file must come last as it can include the routers generated
    //by the others
    bridge(routerClasspath)
      .compile(
        files,
        additionalImports,
        forwardsRouter,
        reverseRouter,
        namespaceReverseRouter,
        generatorType,
        dest
      )(ctx)
  }



}

object RouteCompilerWorkerModule extends ExternalModule {
  def routeCompilerWorker: Worker[RouteCompilerWorker] = T.worker {
    new RouteCompilerWorker()
  }

  lazy val millDiscover = Discover[this.type]
}