package org.webjars;

import java.io.IOException;
import java.util.Collection;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.io.ModelWriter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

/**
 * Adds the specified WebJar as a dependency
 */
@Mojo(name="install", requiresProject=true, requiresDirectInvocation=true)
public class InstallMojo extends AbstractMojo {

  /**
   * The name of the WebJar, optionally suffixed with a version, eg. jquery:2.0.1
   *
   * If the version is omitted, the latest version is taken.
   */
  @Parameter(property="webjar", required=true)
  private String webjar;

  @Component
  private MavenProject project;

  @Component
  private ModelWriter modelWriter;

  public void execute() throws MojoExecutionException, MojoFailureException {
    String[] split = webjar.split(":");
    String artifact = split[0];
    ArtifactVersion requestedVersion = split.length >= 2 ? new DefaultArtifactVersion(split[1]) : null;

    Collection<ArtifactVersion> versions = MavenCentral.getArtifacts(artifact, requestedVersion, getLog()).get(artifact);

    ArtifactVersion resolvedVersion = null;

    if (requestedVersion == null) {
      resolvedVersion = versions.iterator().next();
    } else {
      for (ArtifactVersion artifactVersion : versions) {
        if (artifactVersion.equals(requestedVersion)) {
          resolvedVersion = artifactVersion;
          break;
        }
      }
    }

    Dependency dependency = new Dependency();
    dependency.setGroupId("org.webjars");
    dependency.setArtifactId(artifact);
    dependency.setVersion(resolvedVersion.toString());

    project.getOriginalModel().addDependency(dependency);
    try {
      modelWriter.write(project.getFile(), null, project.getOriginalModel());
    } catch (IOException e) {
      throw new MojoExecutionException("Could not add dependency", e);
    }

    getLog().info("Added dependency: " + artifact + ":" + resolvedVersion);
  }

}

@@ -71,16 +71,43 @@
 lection;
+%0Aimport java.util.Iterator;
 %0A%0Aimport
@@ -2086,24 +2086,764 @@
 String());%0A%0A
+    Dependency updatedDependency = null;%0A%0A    Iterator%3CDependency%3E existingDependencies = project.getOriginalModel().getDependencies().iterator();%0A    while (existingDependencies.hasNext()) %7B%0A      Dependency existingDependency = existingDependencies.next();%0A      if (existingDependency.getGroupId().equals(dependency.getGroupId()) && existingDependency.getArtifactId().equals(dependency.getArtifactId())) %7B%0A        if (existingDependency.getVersion().equals(dependency.getVersion())) %7B%0A          getLog().warn(artifact + %22:%22 + dependency.getVersion() + %22 is already a dependency. Aborting.%22);%0A          return;%0A        %7D%0A        updatedDependency = existingDependency;%0A        existingDependencies.remove();%0A        break;%0A      %7D%0A    %7D%0A%0A
     project.
@@ -3080,24 +3080,191 @@
  e);%0A    %7D%0A%0A
+    if (updatedDependency != null) %7B%0A      getLog().info(%22Updated %22 + artifact + %22 from %22 + updatedDependency.getVersion() + %22 to %22 + resolvedVersion);%0A    %7D else %7B%0A  
     getLog()
@@ -3327,15 +3327,21 @@
 rsion);%0A
+    %7D%0A
   %7D%0A%0A%7D%0A

Update dependency if different version of existing dependency is installed