All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.twirl.maven.AbstractTwirlCompileMojo Maven / Gradle / Ivy

/*
 * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 
 */

package play.twirl.maven;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.codehaus.plexus.util.FileUtils.getExtension;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.DirectoryScanner;
import play.japi.twirl.compiler.TwirlCompiler;
import scala.io.Codec;

public abstract class AbstractTwirlCompileMojo extends AbstractMojo {

  private static final Map DEFAULT_TEMPLATE_FORMATS =
      Map.of(
          "html",
          "play.twirl.api.HtmlFormat",
          "txt",
          "play.twirl.api.TxtFormat",
          "xml",
          "play.twirl.api.XmlFormat",
          "js",
          "play.twirl.api.JavaScriptFormat");

  /** The maven project. */
  @Parameter(property = "project", required = true, readonly = true)
  protected MavenProject project;

  protected abstract File getSourceDirectory();

  protected abstract File getOutputDirectory();

  /**
   * A set of inclusion filters for the Twirl compiler.
   *
   * 

Default: {@code **}{@code /*.scala.*} * *

Example: * *

{@code
   * 
   *   }{@code **}{@code /Include.scala.html
   * 
   * }
*/ @Parameter protected Set includes = new TreeSet<>(); /** * A set of exclusion filters for the Twirl compiler. * *

Example: * *

{@code
   * 
   *   {@code **}{@code *}/Exclude.scala.html
   * 
   * }
*/ @Parameter private Set excludes = new TreeSet<>(); /** * Defined twirl template formats. * *

Default: {@link #DEFAULT_TEMPLATE_FORMATS} * *

Example: * *

{@code
   * 
   *   play.twirl.api.HtmlFormat
   * 
   * }
*/ @Parameter(name = "templateFormats") private Map templateFormats = new LinkedHashMap<>(); /** * Imports that should be added to generated source files. * *

Default: {@link play.japi.twirl.compiler.TwirlCompiler#DEFAULT_IMPORTS} * *

Example: * *

{@code
   * 
   *   org.abc.backend._
   * 
   * }
*/ @Parameter(name = "templateImports") private Set templateImports = new LinkedHashSet<>(); /** * Annotations added to constructors in injectable templates. * *

Example: * *

{@code
   * 
   *   @javax.inject.Inject()
   * 
   * }
*/ @Parameter(name = "constructorAnnotations") private Set constructorAnnotations = new LinkedHashSet<>(); /** * Source encoding for template files and generated scala files. * *

Default: {@code UTF-8} * *

Example: * *

{@code
   * UTF-8
   * }
*/ @Parameter(defaultValue = "UTF-8") private String sourceEncoding; private void initDefaults() { if (includes.isEmpty()) { includes.add("**/*.scala.*"); } if (templateFormats.isEmpty()) { templateFormats = DEFAULT_TEMPLATE_FORMATS; } templateImports.addAll(TwirlCompiler.DEFAULT_IMPORTS); } @Override public void execute() throws MojoExecutionException, MojoFailureException { initDefaults(); getLog().info("Twirl Templates directory: " + getSourceDirectory()); getLog().info("Twirl Generated Sources directory: " + getOutputDirectory()); getLog().info("Twirl Include Filters: " + prettyString(includes)); getLog().info("Twirl Exclude Filters: " + prettyString(excludes)); getLog() .info( "Twirl Template Formats: " + prettyString( templateFormats.entrySet().stream() .map(it -> it.getKey() + " to " + it.getValue()) .collect(toList()))); getLog().info("Twirl Template Imports: " + prettyString(templateImports)); getLog().info("Twirl Constructor Annotations: " + prettyString(constructorAnnotations)); getLog().info("Twirl Source Encoding: " + sourceEncoding); final var templates = findTwirlTemplates(); if (templates.isEmpty()) { getLog().info("Twirl templates to compile weren't found."); return; } for (File file : templates) { final var extension = getExtension(file.getName()); final var format = templateFormats.get(extension); if (format == null) { throw new MojoFailureException( String.format( "Unknown file format of '%s'. Possible extentions: [%s]", file.getName(), prettyString(templateFormats.keySet()))); } if (getLog().isDebugEnabled()) { getLog().debug("Compile file: " + file); } TwirlCompiler.compile( file, getSourceDirectory(), getOutputDirectory(), format, TwirlCompiler.formatImports(templateImports, extension), new ArrayList<>(constructorAnnotations), Codec.string2codec(sourceEncoding), false); } } private String prettyString(Collection collection) { return collection.stream().collect(joining(", ", "[", "]")); } private Collection findTwirlTemplates() { final var scanner = new DirectoryScanner(); scanner.setIncludes(includes.toArray(new String[0])); scanner.setExcludes(excludes.toArray(new String[0])); scanner.addDefaultExcludes(); scanner.setBasedir(getSourceDirectory()); scanner.scan(); return stream(scanner.getIncludedFiles()) .map(path -> new File(getSourceDirectory(), path)) .collect(toList()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy