From 435267714fd9be710945d5d6c2d0ee1f18294e12 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Fri, 17 Mar 2017 03:36:17 -0400 Subject: Add Scalatex plugin --- stage2/plugins/Scalatex.scala | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 stage2/plugins/Scalatex.scala (limited to 'stage2/plugins') diff --git a/stage2/plugins/Scalatex.scala b/stage2/plugins/Scalatex.scala new file mode 100644 index 0000000..6276332 --- /dev/null +++ b/stage2/plugins/Scalatex.scala @@ -0,0 +1,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" + ) + } + } + } + } +} -- cgit v1.2.3