package com.github.ferstl.depgraph;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
import org.apache.maven.shared.dependency.graph.DependencyNode;

@Mojo(
    name = "graph",
    aggregator = true,
    defaultPhase = LifecyclePhase.NONE,
    inheritByDefault = false,
    requiresDependencyCollection = ResolutionScope.TEST,
    requiresDirectInvocation = true,
    threadSafe = true)
public class DepGraphMojo extends AbstractMojo {

  @Parameter(
      alias = "gropIdClusters",
      property = "groupIdClusters",
      readonly = true,
      required = false)
  private List<String> groupIdClusters;

  @Component
  private MavenProject project;

  @Component( hint = "default" )
  private DependencyGraphBuilder dependencyGraphBuilder;

  @Override
  public void execute() throws MojoExecutionException {
    try {
      GraphBuilder graphBuilder = new GraphBuilder();
      String artifactId = this.project.getArtifactId();

      @SuppressWarnings("unchecked")
      List<MavenProject> collectedProjects = this.project.getCollectedProjects();
      for (MavenProject collectedProject : collectedProjects) {
        Edge moduleEdge = new Edge(artifactId, collectedProject.getArtifactId());
        graphBuilder.addEdges(moduleEdge);

        DependencyNode root = this.dependencyGraphBuilder.buildDependencyGraph(collectedProject, null);

        EdgeBuildingVisitor visitor = new EdgeBuildingVisitor(ArtifactIdRenderer.INSTANCE);
        root.accept(visitor);

        graphBuilder.addEdges(visitor.getEdges());
      }

      System.err.println(graphBuilder);

    } catch (DependencyGraphBuilderException e) {
      throw new MojoExecutionException("boom");
    }
  }
}

@@ -652,16 +652,45 @@
 e.%0A */%0A%0A
+import java.util.Collection;%0A
 import j
@@ -2079,64 +2079,8 @@
 r();
-%0A      String artifactId = this.project.getArtifactId();
 %0A%0A  
@@ -2202,16 +2202,73 @@
 ;%0A      
+buildModuleTree(collectedProjects, graphBuilder);%0A%0A      
 for (Mav
@@ -2320,134 +2320,8 @@
 s) %7B
-%0A        Edge moduleEdge = new Edge(artifactId, collectedProject.getArtifactId());%0A        graphBuilder.addEdges(moduleEdge);%0A
 %0A   
@@ -2753,11 +2753,600 @@
    %7D%0A  %7D
+%0A%0A  public void buildModuleTree(Collection%3CMavenProject%3E collectedProjects, GraphBuilder graphBuilder) %7B%0A    System.err.println(%22Project: %22 + this.project + %22, Parent: %22 + this.project.getParent());%0A    for (MavenProject collectedProject : collectedProjects) %7B%0A      MavenProject child = collectedProject;%0A      MavenProject parent = collectedProject.getParent();%0A%0A      while (parent != null) %7B%0A        Edge edge = new Edge(parent.getArtifactId(), child.getArtifactId());%0A        graphBuilder.addEdges(edge);%0A%0A        child = parent;%0A        parent = parent.getParent();%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

Include module dependencies