@@ -2,7 +2,6 @@ package org.utbot.intellij.plugin.util
2
2
3
3
import com.intellij.codeInsight.daemon.impl.quickfix.LocateLibraryDialog
4
4
import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix
5
- import com.intellij.ide.JavaUiBundle
6
5
import com.intellij.jarRepository.JarRepositoryManager
7
6
import com.intellij.openapi.application.WriteAction
8
7
import com.intellij.openapi.module.Module
@@ -14,13 +13,9 @@ import com.intellij.openapi.roots.OrderRootType
14
13
import com.intellij.openapi.roots.impl.IdeaProjectModelModifier
15
14
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar
16
15
import com.intellij.openapi.roots.libraries.LibraryUtil
17
- import com.intellij.openapi.roots.libraries.ui.OrderRoot
18
- import com.intellij.openapi.ui.Messages
19
16
import com.intellij.util.PathUtil
20
17
import com.intellij.util.containers.ContainerUtil
21
- import java.util.stream.Collectors
22
18
import org.jetbrains.concurrency.Promise
23
- import org.jetbrains.concurrency.rejectedPromise
24
19
import org.jetbrains.concurrency.resolvedPromise
25
20
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryDescription
26
21
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties
@@ -31,69 +26,60 @@ class UtProjectModelModifier(val project: Project) : IdeaProjectModelModifier(pr
31
26
modules : Collection <Module >,
32
27
descriptor : ExternalLibraryDescriptor ,
33
28
scope : DependencyScope
34
- ): Promise <Void > {
29
+ ): Promise <Void >? {
35
30
val defaultRoots = descriptor.libraryClassesRoots
36
- val firstModule = ContainerUtil .getFirstItem(modules) ? : return rejectedPromise()
31
+ val firstModule = ContainerUtil .getFirstItem(modules) ? : return null
37
32
val classesRoots = if (defaultRoots.isNotEmpty()) {
38
33
LocateLibraryDialog (
39
34
firstModule,
40
35
defaultRoots,
41
36
descriptor.presentableName
42
37
).showAndGetResult()
43
38
} else {
44
- val libraryProperties = RepositoryLibraryProperties (JpsMavenRepositoryLibraryDescriptor (descriptor.mavenCoordinates()))
45
39
val roots = JarRepositoryManager .loadDependenciesModal(
46
40
project,
47
- libraryProperties ,
41
+ RepositoryLibraryProperties ( JpsMavenRepositoryLibraryDescriptor (descriptor.mavenCoordinates())) ,
48
42
/* loadSources = */ false ,
49
43
/* loadJavadoc = */ false ,
50
44
/* copyTo = */ null ,
51
45
/* repositories = */ null
52
46
)
53
47
if (roots.isEmpty()) {
54
- @Suppress(" SpellCheckingInspection" )
55
- Messages .showErrorDialog(
56
- project,
57
- JavaUiBundle .message(" dialog.mesage.0.was.not.loaded" , descriptor.presentableName),
58
- JavaUiBundle .message(" dialog.title.failed.to.download.library" )
59
- )
60
- return rejectedPromise()
48
+ return null
61
49
}
62
- roots.stream()
63
- .filter { root: OrderRoot -> root.type == = OrderRootType .CLASSES }
64
- .map { root: OrderRoot ->
65
- PathUtil .getLocalPath(
66
- root.file
67
- )
68
- }
69
- .collect(Collectors .toList())
50
+ roots.filter { orderRoot -> orderRoot.type == = OrderRootType .CLASSES }
51
+ .map { PathUtil .getLocalPath(it.file) }.toList()
70
52
}
71
53
if (classesRoots.isNotEmpty()) {
72
- val libraryName = if (classesRoots.size > 1 ) descriptor.presentableName else null
73
54
val urls = OrderEntryFix .refreshAndConvertToUrls(classesRoots)
74
55
if (modules.size == 1 ) {
75
- ModuleRootModificationUtil .addModuleLibrary(firstModule, libraryName, urls, emptyList(), scope)
56
+ ModuleRootModificationUtil .addModuleLibrary(
57
+ firstModule,
58
+ if (classesRoots.size > 1 ) descriptor.presentableName else null ,
59
+ urls,
60
+ emptyList(),
61
+ scope
62
+ )
76
63
} else {
77
64
WriteAction .run<RuntimeException > {
78
- val library =
79
- LibraryUtil .createLibrary(
80
- LibraryTablesRegistrar .getInstance().getLibraryTable(project), descriptor.presentableName
81
- )
82
- val model = library.modifiableModel
83
- for (url in urls) {
84
- model.addRoot(url!! , OrderRootType .CLASSES )
85
- }
86
- model.commit()
87
- for (module in modules) {
88
- ModuleRootModificationUtil .addDependency(module, library, scope, false )
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
+ }
89
75
}
90
76
}
91
77
}
92
78
}
93
79
return resolvedPromise()
94
80
}
95
81
96
- private fun ExternalLibraryDescriptor.mavenCoordinates () : String {
82
+ private fun ExternalLibraryDescriptor.mavenCoordinates (): String {
97
83
return " $libraryGroupId :$libraryArtifactId :${preferredVersion ? : RepositoryLibraryDescription .ReleaseVersionId } "
98
84
}
99
85
}
0 commit comments