<commit_before>package com.uber.okbuck.core.dependency

import com.google.common.collect.ImmutableSet
import com.uber.okbuck.OkBuckGradlePlugin
import com.uber.okbuck.core.model.base.Scope
import com.uber.okbuck.core.model.base.Store
import com.uber.okbuck.core.util.FileUtil
import org.apache.commons.io.IOUtils
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedArtifactResult
import org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.util.concurrent.ConcurrentHashMap
import java.util.jar.JarEntry
import java.util.jar.JarFile

class DependencyCache {

    private static final Logger LOG = LoggerFactory.getLogger(DependencyCache)

    private final File cacheDir
    private final Project rootProject
    private final boolean fetchSources
    private final Store lintJars
    private final Store processors
    private final Store processorExtensions
    private final Store sources

    private final Set<File> copies = ConcurrentHashMap.newKeySet()
    private final Set<ExternalDependency> requested = ConcurrentHashMap.newKeySet()
    private final Map<File, File> links = new ConcurrentHashMap<>()
    private final Map<ExternalDependency.VersionlessDependency, ExternalDependency> forcedDeps = new HashMap<>()

    DependencyCache(Project project, File cacheDir, String forcedConfiguration = null) {
        this.rootProject = project.rootProject
        this.cacheDir = cacheDir
        this.fetchSources = rootProject.okbuck.intellij.sources

        sources = new Store(rootProject.file("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/SOURCES"))
        processors = new Store(rootProject.file("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/PROCESSORS"))
        processorExtensions = new Store(rootProject.file(
                "${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/PROCESSORS_EXTENSIONS"))
        lintJars = new Store(rootProject.file("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/LINT_JARS"))

        if (forcedConfiguration) {
            Scope.from(project, forcedConfiguration).external.each {
                get(it)
                forcedDeps.put(it.versionless, it)
            }
        }
    }

    void finalizeDeps() {
        LOG.info("Finalizing Dependency Cache")
        sources.persist()
        processors.persist()
        lintJars.persist()
        cleanup()
    }

    void cleanup() {
        String changingDeps = requested.findAll { ExternalDependency dependency ->
            String version = dependency.version.toString()
            return version.endsWith("+") || version.endsWith("-SNAPSHOT")
        }.collect { ExternalDependency dependency ->
            dependency.cacheName<commit_msg>Fix fetchSources for IntelliJ project. (#637)<commit_after>package com.uber.okbuck.core.dependency

import com.google.common.collect.ImmutableSet
import com.uber.okbuck.OkBuckGradlePlugin
import com.uber.okbuck.core.model.base.Scope
import com.uber.okbuck.core.model.base.Store
import com.uber.okbuck.core.util.FileUtil
import com.uber.okbuck.core.util.ProjectUtil
import org.apache.commons.io.IOUtils
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedArtifactResult
import org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.util.concurrent.ConcurrentHashMap
import java.util.jar.JarEntry
import java.util.jar.JarFile

class DependencyCache {

    private static final Logger LOG = LoggerFactory.getLogger(DependencyCache)

    private final File cacheDir
    private final Project rootProject
    private final boolean fetchSources
    private final Store lintJars
    private final Store processors
    private final Store processorExtensions
    private final Store sources

    private final Set<File> copies = ConcurrentHashMap.newKeySet()
    private final Set<ExternalDependency> requested = ConcurrentHashMap.newKeySet()
    private final Map<File, File> links = new ConcurrentHashMap<>()
    private final Map<ExternalDependency.VersionlessDependency, ExternalDependency> forcedDeps = new HashMap<>()

    DependencyCache(Project project, File cacheDir, String forcedConfiguration = null) {
        this.rootProject = project.rootProject
        this.cacheDir = cacheDir
        this.fetchSources = ProjectUtil.getOkBuckExtension(project).getIntellijExtension().sources

        sources = new Store(rootProject.file("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/SOURCES"))
        processors = new Store(rootProject.file("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/PROCESSORS"))
        processorExtensions = new Store(rootProject.file(
                "${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/PROCESSORS_EXTENSIONS"))
        lintJars = new Store(rootProject.file("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/LINT_JARS"))

        if (forcedConfiguration) {
            Scope.from(project, forcedConfiguration).external.each {
                get(it)
                forcedDeps.put(it.versionless, it)
            }
        }
    }

    void finalizeDeps() {
        LOG.info("Finalizing Dependency Cache")
        sources.persist()
        processors.persist()
        lintJars.persist()
        cleanup()
    }

    void cleanup() {
        String changingDeps = requested.findAll { ExternalDependency dependency ->
            String version = dependency.version.toString()
            return version.endsWith("+") || version.endsWith("-SNAPSHOT")
        }.collect { ExternalDependency dependency ->
            dependency.cacheName
{'fraction_non_alphanumeric': 0.08708475628686743, 'fraction_numerical': 0.0010866190624029804, 'mean_word_length': 3.8958966565349544, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '6037704', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1739, 'n_words': 344}