<commit_before>package com.cognifide.gradle.aem.common.pkg.validator

import com.cognifide.gradle.aem.AemException
import com.cognifide.gradle.aem.AemExtension
import com.cognifide.gradle.aem.common.pkg.PackageException
import net.adamcin.oakpal.core.*
import net.adamcin.oakpal.core.OakpalPlan as BasePlan
import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore
import java.io.File

class PackageValidator(internal val aem: AemExtension) {

    private val logger = aem.logger

    var enabled = aem.props.boolean("aem.package.validator.enabled") ?: true

    var verbose = aem.props.boolean("aem.package.validator.verbose") ?: true

    var reportFile = aem.project.file("build/aem/package/validator/oakpal-report.json")

    private var opearOptions: OpearOptions.() -> Unit = {}

    fun opear(options: OpearOptions.() -> Unit) {
        this.opearOptions = options
    }

    fun validate(pkg: File, options: OpearOptions.() -> Unit = opearOptions) {
        validate(pkg, OpearOptions(this).apply(options))
    }

    private fun validate(pkg: File, opearDir: OpearOptions) {
        validate(pkg, opearDir.run { prepare(); dir })
    }

    fun validate(pkg: File, opearDir: File) {
        val opear = OpearFile.fromDirectory(opearDir).getOrElse {
            throw AemException("Opear directory cannot be read properly!")
        }

        val scanResult = BasePlan.fromJson(opear.defaultPlan)
                .map { plan ->
                    plan.toOakMachineBuilder(DefaultErrorListener(), opear.getPlanClassLoader(javaClass.classLoader))
                            .withNodeStoreSupplier { MemoryNodeStore() }
                            .build()
                }
                .map { oak -> oak.scanPackage(pkg) }

        if (scanResult.isFailure) {
            throw PackageException("Validating CRX package '$pkg' ended with failure!")
        } else {
            val reports = scanResult.getOrDefault(emptyList<CheckReport>())
            ReportMapper.writeReportsToFile(reports, reportFile)
        }
    }
}<commit_msg>OAKPal integration / PoC works<commit_after>package com.cognifide.gradle.aem.common.pkg.validator

import com.cognifide.gradle.aem.AemException
import com.cognifide.gradle.aem.AemExtension
import com.cognifide.gradle.aem.common.pkg.PackageException
import net.adamcin.oakpal.core.*
import net.adamcin.oakpal.core.OakpalPlan as BasePlan
import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore
import java.io.File

class PackageValidator(internal val aem: AemExtension) {

    private val logger = aem.logger

    var enabled = aem.props.boolean("aem.package.validator.enabled") ?: true

    var verbose = aem.props.boolean("aem.package.validator.verbose") ?: true

    var reportFile = aem.project.file("build/aem/package/validator/oakpal-report.json")

    private var opearOptions: OpearOptions.() -> Unit = {}

    fun opear(options: OpearOptions.() -> Unit) {
        this.opearOptions = options
    }

    fun validate(pkg: File, options: OpearOptions.() -> Unit = opearOptions) {
        validate(pkg, OpearOptions(this).apply(options))
    }

    private fun validate(pkg: File, opearDir: OpearOptions) {
        validate(pkg, opearDir.run { prepare(); dir })
    }

    fun validate(pkg: File, opearDir: File) {
        val opear = OpearFile.fromDirectory(opearDir).getOrElse {
            throw AemException("Opear directory cannot be read properly!")
        }

        val planUsed = File(opearDir, "plan.json").takeIf { it.exists() }?.toURI()?.toURL() ?: opear.defaultPlan
        val scanResult = BasePlan.fromJson(planUsed)
                .map { plan ->
                    plan.toOakMachineBuilder(DefaultErrorListener(), opear.getPlanClassLoader(javaClass.classLoader))
                            .withNodeStoreSupplier { MemoryNodeStore() }
                            .build()
                }
                .map { oak -> oak.scanPackage(pkg) }

        if (scanResult.isFailure) {
            throw PackageException("Validating CRX package '$pkg' ended with failure!")
        } else {
            val reports = scanResult.getOrDefault(emptyList<CheckReport>())
            ReportMapper.writeReportsToFile(reports, reportFile)

            // TODO fail build on errors etc
        }
    }
}
{'fraction_non_alphanumeric': 0.11087719298245614, 'fraction_numerical': 0.0, 'mean_word_length': 2.760773966578716, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3637130', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1285, 'n_words': 242}