-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from jprinet/feature/strip-routes-comments
Strip build dependent comments in Play compilers generated files
- Loading branch information
Showing
14 changed files
with
215 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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. | ||
* Here is an example: | ||
* @GENERATOR:play-routes-compiler | ||
* @(DATE): Mon Apr 03 10:27:51 CEST 2023 | ||
* @(SOURCE):/private/var/folders/79/xmc9yr493y75ptry2_nrx3r00000gn/T/junit4995996226044083355/conf/routes | ||
**/ | ||
class DefaultRoutesPostProcessor implements Serializable { | ||
|
||
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)); | ||
} 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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. | ||
* Here is an example: | ||
* -- 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters