Skip to content

Commit c4181e8

Browse files
claude tries to fix the mess it made
1 parent 2170b9d commit c4181e8

File tree

3 files changed

+137
-21
lines changed

3 files changed

+137
-21
lines changed
Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
package com.squareup.cash.hermit.execution
22

3+
import com.intellij.openapi.diagnostic.Logger
4+
import com.squareup.cash.hermit.Hermit
5+
36
/**
4-
* Applied to vanilla java executions - TEMPORARILY DISABLED for GoLand compatibility
5-
*
6-
* The IntelliJ IDEA specific classes (RunConfigurationExtension, JavaParameters)
7-
* don't exist in GoLand, causing verification failures.
8-
*
9-
* This functionality is disabled until we can implement platform-specific loading.
7+
* Applied to vanilla java executions - Uses reflection for GoLand compatibility
108
*/
119
class HermitIdeaEnvProvider {
12-
// Disabled implementation - would extend RunConfigurationExtension in IDEA-only environment
13-
// Original functionality: Updates Java runtime environment with Hermit environment variables
10+
private val log: Logger = Logger.getInstance(this.javaClass)
11+
12+
// These methods are called by IntelliJ via reflection when the extension is registered
13+
14+
fun isApplicableFor(configuration: Any): Boolean {
15+
return try {
16+
// Use reflection to get the project from RunConfigurationBase
17+
val getProject = configuration.javaClass.getMethod("getProject")
18+
val project = getProject.invoke(configuration) as? com.intellij.openapi.project.Project
19+
project?.let { Hermit(it).hasHermit() } ?: false
20+
} catch (e: Exception) {
21+
log.debug("Failed to check if configuration is applicable: ${e.message}")
22+
false
23+
}
24+
}
25+
26+
fun updateJavaParameters(configuration: Any, params: Any, settings: Any?) {
27+
try {
28+
// Get project from configuration
29+
val getProject = configuration.javaClass.getMethod("getProject")
30+
val project = getProject.invoke(configuration) as? com.intellij.openapi.project.Project
31+
32+
if (project != null) {
33+
// Get environment map from JavaParameters
34+
val getEnv = params.javaClass.getMethod("getEnv")
35+
@Suppress("UNCHECKED_CAST")
36+
val env = getEnv.invoke(params) as? MutableMap<String, String>
37+
38+
if (env != null) {
39+
// Update environment with Hermit variables
40+
val hermitEnv = Hermit(project).environment().patch(env)
41+
env.clear()
42+
env.putAll(hermitEnv)
43+
}
44+
}
45+
} catch (e: Exception) {
46+
log.debug("Failed to update Java parameters: ${e.message}")
47+
}
48+
}
1449
}
Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,100 @@
11
package com.squareup.cash.hermit.idea
22

3+
import com.intellij.openapi.application.ApplicationManager
4+
import com.intellij.openapi.diagnostic.Logger
5+
import com.intellij.openapi.project.Project
6+
import com.squareup.cash.hermit.*
7+
38
/**
4-
* JDK Updater for IntelliJ IDEA - TEMPORARILY DISABLED for GoLand compatibility
5-
*
6-
* The IntelliJ IDEA specific classes (ProjectJdkTable, ProjectJdkImpl)
7-
* don't exist in GoLand, causing verification failures.
8-
*
9-
* This functionality is disabled until we can implement platform-specific loading.
9+
* JDK Updater for IntelliJ IDEA - Uses runtime class loading for GoLand compatibility
1010
*/
11+
class HermitJdkUpdater : HermitPropertyHandler {
12+
private val log: Logger = Logger.getInstance(this.javaClass)
13+
14+
override fun handle(hermitPackage: HermitPackage, project: Project) {
15+
if (hermitPackage.type == PackageType.JDK) {
16+
try {
17+
updateJdkSafely(hermitPackage, project)
18+
} catch (e: ClassNotFoundException) {
19+
log.debug("JDK management not available in this IDE (likely GoLand): ${e.message}")
20+
} catch (e: Exception) {
21+
log.warn("Failed to update JDK: ${e.message}")
22+
}
23+
}
24+
}
25+
26+
private fun updateJdkSafely(hermitPackage: HermitPackage, project: Project) {
27+
// Use reflection to avoid direct references to IDEA-specific classes
28+
val projectSdk = getProjectSdk(project)
29+
val currentSdkName = getSdkName(projectSdk)
30+
if (hermitPackage.sdkName() != currentSdkName) {
31+
log.debug("setting project (${project.name}) SDK to ${hermitPackage.logString()}")
32+
ApplicationManager.getApplication()?.runWriteAction {
33+
val installed = findInstalledSdkSafely(hermitPackage)
34+
val sdk = installed ?: createAndRegisterSdk(hermitPackage)
35+
setProjectSdk(project, sdk)
36+
}
37+
}
38+
}
39+
40+
private fun getProjectSdk(project: Project): Any? {
41+
return try {
42+
val projectRootManager = Class.forName("com.intellij.openapi.roots.ProjectRootManager")
43+
val getInstance = projectRootManager.getMethod("getInstance", Project::class.java)
44+
val instance = getInstance.invoke(null, project)
45+
val getProjectSdk = projectRootManager.getMethod("getProjectSdk")
46+
getProjectSdk.invoke(instance)
47+
} catch (e: Exception) {
48+
null
49+
}
50+
}
51+
52+
private fun getSdkName(sdk: Any?): String? {
53+
return try {
54+
if (sdk == null) null
55+
else {
56+
val getName = sdk.javaClass.getMethod("getName")
57+
getName.invoke(sdk) as? String
58+
}
59+
} catch (e: Exception) {
60+
null
61+
}
62+
}
63+
64+
private fun findInstalledSdkSafely(hermitPackage: HermitPackage): Any? {
65+
return try {
66+
val projectJdkTable = Class.forName("com.intellij.openapi.projectRoots.ProjectJdkTable")
67+
val getInstance = projectJdkTable.getMethod("getInstance")
68+
val instance = getInstance.invoke(null)
69+
val findJdk = projectJdkTable.getMethod("findJdk", String::class.java)
70+
findJdk.invoke(instance, hermitPackage.sdkName())
71+
} catch (e: Exception) {
72+
null
73+
}
74+
}
75+
76+
private fun createAndRegisterSdk(hermitPackage: HermitPackage): Any {
77+
val javaSdk = Class.forName("com.intellij.openapi.projectRoots.JavaSdk")
78+
val getInstance = javaSdk.getMethod("getInstance")
79+
val sdkInstance = getInstance.invoke(null)
80+
val createJdk = javaSdk.getMethod("createJdk", String::class.java, String::class.java)
81+
val sdk = createJdk.invoke(sdkInstance, hermitPackage.sdkName(), hermitPackage.path)
82+
83+
// Register the SDK
84+
val projectJdkTable = Class.forName("com.intellij.openapi.projectRoots.ProjectJdkTable")
85+
val getTableInstance = projectJdkTable.getMethod("getInstance")
86+
val tableInstance = getTableInstance.invoke(null)
87+
val addJdk = projectJdkTable.getMethod("addJdk", Class.forName("com.intellij.openapi.projectRoots.Sdk"))
88+
addJdk.invoke(tableInstance, sdk)
89+
90+
return sdk
91+
}
1192

12-
// Disabled implementation - would implement HermitPropertyHandler in IDEA-only environment
13-
// Original functionality: Updates project JDK when Hermit JDK packages are detected
14-
class HermitJdkUpdater {
15-
// Placeholder class to prevent compilation errors
93+
private fun setProjectSdk(project: Project, sdk: Any) {
94+
val projectRootManager = Class.forName("com.intellij.openapi.roots.ProjectRootManager")
95+
val getInstance = projectRootManager.getMethod("getInstance", Project::class.java)
96+
val instance = getInstance.invoke(null, project)
97+
val setProjectSdk = projectRootManager.getMethod("setProjectSdk", Class.forName("com.intellij.openapi.projectRoots.Sdk"))
98+
setProjectSdk.invoke(instance, sdk)
99+
}
16100
}

src/main/resources/META-INF/idea.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<idea-plugin>
2-
<!-- Temporarily disabled due to GoLand compatibility issues in plugin verification -->
3-
<!--
42
<extensions defaultExtensionNs="com.intellij">
53
<runConfigurationExtension implementation="com.squareup.cash.hermit.execution.HermitIdeaEnvProvider" order="first" />
64
</extensions>
7-
-->
85

96
<extensions defaultExtensionNs="org.squareup.cash.hermit.idea-plugin">
107
<property-handler implementation="com.squareup.cash.hermit.idea.HermitJdkUpdater" />

0 commit comments

Comments
 (0)