package gw.vark.aether;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.resources.FileResource;
import org.sonatype.aether.ant.tasks.Install;
import org.sonatype.aether.ant.tasks.Resolve;
import org.sonatype.aether.ant.types.Artifact;
import org.sonatype.aether.ant.types.Dependencies;
import org.sonatype.aether.ant.types.Pom;

import java.io.File;
import java.util.Iterator;

/**
 */
@SuppressWarnings({"UnusedDeclaration"})
public class AetherUtil {

  private final Project _project;

  public AetherUtil(Project project) {
    _project = project;
  }

  public FileList resolve(Dependencies dependencies, String scopes) {
    Path path = resolveToPath(dependencies, scopes);
    FileList list = new FileList();
    for (Iterator it = path.iterator(); it.hasNext();) {
      FileResource file = (FileResource) it.next();
      list.add(file);
    }
    return list;
  }

  public void resolveToDir(Dependencies dependencies, String scopes, File dir, String layout) {
    Resolve resolveTask = initTask(new Resolve(), "resolve");
    resolveTask.addDependencies(dependencies);

    Resolve.Files files = resolveTask.createFiles();
    files.setProject(_project);
    files.setScopes(scopes);
    files.setDir(dir);
    files.setLayout(layout);

    resolveTask.execute();
  }

  public Path resolveToPath(Dependencies dependencies, String scopes) {
    Resolve resolveTask = initTask(new Resolve(), "resolve");
    resolveTask.addDependencies(dependencies);

    Resolve.Path path = resolveTask.createPath();
    path.setProject(_project);
    path.setRefId("tmp.path");
    path.setScopes(scopes);

    resolveTask.execute();
    return (Path) _project.getReference("tmp.path");
  }

  public void install(Pom pom) {
    install(pom, null);
  }

  public void install(Pom pom, Artifact artifact) {
    Install installTask = initTask(new Install(), "install");
    installTask.addPom(pom);
    if (artifact != null) {
      installTask.addArtifact(artifact);
    }
    installTask.execute();
  }

  private <T extends Task> T initTask(T task, String name) {
    task.setProject(_project);
    task.setTaskName(name);
    task.init();
    return task;
  }
}

@@ -18,16 +18,95 @@
 ether;%0A%0A
+import org.apache.maven.model.Model;%0Aimport org.apache.maven.model.Repository;%0A
 import o
@@ -266,16 +266,94 @@
 source;%0A
+import org.codehaus.plexus.interpolation.reflection.ReflectionValueExtractor;%0A
 import o
@@ -477,131 +477,59 @@
 pes.
-Artifact;%0Aimport org.sonatype.aether.ant.types.Dependencies;%0Aimport org.sonatype.aether.ant.types.Pom;%0A%0Aimport java.io.File
+*;%0A%0Aimport java.io.File;%0Aimport java.util.ArrayList
 ;%0Aim
@@ -663,16 +663,42 @@
 project;
+%0A  private final Pom _pom;
 %0A%0A  publ
@@ -726,24 +726,102 @@
  project) %7B%0A
+    this(project, null);%0A  %7D%0A%0A  public AetherUtil(Project project, Pom pom) %7B%0A
     _project
@@ -832,16 +832,32 @@
 roject;%0A
+    _pom = pom;%0A
   %7D%0A%0A  p
@@ -902,37 +902,56 @@
 pendencies, 
-String scopes
+MavenScopeCategory scopeCategory
 ) %7B%0A    Path
@@ -987,25 +987,32 @@
 ncies, scope
-s
+Category
 );%0A    FileL
@@ -1251,29 +1251,48 @@
 encies, 
-String scopes
+MavenScopeCategory scopeCategory
 , File d
@@ -1334,37 +1334,27 @@
 solveTask = 
-initTask(
 new
- 
 Resolve(), %22
@@ -1346,36 +1346,24 @@
 newResolve()
-, %22resolve%22)
 ;%0A    resolv
@@ -1510,17 +1510,38 @@
 es(scope
-s
+Category.getExpanded()
 );%0A    f
@@ -1679,21 +1679,40 @@
 es, 
-String scopes
+MavenScopeCategory scopeCategory
 ) %7B%0A
@@ -1737,29 +1737,19 @@
 eTask = 
-initTask(
 new
- 
 Resolve(
@@ -1753,20 +1753,8 @@
 ve()
-, %22resolve%22)
 ;%0A  
@@ -1935,17 +1935,38 @@
 es(scope
-s
+Category.getExpanded()
 );%0A%0A    
@@ -2358,16 +2358,802 @@
 );%0A  %7D%0A%0A
+  private Resolve newResolve() %7B%0A    Resolve resolveTask = initTask(new Resolve(), %22resolve%22);%0A    if (_pom != null) %7B%0A      Model model = _pom.getModel(_pom);%0A      try %7B%0A        Object o = ReflectionValueExtractor.evaluate(%22project.repositories%22, model);%0A        if (o != null) %7B%0A          ArrayList pomRepos = (ArrayList) o;%0A          for (Object pomRepoObj : pomRepos) %7B%0A            Repository pomRepo = (Repository) pomRepoObj;%0A            RemoteRepository remoteRepo = new RemoteRepository();%0A            remoteRepo.setId(pomRepo.getId());%0A            remoteRepo.setUrl(pomRepo.getUrl());%0A            resolveTask.addRemoteRepo(remoteRepo);%0A          %7D%0A        %7D%0A      %7D%0A      catch (Exception e) %7B%0A        throw new RuntimeException(e);%0A      %7D%0A    %7D%0A    return resolveTask;%0A  %7D%0A%0A
   privat

add remote repositories as configured in the pom take MavenScopeCategory as params rather than strings