private Map transpileHTLScriptsToJavaClasses()

in src/main/java/org/apache/sling/maven/htl/ValidateMojo.java [273:297]


    private Map<File, CompilationResult> transpileHTLScriptsToJavaClasses(List<File> scripts, SightlyCompiler compiler, JavaImportsAnalyzer
            javaImportsAnalyzer) throws IOException {
        Map<File, CompilationResult> compilationResult = new LinkedHashMap<>(scripts.size());
        for (File script : scripts) {
            JavaClassBackendCompiler backendCompiler = new JavaClassBackendCompiler(javaImportsAnalyzer);
            ScriptCompilationUnit compilationUnit = new ScriptCompilationUnit(sourceDirectory, script);
            compilationResult.put(script, compiler.compile(compilationUnit, backendCompiler));
            
            // strip off source directory path from script path for class info
            File scriptFile = new File(script.getPath());
            File sourceDirectoryDir = new File(sourceDirectory.getAbsolutePath());
            String shortenedScriptPath = StringUtils.substringAfter(scriptFile.getCanonicalPath(), sourceDirectoryDir.getCanonicalPath());
            
            ClassInfo classInfo = StringUtils.isNotEmpty(generatedJavaClassesPrefix)? new HTLClassInfo(generatedJavaClassesPrefix,
                    shortenedScriptPath) : new HTLClassInfo(shortenedScriptPath);
            String javaSourceCode = backendCompiler.build(classInfo);
            File generatedClassFile = new File(generatedJavaClassesDirectory, classInfo.getFullyQualifiedClassName()
                    .replaceAll("\\.", Matcher.quoteReplacement(File.separator)) + ".java");
            FileUtils.forceMkdirParent(generatedClassFile);
            IOUtils.write(javaSourceCode, new FileOutputStream(generatedClassFile), StandardCharsets.UTF_8);
            compilationUnit.dispose();
            getLog().debug(String.format("Transpiled HTL '%s' to Java class '%s'", script, generatedClassFile));
        }
        return compilationResult;
    }