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 19, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 135bc0f commit 83e95d3
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -12,15 +12,19 @@ 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.SdkType
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.util.registry.Registry
import com.intellij.util.application
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch
import java.util.concurrent.CompletableFuture


@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
class GitpodProjectManager(
private val project: Project
) {
@@ -33,23 +37,33 @@ class GitpodProjectManager(
* It is a workaround for https://youtrack.jetbrains.com/issue/GTW-88
*/
private fun configureSdks() {
if (application.isHeadlessEnvironment) {
if (application.isHeadlessEnvironment || Registry.get("gitpod.autoJdk.disabled").asBoolean()) {
return
}
val pendingSdk = CompletableFuture<Sdk>()
application.invokeLaterOnWriteThread {
application.runWriteAction {
try {
ProjectJdkTable.getInstance().preconfigure()
pendingSdk.complete(ProjectJdkTable.getInstance().allJdks.firstOrNull())
val jdkTable = ProjectJdkTable.getInstance()
jdkTable.preconfigure()
val preferredJdkHomePath = JavaHomeFinder.getFinder().findExistingJdks().firstOrNull()
if (preferredJdkHomePath != null) {
val sdk = SdkConfigurationUtil.createAndAddSDK(
preferredJdkHomePath,
SdkType.findByName(jdkTable.defaultSdkType.name)!!
)
pendingSdk.complete(sdk)
} else {
pendingSdk.complete(jdkTable.allJdks.firstOrNull())
}
} catch (t: Throwable) {
pendingSdk.completeExceptionally(t)
}
}
}
GlobalScope.launch {
val sdk = pendingSdk.await() ?: return@launch
thisLogger().warn("gitpod: '${project.name}' project: SDK detected: $sdk")
thisLogger().warn("gitpod: '${project.name}' project: preferred SDK detected: $sdk")
project.messageBus.connect().subscribe(ProjectTopics.MODULES, object : ModuleListener {
override fun moduleAdded(project: Project, module: Module) {
configureSdk(sdk)
@@ -65,17 +79,15 @@ class GitpodProjectManager(
val projectRootManager = ProjectRootManager.getInstance(project)
if (projectRootManager.projectSdk == null) {
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) {
m.sdk = sdk
thisLogger().warn("gitpod: '${module.name}' module: SDK was auto preconfigured: $sdk")
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
<gateway.customization.name implementation="io.gitpod.jetbrains.remote.GitpodGatewayClientCustomizationProvider"/>
<gateway.customization.performance id="gitpodMetricsControl" order="before cpuControl" implementation="io.gitpod.jetbrains.remote.GitpodMetricControlProvider"/>
<gateway.customization.metrics id="gitpodMetricsProvider" implementation="io.gitpod.jetbrains.remote.GitpodMetricProvider" />
<registryKey key="gitpod.autoJdk.disabled" defaultValue="false" description="Disable auto-detection of JDK for the project and its modules" restartRequired="true"/>
</extensions>

</idea-plugin>

0 comments on commit 83e95d3

Please sign in to comment.