// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.testFramework.fixtures;

import com.intellij.jarRepository.JarRepositoryManager;
import com.intellij.jarRepository.RemoteRepositoryDescription;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.ui.OrderRoot;
import com.intellij.project.IntelliJProjectConfiguration;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties;

import java.util.Collection;
import java.util.List;

public class MavenDependencyUtil {
  /**
   * Adds a Maven library to given model.
   *
   * @param model            root model to add a Maven library to
   * @param mavenCoordinates maven coordinates like groupID:artifactID:version
   */
  public static void addFromMaven(@NotNull ModifiableRootModel model, String mavenCoordinates) {
    addFromMaven(model, mavenCoordinates, true);
  }

  /**
   * Adds a Maven library to given model.
   *
   * @param model                         root model to add a Maven library to
   * @param mavenCoordinates              maven coordinates like groupID:artifactID:version
   * @param includeTransitiveDependencies true for include transitive dependencies, false otherwise
   */
  public static void addFromMaven(@NotNull ModifiableRootModel model, String mavenCoordinates, boolean includeTransitiveDependencies) {
    List<RemoteRepositoryDescription> remoteRepositoryDescriptions = getRemoteRepositoryDescriptions();
    RepositoryLibraryProperties libraryProperties = new RepositoryLibraryProperties(mavenCoordinates, includeTransitiveDependencies);
    Collection<OrderRoot> roots = JarRepositoryManager
      .loadDependenciesModal(model.getProject(), libraryProperties, false, false, null, remoteRepositoryDescriptions);
    LibraryTable.ModifiableModel tableModel = model.getModuleLibraryTable().getModifiableModel();
    Library.ModifiableModel libraryModel = tableModel.createLibrary(mavenCoordinates).getModifiableModel();
    for (OrderRoot root : roots) {
      libraryModel.addRoot(root.getFile(), root.getType());
    }
    libraryModel.commit();
    tableModel.commit();
  }

  @NotNull
  private static List<RemoteRepositoryDescription> getRemoteRepositoryDescriptions() {
    return ContainerUtil.map(IntelliJProjectConfiguration.getRemoteRepositoryDescriptions(), repository ->
      new RemoteRepositoryDescription(repository.getId(), repository.getName(), repository.getUrl()));
  }
}

@@ -295,24 +295,128 @@
 escription;%0A
+import com.intellij.openapi.roots.DependencyScope;%0Aimport com.intellij.openapi.roots.LibraryOrderEntry;%0A
 import com.i
@@ -980,32 +980,112 @@
 y to given model
+ as %7B@link DependencyScope#COMPILE%7D dependency including transitive dependencies
 .%0A   *%0A   * @par
@@ -1423,16 +1423,62 @@
 en model
+ as %7B@link DependencyScope#COMPILE%7D dependency
 .%0A   *%0A 
@@ -1885,24 +1885,724 @@
 ndencies) %7B%0A
+    addFromMaven(model, mavenCoordinates, includeTransitiveDependencies, DependencyScope.COMPILE);%0A  %7D%0A%0A  /**%0A   * Adds a Maven library to given model.%0A   *%0A   * @param model                         root model to add a Maven library to%0A   * @param mavenCoordinates              maven coordinates like groupID:artifactID:version%0A   * @param includeTransitiveDependencies true for include transitive dependencies, false otherwise%0A   * @param dependencyScope               scope of the library%0A   */%0A  public static void addFromMaven(@NotNull ModifiableRootModel model, String mavenCoordinates,%0A                                  boolean includeTransitiveDependencies, DependencyScope dependencyScope) %7B%0A
     List%3CRem
@@ -2860,16 +2860,22 @@
  roots =
+%0A     
  JarRepo
@@ -2887,23 +2887,16 @@
 yManager
-%0A      
 .loadDep
@@ -3113,32 +3113,16 @@
 rary
-.ModifiableModel
  library
 Mode
@@ -3117,21 +3117,16 @@
  library
-Model
  = table
@@ -3166,131 +3166,580 @@
 tes)
-.getModifiableModel();%0A    for (OrderRoot root : roots) %7B%0A      libraryModel.addRoot(root.getFile(), root.getType());%0A    %7D
+;%0A    Library.ModifiableModel libraryModel = library.getModifiableModel();%0A    if (roots.isEmpty()) %7B%0A      throw new IllegalStateException(String.format(%22No roots for '%25s'%22, mavenCoordinates));%0A    %7D%0A%0A    for (OrderRoot root : roots) %7B%0A      libraryModel.addRoot(root.getFile(), root.getType());%0A    %7D%0A%0A    LibraryOrderEntry libraryOrderEntry = model.findLibraryOrderEntry(library);%0A    if (libraryOrderEntry == null) %7B%0A      throw new IllegalStateException(%22Unable to find registered library %22 + mavenCoordinates);%0A    %7D%0A    libraryOrderEntry.setScope(dependencyScope);%0A
 %0A   

use MavenDependencyUtil in tests