Skip to content

Commit

Permalink
Remove the generation date, keep the source relative
Browse files Browse the repository at this point in the history
This way the generated code is consistent between runs, even
on different machines (different absolute paths).

Refs #209
  • Loading branch information
raboof committed Oct 20, 2020
1 parent a769228 commit cf4d709
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package play.twirl.compiler

import java.io.File
import java.time.LocalDateTime

import scala.annotation.tailrec
import scala.io.Codec
Expand Down Expand Up @@ -185,7 +184,7 @@ object TwirlCompiler {
templateName,
TwirlIO.readFile(source),
codec,
source.getAbsolutePath,
source.getPath,
resultType,
formatterType,
additionalImports,
Expand Down Expand Up @@ -215,7 +214,7 @@ object TwirlCompiler {
templateName,
content.getBytes(codec.charSet),
codec,
source.getAbsolutePath,
source.getPath,
resultType,
formatterType,
additionalImports,
Expand All @@ -230,7 +229,7 @@ object TwirlCompiler {
templateName: Array[String],
content: Array[Byte],
codec: Codec,
absolutePath: String,
relativePath: String,
resultType: String,
formatterType: String,
additionalImports: collection.Seq[String],
Expand All @@ -241,7 +240,7 @@ object TwirlCompiler {
templateParser.parse(new String(content, codec.charSet)) match {
case templateParser.Success(parsed: Template, rest) if rest.atEnd => {
generateFinalTemplate(
absolutePath,
relativePath,
content,
templateName.dropRight(1).mkString("."),
templateName.takeRight(1).mkString,
Expand All @@ -253,12 +252,12 @@ object TwirlCompiler {
)
}
case templateParser.Success(_, rest) => {
throw new TemplateCompilationError(new File(absolutePath), "Not parsed?", rest.pos.line, rest.pos.column)
throw new TemplateCompilationError(new File(relativePath), "Not parsed?", rest.pos.line, rest.pos.column)
}
case templateParser.Error(_, rest, errors) => {
val firstError = errors.head
throw new TemplateCompilationError(
new File(absolutePath),
new File(relativePath),
firstError.str,
firstError.pos.line,
firstError.pos.column
Expand Down Expand Up @@ -470,7 +469,7 @@ package """ :+ packageName :+ """
}

def generateFinalTemplate(
absolutePath: String,
relativePath: String,
contents: Array[Byte],
packageName: String,
name: String,
Expand All @@ -483,7 +482,7 @@ package """ :+ packageName :+ """
val generated =
generateCode(packageName, name, root, resultType, formatterType, additionalImports, constructorAnnotations)

Source.finalSource(absolutePath, contents, generated, Hash(contents, additionalImports))
Source.finalSource(relativePath, contents, generated, Hash(contents, additionalImports))
}

object TemplateAsFunctionCompiler {
Expand Down Expand Up @@ -710,7 +709,7 @@ object Source {
import scala.collection.mutable.ListBuffer

def finalSource(
absolutePath: String,
relativePath: String,
contents: Array[Byte],
generatedTokens: collection.Seq[Any],
hash: String
Expand All @@ -722,8 +721,7 @@ object Source {
scalaCode.toString + s"""
/*
-- GENERATED --
DATE: ${LocalDateTime.now()}
SOURCE: ${absolutePath.replace(File.separator, "/")}
SOURCE: ${relativePath.replace(File.separator, "/")}
HASH: $hash
MATRIX: ${positions.map(pos => s"${pos._1}->${pos._2}").mkString("|")}
LINES: ${lines.map(line => s"${line._1}->${line._2}").mkString("|")}
Expand Down

0 comments on commit cf4d709

Please sign in to comment.