1
+ package org.utbot.intellij.plugin.util
2
+
3
+ import com.intellij.codeInsight.daemon.impl.quickfix.LocateLibraryDialog
4
+ import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix
5
+ import com.intellij.jarRepository.JarRepositoryManager
6
+ import com.intellij.openapi.application.WriteAction
7
+ import com.intellij.openapi.module.Module
8
+ import com.intellij.openapi.project.Project
9
+ import com.intellij.openapi.roots.DependencyScope
10
+ import com.intellij.openapi.roots.ExternalLibraryDescriptor
11
+ import com.intellij.openapi.roots.ModuleRootModificationUtil
12
+ import com.intellij.openapi.roots.OrderRootType
13
+ import com.intellij.openapi.roots.impl.IdeaProjectModelModifier
14
+ import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar
15
+ import com.intellij.openapi.roots.libraries.LibraryUtil
16
+ import com.intellij.util.PathUtil
17
+ import com.intellij.util.containers.ContainerUtil
18
+ import org.jetbrains.concurrency.Promise
19
+ import org.jetbrains.concurrency.resolvedPromise
20
+ import org.jetbrains.idea.maven.utils.library.RepositoryLibraryDescription
21
+ import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties
22
+ import org.jetbrains.jps.model.library.JpsMavenRepositoryLibraryDescriptor
23
+
24
+ class UtProjectModelModifier (val project : Project ) : IdeaProjectModelModifier(project) {
25
+ override fun addExternalLibraryDependency (
26
+ modules : Collection <Module >,
27
+ descriptor : ExternalLibraryDescriptor ,
28
+ scope : DependencyScope
29
+ ): Promise <Void >? {
30
+ val defaultRoots = descriptor.libraryClassesRoots
31
+ val firstModule = ContainerUtil .getFirstItem(modules) ? : return null
32
+ val classesRoots = if (defaultRoots.isNotEmpty()) {
33
+ LocateLibraryDialog (
34
+ firstModule,
35
+ defaultRoots,
36
+ descriptor.presentableName
37
+ ).showAndGetResult()
38
+ } else {
39
+ val roots = JarRepositoryManager .loadDependenciesModal(
40
+ project,
41
+ RepositoryLibraryProperties (JpsMavenRepositoryLibraryDescriptor (descriptor.mavenCoordinates())),
42
+ /* loadSources = */ false ,
43
+ /* loadJavadoc = */ false ,
44
+ /* copyTo = */ null ,
45
+ /* repositories = */ null
46
+ )
47
+ if (roots.isEmpty()) {
48
+ return null
49
+ }
50
+ roots.filter { orderRoot -> orderRoot.type == = OrderRootType .CLASSES }
51
+ .map { PathUtil .getLocalPath(it.file) }.toList()
52
+ }
53
+ if (classesRoots.isNotEmpty()) {
54
+ val urls = OrderEntryFix .refreshAndConvertToUrls(classesRoots)
55
+ if (modules.size == 1 ) {
56
+ ModuleRootModificationUtil .addModuleLibrary(
57
+ firstModule,
58
+ if (classesRoots.size > 1 ) descriptor.presentableName else null ,
59
+ urls,
60
+ emptyList(),
61
+ scope
62
+ )
63
+ } else {
64
+ WriteAction .run<RuntimeException > {
65
+ LibraryUtil .createLibrary(
66
+ LibraryTablesRegistrar .getInstance().getLibraryTable(project),
67
+ descriptor.presentableName
68
+ ).let {
69
+ val model = it.modifiableModel
70
+ urls.forEach { url -> model.addRoot(url, OrderRootType .CLASSES ) }
71
+ model.commit()
72
+ modules.forEach { module ->
73
+ ModuleRootModificationUtil .addDependency(module, it, scope, false )
74
+ }
75
+ }
76
+ }
77
+ }
78
+ }
79
+ return resolvedPromise()
80
+ }
81
+
82
+ private fun ExternalLibraryDescriptor.mavenCoordinates (): String {
83
+ return " $libraryGroupId :$libraryArtifactId :${preferredVersion ? : RepositoryLibraryDescription .ReleaseVersionId } "
84
+ }
85
+ }
0 commit comments