Skip to content

Commit 2682b48

Browse files
Required libraries can be installed silently #492
After-review cleanup 2
1 parent 3ca852d commit 2682b48

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/util/UtProjectModelModifier.kt

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.utbot.intellij.plugin.util
22

33
import com.intellij.codeInsight.daemon.impl.quickfix.LocateLibraryDialog
44
import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix
5-
import com.intellij.ide.JavaUiBundle
65
import com.intellij.jarRepository.JarRepositoryManager
76
import com.intellij.openapi.application.WriteAction
87
import com.intellij.openapi.module.Module
@@ -14,13 +13,9 @@ import com.intellij.openapi.roots.OrderRootType
1413
import com.intellij.openapi.roots.impl.IdeaProjectModelModifier
1514
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar
1615
import com.intellij.openapi.roots.libraries.LibraryUtil
17-
import com.intellij.openapi.roots.libraries.ui.OrderRoot
18-
import com.intellij.openapi.ui.Messages
1916
import com.intellij.util.PathUtil
2017
import com.intellij.util.containers.ContainerUtil
21-
import java.util.stream.Collectors
2218
import org.jetbrains.concurrency.Promise
23-
import org.jetbrains.concurrency.rejectedPromise
2419
import org.jetbrains.concurrency.resolvedPromise
2520
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryDescription
2621
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties
@@ -31,69 +26,60 @@ class UtProjectModelModifier(val project: Project) : IdeaProjectModelModifier(pr
3126
modules: Collection<Module>,
3227
descriptor: ExternalLibraryDescriptor,
3328
scope: DependencyScope
34-
): Promise<Void> {
29+
): Promise<Void>? {
3530
val defaultRoots = descriptor.libraryClassesRoots
36-
val firstModule = ContainerUtil.getFirstItem(modules) ?: return rejectedPromise()
31+
val firstModule = ContainerUtil.getFirstItem(modules) ?: return null
3732
val classesRoots = if (defaultRoots.isNotEmpty()) {
3833
LocateLibraryDialog(
3934
firstModule,
4035
defaultRoots,
4136
descriptor.presentableName
4237
).showAndGetResult()
4338
} else {
44-
val libraryProperties = RepositoryLibraryProperties(JpsMavenRepositoryLibraryDescriptor(descriptor.mavenCoordinates()))
4539
val roots = JarRepositoryManager.loadDependenciesModal(
4640
project,
47-
libraryProperties,
41+
RepositoryLibraryProperties(JpsMavenRepositoryLibraryDescriptor(descriptor.mavenCoordinates())),
4842
/* loadSources = */ false,
4943
/* loadJavadoc = */ false,
5044
/* copyTo = */ null,
5145
/* repositories = */ null
5246
)
5347
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
6149
}
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()
7052
}
7153
if (classesRoots.isNotEmpty()) {
72-
val libraryName = if (classesRoots.size > 1) descriptor.presentableName else null
7354
val urls = OrderEntryFix.refreshAndConvertToUrls(classesRoots)
7455
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+
)
7663
} else {
7764
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+
}
8975
}
9076
}
9177
}
9278
}
9379
return resolvedPromise()
9480
}
9581

96-
private fun ExternalLibraryDescriptor.mavenCoordinates() : String {
82+
private fun ExternalLibraryDescriptor.mavenCoordinates(): String {
9783
return "$libraryGroupId:$libraryArtifactId:${preferredVersion ?: RepositoryLibraryDescription.ReleaseVersionId}"
9884
}
9985
}

0 commit comments

Comments
 (0)