Skip to content

Commit

Permalink
Use Java SDK defined on JAVA_HOME by default on JetBrains IDEs
Browse files Browse the repository at this point in the history
  • Loading branch information
felladrin committed Aug 18, 2022
1 parent 47c64d4 commit c6ccbe7
Showing 1 changed file with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.ModuleListener
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.projectRoots.*
import com.intellij.openapi.projectRoots.impl.JavaHomeFinder
import com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil
import com.intellij.openapi.roots.ModuleRootModificationUtil
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.util.application
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch
import java.nio.file.Path
import java.util.concurrent.CompletableFuture


@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
class GitpodProjectManager(
private val project: Project
) {
Expand All @@ -40,8 +44,34 @@ class GitpodProjectManager(
application.invokeLaterOnWriteThread {
application.runWriteAction {
try {
ProjectJdkTable.getInstance().preconfigure()
pendingSdk.complete(ProjectJdkTable.getInstance().allJdks.firstOrNull())
val jdkTable = ProjectJdkTable.getInstance()

JavaHomeFinder.getFinder().findExistingJdks().forEach { javaHome ->
val sdkType = SdkType.findByName(jdkTable.defaultSdkType.name)

if (javaHome !== null && sdkType != null && JdkUtil.checkForJdk(javaHome)) {
val javaHomeVirtualFile = VirtualFileManager
.getInstance()
.findFileByNioPath(Path.of(javaHome))

if (javaHomeVirtualFile != null) {
val sdk = SdkConfigurationUtil.setupSdk(
jdkTable.allJdks,
javaHomeVirtualFile,
sdkType,
false,
null,
null
)

if (sdk != null && jdkTable.findJdk(sdk.name) == null) {
jdkTable.addJdk(sdk)
}
}
}
}

pendingSdk.complete(jdkTable.allJdks.firstOrNull())
} catch (t: Throwable) {
pendingSdk.completeExceptionally(t)
}
Expand All @@ -58,24 +88,23 @@ class GitpodProjectManager(
configureSdk(sdk)
}
}

private fun configureSdk(sdk: Sdk) {
application.invokeLaterOnWriteThread {
application.runWriteAction {
val projectRootManager = ProjectRootManager.getInstance(project)
if (projectRootManager.projectSdk == null) {
if (projectRootManager.projectSdk != sdk) {
projectRootManager.projectSdk = sdk
thisLogger().warn("gitpod: '${project.name}' project: SDK was auto preconfigured: $sdk")
}
}
}
for (module in ModuleManager.getInstance(project).modules) {
ModuleRootModificationUtil.updateModel(module) { m ->
if (m.sdk == null) {
if (m.sdk != sdk) {
m.sdk = sdk
thisLogger().warn("gitpod: '${module.name}' module: SDK was auto preconfigured: $sdk")
}
}
}
}
}
}

0 comments on commit c6ccbe7

Please sign in to comment.