aboutsummaryrefslogtreecommitdiff
path: root/stage2/plugins/Scalatex.scala
blob: 62763320ff07a836605d71cc1d2197d7f315089c (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
package cbt
import java.io.File
import java.nio.file.Files
trait Scalatex extends BaseBuild {
  override def dependencies = super.dependencies :+ libraries.cbt.scalatex

  def scalatex = (
    Scalatex.apply(
      lib,
      Math.max( context.cbtLastModified, ( dependencies ++ context.parentBuild ).map( _.lastModified ).max )
    )
    .config(
      lib.autoRelative(
        lib.sourceFiles( sources, _.string endsWith ".scalatex" )
          ++ projectDirectory.listFiles.toVector.filter( _.toString.endsWith( ".scalatex" ) )
      ).map {
          case ( file, name ) => file -> name.stripPrefix( File.separator ).stripSuffix( ".scalatex" )
        },
      lib.autoRelative( Seq( projectDirectory / "static" ) ),
      target / "html",
      projectDirectory / "src_generated" / "scalatex",
      dependencyClasspath
    )
  )
}
/** @param htmlTarget directory where the html files are generated
  */
object Scalatex {
  case class apply(
    lib: Lib, parentsLastModified: Long
  )(
    implicit
    logger: Logger, transientCache: java.util.Map[AnyRef, AnyRef], classLoaderCache: cbt.ClassLoaderCache
  ) {
    case class config(
      scalatexFiles:       Seq[( File, String )],
      staticFiles:         Seq[( File, String )],
      htmlTarget:          File,
      srcGenerated:        File,
      dependencyClasspath: ClassPath,
      transform:           String                = "cbt.plugins.scalatex.runtime.layout", // function String => String
      linkStaticFile:      Boolean               = true,
      `package`:           String                = "cbt.plugins.scalatex.runtime"
    ) {
      def apply = {
        staticFiles.map {
          case ( file, relative ) => file -> htmlTarget / relative
        }.collect {
          case ( from, to ) if from.isDirectory                => to.mkdirs
          case ( from, to ) if !from.isDirectory && !to.exists => Files.createLink( to.toPath, from.toPath )
        }
        lib.cached(
          srcGenerated,
          ( parentsLastModified +: scalatexFiles.map( _._1 ).map( _.lastModified ) ).max
        ) { () =>
          lib.writeIfChanged(
            srcGenerated / "ScalatexGenerated.scala",
            s"""// Last generated at ${java.time.ZonedDateTime.now}
package ${`package`}
object ScalatexGenerate extends cbt.plugins.scalatex.runtime.AbstractMain{
  def dependencyClasspath: Seq[java.io.File] = Seq(
    ${dependencyClasspath.files.map( _.quote ).mkString( ",\n    " )}
  )
  def htmlTarget = ${htmlTarget.quote}
  import scalatags.Text.all._
  def files = Seq[(String, scalatags.Text.all.SeqFrag[scalatags.Text.all.Frag])](
    ${scalatexFiles.map { case ( from, to ) => s"${to.quote} -> scalatex.twf(${from.string.quote})" }.mkString( ",\n    " )}
  )
  def render: scalatags.Text.all.SeqFrag[scalatags.Text.all.Frag] => String => String = $transform
}
""".trim ++ "\n"
          )
        }
      }
    }
  }
}