Skip to content

Commit

Permalink
Support JDK Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
134130 committed Jul 20, 2024
1 parent 5158f7a commit a68c381
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 61 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
alias(libs.plugins.kotlin) // Kotlin support
alias(libs.plugins.gradleIntelliJPlugin) // Gradle IntelliJ Plugin
alias(libs.plugins.changelog) // Gradle Changelog Plugin
alias(libs.plugins.qodana) // Gradle Qodana Plugin
// alias(libs.plugins.qodana) // Gradle Qodana Plugin
alias(libs.plugins.kover) // Gradle Kover Plugin
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.l34130.mise
pluginName = mise
pluginRepositoryUrl = https://github.com/134130/intellij-mise
# SemVer format -> https://semver.org
pluginVersion = 1.0.1
pluginVersion = 1.1.0

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 223
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.l34130.mise.commands

import com.github.l34130.mise.extensions.fromJson
import com.github.l34130.mise.notifications.Notification
import com.google.gson.FieldNamingPolicy
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.notification.NotificationType

object MiseCmd {
fun loadEnv(workDir: String?): Map<String, String> {
try {
val process =
GeneralCommandLine("mise", "env")
.withWorkDirectory(workDir)
.createProcess()

val exitCode = process.waitFor()
if (exitCode != 0) {
val stderr = process.errorReader().use { it.readText() }
Notification.notify("Failed to import Mise environment: $stderr", NotificationType.WARNING)
return emptyMap()
}

val output = process.inputReader().use { it.readText() }
return output
.split("\n")
.map { it.removePrefix("export ") }
.filter { it.isNotBlank() }
.map { it.split("=", limit = 2) }
.associate { it[0] to it[1] }
} catch (e: Exception) {
Notification.notify("Failed to import Mise environment: ${e.message}", NotificationType.ERROR)
return emptyMap()
}
}

fun loadTools(workDir: String?): Map<String, List<MiseTool>> {
try {
val process =
GeneralCommandLine("mise", "ls", "--current", "--json")
.withWorkDirectory(workDir)
.createProcess()

val exitCode = process.waitFor()
if (exitCode != 0) {
val stderr = process.errorReader().use { it.readText() }
Notification.notify("Failed to import Mise tools: $stderr", NotificationType.WARNING)
return emptyMap()
}

return GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()
.fromJson<Map<String, List<MiseTool>>>(process.inputReader())
} catch (e: Exception) {
Notification.notify("Failed to import Mise tools: ${e.message}", NotificationType.ERROR)
return emptyMap()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.l34130.mise.commands

data class MiseSource(
val type: String, // .mise.toml, .mise.config, etc.
val path: String, // absolute path to the source file
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.l34130.mise.commands

data class MiseTool(
val version: String,
val requestedVersion: String,
val installPath: String,
val source: MiseSource,
val installed: Boolean,
val active: Boolean,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.l34130.mise.extensions

import com.google.gson.Gson
import com.google.gson.JsonElement
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import java.io.Reader


inline fun <reified T> Gson.fromJson(json: String) =
fromJson<T>(json, object : TypeToken<T>() {}.type)

inline fun <reified T> Gson.fromJson(json: Reader) =
fromJson<T>(json, object : TypeToken<T>() {}.type)

inline fun <reified T> Gson.fromJson(reader: JsonReader) =
fromJson<T>(reader, object : TypeToken<T>() {}.type)

inline fun <reified T> Gson.fromJson(json: JsonElement) =
fromJson<T>(json, object : TypeToken<T>() {}.type)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.l34130.mise.icons

import com.intellij.openapi.util.IconLoader

object PluginIcons {
@JvmField
val Default = IconLoader.getIcon("/icons/default.svg", javaClass)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.l34130.mise.notifications

import com.github.l34130.mise.icons.PluginIcons
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.project.Project

object Notification {
fun notify(content :String, type: NotificationType, project: Project? = null) {
val notification = NotificationGroupManager.getInstance()
.getNotificationGroup(NOTIFICATION_GROUP_ID)
.createNotification(content, type)

notification.icon = PluginIcons.Default
notification.notify(project)
}

private const val NOTIFICATION_GROUP_ID = "Mise"
}
5 changes: 5 additions & 0 deletions modules/core/src/main/resources/icons/default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.l34130.mise.runconfigs

import com.github.l34130.mise.commands.MiseEnvCmd
import com.github.l34130.mise.commands.MiseCmd
import com.github.l34130.mise.settings.ui.RunConfigurationSettingsEditor
import com.goide.execution.GoRunConfigurationBase
import com.goide.execution.GoRunningState
Expand Down Expand Up @@ -41,7 +41,7 @@ class GoLandRunConfigurationExtension : GoRunConfigurationExtension() {
commandLineType: GoRunningState.CommandLineType,
) {
if (RunConfigurationSettingsEditor.isMiseEnabled(configuration)) {
MiseEnvCmd(configuration.getWorkingDirectory()).load().forEach { k, v ->
MiseCmd.loadEnv(configuration.getWorkingDirectory()).forEach { (k, v) ->
cmdLine.addEnvironmentVariable(k, v)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.l34130.mise.runconfigs

import com.github.l34130.mise.commands.MiseEnvCmd
import com.github.l34130.mise.commands.MiseCmd
import com.intellij.execution.Executor
import com.intellij.execution.application.ApplicationConfiguration
import com.intellij.execution.runners.ExecutionEnvironment
Expand All @@ -27,7 +27,7 @@ class GradleEnvironmentProvider : GradleExecutionEnvironmentProvider {
val sourceConfig = task!!.runProfile as ApplicationConfiguration
val gradleConfig = environment.runProfile as GradleRunConfiguration

gradleConfig.settings.env = MiseEnvCmd(sourceConfig.project.basePath).load() + sourceConfig.envs
gradleConfig.settings.env = MiseCmd.loadEnv(sourceConfig.project.basePath) + sourceConfig.envs
}

return environment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.l34130.mise.jdk

import com.github.l34130.mise.commands.MiseCmd
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.JavaSdk
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.startup.StartupActivity

class IdeaProjectJdkSetup : AnAction(), StartupActivity {
override fun actionPerformed(e: AnActionEvent) {
e.project?.let { runActivity(it) }
}

override fun runActivity(project: Project) {
val javaTools = MiseCmd.loadTools(project.basePath)["java"] ?: return

WriteAction.runAndWait<Throwable> {
for (tool in javaTools) {
val jdkName = createJdkName(project.name)
val newJdk =
JavaSdk.getInstance().createJdk(
jdkName,
tool.installPath,
false,
)

val oldJdk = ProjectJdkTable.getInstance().findJdk(jdkName)
if (oldJdk != null) {
ProjectJdkTable.getInstance().updateJdk(oldJdk, newJdk)
} else {
ProjectJdkTable.getInstance().addJdk(newJdk)
}

if (javaTools.size == 1) {
ProjectRootManager.getInstance(project).projectSdk = newJdk
}
}
}
}

private fun createJdkName(name: String): String = "JDK of project $name (mise)"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.l34130.mise.runconfigs

import com.github.l34130.mise.commands.MiseEnvCmd
import com.github.l34130.mise.commands.MiseCmd
import com.github.l34130.mise.settings.ui.RunConfigurationSettingsEditor
import com.intellij.execution.RunConfigurationExtension
import com.intellij.execution.configurations.GeneralCommandLine
Expand Down Expand Up @@ -50,7 +50,7 @@ class IdeaRunConfigurationExtension : RunConfigurationExtension() {
params.env.putAll(sourceEnv)

if (RunConfigurationSettingsEditor.isMiseEnabled(configuration)) {
params.env.putAll(MiseEnvCmd(params.workingDirectory).load())
params.env.putAll(MiseCmd.loadEnv(params.workingDirectory))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.l34130.mise.runconfigs

import com.github.l34130.mise.commands.MiseEnvCmd
import com.github.l34130.mise.commands.MiseCmd
import com.github.l34130.mise.settings.ui.RunConfigurationSettingsEditor
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.javascript.nodejs.execution.AbstractNodeTargetRunProfile
Expand Down Expand Up @@ -37,7 +37,7 @@ class NodeRunConfigurationExtension : AbstractNodeRunConfigurationExtension() {
environment: ExecutionEnvironment,
): NodeRunConfigurationLaunchSession? {
val config = configuration as NodeJsRunConfiguration
config.envs.putAll(MiseEnvCmd(config.workingDirectory).load())
config.envs.putAll(MiseCmd.loadEnv(config.workingDirectory))
return null
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/META-INF/mise-idea.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<postStartupActivity implementation="com.github.l34130.mise.jdk.IdeaProjectJdkSetup"/>

<runConfigurationExtension implementation="com.github.l34130.mise.runconfigs.IdeaRunConfigurationExtension"
id="miseIdea"/>
</extensions>

<actions>
<group id="ch.mise.actions"
text="Mise"
popup="true"
icon="com.github.l34130.mise.icons.PluginIcons.Default">
<add-to-group group-id="ToolsMenu"/>
</group>
<action id="com.github.l34130.mise.jdk.IdeaProjectJdkSetup"
class="com.github.l34130.mise.jdk.IdeaProjectJdkSetup"
text="Reload JDK"
>
<add-to-group group-id="ch.mise.actions"/>
</action>
</actions>
</idea-plugin>
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

<depends>com.intellij.modules.platform</depends>

<extensions defaultExtensionNs="com.intellij">
<notificationGroup displayType="BALLOON" id="Mise"/>
</extensions>

<depends optional="true" config-file="mise-idea.xml">com.intellij.java</depends>
<depends optional="true" config-file="mise-gradle.xml">com.intellij.gradle</depends>
<depends optional="true" config-file="mise-goland.xml">org.jetbrains.plugins.go</depends>
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/META-INF/pluginIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a68c381

Please sign in to comment.