<commit_before>/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.api.plugins.clover

import org.gradle.api.Action
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 * Clover code instrumentation action.
 *
 * @author <NAME>
 */
class InstrumentCodeAction implements Action<Task> {
    private static final Logger LOGGER = LoggerFactory.getLogger(InstrumentCodeAction)
    String initString
    Boolean compileGroovy
    FileCollection testRuntimeClasspath
    FileCollection groovyClasspath
    @OutputDirectory File classesBackupDir
    @OutputDirectory File testClassesBackupDir
    @InputFile File licenseFile
    @InputDirectory File buildDir
    @InputDirectory File classesDir
    @InputDirectory File testClassesDir
    Set<File> srcDirs
    Set<File> testSrcDirs
    String sourceCompatibility
    String targetCompatibility
    List<String> includes
    List<String> excludes
    List<String> testIncludes
    def statementContexts
    def methodContexts

    @Override
    void execute(Task task) {
        instrumentCode()
    }

    void instrumentCode() {
        if(getClassesDir().exists()) {
            LOGGER.info 'Starting to instrument code using Clover.'

            def ant = new AntBuilder()
            ant.taskdef(resource: 'cloverlib.xml', classpath: getTestRuntimeClasspath().asPath)
            ant.property(name: 'clover.license.path', value: getLicenseFile().canonicalPath)
            ant."clover-clean"(initString: "${getBuildDir()}/${getInitString()}")

            ant.'clover-setup'(initString: "${getBuildDir()}/${getInitString()}") {
                getSrcDirs().each { srcDir ->
                    ant.fileset(dir: srcDir) {
                        getIncludes().each { include ->
                            ant.include(name: include)
                        }

                        getExcludes().each { exclude ->
                            ant.exclude(name: exclude)
                        }
                    }
                }

                getTestSrcDirs().each { testSrcDir ->
                    ant.testsources(dir: testSrcDir) {
                        getTestIncludes().each { include ->
                            ant.include(name: include)
                        }
                    }
                }

                // Apply statement and method coverage contexts
                getStatementContexts().each {
                    ant.statementContext(name: it.name, regexp: it.regexp)
                }

                getMethodContexts().each {
                    ant.methodContext(name: it.name, regexp: it.regexp)
                }
            }

            // Move original classes
            ant.move(file: getClassesDir().canonicalPath, tofile: getClassesBackupDir().canonicalPath)
            ant.move(file: getTestClassesDir().canonicalPath, tofile: getTestClassesBackupDir().canonicalPath, failonerror: false)

            // Compile instrumented classes
            getClassesDir().mkdirs()
            getTestClassesDir().mkdirs()
            compileClasses(ant, getSrcDirs(), getClassesDir())

            if(getTestSrcDirs().size() > 0) {
                compileClasses(ant, getTestSrcDirs(), getTestClassesDir(), getClassesDir().canonicalPath)
            }

            // Copy resources
            ant.copy(todir: getClassesDir().canonicalPath) {
                fileset(dir: getClassesBackupDir().canonicalPath, excludes: '**/*.class')
            }
            ant.copy(todir: getTestClassesDir().canonicalPath, failonerror: false) {
                fileset(dir: getTestClassesBackupDir().canonicalPath, excludes: '**/*.class')
            }

            LOGGER.info 'Finished instrumenting code using Clover.'
        }
    }

    /**
     * Compiles Java and Groovy classes.
     *
     * @param ant Ant Builder
     */
    private void compileClasses(AntBuilder ant, Set<File> srcDirs, File destDir, String additionalClasspath = null) {
        if(getCompileGroovy()) {
            // Make sure the Groovy version define in project is used on classpath to avoid using the default Gradle version
            def groovycClasspath = getGroovyClasspath().asPath + System.getProperty('path.separator') + getTestRuntimeClasspath().asPath

            if(additionalClasspath) {
                groovycClasspath += System.getProperty('path.separator') + additionalClasspath
            }

            ant.taskdef(name: 'groovyc', classname: 'org.codehaus.groovy.ant.Groovyc', classpath: getGroovyClasspath().asPath)

            ant.groovyc(destdir: destDir.canonicalPath, classpath: groovycClasspath) {
                srcDirs.each { srcDir ->
                    src(path: srcDir)
                }

                ant.javac(source: getSourceCompatibility(), target: getTargetCompatibility())
            }
        }
        else {
            def classpath = getTestRuntimeClasspath().asPath

            if(additionalClasspath) {
                classpath += System.getProperty('path.separator') + additionalClasspath
            }

            ant.javac(destdir: destDir.canonicalPath, source: getSourceCompatibility(), target: getTargetCompatibility(),
                      classpath: classpath) {
                srcDirs.each { srcDir ->
                    src(path: srcDir)
                }
            }
        }
    }
}<commit_msg>Extracted code into methods.<commit_after>/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.api.plugins.clover

import org.gradle.api.Action
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 * Clover code instrumentation action.
 *
 * @author <NAME>
 */
class InstrumentCodeAction implements Action<Task> {
    private static final Logger LOGGER = LoggerFactory.getLogger(InstrumentCodeAction)
    String initString
    Boolean compileGroovy
    FileCollection testRuntimeClasspath
    FileCollection groovyClasspath
    @OutputDirectory File classesBackupDir
    @OutputDirectory File testClassesBackupDir
    @InputFile File licenseFile
    @InputDirectory File buildDir
    @InputDirectory File classesDir
    @InputDirectory File testClassesDir
    Set<File> srcDirs
    Set<File> testSrcDirs
    String sourceCompatibility
    String targetCompatibility
    List<String> includes
    List<String> excludes
    List<String> testIncludes
    def statementContexts
    def methodContexts

    @Override
    void execute(Task task) {
        instrumentCode()
    }

    void instrumentCode() {
        if(getClassesDir().exists()) {
            LOGGER.info 'Starting to instrument code using Clover.'

            def ant = new AntBuilder()
            ant.taskdef(resource: 'cloverlib.xml', classpath: getTestRuntimeClasspath().asPath)
            ant.property(name: 'clover.license.path', value: getLicenseFile().canonicalPath)
            ant."clover-clean"(initString: "${getBuildDir()}/${getInitString()}")

            ant.'clover-setup'(initString: "${getBuildDir()}/${getInitString()}") {
                getSrcDirs().each { srcDir ->
                    ant.fileset(dir: srcDir) {
                        getIncludes().each { include ->
                            ant.include(name: include)
                        }

                        getExcludes().each { exclude ->
                            ant.exclude(name: exclude)
                        }
                    }
                }

                getTestSrcDirs().each { testSrcDir ->
                    ant.testsources(dir: testSrcDir) {
                        getTestIncludes().each { include ->
                            ant.include(name: include)
                        }
                    }
                }

                // Apply statement and method coverage contexts
                getStatementContexts().each {
                    ant.statementContext(name: it.name, regexp: it.regexp)
                }

                getMethodContexts().each {
                    ant.methodContext(name: it.name, regexp: it.regexp)
                }
            }

            // Move original classes
            ant.move(file: getClassesDir().canonicalPath, tofile: getClassesBackupDir().canonicalPath)
            ant.move(file: getTestClassesDir().canonicalPath, tofile: getTestClassesBackupDir().canonicalPath, failonerror: false)

            // Compile instrumented classes
            getClassesDir().mkdirs()
            getTestClassesDir().mkdirs()
            compileClasses(ant)

            // Copy resources
            ant.copy(todir: getClassesDir().canonicalPath) {
                fileset(dir: getClassesBackupDir().canonicalPath, excludes: '**/*.class')
            }
            ant.copy(todir: getTestClassesDir().canonicalPath, failonerror: false) {
                fileset(dir: getTestClassesBackupDir().canonicalPath, excludes: '**/*.class')
            }

            LOGGER.info 'Finished instrumenting code using Clover.'
        }
    }

    /**
     * Compiles Java classes. If project has Groovy plugin applied run the joint compiler.
     *
     * @param ant Ant builder
     */
    private void compileClasses(AntBuilder ant) {
        if(getCompileGroovy()) {
            ant.taskdef(name: 'groovyc', classname: 'org.codehaus.groovy.ant.Groovyc', classpath: getGroovyClasspath().asPath)
            compileGroovyAndJavaSrcFiles(ant)

            if(getTestSrcDirs().size() > 0) {
                compileGroovyAndJavaTestSrcFiles(ant)
            }
        }
        else {
            compileJavaSrcFiles(ant)

            if(getTestSrcDirs().size() > 0) {
                compileJavaTestSrcFiles(ant)
            }
        }
    }

    /**
     * Gets Groovyc classpath. Make sure the Groovy version defined in project is used on classpath first to avoid using the
     * default version bundled with Gradle.
     *
     * @return Classpath
     */
    private String getGroovycClasspath() {
        getGroovyClasspath().asPath + System.getProperty('path.separator') + getTestRuntimeClasspath().asPath
    }

    /**
     * Gets Javac classpath.
     *
     * @return Classpath
     */
    private String getJavacClasspath() {
        getTestRuntimeClasspath().asPath
    }

    /**
     * Compiles Groovy and Java source files.
     *
     * @param ant Ant builder
     */
    private void compileGroovyAndJavaSrcFiles(AntBuilder ant) {
        compileGroovyAndJava(ant, getSrcDirs(), getClassesDir(), getGroovycClasspath())
    }

    /**
     * Compiles Java source files.
     *
     * @param ant Ant builder
     */
    private void compileJavaSrcFiles(AntBuilder ant) {
        compileJava(ant, getSrcDirs(), getClassesDir(), getJavacClasspath())
    }

    /**
     * Compiles Groovy and Java test source files.
     *
     * @param ant Ant builder
     */
    private void compileGroovyAndJavaTestSrcFiles(AntBuilder ant) {
        String classpath = addClassesDirToClasspath(getGroovycClasspath())
        compileGroovyAndJava(ant, getTestSrcDirs(), getTestClassesDir(), classpath)
    }

    /**
     * Compiles Java test source files.
     *
     * @param ant Ant builder
     */
    private void compileJavaTestSrcFiles(AntBuilder ant) {
        String classpath = addClassesDirToClasspath(getJavacClasspath())
        compileJava(ant, getTestSrcDirs(), getTestClassesDir(), classpath)
    }

    /**
     * Adds classes directory to classpath.
     *
     * @param classpath Classpath
     * @return Classpath
     */
    private String addClassesDirToClasspath(String classpath) {
        classpath + System.getProperty('path.separator') + getClassesDir().canonicalPath
    }

    /**
     * Compiles given Groovy and Java source files to destination directory.
     *
     * @param ant Ant builder
     * @param srcDirs Source directories
     * @param destDir Destination directory
     * @param classpath Classpath
     */
    private void compileGroovyAndJava(AntBuilder ant, Set<File> srcDirs, File destDir, String classpath) {
        ant.groovyc(destdir: destDir.canonicalPath, classpath: classpath) {
            srcDirs.each { srcDir ->
                src(path: srcDir)
            }

            ant.javac(source: getSourceCompatibility(), target: getTargetCompatibility())
        }
    }

    /**
     * Compiles given Java source files to destination directory.
     *
     * @param ant Ant builder
     * @param srcDirs Source directories
     * @param destDir Destination directory
     * @param classpath Classpath
     */
    private void compileJava(AntBuilder ant, Set<File> srcDirs, File destDir, String classpath) {
        ant.javac(destdir: destDir.canonicalPath, source: getSourceCompatibility(), target: getTargetCompatibility(),
                  classpath: classpath) {
            srcDirs.each { srcDir ->
                src(path: srcDir)
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09109283523836074, 'fraction_numerical': 0.0016030108725954837, 'mean_word_length': 2.1837142223208343, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '6616678', 'n_tokens_mistral': 3888, 'n_tokens_neox': 3663, 'n_words': 906}