Search in sources :

Example 1 with TwirlTemplateFormat

use of org.gradle.language.twirl.TwirlTemplateFormat in project gradle by gradle.

the class TwirlCompiler method findTemplateFormat.

private TwirlTemplateFormat findTemplateFormat(TwirlCompileSpec spec, final File sourceFile) {
    Spec<TwirlTemplateFormat> hasExtension = new Spec<TwirlTemplateFormat>() {

        @Override
        public boolean isSatisfiedBy(TwirlTemplateFormat format) {
            return FileUtils.hasExtensionIgnoresCase(sourceFile.getName(), "." + format.getExtension());
        }
    };
    TwirlTemplateFormat format = CollectionUtils.findFirst(adapter.getDefaultTemplateFormats(), hasExtension);
    if (format == null) {
        format = CollectionUtils.findFirst(spec.getUserTemplateFormats(), hasExtension);
    }
    Preconditions.checkNotNull(format, "Twirl compiler could not find a matching template for '%s'.", sourceFile.getName());
    return format;
}
Also used : Spec(org.gradle.api.specs.Spec) TwirlTemplateFormat(org.gradle.language.twirl.TwirlTemplateFormat)

Example 2 with TwirlTemplateFormat

use of org.gradle.language.twirl.TwirlTemplateFormat in project gradle by gradle.

the class TwirlCompiler method execute.

@Override
public WorkResult execute(TwirlCompileSpec spec) {
    List<File> outputFiles = Lists.newArrayList();
    ClassLoader cl = getClass().getClassLoader();
    ScalaMethod compile = getCompileMethod(cl);
    Iterable<RelativeFile> sources = spec.getSources();
    for (RelativeFile sourceFile : sources) {
        TwirlTemplateFormat format = findTemplateFormat(spec, sourceFile.getFile());
        try {
            Object result = compile.invoke(buildCompileArguments(spec, cl, sourceFile, format));
            ScalaOptionInvocationWrapper<File> maybeFile = new ScalaOptionInvocationWrapper<File>(result);
            if (maybeFile.isDefined()) {
                File outputFile = maybeFile.get();
                outputFiles.add(outputFile);
            }
        } catch (Exception e) {
            throw new RuntimeException("Error invoking Play Twirl template compiler.", e);
        }
    }
    return WorkResults.didWork(!outputFiles.isEmpty());
}
Also used : RelativeFile(org.gradle.api.internal.file.RelativeFile) ScalaMethod(org.gradle.scala.internal.reflect.ScalaMethod) ScalaOptionInvocationWrapper(org.gradle.scala.internal.reflect.ScalaOptionInvocationWrapper) RelativeFile(org.gradle.api.internal.file.RelativeFile) File(java.io.File) TwirlTemplateFormat(org.gradle.language.twirl.TwirlTemplateFormat)

Aggregations

TwirlTemplateFormat (org.gradle.language.twirl.TwirlTemplateFormat)2 File (java.io.File)1 RelativeFile (org.gradle.api.internal.file.RelativeFile)1 Spec (org.gradle.api.specs.Spec)1 ScalaMethod (org.gradle.scala.internal.reflect.ScalaMethod)1 ScalaOptionInvocationWrapper (org.gradle.scala.internal.reflect.ScalaOptionInvocationWrapper)1