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 authored and roboquat committed Aug 23, 2022
1 parent d3147bc commit 6b6bac3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand All @@ -33,15 +37,30 @@ 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 preconfiguredJdk = ProjectRootManager.getInstance(project).projectSdk
val preferredJdkHomePath = JavaHomeFinder.getFinder().findExistingJdks().firstOrNull()
pendingSdk.complete(
when {
preconfiguredJdk != null -> preconfiguredJdk
preferredJdkHomePath != null ->
jdkTable.allJdks.find { sdk -> sdk.homePath == preferredJdkHomePath }
?: SdkConfigurationUtil.createAndAddSDK(
preferredJdkHomePath,
SdkType.findByName(jdkTable.defaultSdkType.name)!!
)

else -> jdkTable.allJdks.firstOrNull()
}
)
} catch (t: Throwable) {
pendingSdk.completeExceptionally(t)
}
Expand Down Expand Up @@ -78,4 +97,4 @@ class GitpodProjectManager(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 6b6bac3

Please sign in to comment.