Skip to content

Commit

Permalink
Merge pull request #396 from playframework/mergify/bp/1.5.x/pr-378
Browse files Browse the repository at this point in the history
Remove the generation date, keep the source relative (bp #378)
  • Loading branch information
mergify[bot] authored Feb 20, 2021
2 parents 55871df + 5113d4f commit 2c7dfa9
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 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,
relativePath(source),
resultType,
formatterType,
additionalImports,
Expand Down Expand Up @@ -215,7 +214,7 @@ object TwirlCompiler {
templateName,
content.getBytes(codec.charSet),
codec,
source.getAbsolutePath,
relativePath(source),
resultType,
formatterType,
additionalImports,
Expand All @@ -226,11 +225,14 @@ object TwirlCompiler {
generatedSource
}

private def relativePath(file: File): String =
new File(".").toURI.relativize(file.toURI).getPath

def parseAndGenerateCode(
templateName: Array[String],
content: Array[Byte],
codec: Codec,
absolutePath: String,
relativePath: String,
resultType: String,
formatterType: String,
additionalImports: collection.Seq[String],
Expand All @@ -241,7 +243,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 +255,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 @@ -465,7 +467,7 @@ package """ :+ packageName :+ """
}

def generateFinalTemplate(
absolutePath: String,
relativePath: String,
contents: Array[Byte],
packageName: String,
name: String,
Expand All @@ -478,7 +480,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 @@ -704,7 +706,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 @@ -716,8 +718,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 2c7dfa9

Please sign in to comment.