<commit_before>package com.cognifide.gradle.aem.tooling.tasks

import com.cognifide.gradle.aem.common.AemDefaultTask
import com.cognifide.gradle.aem.common.AemException
import com.cognifide.gradle.aem.common.AemTask
import com.cognifide.gradle.aem.common.Formats
import com.cognifide.gradle.aem.common.Patterns
import com.cognifide.gradle.aem.pkg.PackageDownloader
import com.cognifide.gradle.aem.tooling.clean.Cleaner
import com.cognifide.gradle.aem.tooling.vlt.VltRunner
import java.io.File
import java.util.regex.Pattern
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction

open class Sync : AemDefaultTask() {

    /**
     * Determines what need to be done (content copied and clean or something else).
     */
    @Internal
    var mode = Mode.of(aem.props.string("aem.sync.mode") ?: Mode.COPY_AND_CLEAN.name)

    /**
     * Determines a method of getting JCR content from remote instance.
     */
    @Internal
    var transfer = Transfer.of(aem.props.string("aem.sync.transfer") ?: Transfer.PACKAGE_DOWNLOAD.name)

    /**
     * Source instance from which JCR content will be copied.
     */
    @Internal
    var instance = aem.anyInstance

    /**
     * Determines which content will be copied from source instance.
     */
    @Internal
    var filter = aem.filter

    /**
     * Location of JCR content root to which content will be copied.
     */
    @Internal
    var contentPath = aem.config.packageRoot

    private val cleaner = Cleaner(project)

    private val downloader = PackageDownloader(aem, AemTask.temporaryDir(project, name))

    private val vlt = VltRunner(aem)

    private val filterRootDirs: List<File>
        get() {
            val contentDir = project.file(contentPath)
            if (!contentDir.exists()) {
                logger.warn("JCR content directory does not exist: $contentPath")
                return listOf()
            }

            return filter.rootDirs(contentDir)
        }

    private val filterNormalizedRootDirs by lazy { filterRootDirs.map { normalizeRoot(it) } }

    fun cleaner(options: Cleaner.() -> Unit) {
        cleaner.apply(options)
    }

    fun download(options: PackageDownloader.() -> Unit) {
        downloader.apply(options)
    }

    fun vlt(options: VltRunner.() -> Unit) {
        vlt.apply(options)
    }

    init {
        description = "Check out then clean JCR content."
    }

    @TaskAction
    fun sync() {
        try {
            if (mode != Mode.COPY_ONLY) {
                prepareContent()
            }

            if (!File(contentPath).exists()) {
                aem.notifier.notify("Cannot synchronize JCR content", "Directory does not exist: ${aem.config.packageJcrRoot}")<commit_msg>refactor<commit_after>package com.cognifide.gradle.aem.tooling.tasks

import com.cognifide.gradle.aem.common.AemDefaultTask
import com.cognifide.gradle.aem.common.AemException
import com.cognifide.gradle.aem.common.AemTask
import com.cognifide.gradle.aem.common.Formats
import com.cognifide.gradle.aem.common.Patterns
import com.cognifide.gradle.aem.pkg.PackageDownloader
import com.cognifide.gradle.aem.tooling.clean.Cleaner
import com.cognifide.gradle.aem.tooling.vlt.VltRunner
import java.io.File
import java.util.regex.Pattern
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction

open class Sync : AemDefaultTask() {

    /**
     * Determines what need to be done (content copied and clean or something else).
     */
    @Internal
    var mode = Mode.of(aem.props.string("aem.sync.mode") ?: Mode.COPY_AND_CLEAN.name)

    /**
     * Determines a method of getting JCR content from remote instance.
     */
    @Internal
    var transfer = Transfer.of(aem.props.string("aem.sync.transfer") ?: Transfer.PACKAGE_DOWNLOAD.name)

    /**
     * Source instance from which JCR content will be copied.
     */
    @Internal
    var instance = aem.anyInstance

    /**
     * Determines which content will be copied from source instance.
     */
    @Internal
    var filter = aem.filter

    /**
     * Location of JCR content root to which content will be copied.
     */
    @Internal
    var contentPath = aem.config.packageRoot

    private val cleaner = Cleaner(project)

    private val downloader = PackageDownloader(aem, AemTask.temporaryDir(project, name))

    private val vlt = VltRunner(aem)

    private val filterRootDirs: List<File>
        get() {
            val contentDir = project.file(contentPath)
            if (!contentDir.exists()) {
                logger.warn("JCR content directory does not exist: $contentPath")
                return listOf()
            }

            return filter.rootDirs(contentDir)
        }

    private val filterNormalizedRootDirs by lazy { filterRootDirs.map { normalizeRoot(it) }.distinct() }

    fun cleaner(options: Cleaner.() -> Unit) {
        cleaner.apply(options)
    }

    fun download(options: PackageDownloader.() -> Unit) {
        downloader.apply(options)
    }

    fun vlt(options: VltRunner.() -> Unit) {
        vlt.apply(options)
    }

    init {
        description = "Check out then clean JCR content."
    }

    @TaskAction
    fun sync() {
        try {
            if (mode != Mode.COPY_ONLY) {
                prepareContent()
            }

            if (!File(contentPath).exists()) {
                aem.notifier.notify("Cannot synchronize JCR content", "Directory does not exist: ${aem.config.packageJcrRoot}")
{'fraction_non_alphanumeric': 0.10451873622336517, 'fraction_numerical': 0.0, 'mean_word_length': 2.63, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1371505', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1635, 'n_words': 376}