Skip to content

Commit

Permalink
Merge pull request #719 from ihostage/format-imports
Browse files Browse the repository at this point in the history
Fix format imports for Gradle/Maven
  • Loading branch information
mkurz authored Dec 7, 2023
2 parents b11f34f + 7a5b06f commit 4fdf417
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 27 deletions.
35 changes: 20 additions & 15 deletions compiler/src/main/java/play/japi/twirl/compiler/TwirlCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -24,14 +22,10 @@ public class TwirlCompiler {
public static final Set<String> DEFAULT_IMPORTS;

static {
Set<String> imports = new HashSet<>();
String scalaVersion = play.twirl.compiler.BuildInfo$.MODULE$.scalaVersion();
imports.addAll(
JavaConverters$.MODULE$
.seqAsJavaListConverter(
play.twirl.compiler.TwirlCompiler$.MODULE$.defaultImports(scalaVersion))
.asJava());
DEFAULT_IMPORTS = Collections.unmodifiableSet(imports);
DEFAULT_IMPORTS =
Set.copyOf(
toJavaList(play.twirl.compiler.TwirlCompiler$.MODULE$.defaultImports(scalaVersion)));
}

public static Optional<File> compile(
Expand Down Expand Up @@ -62,12 +56,8 @@ public static Optional<File> compile(
List<String> constructorAnnotations,
Codec codec,
boolean inclusiveDot) {
Seq<String> scalaAdditionalImports =
JavaConverters$.MODULE$
.asScalaBufferConverter(new ArrayList<String>(additionalImports))
.asScala();
Seq<String> scalaConstructorAnnotations =
JavaConverters$.MODULE$.asScalaBufferConverter(constructorAnnotations).asScala();
Seq<String> scalaAdditionalImports = toScalaSeq(additionalImports);
Seq<String> scalaConstructorAnnotations = toScalaSeq(constructorAnnotations);

Option<File> option =
play.twirl.compiler.TwirlCompiler.compile(
Expand All @@ -81,4 +71,19 @@ public static Optional<File> compile(
inclusiveDot);
return Optional.ofNullable(option.nonEmpty() ? option.get() : null);
}

public static Collection<String> formatImports(
Collection<String> templateImports, String extension) {
return toJavaList(
play.twirl.compiler.TwirlCompiler.formatImports(
toScalaSeq(templateImports).toSeq(), extension));
}

private static <T> Seq<T> toScalaSeq(Collection<T> collection) {
return JavaConverters$.MODULE$.asScalaBufferConverter(new ArrayList<>(collection)).asScala();
}

private static <T> List<T> toJavaList(Seq<T> seq) {
return JavaConverters$.MODULE$.seqAsJavaListConverter(seq).asJava();
}
}
14 changes: 7 additions & 7 deletions gradle-twirl/src/main/java/play/twirl/gradle/TwirlCompile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
Expand Down Expand Up @@ -83,21 +84,20 @@ void compile(InputChanges changes) {
parameters.getSourceFile().set(sourceFile.getFile());
parameters.getSourceDirectory().set(sourceFile.getBaseDir());
parameters.getDestinationDirectory().set(getDestinationDirectory());
parameters
.getFormatterType()
.set(getFormatterType(templateFormats, sourceFile.getFile()));
Entry<String, String> format = getFormat(templateFormats, sourceFile.getFile());
parameters.getFormatExtension().set(format.getKey());
parameters.getFormatterType().set(format.getValue());
parameters.getTemplateImports().set(getTemplateImports());
parameters.getConstructorAnnotations().set(getConstructorAnnotations());
parameters.getSourceEncoding().set(getSourceEncoding());
});
}
}

private String getFormatterType(Map<String, String> formats, File file) {
return formats.keySet().stream()
.filter(ext -> FileUtils.hasExtensionIgnoresCase(file.getName(), ext))
private Entry<String, String> getFormat(Map<String, String> formats, File file) {
return formats.entrySet().stream()
.filter(f -> FileUtils.hasExtensionIgnoresCase(file.getName(), f.getKey()))
.findFirst()
.map(formats::get)
.orElseThrow(
() ->
new GradleException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private void compile() {
File sourceDirectory = getParameters().getSourceDirectory().getAsFile().get();
File destinationDirectory = getParameters().getDestinationDirectory().getAsFile().get();
String formatterType = getParameters().getFormatterType().get();
String extension = getParameters().getFormatExtension().get();
getParameters().getTemplateImports().addAll(TwirlCompiler.DEFAULT_IMPORTS);
Collection<String> imports = getParameters().getTemplateImports().get();
List<String> constructorAnnotations = getParameters().getConstructorAnnotations().get();
Expand All @@ -84,7 +85,7 @@ private void compile() {
sourceDirectory,
destinationDirectory,
formatterType,
imports,
TwirlCompiler.formatImports(imports, extension),
constructorAnnotations,
Codec.string2codec(sourceEncoding),
false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface TwirlCompileParams extends WorkParameters {

Property<String> getFormatterType();

Property<String> getFormatExtension();

SetProperty<String> getTemplateImports();

ListProperty<String> getConstructorAnnotations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void testCommonBuild(String gradleVersion) throws IOException {
.isNotEmptyFile()
.binaryContent()
.asString()
.contains("import java.lang._", "class c @java.lang.Deprecated()");
.contains("import java.lang._", "class c @java.lang.Deprecated()", "import a.b.html._");

BuildTask compileScalaResult = result.task(":compileScala");
assertThat(compileScalaResult).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sourceSets {
main {
twirl {
templateImports.add("java.lang._")
templateImports.add("a.b.%format%._")
constructorAnnotations.add("@java.lang.Deprecated()")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

for (File file : templates) {
final var format = templateFormats.get(getExtension(file.getName()));
final var extension = getExtension(file.getName());
final var format = templateFormats.get(extension);
if (format == null) {
throw new MojoFailureException(
String.format(
Expand All @@ -186,7 +187,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getSourceDirectory(),
getOutputDirectory(),
format,
templateImports,
TwirlCompiler.formatImports(templateImports, extension),
new ArrayList<>(constructorAnnotations),
Codec.string2codec(sourceEncoding),
false);
Expand Down
1 change: 1 addition & 0 deletions maven-twirl/src/maven-test/simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<configuration>
<templateImports>
<import>java.lang._</import>
<import>a.b.%format%._</import>
</templateImports>
<constructorAnnotations>
<annotation>@java.lang.Deprecated()</annotation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void testCommonBuild() {
.isNotEmptyFile()
.binaryContent()
.asString()
.contains("import java.lang._", "@java.lang.Deprecated()"));
.contains(
"import java.lang._", "@java.lang.Deprecated()", "import a.b.html._"));

var compiledScalaSources =
list(
Expand Down

0 comments on commit 4fdf417

Please sign in to comment.