<commit_before>package com.github.blindpirate.gogradle.core.dependency.resolve;

import com.github.blindpirate.gogradle.core.cache.GlobalCacheManager;
import com.github.blindpirate.gogradle.core.dependency.NotationDependency;
import com.github.blindpirate.gogradle.core.dependency.ResolvedDependency;
import com.github.blindpirate.gogradle.core.exceptions.DependencyInstallationException;
import com.github.blindpirate.gogradle.core.exceptions.DependencyResolutionException;
import com.github.blindpirate.gogradle.util.IOUtils;

import javax.inject.Inject;
import java.io.File;
import java.nio.file.Path;
import java.util.Optional;

public abstract class AbstractVcsResolver<REPOSITORY, VERSION> implements DependencyResolver {

    @Inject
    private GlobalCacheManager globalCacheManager;

    @Override
    public ResolvedDependency resolve(final NotationDependency dependency) {
        try {
            return globalCacheManager.runWithGlobalCacheLock(dependency, () -> {
                Path path = globalCacheManager.getGlobalCachePath(dependency.getName());
                return doResolve(dependency, path.toFile());
            });
        } catch (Exception e) {
            throw DependencyResolutionException.cannotResolveDependency(dependency, e);
        }
    }

    @Override
    public void reset(ResolvedDependency dependency, File targetDirectory) {
        try {
            globalCacheManager.runWithGlobalCacheLock(dependency, () -> {
                Path globalCachePath = globalCacheManager.getGlobalCachePath(dependency.getName());
                doReset(dependency, globalCachePath);
                IOUtils.copyDirectory(globalCachePath.toFile(), targetDirectory);
                return null;
            });
        } catch (Exception e) {
            throw DependencyInstallationException.cannotResetResolvedDependency(dependency, e);
        }
    }

    protected abstract void doReset(ResolvedDependency dependency, Path globalCachePath);

    private ResolvedDependency doResolve(NotationDependency dependency, File targetDirectory) {<commit_msg>let VcsResolver be able to resolve VendorDependency<commit_after>package com.github.blindpirate.gogradle.core.dependency.resolve;

import com.github.blindpirate.gogradle.core.cache.GlobalCacheManager;
import com.github.blindpirate.gogradle.core.dependency.NotationDependency;
import com.github.blindpirate.gogradle.core.dependency.ResolvedDependency;
import com.github.blindpirate.gogradle.core.dependency.VendorResolvedDependency;
import com.github.blindpirate.gogradle.core.dependency.install.DependencyInstaller;
import com.github.blindpirate.gogradle.core.exceptions.DependencyInstallationException;
import com.github.blindpirate.gogradle.core.exceptions.DependencyResolutionException;
import com.github.blindpirate.gogradle.util.Cast;
import com.github.blindpirate.gogradle.util.IOUtils;

import javax.inject.Inject;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

public abstract class AbstractVcsResolver<REPOSITORY, VERSION> implements DependencyResolver, DependencyInstaller {

    @Inject
    private GlobalCacheManager globalCacheManager;

    @Override
    public ResolvedDependency resolve(final NotationDependency dependency) {
        try {
            return globalCacheManager.runWithGlobalCacheLock(dependency, () -> {
                Path path = globalCacheManager.getGlobalCachePath(dependency.getName());
                return doResolve(dependency, path.toFile());
            });
        } catch (Exception e) {
            throw DependencyResolutionException.cannotResolveDependency(dependency, e);
        }
    }

    @Override
    public void install(ResolvedDependency dependency, File targetDirectory) {
        try {
            globalCacheManager.runWithGlobalCacheLock(dependency, () -> {
                installUnderLock(dependency, targetDirectory);
                return null;
            });
        } catch (Exception e) {
            throw DependencyInstallationException.cannotResetResolvedDependency(dependency, e);
        }
    }


    private void installUnderLock(ResolvedDependency dependency, File targetDirectory) {
        ResolvedDependency realDependency = determineDependency(dependency);
        Path globalCachePath = globalCacheManager.getGlobalCachePath(realDependency.getName());
        doReset(realDependency, globalCachePath);

        Path srcPath = globalCachePath.resolve(determineRelativePath(dependency));
        IOUtils.copyDirectory(srcPath.toFile(), targetDirectory);
    }

    private ResolvedDependency determineDependency(ResolvedDependency dependency) {
        if (dependency instanceof VendorResolvedDependency) {
            return Cast.cast(VendorResolvedDependency.class, dependency).getHostDependency();
        } else {
            return dependency;
        }
    }

    private Path determineRelativePath(ResolvedDependency dependency) {
        if (dependency instanceof VendorResolvedDependency) {
            return Cast.cast(VendorResolvedDependency.class, dependency).getRelativePathToHost();
        } else {
            return Paths.get(".");
        }
    }


    protected abstract void doReset(ResolvedDependency dependency, Path globalCachePath);

    private ResolvedDependency doResolve(NotationDependency dependency, File targetDirectory) {
{'fraction_non_alphanumeric': 0.07995537374488658, 'fraction_numerical': 0.0, 'mean_word_length': 3.8026785714285714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '2282222', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1346, 'n_words': 239}