package com.github.blindpirate.gogradle.core.dependency.produce;

import com.github.blindpirate.gogradle.core.dependency.GolangDependencySet;
import com.github.blindpirate.gogradle.core.dependency.parse.MapNotationParser;
import com.github.blindpirate.gogradle.util.Assert;
import com.github.blindpirate.gogradle.util.DependencySetUtils;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public abstract class ExternalDependencyFactory {
    protected final MapNotationParser mapNotationParser;

    public ExternalDependencyFactory(MapNotationParser mapNotationParser) {
        this.mapNotationParser = mapNotationParser;
    }

    /**
     * Relative paths of identity files.
     * For example, "Godeps/Godeps.json","glide.yaml"
     *
     * @return
     */
    protected abstract String identityFileName();

    public Optional<GolangDependencySet> produce(File rootDir) {
        File identityFile = identityFile(rootDir);
        if (identityFile.exists()) {
            List<Map<String, Object>> mapNotations = adapt(identityFile);
            return Optional.of(DependencySetUtils.parseMany(mapNotations, mapNotationParser));
        } else {
            return Optional.empty();
        }
    }

    protected abstract List<Map<String, Object>> adapt(File file);

    private File identityFile(File rootDir) {
        String identityFile = identityFileName();
        Assert.isNotBlank(identityFile, "Identity file must not be empty!");
        return rootDir.toPath().resolve(identityFile).toFile();
    }
}

@@ -59,16 +59,76 @@
 oduce;%0A%0A
+import com.github.blindpirate.gogradle.build.Configuration;%0A
 import c
@@ -390,16 +390,63 @@
 etUtils;
+%0Aimport com.google.common.collect.ImmutableMap;
 %0A%0Aimport
@@ -460,16 +460,46 @@
 o.File;%0A
+import java.util.Collections;%0A
 import j
@@ -566,58 +566,377 @@
 al;%0A
-%0Apublic abstract class ExternalDependencyFactory %7B
+import java.util.function.Function;%0A%0Aimport static com.github.blindpirate.gogradle.build.Configuration.BUILD;%0Aimport static com.github.blindpirate.gogradle.build.Configuration.TEST;%0A%0Apublic abstract class ExternalDependencyFactory %7B%0A    private Map%3CConfiguration, Function%3CFile, List%3E%3E adapters =%0A            ImmutableMap.of(BUILD, this::adapt, TEST, this::adaptTest);%0A
 %0A   
@@ -1158,16 +1158,20 @@
 aths of 
+the 
 identity
@@ -1175,17 +1175,16 @@
 ity file
-s
 .%0A     *
@@ -1308,24 +1308,59 @@
 ileName();%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     public O
@@ -1400,32 +1400,61 @@
 uce(File rootDir
+, Configuration configuration
 ) %7B%0A        File
@@ -1525,24 +1525,96 @@
 exists()) %7B%0A
+            Function%3CFile, List%3E adapter = adapters.get(configuration);%0A
             
@@ -1659,16 +1659,24 @@
  = adapt
+er.apply
 (identit
@@ -1845,24 +1845,571 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * In most cases, this method won't be used because all we need is build dependencies of%0A     * external package. However, when gogradle is building a golang project which was originally%0A     * managed by an external management tool, this method will be used to analyze test dependencies of the project.%0A     *%0A     * @param identityFile the identity file%0A     * @return test dependency of this identity file%0A     */%0A    protected List%3CMap%3CString, Object%3E%3E adaptTest(File identityFile) %7B%0A        return Collections.emptyList();%0A    %7D%0A%0A
     protecte

Add support for test dependencies