From 480e2c60b4978ce7f416ee5f540d79679228c41a Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Fri, 24 Mar 2023 16:43:15 +0100 Subject: [PATCH 01/15] Add configuration to strip build dependent comments in generated routes files --- .../asciidoc/30-plugin-configuration.adoc | 4 +- .../asciidoc/36-strip-routes-comments.adoc | 10 +++++ src/docs/asciidoc/50-changes.adoc | 4 ++ src/docs/samples/play-2.4/groovy/build.gradle | 6 +++ ...bstractRoutesCompileIntegrationTest.groovy | 35 ++++++++++++++++++ .../extensions/PlayExtension.java | 12 ++++++ .../plugins/PlayRoutesPlugin.java | 6 ++- .../playframework/tasks/RoutesCompile.java | 16 +++++++- .../routes/DefaultRoutesCompileSpec.java | 9 ++++- .../routes/DefaultRoutesPostProcessor.java | 37 +++++++++++++++++++ .../internal/routes/RoutesCompileSpec.java | 2 + .../tools/internal/routes/RoutesCompiler.java | 6 +++ 12 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 src/docs/asciidoc/36-strip-routes-comments.adoc create mode 100644 src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java diff --git a/src/docs/asciidoc/30-plugin-configuration.adoc b/src/docs/asciidoc/30-plugin-configuration.adoc index 31097771..e5301a6a 100644 --- a/src/docs/asciidoc/30-plugin-configuration.adoc +++ b/src/docs/asciidoc/30-plugin-configuration.adoc @@ -8,4 +8,6 @@ include::33-add-source-directories.adoc[] include::34-compiler-options.adoc[] -include::35-add-distribution-files.adoc[] \ No newline at end of file +include::35-add-distribution-files.adoc[] + +include::36-strip-routes-comments.adoc[] \ No newline at end of file diff --git a/src/docs/asciidoc/36-strip-routes-comments.adoc b/src/docs/asciidoc/36-strip-routes-comments.adoc new file mode 100644 index 00000000..053ff640 --- /dev/null +++ b/src/docs/asciidoc/36-strip-routes-comments.adoc @@ -0,0 +1,10 @@ +=== Configuring generated routes files comments + +Routes files generated by the compiler contain comments which are changing across builds, this prevents tasks using those files as inputs to benefit from build cache. +The plugin extension can be configured to remove those comments by post-processing the generated routes files. + +[source,groovy] +.build.gradle +---- +include::{samplesCodeDir}/play-2.4/groovy/build.gradle[tag=strip-routes-comments] +---- \ No newline at end of file diff --git a/src/docs/asciidoc/50-changes.adoc b/src/docs/asciidoc/50-changes.adoc index 5c8f0254..b638b757 100644 --- a/src/docs/asciidoc/50-changes.adoc +++ b/src/docs/asciidoc/50-changes.adoc @@ -1,5 +1,9 @@ == Change Log +[discrete] +=== v0.14 (TBD) +* Add configuration to strip routes comments ({uri-github-issues}/109[issue #109]). + [discrete] === v0.13 (2023-01-24) * Remove usage of internal Gradle API org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact diff --git a/src/docs/samples/play-2.4/groovy/build.gradle b/src/docs/samples/play-2.4/groovy/build.gradle index b1c2f23a..0ffa01c7 100644 --- a/src/docs/samples/play-2.4/groovy/build.gradle +++ b/src/docs/samples/play-2.4/groovy/build.gradle @@ -33,3 +33,9 @@ play { injectedRoutesGenerator = true } // end::injected-routes-compiler[] + +// tag::strip-routes-comments[] +play { + stripRoutesComments = true +} +// end::strip-routes-comments[] diff --git a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy index 02464268..0362e4d7 100644 --- a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy +++ b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy @@ -6,6 +6,8 @@ import org.gradle.testkit.runner.TaskOutcome import org.gradle.util.VersionNumber import org.junit.Assume +import java.nio.charset.StandardCharsets + import static org.gradle.playframework.fixtures.Repositories.playRepositories import static org.gradle.playframework.fixtures.file.FileFixtures.assertContentsHaveChangedSince import static org.gradle.playframework.fixtures.file.FileFixtures.snapshot @@ -259,4 +261,37 @@ $ROUTES_COMPILE_TASK_NAME { new File(destinationDir, getReverseRoutesFileName('', '')).text.contains("extra.package") new File(destinationDir, getScalaRoutesFileName('', '')).text.contains("extra.package") } + + def "do not strip generated comments by default"() { + when: + withRoutesTemplate() + then: + build(ROUTES_COMPILE_TASK_NAME) + and: + createRouteFileList().each { + def generatedFile = new File(destinationDir, it) + assert generatedFile.isFile() + assert generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @SOURCE") + assert generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @DATE") + } + } + + def "can strip generated comments"() { + when: + buildFile << """ +play { + stripRoutesComments = true +} +""" + withRoutesTemplate() + then: + build(ROUTES_COMPILE_TASK_NAME) + and: + createRouteFileList().each { + def generatedFile = new File(destinationDir, it) + assert generatedFile.isFile() + assert !generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @SOURCE") + assert !generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @DATE") + } + } } diff --git a/src/main/java/org/gradle/playframework/extensions/PlayExtension.java b/src/main/java/org/gradle/playframework/extensions/PlayExtension.java index da5177fb..ffd4211f 100644 --- a/src/main/java/org/gradle/playframework/extensions/PlayExtension.java +++ b/src/main/java/org/gradle/playframework/extensions/PlayExtension.java @@ -29,6 +29,7 @@ public class PlayExtension { private final PlayPlatform platform; private final Property injectedRoutesGenerator; + private final Property stripRoutesComments; public PlayExtension(ObjectFactory objectFactory) { this.platform = objectFactory.newInstance(PlayPlatform.class, objectFactory); @@ -37,6 +38,8 @@ public PlayExtension(ObjectFactory objectFactory) { this.platform.getScalaVersion().convention(platform.getPlayVersion().map(playVersion -> PlayMajorVersion.forPlayVersion(playVersion).getDefaultScalaPlatform())); this.injectedRoutesGenerator = objectFactory.property(Boolean.class); injectedRoutesGenerator.convention(platform.getPlayVersion().map(playVersion -> !PlayMajorVersion.forPlayVersion(playVersion).hasSupportForStaticRoutesGenerator())); + this.stripRoutesComments = objectFactory.property(Boolean.class); + stripRoutesComments.convention(Boolean.FALSE); } /** @@ -66,4 +69,13 @@ public PlayPlatform getPlatform() { public Property getInjectedRoutesGenerator() { return injectedRoutesGenerator; } + + /** + * Returns the property configuring if build dependent comments should be stripped from the generated routes files. + * + * @return Property configuring whether build dependent comments should be stripped + */ + public Property getStripRoutesComments() { + return stripRoutesComments; + } } diff --git a/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java b/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java index 0278401e..3649b7ae 100644 --- a/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java +++ b/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java @@ -1,5 +1,6 @@ package org.gradle.playframework.plugins; +import org.gradle.api.provider.Property; import org.gradle.playframework.sourcesets.internal.DefaultRoutesSourceSet; import org.gradle.playframework.extensions.PlayExtension; import org.gradle.playframework.sourcesets.RoutesSourceSet; @@ -30,7 +31,7 @@ public void apply(Project project) { Configuration routesCompilerConfiguration = createRoutesCompilerConfiguration(project); declareDefaultDependencies(project, routesCompilerConfiguration, playExtension); RoutesSourceSet routesSourceSet = createCustomSourceSet(project, DefaultRoutesSourceSet.class, "routes"); - createDefaultRoutesCompileTask(project, routesSourceSet.getRoutes(), routesCompilerConfiguration, playExtension, playExtension.getInjectedRoutesGenerator()); + createDefaultRoutesCompileTask(project, routesSourceSet.getRoutes(), routesCompilerConfiguration, playExtension, playExtension.getInjectedRoutesGenerator(), playExtension.getStripRoutesComments()); } private Configuration createRoutesCompilerConfiguration(Project project) { @@ -48,7 +49,7 @@ private void declareDefaultDependencies(Project project, Configuration configura }); } - private void createDefaultRoutesCompileTask(Project project, SourceDirectorySet sourceDirectory, Configuration compilerConfiguration, PlayExtension playExtension, Provider injectedRoutesGenerator) { + private void createDefaultRoutesCompileTask(Project project, SourceDirectorySet sourceDirectory, Configuration compilerConfiguration, PlayExtension playExtension, Provider injectedRoutesGenerator, Property stripRoutesComments) { project.getTasks().register(ROUTES_COMPILE_TASK_NAME, RoutesCompile.class, routesCompile -> { routesCompile.setDescription("Generates routes for the '" + sourceDirectory.getDisplayName() + "' source set."); routesCompile.getPlatform().set(project.provider(() -> playExtension.getPlatform())); @@ -57,6 +58,7 @@ private void createDefaultRoutesCompileTask(Project project, SourceDirectorySet routesCompile.getOutputDirectory().set(getOutputDir(project, sourceDirectory)); routesCompile.getInjectedRoutesGenerator().set(injectedRoutesGenerator); routesCompile.getRoutesCompilerClasspath().setFrom(compilerConfiguration); + routesCompile.getStripRoutesComments().set(stripRoutesComments); }); } } diff --git a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java index 9d574a01..53f13ee5 100644 --- a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java +++ b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java @@ -52,6 +52,7 @@ public class RoutesCompile extends SourceTask { private final Property platform; private final Property injectedRoutesGenerator; private final ConfigurableFileCollection routesCompilerClasspath; + private final Property stripRoutesComments; @Inject public RoutesCompile(WorkerExecutor workerExecutor) { @@ -66,6 +67,8 @@ public RoutesCompile(WorkerExecutor workerExecutor) { this.injectedRoutesGenerator = getProject().getObjects().property(Boolean.class); this.injectedRoutesGenerator.set(false); this.routesCompilerClasspath = getProject().files(); + this.stripRoutesComments = getProject().getObjects().property(Boolean.class); + this.stripRoutesComments.set(false); } /** @@ -105,7 +108,7 @@ public ConfigurableFileCollection getRoutesCompilerClasspath() { @TaskAction @SuppressWarnings("Convert2Lambda") void compile() { - RoutesCompileSpec spec = new DefaultRoutesCompileSpec(getSource().getFiles(), getOutputDirectory().get().getAsFile(), isJavaProject(), getNamespaceReverseRouter().get(), getGenerateReverseRoutes().get(), getInjectedRoutesGenerator().get(), getAdditionalImports().get()); + RoutesCompileSpec spec = new DefaultRoutesCompileSpec(getSource().getFiles(), getOutputDirectory().get().getAsFile(), isJavaProject(), getNamespaceReverseRouter().get(), getGenerateReverseRoutes().get(), getInjectedRoutesGenerator().get(), getAdditionalImports().get(), getStripRoutesComments().get()); if (GradleVersion.current().compareTo(GradleVersion.version("5.6")) < 0) { workerExecutor.submit(RoutesCompileRunnable.class, workerConfiguration -> { @@ -177,4 +180,15 @@ public Property getGenerateReverseRoutes() { public Property getInjectedRoutesGenerator() { return injectedRoutesGenerator; } + + /** + * Should build dependent comments in generated routes files be stripped? Default is false. + * + * @return false if build dependent comments in generated route files should not be stripped, + * true otherwise. + */ + @Input + public Property getStripRoutesComments() { + return stripRoutesComments; + } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java index 0dd026d0..4cc41de8 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java @@ -11,8 +11,9 @@ public class DefaultRoutesCompileSpec implements RoutesCompileSpec { private final boolean generateReverseRoutes; private final boolean injectedRoutesGenerator; private final Collection additionalImports; + private final boolean stripRoutesComments; - public DefaultRoutesCompileSpec(Iterable sourceFiles, File outputDirectory, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) { + public DefaultRoutesCompileSpec(Iterable sourceFiles, File outputDirectory, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, boolean stripRoutesComments) { this.sourceFiles = sourceFiles; this.outputDirectory = outputDirectory; this.javaProject = javaProject; @@ -20,6 +21,7 @@ public DefaultRoutesCompileSpec(Iterable sourceFiles, File outputDirectory this.generateReverseRoutes = generateReverseRoutes; this.injectedRoutesGenerator = injectedRoutesGenerator; this.additionalImports = additionalImports; + this.stripRoutesComments = stripRoutesComments; } @Override @@ -56,4 +58,9 @@ public boolean isInjectedRoutesGenerator() { public Collection getAdditionalImports() { return additionalImports; } + + @Override + public boolean isStripRoutesComments() { + return stripRoutesComments; + } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java new file mode 100644 index 00000000..cde7eaee --- /dev/null +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java @@ -0,0 +1,37 @@ +package org.gradle.playframework.tools.internal.routes; + +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.stream.Stream; + +class DefaultRoutesPostProcessor implements Serializable { + + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRoutesPostProcessor.class); + + void execute(RoutesCompileSpec spec) { + if(spec.isStripRoutesComments()) { + try (Stream stream = Files.find(spec.getDestinationDir().toPath(), Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile())) { + stream.forEach(this::process); + } catch (IOException e) { + LOGGER.warn("Unable to post-process routes", e); + } + } + } + + private void process(Path routeFile) { + try { + String content = new String(Files.readAllBytes(routeFile), StandardCharsets.UTF_8); + content = content.replaceAll("(?m)^// @(SOURCE):.*", ""); + content = content.replaceAll("(?m)^// @(DATE):.*", ""); + Files.write(routeFile, content.getBytes(StandardCharsets.UTF_8)); + } catch (IOException e) { + LOGGER.warn(String.format("Unable to post-process route file %s", routeFile.getFileName()), e); + } + } +} diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java index a033910b..bc7ad30b 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java @@ -18,4 +18,6 @@ public interface RoutesCompileSpec extends PlayCompileSpec, Serializable { boolean isInjectedRoutesGenerator(); Collection getAdditionalImports(); + + boolean isStripRoutesComments(); } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java index 435a3d22..7a38ba12 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java @@ -12,8 +12,11 @@ public class RoutesCompiler implements Compiler, Serializable { private final VersionedRoutesCompilerAdapter adapter; + private final DefaultRoutesPostProcessor postProcessor; + public RoutesCompiler(VersionedRoutesCompilerAdapter adapter) { this.adapter = adapter; + this.postProcessor = new DefaultRoutesPostProcessor(); } @Override @@ -42,6 +45,9 @@ public WorkResult execute(RoutesCompileSpec spec) { didWork = ret || didWork; } + // Post-process routes files + postProcessor.execute(spec); + return WorkResults.didWork(didWork); } From d7d6b85906eda96baf9a192b096ea4bf17bd4ded Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Thu, 30 Mar 2023 09:54:35 +0200 Subject: [PATCH 02/15] Remove feature toggle --- build.gradle.kts | 9 ++++++- src/docs/asciidoc/10-plugin-conventions.adoc | 4 ++- src/docs/asciidoc/16-routes-comments.adoc | 4 +++ .../asciidoc/30-plugin-configuration.adoc | 2 -- .../asciidoc/36-strip-routes-comments.adoc | 10 -------- src/docs/asciidoc/50-changes.adoc | 2 +- src/docs/samples/play-2.4/groovy/build.gradle | 8 +----- ...bstractRoutesCompileIntegrationTest.groovy | 25 +++---------------- .../extensions/PlayExtension.java | 12 --------- .../plugins/PlayRoutesPlugin.java | 7 +++--- .../playframework/tasks/RoutesCompile.java | 21 +++------------- .../routes/DefaultRoutesCompileSpec.java | 10 ++++---- .../routes/DefaultRoutesPostProcessor.java | 10 +++----- .../internal/routes/RoutesCompileSpec.java | 2 +- .../tools/internal/routes/RoutesCompiler.java | 3 ++- .../routes/RoutesCompilerAdapterV23X.java | 7 +++--- .../routes/RoutesCompilerAdapterV24X.java | 5 ++-- .../routes/RoutesCompilerAdapterV27X.java | 10 +++++--- .../routes/RoutesCompilerFactory.java | 10 ++++---- .../VersionedRoutesCompilerAdapter.java | 2 +- 20 files changed, 59 insertions(+), 104 deletions(-) create mode 100644 src/docs/asciidoc/16-routes-comments.adoc delete mode 100644 src/docs/asciidoc/36-strip-routes-comments.adoc diff --git a/build.gradle.kts b/build.gradle.kts index fff5ac78..f910b83e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,10 +8,11 @@ plugins { org.gradle.playframework.`github-pages` org.gradle.playframework.`documentation-test` id("com.gradle.plugin-publish") version "0.12.0" + id("maven-publish") } group = "org.gradle.playframework" -version = "0.13" +version = "0.14" repositories { mavenCentral() @@ -109,3 +110,9 @@ pluginBundle { artifactId = base.archivesBaseName } } + +publishing { + repositories { + mavenLocal() + } +} \ No newline at end of file diff --git a/src/docs/asciidoc/10-plugin-conventions.adoc b/src/docs/asciidoc/10-plugin-conventions.adoc index ecc4da95..08874f46 100644 --- a/src/docs/asciidoc/10-plugin-conventions.adoc +++ b/src/docs/asciidoc/10-plugin-conventions.adoc @@ -8,4 +8,6 @@ include::13-tasks.adoc[] include::14-source-sets.adoc[] -include::15-dependency-configurations.adoc[] \ No newline at end of file +include::15-dependency-configurations.adoc[] + +include::16-routes-comments.adoc[] \ No newline at end of file diff --git a/src/docs/asciidoc/16-routes-comments.adoc b/src/docs/asciidoc/16-routes-comments.adoc new file mode 100644 index 00000000..7b4ed6c8 --- /dev/null +++ b/src/docs/asciidoc/16-routes-comments.adoc @@ -0,0 +1,4 @@ +=== Generated routes files comments + +Routes files generated by the compiler 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. diff --git a/src/docs/asciidoc/30-plugin-configuration.adoc b/src/docs/asciidoc/30-plugin-configuration.adoc index e5301a6a..da83150a 100644 --- a/src/docs/asciidoc/30-plugin-configuration.adoc +++ b/src/docs/asciidoc/30-plugin-configuration.adoc @@ -9,5 +9,3 @@ include::33-add-source-directories.adoc[] include::34-compiler-options.adoc[] include::35-add-distribution-files.adoc[] - -include::36-strip-routes-comments.adoc[] \ No newline at end of file diff --git a/src/docs/asciidoc/36-strip-routes-comments.adoc b/src/docs/asciidoc/36-strip-routes-comments.adoc deleted file mode 100644 index 053ff640..00000000 --- a/src/docs/asciidoc/36-strip-routes-comments.adoc +++ /dev/null @@ -1,10 +0,0 @@ -=== Configuring generated routes files comments - -Routes files generated by the compiler contain comments which are changing across builds, this prevents tasks using those files as inputs to benefit from build cache. -The plugin extension can be configured to remove those comments by post-processing the generated routes files. - -[source,groovy] -.build.gradle ----- -include::{samplesCodeDir}/play-2.4/groovy/build.gradle[tag=strip-routes-comments] ----- \ No newline at end of file diff --git a/src/docs/asciidoc/50-changes.adoc b/src/docs/asciidoc/50-changes.adoc index b638b757..b9938f50 100644 --- a/src/docs/asciidoc/50-changes.adoc +++ b/src/docs/asciidoc/50-changes.adoc @@ -2,7 +2,7 @@ [discrete] === v0.14 (TBD) -* Add configuration to strip routes comments ({uri-github-issues}/109[issue #109]). +* Add configuration to post-process routes comments ({uri-github-issues}/109[issue #109]). [discrete] === v0.13 (2023-01-24) diff --git a/src/docs/samples/play-2.4/groovy/build.gradle b/src/docs/samples/play-2.4/groovy/build.gradle index 0ffa01c7..af6f2241 100644 --- a/src/docs/samples/play-2.4/groovy/build.gradle +++ b/src/docs/samples/play-2.4/groovy/build.gradle @@ -32,10 +32,4 @@ play { play { injectedRoutesGenerator = true } -// end::injected-routes-compiler[] - -// tag::strip-routes-comments[] -play { - stripRoutesComments = true -} -// end::strip-routes-comments[] +// end::injected-routes-compiler[] \ No newline at end of file diff --git a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy index 0362e4d7..7e8defb2 100644 --- a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy +++ b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy @@ -262,31 +262,12 @@ $ROUTES_COMPILE_TASK_NAME { new File(destinationDir, getScalaRoutesFileName('', '')).text.contains("extra.package") } - def "do not strip generated comments by default"() { - when: + def "post-process generated comments"() { + given: withRoutesTemplate() - then: - build(ROUTES_COMPILE_TASK_NAME) - and: - createRouteFileList().each { - def generatedFile = new File(destinationDir, it) - assert generatedFile.isFile() - assert generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @SOURCE") - assert generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @DATE") - } - } - - def "can strip generated comments"() { when: - buildFile << """ -play { - stripRoutesComments = true -} -""" - withRoutesTemplate() - then: build(ROUTES_COMPILE_TASK_NAME) - and: + expect: createRouteFileList().each { def generatedFile = new File(destinationDir, it) assert generatedFile.isFile() diff --git a/src/main/java/org/gradle/playframework/extensions/PlayExtension.java b/src/main/java/org/gradle/playframework/extensions/PlayExtension.java index ffd4211f..da5177fb 100644 --- a/src/main/java/org/gradle/playframework/extensions/PlayExtension.java +++ b/src/main/java/org/gradle/playframework/extensions/PlayExtension.java @@ -29,7 +29,6 @@ public class PlayExtension { private final PlayPlatform platform; private final Property injectedRoutesGenerator; - private final Property stripRoutesComments; public PlayExtension(ObjectFactory objectFactory) { this.platform = objectFactory.newInstance(PlayPlatform.class, objectFactory); @@ -38,8 +37,6 @@ public PlayExtension(ObjectFactory objectFactory) { this.platform.getScalaVersion().convention(platform.getPlayVersion().map(playVersion -> PlayMajorVersion.forPlayVersion(playVersion).getDefaultScalaPlatform())); this.injectedRoutesGenerator = objectFactory.property(Boolean.class); injectedRoutesGenerator.convention(platform.getPlayVersion().map(playVersion -> !PlayMajorVersion.forPlayVersion(playVersion).hasSupportForStaticRoutesGenerator())); - this.stripRoutesComments = objectFactory.property(Boolean.class); - stripRoutesComments.convention(Boolean.FALSE); } /** @@ -69,13 +66,4 @@ public PlayPlatform getPlatform() { public Property getInjectedRoutesGenerator() { return injectedRoutesGenerator; } - - /** - * Returns the property configuring if build dependent comments should be stripped from the generated routes files. - * - * @return Property configuring whether build dependent comments should be stripped - */ - public Property getStripRoutesComments() { - return stripRoutesComments; - } } diff --git a/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java b/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java index 3649b7ae..fb7bc63f 100644 --- a/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java +++ b/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java @@ -31,7 +31,7 @@ public void apply(Project project) { Configuration routesCompilerConfiguration = createRoutesCompilerConfiguration(project); declareDefaultDependencies(project, routesCompilerConfiguration, playExtension); RoutesSourceSet routesSourceSet = createCustomSourceSet(project, DefaultRoutesSourceSet.class, "routes"); - createDefaultRoutesCompileTask(project, routesSourceSet.getRoutes(), routesCompilerConfiguration, playExtension, playExtension.getInjectedRoutesGenerator(), playExtension.getStripRoutesComments()); + createDefaultRoutesCompileTask(project, routesSourceSet.getRoutes(), routesCompilerConfiguration, playExtension, playExtension.getInjectedRoutesGenerator()); } private Configuration createRoutesCompilerConfiguration(Project project) { @@ -44,12 +44,12 @@ private Configuration createRoutesCompilerConfiguration(Project project) { private void declareDefaultDependencies(Project project, Configuration configuration, PlayExtension playExtension) { configuration.defaultDependencies(dependencies -> { - String dependencyNotation = RoutesCompilerFactory.createAdapter(playExtension.getPlatform()).getDependencyNotation(); + String dependencyNotation = RoutesCompilerFactory.createAdapter(playExtension.getPlatform(), project.getProjectDir().getAbsolutePath()).getDependencyNotation(); dependencies.add(project.getDependencies().create(dependencyNotation)); }); } - private void createDefaultRoutesCompileTask(Project project, SourceDirectorySet sourceDirectory, Configuration compilerConfiguration, PlayExtension playExtension, Provider injectedRoutesGenerator, Property stripRoutesComments) { + private void createDefaultRoutesCompileTask(Project project, SourceDirectorySet sourceDirectory, Configuration compilerConfiguration, PlayExtension playExtension, Provider injectedRoutesGenerator) { project.getTasks().register(ROUTES_COMPILE_TASK_NAME, RoutesCompile.class, routesCompile -> { routesCompile.setDescription("Generates routes for the '" + sourceDirectory.getDisplayName() + "' source set."); routesCompile.getPlatform().set(project.provider(() -> playExtension.getPlatform())); @@ -58,7 +58,6 @@ private void createDefaultRoutesCompileTask(Project project, SourceDirectorySet routesCompile.getOutputDirectory().set(getOutputDir(project, sourceDirectory)); routesCompile.getInjectedRoutesGenerator().set(injectedRoutesGenerator); routesCompile.getRoutesCompilerClasspath().setFrom(compilerConfiguration); - routesCompile.getStripRoutesComments().set(stripRoutesComments); }); } } diff --git a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java index 53f13ee5..25410f40 100644 --- a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java +++ b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java @@ -2,7 +2,6 @@ import org.gradle.api.Action; import org.gradle.api.file.ConfigurableFileCollection; -import org.gradle.api.file.Directory; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.FileTree; import org.gradle.api.provider.ListProperty; @@ -52,7 +51,7 @@ public class RoutesCompile extends SourceTask { private final Property platform; private final Property injectedRoutesGenerator; private final ConfigurableFileCollection routesCompilerClasspath; - private final Property stripRoutesComments; + private final String projectDir; @Inject public RoutesCompile(WorkerExecutor workerExecutor) { @@ -67,8 +66,7 @@ public RoutesCompile(WorkerExecutor workerExecutor) { this.injectedRoutesGenerator = getProject().getObjects().property(Boolean.class); this.injectedRoutesGenerator.set(false); this.routesCompilerClasspath = getProject().files(); - this.stripRoutesComments = getProject().getObjects().property(Boolean.class); - this.stripRoutesComments.set(false); + this.projectDir = getProject().getProjectDir().getAbsolutePath(); } /** @@ -108,7 +106,7 @@ public ConfigurableFileCollection getRoutesCompilerClasspath() { @TaskAction @SuppressWarnings("Convert2Lambda") void compile() { - RoutesCompileSpec spec = new DefaultRoutesCompileSpec(getSource().getFiles(), getOutputDirectory().get().getAsFile(), isJavaProject(), getNamespaceReverseRouter().get(), getGenerateReverseRoutes().get(), getInjectedRoutesGenerator().get(), getAdditionalImports().get(), getStripRoutesComments().get()); + RoutesCompileSpec spec = new DefaultRoutesCompileSpec(getSource().getFiles(), getOutputDirectory().get().getAsFile(), isJavaProject(), getNamespaceReverseRouter().get(), getGenerateReverseRoutes().get(), getInjectedRoutesGenerator().get(), getAdditionalImports().get(), projectDir); if (GradleVersion.current().compareTo(GradleVersion.version("5.6")) < 0) { workerExecutor.submit(RoutesCompileRunnable.class, workerConfiguration -> { @@ -136,7 +134,7 @@ public void execute(RoutesCompileParameters parameters) { } private Compiler getCompiler() { - return RoutesCompilerFactory.create(getPlatform().get()); + return RoutesCompilerFactory.create(getPlatform().get(), projectDir); } @Internal @@ -180,15 +178,4 @@ public Property getGenerateReverseRoutes() { public Property getInjectedRoutesGenerator() { return injectedRoutesGenerator; } - - /** - * Should build dependent comments in generated routes files be stripped? Default is false. - * - * @return false if build dependent comments in generated route files should not be stripped, - * true otherwise. - */ - @Input - public Property getStripRoutesComments() { - return stripRoutesComments; - } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java index 4cc41de8..5b6193d7 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java @@ -11,9 +11,9 @@ public class DefaultRoutesCompileSpec implements RoutesCompileSpec { private final boolean generateReverseRoutes; private final boolean injectedRoutesGenerator; private final Collection additionalImports; - private final boolean stripRoutesComments; + private final String projectDir; - public DefaultRoutesCompileSpec(Iterable sourceFiles, File outputDirectory, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, boolean stripRoutesComments) { + public DefaultRoutesCompileSpec(Iterable sourceFiles, File outputDirectory, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDir) { this.sourceFiles = sourceFiles; this.outputDirectory = outputDirectory; this.javaProject = javaProject; @@ -21,7 +21,7 @@ public DefaultRoutesCompileSpec(Iterable sourceFiles, File outputDirectory this.generateReverseRoutes = generateReverseRoutes; this.injectedRoutesGenerator = injectedRoutesGenerator; this.additionalImports = additionalImports; - this.stripRoutesComments = stripRoutesComments; + this.projectDir = projectDir; } @Override @@ -60,7 +60,7 @@ public Collection getAdditionalImports() { } @Override - public boolean isStripRoutesComments() { - return stripRoutesComments; + public String getProjectDir() { + return projectDir; } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java index cde7eaee..0fb01818 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java @@ -15,12 +15,10 @@ class DefaultRoutesPostProcessor implements Serializable { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRoutesPostProcessor.class); void execute(RoutesCompileSpec spec) { - if(spec.isStripRoutesComments()) { - try (Stream stream = Files.find(spec.getDestinationDir().toPath(), Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile())) { - stream.forEach(this::process); - } catch (IOException e) { - LOGGER.warn("Unable to post-process routes", e); - } + try (Stream stream = Files.find(spec.getDestinationDir().toPath(), Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile())) { + stream.forEach(this::process); + } catch (IOException e) { + LOGGER.warn("Unable to post-process routes", e); } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java index bc7ad30b..568e23fb 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java @@ -19,5 +19,5 @@ public interface RoutesCompileSpec extends PlayCompileSpec, Serializable { Collection getAdditionalImports(); - boolean isStripRoutesComments(); + String getProjectDir(); } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java index 7a38ba12..c75f0711 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java @@ -56,7 +56,8 @@ private Boolean compile(File sourceFile, RoutesCompileSpec spec) { try { ClassLoader cl = getClass().getClassLoader(); ScalaMethod compile = adapter.getCompileMethod(cl); - return adapter.interpretResult(compile.invoke(adapter.createCompileParameters(cl, sourceFile, spec.getDestinationDir(), spec.isJavaProject(), spec.isNamespaceReverseRouter(), spec.isGenerateReverseRoutes(), spec.isInjectedRoutesGenerator(), spec.getAdditionalImports()))); + System.setProperty("custompath", spec.getProjectDir()); + return adapter.interpretResult(compile.invoke(adapter.createCompileParameters(cl, sourceFile, spec.getDestinationDir(), spec.isJavaProject(), spec.isNamespaceReverseRouter(), spec.isGenerateReverseRoutes(), spec.isInjectedRoutesGenerator(), spec.getAdditionalImports(), "barbar1"))); } catch (Exception e) { throw new RuntimeException("Error invoking the Play routes compiler.", e); } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV23X.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV23X.java index 52ee26ec..5bb24050 100755 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV23X.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV23X.java @@ -15,7 +15,7 @@ class RoutesCompilerAdapterV23X extends DefaultVersionedRoutesCompilerAdapter { private final List defaultScalaImports = Arrays.asList("controllers.Assets.Asset"); private final List defaultJavaImports = Arrays.asList("controllers.Assets.Asset", "play.libs.F"); - public RoutesCompilerAdapterV23X(String playVersion) { + public RoutesCompilerAdapterV23X(String playVersion, String projectDir) { // No 2.11 version of routes compiler published super(playVersion, "2.10"); } @@ -36,7 +36,7 @@ public ScalaMethod getCompileMethod(ClassLoader cl) throws ClassNotFoundExceptio } @Override - public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDir) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { List defaultImports = javaProject ? defaultJavaImports : defaultScalaImports; defaultImports.addAll(additionalImports); return new Object[] { @@ -45,7 +45,8 @@ public Object[] createCompileParameters(ClassLoader cl, File file, File destinat ScalaListBuffer.fromList(cl, defaultImports), generateReverseRoutes, isGenerateRefReverseRouter(), - namespaceReverseRouter + namespaceReverseRouter, + "barbar2" }; } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV24X.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV24X.java index 22c211e8..54876436 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV24X.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV24X.java @@ -44,7 +44,7 @@ public ScalaMethod getCompileMethod(ClassLoader cl) throws ClassNotFoundExceptio } @Override - public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) throws ClassNotFoundException { + public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDir) throws ClassNotFoundException { List defaultImports = getDefaultImports(javaProject); defaultImports.addAll(additionalImports); @@ -53,7 +53,8 @@ public Object[] createCompileParameters(ClassLoader cl, File file, File destinat ScalaListBuffer.fromList(cl, defaultImports), isGenerateForwardsRouter(), generateReverseRoutes, - namespaceReverseRouter + namespaceReverseRouter, + "fuufuu" ); String routeGenerator; diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV27X.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV27X.java index 84056d56..a7c45299 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV27X.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV27X.java @@ -10,12 +10,15 @@ import java.util.List; public class RoutesCompilerAdapterV27X extends RoutesCompilerAdapterV24X { - public RoutesCompilerAdapterV27X(String playVersion, String scalaVersion) { + private final String projectDir; + + public RoutesCompilerAdapterV27X(String playVersion, String scalaVersion, String projectDir) { super(playVersion, scalaVersion); + this.projectDir = projectDir; } @Override - public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) throws ClassNotFoundException { + public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDirFoo) throws ClassNotFoundException { List defaultImports = getDefaultImports(javaProject); defaultImports.addAll(additionalImports); @@ -24,7 +27,8 @@ public Object[] createCompileParameters(ClassLoader cl, File file, File destinat ScalaSeq.fromList(cl, defaultImports), isGenerateForwardsRouter(), generateReverseRoutes, - namespaceReverseRouter + namespaceReverseRouter, + projectDir ); String routeGenerator = PLAY_ROUTES_COMPILER_INJECTED_ROUTES_GENERATOR; diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerFactory.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerFactory.java index c9746720..3cd44346 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerFactory.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerFactory.java @@ -5,16 +5,16 @@ import org.gradle.util.VersionNumber; public class RoutesCompilerFactory { - public static RoutesCompiler create(PlayPlatform playPlatform) { - return new RoutesCompiler(createAdapter(playPlatform)); + public static RoutesCompiler create(PlayPlatform playPlatform, String projectDir) { + return new RoutesCompiler(createAdapter(playPlatform, projectDir)); } - public static VersionedRoutesCompilerAdapter createAdapter(PlayPlatform playPlatform) { + public static VersionedRoutesCompilerAdapter createAdapter(PlayPlatform playPlatform, String projectDir) { String playVersion = playPlatform.getPlayVersion().get(); String scalaVersion = playPlatform.getScalaCompatibilityVersion().get(); switch (PlayMajorVersion.forPlatform(playPlatform)) { case PLAY_2_3_X: - return new RoutesCompilerAdapterV23X(playVersion); + return new RoutesCompilerAdapterV23X(playVersion, projectDir); case PLAY_2_4_X: if (VersionNumber.parse(playVersion).getMicro() < 6 && !"2.10".equals(scalaVersion)) { scalaVersion = "2.10"; @@ -26,7 +26,7 @@ public static VersionedRoutesCompilerAdapter createAdapter(PlayPlatform playPlat case PLAY_2_7_X: case PLAY_2_8_X: default: - return new RoutesCompilerAdapterV27X(playVersion, scalaVersion); + return new RoutesCompilerAdapterV27X(playVersion, scalaVersion, projectDir); } } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/VersionedRoutesCompilerAdapter.java b/src/main/java/org/gradle/playframework/tools/internal/routes/VersionedRoutesCompilerAdapter.java index c42b372d..ba67b216 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/VersionedRoutesCompilerAdapter.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/VersionedRoutesCompilerAdapter.java @@ -12,7 +12,7 @@ public interface VersionedRoutesCompilerAdapter extends Serializable { ScalaMethod getCompileMethod(ClassLoader cl) throws ClassNotFoundException; - Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException; + Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDir) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException; Iterable getClassLoaderPackages(); From 0b4e76127163617398c04253afa7bc68a281e5c1 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Thu, 30 Mar 2023 12:53:00 +0200 Subject: [PATCH 03/15] Relativize routes files path --- build.gradle.kts | 9 +------- .../asciidoc/30-plugin-configuration.adoc | 2 +- src/docs/samples/play-2.4/groovy/build.gradle | 2 +- ...bstractRoutesCompileIntegrationTest.groovy | 6 ++--- .../plugins/PlayRoutesPlugin.java | 3 +-- .../playframework/tasks/RoutesCompile.java | 20 ++++++++++++---- .../routes/DefaultRoutesCompileSpec.java | 6 ++--- .../routes/DefaultRoutesPostProcessor.java | 23 ++++++++++++++++--- .../internal/routes/RoutesCompileSpec.java | 2 +- .../tools/internal/routes/RoutesCompiler.java | 3 +-- .../routes/RoutesCompilerAdapterV23X.java | 7 +++--- .../routes/RoutesCompilerAdapterV24X.java | 5 ++-- .../routes/RoutesCompilerAdapterV27X.java | 10 +++----- .../routes/RoutesCompilerFactory.java | 10 ++++---- .../VersionedRoutesCompilerAdapter.java | 2 +- 15 files changed, 62 insertions(+), 48 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f910b83e..fff5ac78 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,11 +8,10 @@ plugins { org.gradle.playframework.`github-pages` org.gradle.playframework.`documentation-test` id("com.gradle.plugin-publish") version "0.12.0" - id("maven-publish") } group = "org.gradle.playframework" -version = "0.14" +version = "0.13" repositories { mavenCentral() @@ -110,9 +109,3 @@ pluginBundle { artifactId = base.archivesBaseName } } - -publishing { - repositories { - mavenLocal() - } -} \ No newline at end of file diff --git a/src/docs/asciidoc/30-plugin-configuration.adoc b/src/docs/asciidoc/30-plugin-configuration.adoc index da83150a..31097771 100644 --- a/src/docs/asciidoc/30-plugin-configuration.adoc +++ b/src/docs/asciidoc/30-plugin-configuration.adoc @@ -8,4 +8,4 @@ include::33-add-source-directories.adoc[] include::34-compiler-options.adoc[] -include::35-add-distribution-files.adoc[] +include::35-add-distribution-files.adoc[] \ No newline at end of file diff --git a/src/docs/samples/play-2.4/groovy/build.gradle b/src/docs/samples/play-2.4/groovy/build.gradle index af6f2241..b1c2f23a 100644 --- a/src/docs/samples/play-2.4/groovy/build.gradle +++ b/src/docs/samples/play-2.4/groovy/build.gradle @@ -32,4 +32,4 @@ play { play { injectedRoutesGenerator = true } -// end::injected-routes-compiler[] \ No newline at end of file +// end::injected-routes-compiler[] diff --git a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy index 7e8defb2..54c1fa08 100644 --- a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy +++ b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy @@ -267,12 +267,12 @@ $ROUTES_COMPILE_TASK_NAME { withRoutesTemplate() when: build(ROUTES_COMPILE_TASK_NAME) - expect: + then: createRouteFileList().each { def generatedFile = new File(destinationDir, it) assert generatedFile.isFile() - assert !generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @SOURCE") - assert !generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @DATE") + assert generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @(SOURCE):conf/routes") + assert !generatedFile.getText(StandardCharsets.UTF_8.toString()).contains("// @(DATE)") } } } diff --git a/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java b/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java index fb7bc63f..0278401e 100644 --- a/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java +++ b/src/main/java/org/gradle/playframework/plugins/PlayRoutesPlugin.java @@ -1,6 +1,5 @@ package org.gradle.playframework.plugins; -import org.gradle.api.provider.Property; import org.gradle.playframework.sourcesets.internal.DefaultRoutesSourceSet; import org.gradle.playframework.extensions.PlayExtension; import org.gradle.playframework.sourcesets.RoutesSourceSet; @@ -44,7 +43,7 @@ private Configuration createRoutesCompilerConfiguration(Project project) { private void declareDefaultDependencies(Project project, Configuration configuration, PlayExtension playExtension) { configuration.defaultDependencies(dependencies -> { - String dependencyNotation = RoutesCompilerFactory.createAdapter(playExtension.getPlatform(), project.getProjectDir().getAbsolutePath()).getDependencyNotation(); + String dependencyNotation = RoutesCompilerFactory.createAdapter(playExtension.getPlatform()).getDependencyNotation(); dependencies.add(project.getDependencies().create(dependencyNotation)); }); } diff --git a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java index 25410f40..2766eb93 100644 --- a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java +++ b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java @@ -28,6 +28,7 @@ import org.gradle.workers.WorkerExecutor; import javax.inject.Inject; +import java.io.File; /** * Task for compiling routes templates into Scala code. @@ -51,7 +52,7 @@ public class RoutesCompile extends SourceTask { private final Property platform; private final Property injectedRoutesGenerator; private final ConfigurableFileCollection routesCompilerClasspath; - private final String projectDir; + private final File projectDir; @Inject public RoutesCompile(WorkerExecutor workerExecutor) { @@ -66,7 +67,7 @@ public RoutesCompile(WorkerExecutor workerExecutor) { this.injectedRoutesGenerator = getProject().getObjects().property(Boolean.class); this.injectedRoutesGenerator.set(false); this.routesCompilerClasspath = getProject().files(); - this.projectDir = getProject().getProjectDir().getAbsolutePath(); + this.projectDir = getProject().getProjectDir(); } /** @@ -106,7 +107,7 @@ public ConfigurableFileCollection getRoutesCompilerClasspath() { @TaskAction @SuppressWarnings("Convert2Lambda") void compile() { - RoutesCompileSpec spec = new DefaultRoutesCompileSpec(getSource().getFiles(), getOutputDirectory().get().getAsFile(), isJavaProject(), getNamespaceReverseRouter().get(), getGenerateReverseRoutes().get(), getInjectedRoutesGenerator().get(), getAdditionalImports().get(), projectDir); + RoutesCompileSpec spec = new DefaultRoutesCompileSpec(getSource().getFiles(), getOutputDirectory().get().getAsFile(), isJavaProject(), getNamespaceReverseRouter().get(), getGenerateReverseRoutes().get(), getInjectedRoutesGenerator().get(), getAdditionalImports().get(), getProjectDir()); if (GradleVersion.current().compareTo(GradleVersion.version("5.6")) < 0) { workerExecutor.submit(RoutesCompileRunnable.class, workerConfiguration -> { @@ -134,7 +135,7 @@ public void execute(RoutesCompileParameters parameters) { } private Compiler getCompiler() { - return RoutesCompilerFactory.create(getPlatform().get(), projectDir); + return RoutesCompilerFactory.create(getPlatform().get()); } @Internal @@ -178,4 +179,15 @@ public Property getGenerateReverseRoutes() { public Property getInjectedRoutesGenerator() { return injectedRoutesGenerator; } + + /** + * The project directory is used to relativize the route source folder + * when post-processing the generated routes files. + * + * @return The project directory. + */ + @Internal + public File getProjectDir() { + return projectDir; + } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java index 5b6193d7..01f4411d 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesCompileSpec.java @@ -11,9 +11,9 @@ public class DefaultRoutesCompileSpec implements RoutesCompileSpec { private final boolean generateReverseRoutes; private final boolean injectedRoutesGenerator; private final Collection additionalImports; - private final String projectDir; + private final File projectDir; - public DefaultRoutesCompileSpec(Iterable sourceFiles, File outputDirectory, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDir) { + public DefaultRoutesCompileSpec(Iterable sourceFiles, File outputDirectory, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, File projectDir) { this.sourceFiles = sourceFiles; this.outputDirectory = outputDirectory; this.javaProject = javaProject; @@ -60,7 +60,7 @@ public Collection getAdditionalImports() { } @Override - public String getProjectDir() { + public File getProjectDir() { return projectDir; } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java index 0fb01818..90cf1f6d 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java @@ -3,6 +3,7 @@ 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; @@ -15,17 +16,33 @@ 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 stream = Files.find(spec.getDestinationDir().toPath(), Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile())) { - stream.forEach(this::process); + stream.forEach(routeFile -> process(routeFile, sourceReplacementString)); } catch (IOException e) { LOGGER.warn("Unable to post-process routes", e); } } - private void process(Path routeFile) { + private String getSourceReplacementString(Iterable sources, File projectDir) { + String sourceReplacementString = ""; + + if(sources.iterator().hasNext()) { + File sourceFile = sources.iterator().next(); + sourceReplacementString = "// @(SOURCE):" + + projectDir.toURI().relativize(sourceFile.toURI()) + .getPath() + .replace(File.separator, "/"); + } + + 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):.*", ""); + content = content.replaceAll("(?m)^// @(SOURCE):.*", sourceReplacementString); content = content.replaceAll("(?m)^// @(DATE):.*", ""); Files.write(routeFile, content.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java index 568e23fb..ba8febef 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompileSpec.java @@ -19,5 +19,5 @@ public interface RoutesCompileSpec extends PlayCompileSpec, Serializable { Collection getAdditionalImports(); - String getProjectDir(); + File getProjectDir(); } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java index c75f0711..7a38ba12 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompiler.java @@ -56,8 +56,7 @@ private Boolean compile(File sourceFile, RoutesCompileSpec spec) { try { ClassLoader cl = getClass().getClassLoader(); ScalaMethod compile = adapter.getCompileMethod(cl); - System.setProperty("custompath", spec.getProjectDir()); - return adapter.interpretResult(compile.invoke(adapter.createCompileParameters(cl, sourceFile, spec.getDestinationDir(), spec.isJavaProject(), spec.isNamespaceReverseRouter(), spec.isGenerateReverseRoutes(), spec.isInjectedRoutesGenerator(), spec.getAdditionalImports(), "barbar1"))); + return adapter.interpretResult(compile.invoke(adapter.createCompileParameters(cl, sourceFile, spec.getDestinationDir(), spec.isJavaProject(), spec.isNamespaceReverseRouter(), spec.isGenerateReverseRoutes(), spec.isInjectedRoutesGenerator(), spec.getAdditionalImports()))); } catch (Exception e) { throw new RuntimeException("Error invoking the Play routes compiler.", e); } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV23X.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV23X.java index 5bb24050..52ee26ec 100755 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV23X.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV23X.java @@ -15,7 +15,7 @@ class RoutesCompilerAdapterV23X extends DefaultVersionedRoutesCompilerAdapter { private final List defaultScalaImports = Arrays.asList("controllers.Assets.Asset"); private final List defaultJavaImports = Arrays.asList("controllers.Assets.Asset", "play.libs.F"); - public RoutesCompilerAdapterV23X(String playVersion, String projectDir) { + public RoutesCompilerAdapterV23X(String playVersion) { // No 2.11 version of routes compiler published super(playVersion, "2.10"); } @@ -36,7 +36,7 @@ public ScalaMethod getCompileMethod(ClassLoader cl) throws ClassNotFoundExceptio } @Override - public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDir) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { List defaultImports = javaProject ? defaultJavaImports : defaultScalaImports; defaultImports.addAll(additionalImports); return new Object[] { @@ -45,8 +45,7 @@ public Object[] createCompileParameters(ClassLoader cl, File file, File destinat ScalaListBuffer.fromList(cl, defaultImports), generateReverseRoutes, isGenerateRefReverseRouter(), - namespaceReverseRouter, - "barbar2" + namespaceReverseRouter }; } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV24X.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV24X.java index 54876436..22c211e8 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV24X.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV24X.java @@ -44,7 +44,7 @@ public ScalaMethod getCompileMethod(ClassLoader cl) throws ClassNotFoundExceptio } @Override - public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDir) throws ClassNotFoundException { + public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) throws ClassNotFoundException { List defaultImports = getDefaultImports(javaProject); defaultImports.addAll(additionalImports); @@ -53,8 +53,7 @@ public Object[] createCompileParameters(ClassLoader cl, File file, File destinat ScalaListBuffer.fromList(cl, defaultImports), isGenerateForwardsRouter(), generateReverseRoutes, - namespaceReverseRouter, - "fuufuu" + namespaceReverseRouter ); String routeGenerator; diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV27X.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV27X.java index a7c45299..84056d56 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV27X.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerAdapterV27X.java @@ -10,15 +10,12 @@ import java.util.List; public class RoutesCompilerAdapterV27X extends RoutesCompilerAdapterV24X { - private final String projectDir; - - public RoutesCompilerAdapterV27X(String playVersion, String scalaVersion, String projectDir) { + public RoutesCompilerAdapterV27X(String playVersion, String scalaVersion) { super(playVersion, scalaVersion); - this.projectDir = projectDir; } @Override - public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDirFoo) throws ClassNotFoundException { + public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) throws ClassNotFoundException { List defaultImports = getDefaultImports(javaProject); defaultImports.addAll(additionalImports); @@ -27,8 +24,7 @@ public Object[] createCompileParameters(ClassLoader cl, File file, File destinat ScalaSeq.fromList(cl, defaultImports), isGenerateForwardsRouter(), generateReverseRoutes, - namespaceReverseRouter, - projectDir + namespaceReverseRouter ); String routeGenerator = PLAY_ROUTES_COMPILER_INJECTED_ROUTES_GENERATOR; diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerFactory.java b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerFactory.java index 3cd44346..c9746720 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerFactory.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/RoutesCompilerFactory.java @@ -5,16 +5,16 @@ import org.gradle.util.VersionNumber; public class RoutesCompilerFactory { - public static RoutesCompiler create(PlayPlatform playPlatform, String projectDir) { - return new RoutesCompiler(createAdapter(playPlatform, projectDir)); + public static RoutesCompiler create(PlayPlatform playPlatform) { + return new RoutesCompiler(createAdapter(playPlatform)); } - public static VersionedRoutesCompilerAdapter createAdapter(PlayPlatform playPlatform, String projectDir) { + public static VersionedRoutesCompilerAdapter createAdapter(PlayPlatform playPlatform) { String playVersion = playPlatform.getPlayVersion().get(); String scalaVersion = playPlatform.getScalaCompatibilityVersion().get(); switch (PlayMajorVersion.forPlatform(playPlatform)) { case PLAY_2_3_X: - return new RoutesCompilerAdapterV23X(playVersion, projectDir); + return new RoutesCompilerAdapterV23X(playVersion); case PLAY_2_4_X: if (VersionNumber.parse(playVersion).getMicro() < 6 && !"2.10".equals(scalaVersion)) { scalaVersion = "2.10"; @@ -26,7 +26,7 @@ public static VersionedRoutesCompilerAdapter createAdapter(PlayPlatform playPlat case PLAY_2_7_X: case PLAY_2_8_X: default: - return new RoutesCompilerAdapterV27X(playVersion, scalaVersion, projectDir); + return new RoutesCompilerAdapterV27X(playVersion, scalaVersion); } } } diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/VersionedRoutesCompilerAdapter.java b/src/main/java/org/gradle/playframework/tools/internal/routes/VersionedRoutesCompilerAdapter.java index ba67b216..c42b372d 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/VersionedRoutesCompilerAdapter.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/VersionedRoutesCompilerAdapter.java @@ -12,7 +12,7 @@ public interface VersionedRoutesCompilerAdapter extends Serializable { ScalaMethod getCompileMethod(ClassLoader cl) throws ClassNotFoundException; - Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports, String projectDir) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException; + Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection additionalImports) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException; Iterable getClassLoaderPackages(); From 6c7e6809c7b28fe1d31a8cc377581bb5c91f09ff Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Thu, 30 Mar 2023 16:37:44 +0200 Subject: [PATCH 04/15] Fix integration test relying on @DATE comment presence --- .../tasks/AbstractRoutesCompileIntegrationTest.groovy | 3 ++- .../gradle/playframework/fixtures/file/FileFixtures.groovy | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy index 54c1fa08..6cd2827d 100644 --- a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy +++ b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy @@ -10,6 +10,7 @@ import java.nio.charset.StandardCharsets import static org.gradle.playframework.fixtures.Repositories.playRepositories import static org.gradle.playframework.fixtures.file.FileFixtures.assertContentsHaveChangedSince +import static org.gradle.playframework.fixtures.file.FileFixtures.assertModificationTimeHasChangedSince import static org.gradle.playframework.fixtures.file.FileFixtures.snapshot import static org.gradle.playframework.plugins.PlayRoutesPlugin.ROUTES_COMPILE_TASK_NAME @@ -123,7 +124,7 @@ GET /newroute ${controllers()}.Application.index() and: assertContentsHaveChangedSince(scalaRoutesFileSnapshot, getScalaRoutesFile()) - assertContentsHaveChangedSince(javaRoutesFileSnapshot, getJavaRoutesFile()) + assertModificationTimeHasChangedSince(javaRoutesFileSnapshot, getJavaRoutesFile()) assertContentsHaveChangedSince(reverseRoutesFileSnapshot, getReverseRoutesFile()) when: diff --git a/src/integTestFixtures/groovy/org/gradle/playframework/fixtures/file/FileFixtures.groovy b/src/integTestFixtures/groovy/org/gradle/playframework/fixtures/file/FileFixtures.groovy index ddb74290..fe40d919 100644 --- a/src/integTestFixtures/groovy/org/gradle/playframework/fixtures/file/FileFixtures.groovy +++ b/src/integTestFixtures/groovy/org/gradle/playframework/fixtures/file/FileFixtures.groovy @@ -32,6 +32,11 @@ final class FileFixtures { assertNotEquals(oldSnapshot.hash, now.hash) } + static void assertModificationTimeHasChangedSince(Snapshot oldSnapshot, File file) { + Snapshot now = snapshot(file) + assertNotEquals(oldSnapshot.modTime, now.modTime) + } + private static File assertIsFile(File file) { assertTrue(file.isFile()) } From 482d85087e5e681607480727435295f1e444fdfa Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 3 Apr 2023 13:12:35 +0200 Subject: [PATCH 05/15] Relativize with RelativePathUtil --- .../tools/internal/routes/DefaultRoutesPostProcessor.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java index 90cf1f6d..b479162c 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java @@ -1,5 +1,6 @@ package org.gradle.playframework.tools.internal.routes; +import org.gradle.util.RelativePathUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,10 +31,7 @@ private String getSourceReplacementString(Iterable sources, File projectDi if(sources.iterator().hasNext()) { File sourceFile = sources.iterator().next(); - sourceReplacementString = "// @(SOURCE):" + - projectDir.toURI().relativize(sourceFile.toURI()) - .getPath() - .replace(File.separator, "/"); + sourceReplacementString = "// @(SOURCE):" + RelativePathUtil.relativePath(projectDir, sourceFile); } return sourceReplacementString; From 7da3b629928065b9bed10d4d8988ebe3d8d23778 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 3 Apr 2023 13:17:13 +0200 Subject: [PATCH 06/15] Post-process files compiled by Twirl --- .../tasks/TwirlCompileIntegrationTest.groovy | 20 +++++ .../twirl/DefaultTwirlPostProcessor.java | 76 +++++++++++++++++++ .../tools/internal/twirl/TwirlCompiler.java | 6 ++ 3 files changed, 102 insertions(+) create mode 100644 src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java diff --git a/src/integTest/groovy/org/gradle/playframework/tasks/TwirlCompileIntegrationTest.groovy b/src/integTest/groovy/org/gradle/playframework/tasks/TwirlCompileIntegrationTest.groovy index 7f19adbb..909a90a3 100644 --- a/src/integTest/groovy/org/gradle/playframework/tasks/TwirlCompileIntegrationTest.groovy +++ b/src/integTest/groovy/org/gradle/playframework/tasks/TwirlCompileIntegrationTest.groovy @@ -290,6 +290,26 @@ class TwirlCompileIntegrationTest extends PlayMultiVersionIntegrationTest { result.output.contains("Twirl compiler could not find a matching template for 'test.scala.custom'.") } + @Unroll + def "post-process generated comments"() { + given: + twirlTemplate("test.scala.${format}") << template + when: + build(SCALA_COMPILE_TASK_NAME) + then: + def generatedFile = new File(destinationDir, "${format}/test.template.scala") + generatedFile.isFile() + generatedFile.text.contains("SOURCE: views/test.scala.${format}") + !generatedFile.text.contains("DATE:") + + where: + format | templateFormat | template + "js" | 'JavaScriptFormat' | '@(username: String) alert(@helper.json(username));' + "xml" | 'XmlFormat' | '@(username: String) @username ' + "txt" | 'TxtFormat' | '@(username: String) @username' + "html" | 'HtmlFormat' | '@(username: String)

Hello @username

' + } + def withTemplateSource(File templateFile) { templateFile << """@(message: String) diff --git a/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java new file mode 100644 index 00000000..95e28891 --- /dev/null +++ b/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java @@ -0,0 +1,76 @@ +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.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Stream; + +class DefaultTwirlPostProcessor implements Serializable { + + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTwirlPostProcessor.class); + + private static final String GENERATED_TAG = "-- GENERATED --"; + private static final String ANY_CHAR_INCLUDING_NEW_LINE = "[\\S\\n\\r\\s]"; + private static final String ANY_LINES = ANY_CHAR_INCLUDING_NEW_LINE + "*"; + private static final String ANY_LINE = ".*" + ANY_CHAR_INCLUDING_NEW_LINE; + + // Example of text to match: + // /* + // -- 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 -- + // */ + private static final String PATTERN_MATCHER = "(/\\*" + ANY_LINES + GENERATED_TAG + ANY_LINES + ")(%s:\\s" + ANY_LINE + ")(" + ANY_LINES + GENERATED_TAG + ANY_LINES + "\\*/)"; + + void execute(TwirlCompileSpec spec) { + String sourceReplacementString = getSourceReplacementString(spec.getSources()); + + try (Stream 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 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 { + String content = new String(Files.readAllBytes(generatedFile), StandardCharsets.UTF_8); + String regexSource = String.format(PATTERN_MATCHER, "SOURCE"); + Matcher matcherSource = Pattern.compile(regexSource, Pattern.MULTILINE).matcher(content); + if(matcherSource.find() && matcherSource.groupCount() == 3) { + content = matcherSource.replaceAll("$1" + sourceReplacementString + "\n$3"); + } + String regexDate = String.format(PATTERN_MATCHER, "DATE"); + Matcher matcherDate = Pattern.compile(regexDate, Pattern.MULTILINE).matcher(content); + if(matcherDate.find() && matcherDate.groupCount() == 3) { + content = matcherDate.replaceAll("$1\n$3"); + } + Files.write(generatedFile, content.getBytes(StandardCharsets.UTF_8)); + } catch (IOException e) { + LOGGER.warn(String.format("Unable to post-process file %s", generatedFile.getFileName()), e); + } + } +} diff --git a/src/main/java/org/gradle/playframework/tools/internal/twirl/TwirlCompiler.java b/src/main/java/org/gradle/playframework/tools/internal/twirl/TwirlCompiler.java index 153f400d..7ad2d760 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/twirl/TwirlCompiler.java +++ b/src/main/java/org/gradle/playframework/tools/internal/twirl/TwirlCompiler.java @@ -23,8 +23,11 @@ public class TwirlCompiler implements Compiler, Serializable { private final VersionedTwirlCompilerAdapter adapter; + private final DefaultTwirlPostProcessor postProcessor; + public TwirlCompiler(VersionedTwirlCompilerAdapter adapter) { this.adapter = adapter; + this.postProcessor = new DefaultTwirlPostProcessor(); } @Override @@ -48,6 +51,9 @@ public WorkResult execute(TwirlCompileSpec spec) { } } + // Post-process files + postProcessor.execute(spec); + return WorkResults.didWork(!outputFiles.isEmpty()); } From 5e87c60523befcc1fae88045e0bf99896c56d9e2 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 3 Apr 2023 13:26:53 +0200 Subject: [PATCH 07/15] Bump up Twirl version --- .../tools/internal/twirl/TwirlCompilerFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gradle/playframework/tools/internal/twirl/TwirlCompilerFactory.java b/src/main/java/org/gradle/playframework/tools/internal/twirl/TwirlCompilerFactory.java index a8ef5152..189a4b81 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/twirl/TwirlCompilerFactory.java +++ b/src/main/java/org/gradle/playframework/tools/internal/twirl/TwirlCompilerFactory.java @@ -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); } } From b96b6330c7f4695dd08074267ebba9cb4e36e926 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 3 Apr 2023 13:31:51 +0200 Subject: [PATCH 08/15] Adjust documentation --- src/docs/asciidoc/10-plugin-conventions.adoc | 2 +- src/docs/asciidoc/16-generated-files-comments.adoc | 4 ++++ src/docs/asciidoc/16-routes-comments.adoc | 4 ---- 3 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 src/docs/asciidoc/16-generated-files-comments.adoc delete mode 100644 src/docs/asciidoc/16-routes-comments.adoc diff --git a/src/docs/asciidoc/10-plugin-conventions.adoc b/src/docs/asciidoc/10-plugin-conventions.adoc index 08874f46..af726a3b 100644 --- a/src/docs/asciidoc/10-plugin-conventions.adoc +++ b/src/docs/asciidoc/10-plugin-conventions.adoc @@ -10,4 +10,4 @@ include::14-source-sets.adoc[] include::15-dependency-configurations.adoc[] -include::16-routes-comments.adoc[] \ No newline at end of file +include::16-generated-files-comments.adoc[] \ No newline at end of file diff --git a/src/docs/asciidoc/16-generated-files-comments.adoc b/src/docs/asciidoc/16-generated-files-comments.adoc new file mode 100644 index 00000000..f9c2feb4 --- /dev/null +++ b/src/docs/asciidoc/16-generated-files-comments.adoc @@ -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. diff --git a/src/docs/asciidoc/16-routes-comments.adoc b/src/docs/asciidoc/16-routes-comments.adoc deleted file mode 100644 index 7b4ed6c8..00000000 --- a/src/docs/asciidoc/16-routes-comments.adoc +++ /dev/null @@ -1,4 +0,0 @@ -=== Generated routes files comments - -Routes files generated by the compiler 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. From 1cf9a7e0a38d967806ee92639e9e3a0f4de01a5a Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Tue, 4 Apr 2023 15:26:50 +0200 Subject: [PATCH 09/15] Fix integration test --- .../playframework/tasks/TwirlCompileIntegrationTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/integTest/groovy/org/gradle/playframework/tasks/TwirlCompileIntegrationTest.groovy b/src/integTest/groovy/org/gradle/playframework/tasks/TwirlCompileIntegrationTest.groovy index 909a90a3..5a57eff2 100644 --- a/src/integTest/groovy/org/gradle/playframework/tasks/TwirlCompileIntegrationTest.groovy +++ b/src/integTest/groovy/org/gradle/playframework/tasks/TwirlCompileIntegrationTest.groovy @@ -83,7 +83,7 @@ class TwirlCompileIntegrationTest extends PlayMultiVersionIntegrationTest { BuildResult result = build(SCALA_COMPILE_TASK_NAME) then: result.task(TWIRL_COMPILE_TASK_PATH).outcome == TaskOutcome.SUCCESS - result.task(SCALA_COMPILE_TASK_PATH).outcome == TaskOutcome.SUCCESS + result.task(SCALA_COMPILE_TASK_PATH).outcome == TaskOutcome.UP_TO_DATE } def "can specify additional imports for a Twirl template"() { From e9f8d8f57b71ddc5d2e3f681841136b8445bb1c6 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 24 Apr 2023 11:01:31 +0200 Subject: [PATCH 10/15] CR review: update test name --- .../tasks/AbstractRoutesCompileIntegrationTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy index 6cd2827d..b7c3622c 100644 --- a/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy +++ b/src/integTest/groovy/org/gradle/playframework/tasks/AbstractRoutesCompileIntegrationTest.groovy @@ -263,7 +263,7 @@ $ROUTES_COMPILE_TASK_NAME { new File(destinationDir, getScalaRoutesFileName('', '')).text.contains("extra.package") } - def "post-process generated comments"() { + def "post-processed generated comments contain path and timestamp replacements"() { given: withRoutesTemplate() when: From abe09b3d3c2cc7d08c1cf4934254288a33047a30 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 24 Apr 2023 11:01:54 +0200 Subject: [PATCH 11/15] CR review: add Javadoc --- .../tools/internal/routes/DefaultRoutesPostProcessor.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java index b479162c..5c3c021f 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java @@ -12,6 +12,10 @@ 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 class DefaultRoutesPostProcessor implements Serializable { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRoutesPostProcessor.class); @@ -22,7 +26,7 @@ void execute(RoutesCompileSpec spec) { try (Stream 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 routes", e); + LOGGER.warn("Unable to post-process files", e); } } @@ -44,7 +48,7 @@ private void process(Path routeFile, String 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 route file %s", routeFile.getFileName()), e); + LOGGER.warn(String.format("Unable to post-process file %s", routeFile.getFileName()), e); } } } From 3b7b0fdd82ea68af9d77a7f42ed643042e220952 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 24 Apr 2023 11:02:52 +0200 Subject: [PATCH 12/15] CR review: Do not expose projectDir as a member --- .../gradle/playframework/tasks/RoutesCompile.java | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java index 2766eb93..528ef6fb 100644 --- a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java +++ b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java @@ -52,8 +52,6 @@ public class RoutesCompile extends SourceTask { private final Property platform; private final Property injectedRoutesGenerator; private final ConfigurableFileCollection routesCompilerClasspath; - private final File projectDir; - @Inject public RoutesCompile(WorkerExecutor workerExecutor) { this.workerExecutor = workerExecutor; @@ -67,7 +65,6 @@ public RoutesCompile(WorkerExecutor workerExecutor) { this.injectedRoutesGenerator = getProject().getObjects().property(Boolean.class); this.injectedRoutesGenerator.set(false); this.routesCompilerClasspath = getProject().files(); - this.projectDir = getProject().getProjectDir(); } /** @@ -107,7 +104,7 @@ public ConfigurableFileCollection getRoutesCompilerClasspath() { @TaskAction @SuppressWarnings("Convert2Lambda") void compile() { - RoutesCompileSpec spec = new DefaultRoutesCompileSpec(getSource().getFiles(), getOutputDirectory().get().getAsFile(), isJavaProject(), getNamespaceReverseRouter().get(), getGenerateReverseRoutes().get(), getInjectedRoutesGenerator().get(), getAdditionalImports().get(), getProjectDir()); + RoutesCompileSpec spec = new DefaultRoutesCompileSpec(getSource().getFiles(), getOutputDirectory().get().getAsFile(), isJavaProject(), getNamespaceReverseRouter().get(), getGenerateReverseRoutes().get(), getInjectedRoutesGenerator().get(), getAdditionalImports().get(), getProject().getProjectDir()); if (GradleVersion.current().compareTo(GradleVersion.version("5.6")) < 0) { workerExecutor.submit(RoutesCompileRunnable.class, workerConfiguration -> { @@ -180,14 +177,4 @@ public Property getInjectedRoutesGenerator() { return injectedRoutesGenerator; } - /** - * The project directory is used to relativize the route source folder - * when post-processing the generated routes files. - * - * @return The project directory. - */ - @Internal - public File getProjectDir() { - return projectDir; - } } From 909df3e7d54500d92f77abf5629a63e70a17219c Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 24 Apr 2023 11:03:18 +0200 Subject: [PATCH 13/15] CR review: Process lines by iteration instead of regex --- .../twirl/DefaultTwirlPostProcessor.java | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java index 95e28891..484b285d 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java +++ b/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java @@ -9,30 +9,25 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +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 ANY_CHAR_INCLUDING_NEW_LINE = "[\\S\\n\\r\\s]"; - private static final String ANY_LINES = ANY_CHAR_INCLUDING_NEW_LINE + "*"; - private static final String ANY_LINE = ".*" + ANY_CHAR_INCLUDING_NEW_LINE; - - // Example of text to match: - // /* - // -- 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 -- - // */ - private static final String PATTERN_MATCHER = "(/\\*" + ANY_LINES + GENERATED_TAG + ANY_LINES + ")(%s:\\s" + ANY_LINE + ")(" + ANY_LINES + GENERATED_TAG + ANY_LINES + "\\*/)"; + 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()); @@ -57,18 +52,24 @@ private String getSourceReplacementString(Iterable sources) { private void process(Path generatedFile, String sourceReplacementString) { try { - String content = new String(Files.readAllBytes(generatedFile), StandardCharsets.UTF_8); - String regexSource = String.format(PATTERN_MATCHER, "SOURCE"); - Matcher matcherSource = Pattern.compile(regexSource, Pattern.MULTILINE).matcher(content); - if(matcherSource.find() && matcherSource.groupCount() == 3) { - content = matcherSource.replaceAll("$1" + sourceReplacementString + "\n$3"); - } - String regexDate = String.format(PATTERN_MATCHER, "DATE"); - Matcher matcherDate = Pattern.compile(regexDate, Pattern.MULTILINE).matcher(content); - if(matcherDate.find() && matcherDate.groupCount() == 3) { - content = matcherDate.replaceAll("$1\n$3"); + List generatedSourceLines = Files.readAllLines(generatedFile, StandardCharsets.UTF_8); + List 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, content.getBytes(StandardCharsets.UTF_8)); + + Files.write(generatedFile, updatedSourceLines, StandardCharsets.UTF_8); } catch (IOException e) { LOGGER.warn(String.format("Unable to post-process file %s", generatedFile.getFileName()), e); } From cb91bae96118881b4c5fd9e108dd29df7457e1a1 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 24 Apr 2023 11:16:48 +0200 Subject: [PATCH 14/15] CR review: Remove unused import --- src/main/java/org/gradle/playframework/tasks/RoutesCompile.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java index 528ef6fb..4ce2aa3b 100644 --- a/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java +++ b/src/main/java/org/gradle/playframework/tasks/RoutesCompile.java @@ -28,7 +28,6 @@ import org.gradle.workers.WorkerExecutor; import javax.inject.Inject; -import java.io.File; /** * Task for compiling routes templates into Scala code. From 2940cdd79690a56d579e2436b7654807fb7793c9 Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Mon, 24 Apr 2023 11:52:59 +0200 Subject: [PATCH 15/15] CR review: Adjust Javadoc --- .../routes/DefaultRoutesPostProcessor.java | 10 ++++++---- .../twirl/DefaultTwirlPostProcessor.java | 20 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java index 5c3c021f..39c69472 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java +++ b/src/main/java/org/gradle/playframework/tools/internal/routes/DefaultRoutesPostProcessor.java @@ -12,10 +12,12 @@ 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 +/** 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); diff --git a/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java b/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java index 484b285d..f836fa6c 100644 --- a/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java +++ b/src/main/java/org/gradle/playframework/tools/internal/twirl/DefaultTwirlPostProcessor.java @@ -13,15 +13,17 @@ 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 { +/** 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);