Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the generation date, keep the source relative #378

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -470,7 +472,7 @@ package """ :+ packageName :+ """
}

def generateFinalTemplate(
absolutePath: String,
relativePath: String,
contents: Array[Byte],
packageName: String,
name: String,
Expand All @@ -483,7 +485,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 +712,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 +724,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