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

import com.cognifide.gradle.aem.AemException
import com.cognifide.gradle.aem.AemExtension
import com.cognifide.gradle.aem.common.instance.Instance
import com.cognifide.gradle.aem.tooling.vlt.VltException
import org.apache.commons.lang3.time.StopWatch
import java.io.File

class RcpClient(private val aem: AemExtension) {

    var sourceInstance: Instance? = aem.props.string("rcp.source")?.run { aem.instance(this) }

    var targetInstance: Instance? = aem.props.string("rcp.target")?.run { aem.instance(this) }

    var paths = aem.props.list("rcp.paths")

    var pathsFile = aem.props.string("rcp.pathsFile")?.let { aem.project.file(it) }

    var opts: String = aem.props.string("rcp.opts") ?: "-b 100 -r -u"

    val stopWatch = StopWatch()

    var copiedPaths = 0L

    fun copy() {
        if (paths == null && pathsFile == null) {
            throw AemException("RCP task need to have defined 'rcp.paths' or 'rcp.pathsFile")
        }

        paths?.let { copy(it) }
        pathsFile?.let { copy(it) }
    }

    fun copy(paths: List<String>) {
        paths.asSequence().map { pathMapping(it) }.forEach { copy(it.first, it.second) }
    }

    fun copy(pathsFile: File) {
        if (!pathsFile.exists()) {
            throw VltException("RCP paths file does not exist: $pathsFile")
        }

        pathsFile.useLines { line -> line.map { pathMapping(it) }.forEach { copy(it.first, it.second) } }
    }

    fun copy(path: String) = pathMapping(path).let { copy(it.first, it.second) }

    fun copy(paths: Map<String, String>) = paths.forEach { (sourcePath, targetPath) -> copy(sourcePath, targetPath) }

    fun copy(sourcePath: String, targetPath: String) {
        checkInstances()
        stopWatch.apply { if (!isStarted) start() else resume() }
        aem.vlt("rcp $opts ${sourceInstance!!.httpBasicAuthUrl}/crx/-/jcr:root$sourcePath ${targetInstance!!.httpBasicAuthUrl}/crx/-/jcr:root$targetPath")
        copiedPaths++
        stopWatch.stop()
    }<commit_msg>Simplification<commit_after>package com.cognifide.gradle.aem.tooling.rcp

import com.cognifide.gradle.aem.AemException
import com.cognifide.gradle.aem.AemExtension
import com.cognifide.gradle.aem.common.instance.Instance
import com.cognifide.gradle.aem.tooling.vlt.VltException
import org.apache.commons.lang3.time.StopWatch
import java.io.File

class RcpClient(private val aem: AemExtension) {

    var sourceInstance: Instance? = aem.props.string("rcp.source")?.run { aem.instance(this) }

    var targetInstance: Instance? = aem.props.string("rcp.target")?.run { aem.instance(this) }

    var paths = aem.props.list("rcp.paths")

    var pathsFile = aem.props.string("rcp.pathsFile")?.let { aem.project.file(it) }

    var opts: String = aem.props.string("rcp.opts") ?: "-b 100 -r -u"

    val stopWatch = StopWatch()

    var copiedPaths = 0L

    fun copy() {
        if (paths == null && pathsFile == null) {
            throw AemException("RCP need to have defined 'rcp.paths' or 'rcp.pathsFile'!")
        }

        paths?.let { copy(it) }
        pathsFile?.let { copy(it) }
    }

    fun copy(pathsFile: File) {
        if (!pathsFile.exists()) {
            throw VltException("RCP paths file does not exist: $pathsFile")
        }

        pathsFile.useLines { copy(it) }
    }

    fun copy(paths: Sequence<String>) = paths.forEach { copy(it) }

    fun copy(paths: Iterable<String>) = paths.forEach { copy(it) }

    fun copy(paths: Map<String, String>) = paths.forEach { (sourcePath, targetPath) -> copy(sourcePath, targetPath) }

    fun copy(path: String) = pathMapping(path).let { copy(it.first, it.second) }

    fun copy(sourcePath: String, targetPath: String) {
        checkInstances()
        stopWatch.apply { if (!isStarted) start() else resume() }
        aem.vlt("rcp $opts ${sourceInstance!!.httpBasicAuthUrl}/crx/-/jcr:root$sourcePath ${targetInstance!!.httpBasicAuthUrl}/crx/-/jcr:root$targetPath")
        copiedPaths++
        stopWatch.stop()
    }
{'fraction_non_alphanumeric': 0.14491315136476426, 'fraction_numerical': 0.0024813895781637717, 'mean_word_length': 3.229800629590766, 'pattern_counts': {'":': 0, '<': 8, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1890133', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1335, 'n_words': 260}