<commit_before>package com.uber.okbuck.core.model.base

import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.api.BaseVariantOutput
import com.uber.okbuck.OkBuckGradlePlugin
import com.uber.okbuck.core.dependency.DependencyCache
import com.uber.okbuck.core.dependency.ExternalDependency
import com.uber.okbuck.core.model.android.AndroidLibTarget
import com.uber.okbuck.core.model.groovy.GroovyLibTarget
import com.uber.okbuck.core.model.java.JavaLibTarget
import com.uber.okbuck.core.model.jvm.JvmTarget
import com.uber.okbuck.core.util.FileUtil
import com.uber.okbuck.core.util.ProjectUtil
import com.uber.okbuck.extension.OkBuckExtension
import groovy.transform.EqualsAndHashCode
import org.apache.commons.io.FilenameUtils
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.artifacts.UnknownConfigurationException

@EqualsAndHashCode
class Scope {

    final String resourcesDir
    final Set<String> sources
    final Set<Target> targetDeps = [] as Set
    List<String> jvmArgs
    DependencyCache depCache

    protected final Project project
    protected final Set<ExternalDependency> external = [] as Set

    Scope(Project project,
          Collection<String> configurations,
          Set<File> sourceDirs = [],
          File resDir = null,
          List<String> jvmArguments = [],
          DependencyCache depCache = OkBuckGradlePlugin.depCache) {

        this.project = project
        sources = FileUtil.getAvailable(project, sourceDirs)
        resourcesDir = FileUtil.getAvailableFile(project, resDir)
        jvmArgs = jvmArguments
        this.depCache = depCache

        extractConfigurations(configurations)
    }

    Set<String> getExternalDeps() {
        external.collect { ExternalDependency dependency ->
            depCache.get(dependency)
        }
    }

    Set<String> getPackagedLintJars() {
        external.findAll { ExternalDependency dependency ->
            depCache.getLintJar(dependency) != null
        }.collect { ExternalDependency dependency ->
            depCache.getLintJar(dependency)
        }
    }

    private void extractConfigurations(Collection<String> configurations) {
        List<Configuration> validConfigurations = []
        configurations.each { String configName ->
            try {
                Configuration configuration = project.configurations.getByName(configName)
                validConfigurations.add(configuration)
                Set<File> resolvedFiles = [] as Set
                configuration.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact artifact ->

                    String identifier = artifact.id.componentIdentifier.displayName
                    File dep = artifact.file

                    resolvedFiles.add(dep)

                    if (identifier.contains(" ")) {
                        Target target = getTargetForOutput(project.rootProject, dep)
                        if (target != null && target.project != project) {
                            targetDeps.add(target)
                        }
                    } else {
                        external.add(new ExternalDependency(artifact.moduleVersion.id, dep))
                    }
                }

                configuration.files.findAll { File resolved ->
                    !resolvedFiles.contains(resolved)
                }.each { File localDep ->
                    external.add(ExternalDependency.fromLocal(localDep))
                }
            } catch (UnknownConfigurationException ignored) {
            }
        }
    }

    @SuppressWarnings("GrReassignedInClosureLocalVar")
    static Target getTargetForOutput(Project rootProject, File output) {
        Target result = null
        OkBuckExtension okbuck = rootProject.okbuck
        Project project = okbuck.buckProjects.find { Project project ->
            FilenameUtils.directoryContains(project.buildDir.absolutePath, output.absolutePath)
        }

        if (project != null) {
            ProjectType type = ProjectUtil.getType(project)
            switch (type) {
                case ProjectType.ANDROID_LIB:
                    def baseVariants = project.android.libraryVariants
                    baseVariants.all { BaseVariant baseVariant ->
                        def variant = baseVariant.outputs.find { BaseVariantOutput out ->
                            (out.outputFile == output)
                        }
                        if (variant != null) {
                            result = new AndroidLibTarget(project, variant.name)
                        }
                    }
                    break
                case ProjectType.GROOVY_LIB:
                    result = new GroovyLibTarget(project, JvmTarget.MAIN)
                    break
                case ProjectType.JAVA_APP:
                case ProjectType.JAVA_LIB:
                    result = new JavaLibTarget(project, JvmTarget.MAIN)
                    break
                default:
                    result = null
            }
        }
        return result
    }
}<commit_msg>Cleanup scope resolution for better performance (#281)<commit_after>package com.uber.okbuck.core.model.base

import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.api.BaseVariantOutput
import com.uber.okbuck.OkBuckGradlePlugin
import com.uber.okbuck.core.dependency.DependencyCache
import com.uber.okbuck.core.dependency.ExternalDependency
import com.uber.okbuck.core.model.android.AndroidLibTarget
import com.uber.okbuck.core.model.groovy.GroovyLibTarget
import com.uber.okbuck.core.model.java.JavaLibTarget
import com.uber.okbuck.core.model.jvm.JvmTarget
import com.uber.okbuck.core.util.FileUtil
import com.uber.okbuck.core.util.ProjectUtil
import groovy.transform.EqualsAndHashCode
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.artifacts.UnknownConfigurationException

@EqualsAndHashCode
class Scope {

    final String resourcesDir
    final Set<String> sources
    final Set<Target> targetDeps = [] as Set
    List<String> jvmArgs
    DependencyCache depCache

    protected final Project project
    protected final Set<ExternalDependency> external = [] as Set

    Scope(Project project,
          Collection<String> configurations,
          Set<File> sourceDirs = [],
          File resDir = null,
          List<String> jvmArguments = [],
          DependencyCache depCache = OkBuckGradlePlugin.depCache) {

        this.project = project
        sources = FileUtil.getAvailable(project, sourceDirs)
        resourcesDir = FileUtil.getAvailableFile(project, resDir)
        jvmArgs = jvmArguments
        this.depCache = depCache

        extractConfigurations(configurations)
    }

    Set<String> getExternalDeps() {
        external.collect { ExternalDependency dependency ->
            depCache.get(dependency)
        }
    }

    Set<String> getPackagedLintJars() {
        external.findAll { ExternalDependency dependency ->
            depCache.getLintJar(dependency) != null
        }.collect { ExternalDependency dependency ->
            depCache.getLintJar(dependency)
        }
    }

    private void extractConfigurations(Collection<String> configurations) {
        List<Configuration> validConfigurations = []
        configurations.each { String configName ->
            try {
                Configuration configuration = project.configurations.getByName(configName)
                validConfigurations.add(configuration)
            } catch (UnknownConfigurationException ignored) {
            }
        }

        Set<ResolvedArtifact> artifacts = validConfigurations.collect {
            it.resolvedConfiguration.resolvedArtifacts
        }.flatten() as Set<ResolvedArtifact>

        Set<File> files = validConfigurations.collect {
            it.files
        }.flatten() as Set<File>

        Set<File> resolvedFiles = [] as Set
        artifacts.each { ResolvedArtifact artifact ->
            String identifier = artifact.id.componentIdentifier.displayName
            File dep = artifact.file

            resolvedFiles.add(dep)

            if (identifier.contains(" ")) {
                Project targetProject = project.project(identifier.replaceFirst("project ", ""))
                Target target = getTargetForOutput(targetProject, dep)
                if (target) {
                    targetDeps.add(target)
                }
            } else {
                external.add(new ExternalDependency(artifact.moduleVersion.id, dep))
            }
        }

        files.findAll { File resolved ->
            !resolvedFiles.contains(resolved)
        }.each { File localDep ->
            external.add(ExternalDependency.fromLocal(localDep))
        }

    }

    @SuppressWarnings("GrReassignedInClosureLocalVar")
    static Target getTargetForOutput(Project targetProject, File output) {
        Target result = null
        ProjectType type = ProjectUtil.getType(targetProject)
        switch (type) {
            case ProjectType.ANDROID_LIB:
                def baseVariants = targetProject.android.libraryVariants
                baseVariants.all { BaseVariant baseVariant ->
                    def variant = baseVariant.outputs.find { BaseVariantOutput out ->
                        (out.outputFile == output)
                    }
                    if (variant != null) {
                        result = new AndroidLibTarget(targetProject, variant.name)
                    }
                }
                break
            case ProjectType.GROOVY_LIB:
                result = new GroovyLibTarget(targetProject, JvmTarget.MAIN)
                break
            case ProjectType.JAVA_APP:
            case ProjectType.JAVA_LIB:
                result = new JavaLibTarget(targetProject, JvmTarget.MAIN)
                break
            default:
                result = null
        }
        return result
    }
}
{'fraction_non_alphanumeric': 0.07691544263596665, 'fraction_numerical': 0.00029773719730051605, 'mean_word_length': 2.1101851851851854, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 50, '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': '6203508', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2419, 'n_words': 477}