Skip to content

Commit

Permalink
Relativize routes files path
Browse files Browse the repository at this point in the history
  • Loading branch information
jprinet committed Mar 30, 2023
1 parent d7d6b85 commit 0a0728a
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 46 deletions.
6 changes: 0 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,3 @@ pluginBundle {
artifactId = base.archivesBaseName
}
}

publishing {
repositories {
mavenLocal()
}
}
2 changes: 1 addition & 1 deletion src/docs/asciidoc/30-plugin-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
2 changes: 1 addition & 1 deletion src/docs/samples/play-2.4/groovy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ play {
play {
injectedRoutesGenerator = true
}
// end::injected-routes-compiler[]
// end::injected-routes-compiler[]
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
});
}
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/gradle/playframework/tasks/RoutesCompile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -51,7 +52,7 @@ public class RoutesCompile extends SourceTask {
private final Property<PlayPlatform> platform;
private final Property<Boolean> injectedRoutesGenerator;
private final ConfigurableFileCollection routesCompilerClasspath;
private final String projectDir;
private final File projectDir;

@Inject
public RoutesCompile(WorkerExecutor workerExecutor) {
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -134,7 +135,7 @@ public void execute(RoutesCompileParameters parameters) {
}

private Compiler<RoutesCompileSpec> getCompiler() {
return RoutesCompilerFactory.create(getPlatform().get(), projectDir);
return RoutesCompilerFactory.create(getPlatform().get());
}

@Internal
Expand Down Expand Up @@ -178,4 +179,15 @@ public Property<Boolean> getGenerateReverseRoutes() {
public Property<Boolean> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class DefaultRoutesCompileSpec implements RoutesCompileSpec {
private final boolean generateReverseRoutes;
private final boolean injectedRoutesGenerator;
private final Collection<String> additionalImports;
private final String projectDir;
private final File projectDir;

public DefaultRoutesCompileSpec(Iterable<File> sourceFiles, File outputDirectory, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection<String> additionalImports, String projectDir) {
public DefaultRoutesCompileSpec(Iterable<File> sourceFiles, File outputDirectory, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection<String> additionalImports, File projectDir) {
this.sourceFiles = sourceFiles;
this.outputDirectory = outputDirectory;
this.javaProject = javaProject;
Expand Down Expand Up @@ -60,7 +60,7 @@ public Collection<String> getAdditionalImports() {
}

@Override
public String getProjectDir() {
public File getProjectDir() {
return projectDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Path> 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<File> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public interface RoutesCompileSpec extends PlayCompileSpec, Serializable {

Collection<String> getAdditionalImports();

String getProjectDir();
File getProjectDir();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RoutesCompilerAdapterV23X extends DefaultVersionedRoutesCompilerAdapter {
private final List<String> defaultScalaImports = Arrays.asList("controllers.Assets.Asset");
private final List<String> 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");
}
Expand All @@ -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<String> 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<String> additionalImports) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
List<String> defaultImports = javaProject ? defaultJavaImports : defaultScalaImports;
defaultImports.addAll(additionalImports);
return new Object[] {
Expand All @@ -45,8 +45,7 @@ public Object[] createCompileParameters(ClassLoader cl, File file, File destinat
ScalaListBuffer.fromList(cl, defaultImports),
generateReverseRoutes,
isGenerateRefReverseRouter(),
namespaceReverseRouter,
"barbar2"
namespaceReverseRouter
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> additionalImports, String projectDir) throws ClassNotFoundException {
public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection<String> additionalImports) throws ClassNotFoundException {
List<String> defaultImports = getDefaultImports(javaProject);
defaultImports.addAll(additionalImports);

Expand All @@ -53,8 +53,7 @@ public Object[] createCompileParameters(ClassLoader cl, File file, File destinat
ScalaListBuffer.fromList(cl, defaultImports),
isGenerateForwardsRouter(),
generateReverseRoutes,
namespaceReverseRouter,
"fuufuu"
namespaceReverseRouter
);

String routeGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> additionalImports, String projectDirFoo) throws ClassNotFoundException {
public Object[] createCompileParameters(ClassLoader cl, File file, File destinationDir, boolean javaProject, boolean namespaceReverseRouter, boolean generateReverseRoutes, boolean injectedRoutesGenerator, Collection<String> additionalImports) throws ClassNotFoundException {
List<String> defaultImports = getDefaultImports(javaProject);
defaultImports.addAll(additionalImports);

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> additionalImports) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException;

Iterable<String> getClassLoaderPackages();

Expand Down

0 comments on commit 0a0728a

Please sign in to comment.