<commit_before>package com.github.jengelman.gradle.plugins.shadow.tasks

import com.github.jengelman.gradle.plugins.shadow.ShadowStats
import com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator
import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer
import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import org.apache.commons.io.FilenameUtils
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree
import org.gradle.api.file.FileTreeElement
import org.gradle.api.internal.DocumentationRegistry
import org.gradle.api.internal.file.copy.CopyAction
import org.gradle.api.specs.Spec
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.util.PatternSet
import org.gradle.util.ConfigureUtil

class ShadowJar extends Jar {

    List<Transformer> transformers = []
    List<Relocator> relocators = []

    private final ShadowStats shadowStats = new ShadowStats()
    private final DependencyFilter dependencyFilter

    ShadowJar() {
        dependencyFilter = new DependencyFilter(project)
    }

    @Override
    protected CopyAction createCopyAction() {
        DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry)
        return new ShadowCopyAction(getArchivePath(), getCompressor(), documentationRegistry,
                transformers, relocators, (PatternSet) mainSpec.getPatternSet(),
                dependencyFilter.patternSet, shadowStats)
    }

    @TaskAction
    protected void copy() {
        super.copy()
        logger.info(shadowStats.toString())
    }

    @Override
    @InputFiles @SkipWhenEmpty @Optional
    // SHADOW-54 Need to remove filtered dependencies from inputs list
    public FileCollection getSource() {
        super.source - excludedDependencies
    }

    /**
     * Gets a list of dependency files that are being excluded
     * @return
     */
    protected FileCollection getExcludedDependencies() {
        def allDependencies = super.source.filter {
            def ext = FilenameUtils.getExtension(it.name)
            return ext == 'zip' || ext == 'jar'
        }.asFileTree
        def includedDependencies = allDependencies.matching(dependencyFilter.patternSet)
        return allDependencies - includedDependencies
    }

    /**
     * Configure inclusion/exclusion of module & project dependencies into uber jar
     * @param c
     * @return
     */
    ShadowJar dependencies(Closure c) {
        ConfigureUtil.configure(c, dependencyFilter)
        return this<commit_msg>close #66, intelligently getPatternSet() based on Gradle version<commit_after>package com.github.jengelman.gradle.plugins.shadow.tasks

import com.github.jengelman.gradle.plugins.shadow.ShadowStats
import com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator
import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer
import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import org.apache.commons.io.FilenameUtils
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree
import org.gradle.api.file.FileTreeElement
import org.gradle.api.internal.DocumentationRegistry
import org.gradle.api.internal.file.copy.CopyAction
import org.gradle.api.specs.Spec
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.util.PatternSet
import org.gradle.util.ConfigureUtil

class ShadowJar extends Jar {

    List<Transformer> transformers = []
    List<Relocator> relocators = []

    private final ShadowStats shadowStats = new ShadowStats()
    private final DependencyFilter dependencyFilter

    ShadowJar() {
        dependencyFilter = new DependencyFilter(project)
    }

    @Override
    protected CopyAction createCopyAction() {
        DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry)
        return new ShadowCopyAction(getArchivePath(), getCompressor(), documentationRegistry,
                transformers, relocators, rootPatternSet,
                dependencyFilter.patternSet, shadowStats)
    }

    @TaskAction
    protected void copy() {
        super.copy()
        logger.info(shadowStats.toString())
    }

    @Override
    @InputFiles @SkipWhenEmpty @Optional
    // SHADOW-54 Need to remove filtered dependencies from inputs list
    public FileCollection getSource() {
        super.source - excludedDependencies
    }

    protected PatternSet getRootPatternSet() {
        if (mainSpec.respondsTo('getPatternSet')) {
            return mainSpec.getPatternSet()
        } else {
            return mainSpec.buildRootResolver().getPatternSet()
        }
    }

    /**
     * Gets a list of dependency files that are being excluded
     * @return
     */
    protected FileCollection getExcludedDependencies() {
        def allDependencies = super.source.filter {
            def ext = FilenameUtils.getExtension(it.name)
            return ext == 'zip' || ext == 'jar'
        }.asFileTree
        def includedDependencies = allDependencies.matching(dependencyFilter.patternSet)
        return allDependencies - includedDependencies
    }

    /**
     * Configure inclusion/exclusion of module & project dependencies into uber jar
     * @param c
     * @return
     */
    ShadowJar dependencies(Closure c) {
        ConfigureUtil.configure(c, dependencyFilter)
        return this
{'fraction_non_alphanumeric': 0.08566853482786228, 'fraction_numerical': 0.0009607686148919135, 'mean_word_length': 3.6786516853932585, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5531121', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1635, 'n_words': 337}