-
Notifications
You must be signed in to change notification settings - Fork 41
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
Strip build dependent comments in Play compilers generated files #183
Changes from 13 commits
480e2c6
d7d6b85
0b4e761
6c7e680
482d850
7da3b62
5e87c60
b96b633
1cf9a7e
e9f8d8f
abe09b3
3b7b0fd
909df3e
cb91bae
2940cdd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
=== Generated files comments | ||
|
||
Files generated by Twirl and Route compilers contain comments which are changing across builds (absolute path and date depending on the framework version), this prevents tasks using those files as inputs to benefit from build cache. | ||
The plugin is post-processing those files to remove timestamp and convert absolute paths to relative paths. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.gradle.playframework.tools.internal.routes; | ||
|
||
import org.gradle.util.RelativePathUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
|
||
// This post processor fixes build / project dependent comments (DATE and SOURCE) from the RoutesCompiler generated files: | ||
// @GENERATOR:play-routes-compiler | ||
// @(DATE): Mon Apr 03 10:27:51 CEST 2023 | ||
// @(SOURCE):/private/var/folders/79/xmc9yr493y75ptry2_nrx3r00000gn/T/junit4995996226044083355/conf/routes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: multi-line comments usually use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I adjusted the Javadoc comments 👍 |
||
class DefaultRoutesPostProcessor implements Serializable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a javadoc to clarify what exactly this class does and what it is processing? |
||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRoutesPostProcessor.class); | ||
|
||
void execute(RoutesCompileSpec spec) { | ||
String sourceReplacementString = getSourceReplacementString(spec.getSources(), spec.getProjectDir()); | ||
|
||
try (Stream<Path> stream = Files.find(spec.getDestinationDir().toPath(), Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile())) { | ||
stream.forEach(routeFile -> process(routeFile, sourceReplacementString)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} catch (IOException e) { | ||
LOGGER.warn("Unable to post-process files", e); | ||
} | ||
} | ||
|
||
private String getSourceReplacementString(Iterable<File> sources, File projectDir) { | ||
String sourceReplacementString = ""; | ||
|
||
if(sources.iterator().hasNext()) { | ||
File sourceFile = sources.iterator().next(); | ||
sourceReplacementString = "// @(SOURCE):" + RelativePathUtil.relativePath(projectDir, sourceFile); | ||
} | ||
|
||
return sourceReplacementString; | ||
} | ||
|
||
private void process(Path routeFile, String sourceReplacementString) { | ||
try { | ||
String content = new String(Files.readAllBytes(routeFile), StandardCharsets.UTF_8); | ||
content = content.replaceAll("(?m)^// @(SOURCE):.*", sourceReplacementString); | ||
content = content.replaceAll("(?m)^// @(DATE):.*", ""); | ||
Files.write(routeFile, content.getBytes(StandardCharsets.UTF_8)); | ||
} catch (IOException e) { | ||
LOGGER.warn(String.format("Unable to post-process file %s", routeFile.getFileName()), e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.gradle.playframework.tools.internal.twirl; | ||
|
||
import org.gradle.api.internal.file.RelativeFile; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
// This post processor fixes build / project dependent comments (DATE and SOURCE) from the TwirlCompiler generated files: | ||
// -- GENERATED -- | ||
// DATE: Mon Apr 03 10:27:51 CEST 2023 | ||
// SOURCE: /private/var/folders/79/xmc9yr493y75ptry2_nrx3r00000gn/T/junit4995996226044083355/app/views/test.scala.html | ||
// HASH: 4bbbe5fde39afa0d46da8df7714a136a78170d6a | ||
// MATRIX: 728->1|841->19|869->20|920->45|948->53 | ||
// LINES: 21->1|26->1|26->1|26->1|26->1 | ||
// -- GENERATED -- | ||
class DefaultTwirlPostProcessor implements Serializable { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTwirlPostProcessor.class); | ||
|
||
private static final String GENERATED_TAG = "-- GENERATED --"; | ||
private static final String GENERATED_LINE_PREFIX_DATE = "DATE: "; | ||
private static final String GENERATED_LINE_PREFIX_SOURCE = "SOURCE: "; | ||
|
||
void execute(TwirlCompileSpec spec) { | ||
String sourceReplacementString = getSourceReplacementString(spec.getSources()); | ||
|
||
try (Stream<Path> stream = Files.find(spec.getDestinationDir().toPath(), Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile())) { | ||
stream.forEach(routeFile -> process(routeFile, sourceReplacementString)); | ||
} catch (IOException e) { | ||
LOGGER.warn("Unable to post-process files", e); | ||
} | ||
} | ||
|
||
private String getSourceReplacementString(Iterable<RelativeFile> sources) { | ||
String sourceReplacementString = ""; | ||
|
||
if(sources.iterator().hasNext()) { | ||
RelativeFile sourceFile = sources.iterator().next(); | ||
sourceReplacementString = "SOURCE: " + sourceFile.getRelativePath(); | ||
} | ||
|
||
return sourceReplacementString; | ||
} | ||
|
||
private void process(Path generatedFile, String sourceReplacementString) { | ||
try { | ||
List<String> generatedSourceLines = Files.readAllLines(generatedFile, StandardCharsets.UTF_8); | ||
List<String> updatedSourceLines = new ArrayList<>(); | ||
|
||
boolean isInGeneratedSection = false; | ||
for (String currentLine : generatedSourceLines) { | ||
if(currentLine.contains(GENERATED_TAG)) { | ||
isInGeneratedSection = !isInGeneratedSection; | ||
} | ||
if(isInGeneratedSection && currentLine.contains(GENERATED_LINE_PREFIX_SOURCE)) { | ||
// update path to relative and keep trailing spaces | ||
String updatedLine = currentLine.substring(0, currentLine.indexOf(GENERATED_LINE_PREFIX_SOURCE)) + sourceReplacementString; | ||
updatedSourceLines.add(updatedLine); | ||
} else if(!(isInGeneratedSection && currentLine.contains(GENERATED_LINE_PREFIX_DATE))) { | ||
updatedSourceLines.add(currentLine); | ||
} | ||
} | ||
|
||
Files.write(generatedFile, updatedSourceLines, StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
LOGGER.warn(String.format("Unable to post-process file %s", generatedFile.getFileName()), e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ public static VersionedTwirlCompilerAdapter createAdapter(PlayPlatform playPlatf | |
return new TwirlCompilerAdapterV13X("1.3.13", scalaCompatibilityVersion, playTwirlAdapter); | ||
case PLAY_2_7_X: | ||
default: | ||
return new TwirlCompilerAdapterV13X("1.4.2", scalaCompatibilityVersion, playTwirlAdapter); | ||
return new TwirlCompilerAdapterV13X("1.5.1", scalaCompatibilityVersion, playTwirlAdapter); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that there is no breaking change to address There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there were breaking changes. |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the @(DATE) with post-processing actually makes the content identical