Skip to content

Commit

Permalink
fix mima warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ahjohannessen committed Feb 23, 2024
1 parent c9bef0a commit 3190e1e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static Optional<File> compile(
sourceDirectory,
generatedDirectory,
formatterType,
scalaVersion,
scala.Option.apply(scalaVersion),
scalaAdditionalImports,
scalaConstructorAnnotations,
codec,
Expand Down
137 changes: 127 additions & 10 deletions compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ case class GeneratedSourceVirtual(path: String) extends AbstractGeneratedSource

object TwirlCompiler {

// For constants that depends on the Scala 2 or 3 mode.
// For constants that depend on Scala 2 or 3 mode.
private[compiler] class ScalaCompat(emitScala3Sources: Boolean) {
val varargSplicesSyntax: String =
if (emitScala3Sources) "*" else ": _*"
}

private[compiler] object ScalaCompat {
def apply(scalaVersion: String): ScalaCompat =
new ScalaCompat(scalaVersion.startsWith("3."))
def apply(scalaVersion: Option[String]): ScalaCompat =
new ScalaCompat(scalaVersion.exists(_.startsWith("3.")))
}

def defaultImports(scalaVersion: String) = {
Expand Down Expand Up @@ -206,12 +206,34 @@ object TwirlCompiler {
sourceDirectory: File,
generatedDirectory: File,
formatterType: String,
scalaVersion: String,
additionalImports: collection.Seq[String] = Nil,
constructorAnnotations: collection.Seq[String] = Nil,
codec: Codec = TwirlIO.defaultCodec,
inclusiveDot: Boolean = false
) = {
): Option[File] =
compile(
source,
sourceDirectory,
generatedDirectory,
formatterType,
None,
additionalImports,
constructorAnnotations,
codec,
inclusiveDot
)

def compile(
source: File,
sourceDirectory: File,
generatedDirectory: File,
formatterType: String,
scalaVersion: Option[String],
additionalImports: collection.Seq[String],
constructorAnnotations: collection.Seq[String],
codec: Codec,
inclusiveDot: Boolean
): Option[File] = {
val resultType = formatterType + ".Appendable"
val (templateName, generatedSource) =
generatedFile(source, codec, sourceDirectory, generatedDirectory, inclusiveDot)
Expand Down Expand Up @@ -241,12 +263,36 @@ object TwirlCompiler {
sourceDirectory: File,
resultType: String,
formatterType: String,
scalaVersion: String,
additionalImports: collection.Seq[String] = Nil,
constructorAnnotations: collection.Seq[String] = Nil,
codec: Codec = TwirlIO.defaultCodec,
inclusiveDot: Boolean = false
) = {
): GeneratedSourceVirtual =
compileVirtual(
content,
source,
sourceDirectory,
resultType,
formatterType,
None,
additionalImports,
constructorAnnotations,
codec,
inclusiveDot
)

def compileVirtual(
content: String,
source: File,
sourceDirectory: File,
resultType: String,
formatterType: String,
scalaVersion: Option[String],
additionalImports: collection.Seq[String],
constructorAnnotations: collection.Seq[String],
codec: Codec,
inclusiveDot: Boolean
): GeneratedSourceVirtual = {

val (templateName, generatedSource) = generatedFileVirtual(source, sourceDirectory, inclusiveDot)
val generated = parseAndGenerateCode(
Expand All @@ -268,18 +314,41 @@ object TwirlCompiler {
private def relativePath(file: File): String =
new File(".").toURI.relativize(file.toURI).getPath

def parseAndGenerateCode(
templateName: Array[String],
content: Array[Byte],
codec: Codec,
relativePath: String,
resultType: String,
formatterType: String,
additionalImports: collection.Seq[String],
constructorAnnotations: collection.Seq[String],
inclusiveDot: Boolean
): String = parseAndGenerateCode(
templateName,
content,
codec,
relativePath,
resultType,
formatterType,
None,
additionalImports,
constructorAnnotations,
inclusiveDot
)

private def parseAndGenerateCode(
templateName: Array[String],
content: Array[Byte],
codec: Codec,
relativePath: String,
resultType: String,
formatterType: String,
scalaVersion: String,
scalaVersion: Option[String],
additionalImports: collection.Seq[String],
constructorAnnotations: collection.Seq[String],
inclusiveDot: Boolean
) = {
): String = {
val templateParser = new TwirlParser(inclusiveDot)
templateParser.parse(new String(content, codec.charSet)) match {
case templateParser.Success(parsed: Template, rest) if rest.atEnd() => {
Expand Down Expand Up @@ -459,6 +528,25 @@ object TwirlCompiler {
}

def generateCode(
packageName: String,
name: String,
root: Template,
resultType: String,
formatterType: String,
additionalImports: collection.Seq[String],
constructorAnnotations: collection.Seq[String]
): collection.Seq[Any] = generateCode(
packageName,
name,
root,
resultType,
formatterType,
ScalaCompat(None),
additionalImports,
constructorAnnotations
)

private def generateCode(
packageName: String,
name: String,
root: Template,
Expand Down Expand Up @@ -519,7 +607,30 @@ package """ :+ packageName :+ """
templateImports.map(_.replace("%format%", extension))
}

private[compiler] def generateFinalTemplate(
def generateFinalTemplate(
relativePath: String,
contents: Array[Byte],
packageName: String,
name: String,
root: Template,
resultType: String,
formatterType: String,
additionalImports: collection.Seq[String],
constructorAnnotations: collection.Seq[String]
): String = generateFinalTemplate(
relativePath,
contents,
packageName,
name,
root,
resultType,
formatterType,
ScalaCompat(None),
additionalImports,
constructorAnnotations
)

private def generateFinalTemplate(
relativePath: String,
contents: Array[Byte],
packageName: String,
Expand Down Expand Up @@ -561,6 +672,12 @@ package """ :+ packageName :+ """
}
}

def getFunctionMapping(
signature: String,
returnType: String,
): (String, String, String) =
getFunctionMapping(signature, returnType, ScalaCompat(None))

private[compiler] def getFunctionMapping(
signature: String,
returnType: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package play.twirl.compiler
package test

import java.io._
import play.twirl.parser.TwirlIO

object Helper {
case class CompilationError(message: String, line: Int, column: Int) extends RuntimeException(message)
Expand Down Expand Up @@ -99,8 +100,11 @@ object Helper {
sourceDir,
generatedDir,
"play.twirl.api.HtmlFormat",
scalaVersion,
additionalImports = TwirlCompiler.defaultImports(scalaVersion) ++ additionalImports
Option(scalaVersion),
additionalImports = TwirlCompiler.defaultImports(scalaVersion) ++ additionalImports,
constructorAnnotations = Nil,
codec = TwirlIO.defaultCodec,
inclusiveDot = false
)

val mapper = GeneratedSource(generated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import dotty.tools.io.PlainDirectory
import dotty.tools.io.Directory
import dotty.tools.io.ClassPath
import scala.jdk.CollectionConverters._
import play.twirl.parser.TwirlIO

object Helper {
case class CompilationError(message: String, line: Int, column: Int) extends RuntimeException(message)
Expand Down Expand Up @@ -65,8 +66,11 @@ object Helper {
sourceDir,
generatedDir,
"play.twirl.api.HtmlFormat",
scalaVersion,
additionalImports = TwirlCompiler.defaultImports(scalaVersion) ++ additionalImports
Option(scalaVersion),
additionalImports = TwirlCompiler.defaultImports(scalaVersion) ++ additionalImports,
constructorAnnotations = Nil,
codec = TwirlIO.defaultCodec,
inclusiveDot = false
)

val generated = generatedOpt.getOrElse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object TemplateCompiler {
sourceDirectory,
targetDirectory,
format,
scalaVersion,
Some(scalaVersion),
imports,
constructorAnnotations,
codec,
Expand Down

0 comments on commit 3190e1e

Please sign in to comment.