Search in sources :

Example 1 with SightlyCompiler

use of org.apache.sling.scripting.sightly.compiler.SightlyCompiler in project sling by apache.

the class ValidateMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("Skipping validation.");
        return;
    }
    long start = System.currentTimeMillis();
    if (!sourceDirectory.isAbsolute()) {
        sourceDirectory = new File(project.getBasedir(), sourceDirectory.getPath());
    }
    if (!sourceDirectory.exists()) {
        getLog().info("Source directory does not exist, skipping.");
        return;
    }
    if (!sourceDirectory.isDirectory()) {
        throw new MojoExecutionException(String.format("Configured sourceDirectory={%s} is not a directory.", sourceDirectory.getAbsolutePath()));
    }
    if (!buildContext.hasDelta(sourceDirectory)) {
        getLog().info("No files found to validate, skipping.");
        return;
    }
    // don't fail execution in Eclipse as it generates an error marker in the POM file, which is not desired
    boolean mayFailExecution = !buildContext.getClass().getName().startsWith("org.eclipse.m2e");
    sourceDirectoryLength = sourceDirectory.getAbsolutePath().length();
    processedIncludes = processIncludes();
    processedExcludes = processExcludes();
    try {
        SightlyCompiler compiler = new SightlyCompiler();
        Scanner scanner = buildContext.newScanner(sourceDirectory);
        scanner.setExcludes(new String[] { processedExcludes });
        scanner.setIncludes(new String[] { processedIncludes });
        scanner.scan();
        String[] includedFiles = scanner.getIncludedFiles();
        processedFiles = new ArrayList<>(includedFiles.length);
        for (String includedFile : includedFiles) {
            processedFiles.add(new File(sourceDirectory, includedFile));
        }
        Map<File, CompilationResult> compilationResults = new HashMap<>();
        for (File script : processedFiles) {
            compilationResults.put(script, compiler.compile(getCompilationUnit(script)));
        }
        for (Map.Entry<File, CompilationResult> entry : compilationResults.entrySet()) {
            File script = entry.getKey();
            CompilationResult result = entry.getValue();
            buildContext.removeMessages(script);
            if (result.getWarnings().size() > 0) {
                for (CompilerMessage message : result.getWarnings()) {
                    buildContext.addMessage(script, message.getLine(), message.getColumn(), message.getMessage(), BuildContext.SEVERITY_WARNING, null);
                }
                hasWarnings = true;
            }
            if (result.getErrors().size() > 0) {
                for (CompilerMessage message : result.getErrors()) {
                    String messageString = message.getMessage().replaceAll(System.lineSeparator(), "");
                    buildContext.addMessage(script, message.getLine(), message.getColumn(), messageString, BuildContext.SEVERITY_ERROR, null);
                }
                hasErrors = true;
            }
        }
        getLog().info("Processed " + processedFiles.size() + " files in " + (System.currentTimeMillis() - start) + " milliseconds");
        if (mayFailExecution && hasWarnings && failOnWarnings) {
            throw new MojoFailureException("Compilation warnings were configured to fail the build.");
        }
        if (mayFailExecution && hasErrors) {
            throw new MojoFailureException("Please check the reported syntax errors.");
        }
    } catch (IOException e) {
        throw new MojoExecutionException(String.format("Cannot filter files from {%s} with includes {%s} and excludes {%s}.", sourceDirectory.getAbsolutePath(), processedIncludes, processedExcludes), e);
    }
}
Also used : Scanner(org.codehaus.plexus.util.Scanner) CompilerMessage(org.apache.sling.scripting.sightly.compiler.CompilerMessage) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) SightlyCompiler(org.apache.sling.scripting.sightly.compiler.SightlyCompiler) CompilationResult(org.apache.sling.scripting.sightly.compiler.CompilationResult) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SightlyCompiler

use of org.apache.sling.scripting.sightly.compiler.SightlyCompiler in project sling by apache.

the class JavaClassBackendCompilerTest method testScript.

@Test
public void testScript() throws Exception {
    CompilationUnit compilationUnit = TestUtils.readScriptFromClasspath("/test.html");
    JavaClassBackendCompiler backendCompiler = new JavaClassBackendCompiler();
    SightlyCompiler sightlyCompiler = new SightlyCompiler();
    sightlyCompiler.compile(compilationUnit, backendCompiler);
    ClassInfo classInfo = buildClassInfo("testScript");
    String source = backendCompiler.build(classInfo);
    StringWriter writer = new StringWriter();
    Bindings bindings = new SimpleBindings();
    RenderContext renderContext = buildRenderContext(bindings);
    render(writer, classInfo, source, renderContext, new SimpleBindings());
    String expectedOutput = IOUtils.toString(this.getClass().getResourceAsStream("/test-output.html"), "UTF-8");
    assertEquals(expectedOutput, writer.toString());
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) JavaClassBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler) StringWriter(java.io.StringWriter) SimpleBindings(javax.script.SimpleBindings) SightlyCompiler(org.apache.sling.scripting.sightly.compiler.SightlyCompiler) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ClassInfo(org.apache.sling.scripting.sightly.java.compiler.ClassInfo) Test(org.junit.Test)

Example 3 with SightlyCompiler

use of org.apache.sling.scripting.sightly.compiler.SightlyCompiler in project sling by apache.

the class JavaClassBackendCompilerTest method sling_6094_2.

@Test
public void sling_6094_2() throws Exception {
    CompilationUnit compilationUnit = TestUtils.readScriptFromClasspath("/SLING-6094.2.html");
    JavaClassBackendCompiler backendCompiler = new JavaClassBackendCompiler();
    SightlyCompiler sightlyCompiler = new SightlyCompiler();
    sightlyCompiler.compile(compilationUnit, backendCompiler);
    ClassInfo classInfo = buildClassInfo("sling_6094_2");
    String source = backendCompiler.build(classInfo);
    StringWriter writer = new StringWriter();
    Bindings bindings = new SimpleBindings();
    RenderContext renderContext = buildRenderContext(bindings);
    render(writer, classInfo, source, renderContext, new SimpleBindings());
    String expectedOutput = IOUtils.toString(this.getClass().getResourceAsStream("/SLING-6094.2.output.html"), "UTF-8");
    assertEquals(expectedOutput, writer.toString());
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) JavaClassBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler) StringWriter(java.io.StringWriter) SimpleBindings(javax.script.SimpleBindings) SightlyCompiler(org.apache.sling.scripting.sightly.compiler.SightlyCompiler) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ClassInfo(org.apache.sling.scripting.sightly.java.compiler.ClassInfo) Test(org.junit.Test)

Example 4 with SightlyCompiler

use of org.apache.sling.scripting.sightly.compiler.SightlyCompiler in project sling by apache.

the class JavaClassBackendCompilerTest method sling_6094_1.

@Test
public void sling_6094_1() throws Exception {
    CompilationUnit compilationUnit = TestUtils.readScriptFromClasspath("/SLING-6094.1.html");
    JavaClassBackendCompiler backendCompiler = new JavaClassBackendCompiler();
    SightlyCompiler sightlyCompiler = new SightlyCompiler();
    sightlyCompiler.compile(compilationUnit, backendCompiler);
    ClassInfo classInfo = buildClassInfo("sling_6094_1");
    String source = backendCompiler.build(classInfo);
    StringWriter writer = new StringWriter();
    Bindings bindings = new SimpleBindings();
    bindings.put("img", new HashMap<String, Object>() {

        {
            put("attributes", new HashMap<String, String>() {

                {
                    put("v-bind:src", "replaced");
                }
            });
        }
    });
    RenderContext renderContext = buildRenderContext(bindings);
    render(writer, classInfo, source, renderContext, new SimpleBindings());
    String expectedOutput = IOUtils.toString(this.getClass().getResourceAsStream("/SLING-6094.1.output.html"), "UTF-8");
    assertEquals(expectedOutput, writer.toString());
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) JavaClassBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) SimpleBindings(javax.script.SimpleBindings) SightlyCompiler(org.apache.sling.scripting.sightly.compiler.SightlyCompiler) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ClassInfo(org.apache.sling.scripting.sightly.java.compiler.ClassInfo) Test(org.junit.Test)

Aggregations

SightlyCompiler (org.apache.sling.scripting.sightly.compiler.SightlyCompiler)4 StringWriter (java.io.StringWriter)3 Bindings (javax.script.Bindings)3 SimpleBindings (javax.script.SimpleBindings)3 CompilationUnit (org.apache.sling.scripting.sightly.compiler.CompilationUnit)3 ClassInfo (org.apache.sling.scripting.sightly.java.compiler.ClassInfo)3 JavaClassBackendCompiler (org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler)3 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 CompilationResult (org.apache.sling.scripting.sightly.compiler.CompilationResult)1 CompilerMessage (org.apache.sling.scripting.sightly.compiler.CompilerMessage)1 Scanner (org.codehaus.plexus.util.Scanner)1