Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
134130 committed Dec 28, 2024
1 parent ff0c27c commit c90208b
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
pluginModule(implementation(project(":mise-products-gradle")))
pluginModule(implementation(project(":mise-products-idea")))
pluginModule(implementation(project(":mise-products-nodejs")))
pluginModule(implementation(project(":mise-products-pycharm")))
pluginModule(implementation(project(":mise-products-rider")))
pluginModule(implementation(project(":mise-products-toml")))

Expand Down
21 changes: 21 additions & 0 deletions modules/products/pycharm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType

fun properties(key: String) = project.findProperty(key).toString()

plugins {
id("org.jetbrains.intellij.platform.module")
alias(libs.plugins.kotlin) // Kotlin support
}

dependencies {
implementation(project(":mise-core"))

// Configure Gradle IntelliJ Plugin - read more: https://github.com/JetBrains/gradle-intellij-plugin
intellijPlatform {
create(IntelliJPlatformType.PyCharmCommunity, properties("platformVersion"))

bundledPlugins("PythonCore")

jetbrainsRuntime()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.l34130.mise.python.run

import com.github.l34130.mise.core.command.MiseCommandLineHelper
import com.github.l34130.mise.core.command.MiseCommandLineNotFoundException
import com.github.l34130.mise.core.notification.MiseNotificationServiceUtils
import com.github.l34130.mise.core.run.MiseRunConfigurationSettingsEditor
import com.github.l34130.mise.core.setting.MiseSettings
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.jetbrains.python.run.AbstractPythonRunConfiguration
import com.jetbrains.python.run.PythonExecution
import com.jetbrains.python.run.PythonRunParams
import com.jetbrains.python.run.target.HelpersAwareTargetEnvironmentRequest
import com.jetbrains.python.run.target.PythonCommandLineTargetEnvironmentProvider

@Suppress("UnstableApiUsage")
class PythonCommandLineTargetEnvironmentProvider : PythonCommandLineTargetEnvironmentProvider {
override fun extendTargetEnvironment(
project: Project,
helpersAwareTargetRequest: HelpersAwareTargetEnvironmentRequest,
pythonExecution: PythonExecution,
runParams: PythonRunParams,
) {
if (runParams is AbstractPythonRunConfiguration<*>) {
val projectState = project.service<MiseSettings>().state
val runConfigState = MiseRunConfigurationSettingsEditor.getMiseRunConfigurationState(runParams)

val (workDir, configEnvironment) =
when {
projectState.useMiseDirEnv -> project.basePath to projectState.miseConfigEnvironment
runConfigState?.useMiseDirEnv == true -> {
val pythonWorkDir = runParams.workingDirectory ?: runParams.projectPathOnTarget
(pythonWorkDir ?: runParams.project.basePath) to runConfigState.miseConfigEnvironment
}
else -> null to null
}

val envVars =
MiseCommandLineHelper
.getEnvVars(workDir, configEnvironment)
.fold(
onSuccess = { envVars -> envVars },
onFailure = {
if (it !is MiseCommandLineNotFoundException) {
MiseNotificationServiceUtils.notifyException("Failed to load environment variables", it)
}
emptyMap()
},
)

envVars.forEach { (key, value) ->
pythonExecution.addEnvironmentVariable(key, value)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.github.l34130.mise.python.run

import com.github.l34130.mise.core.command.MiseCommandLineHelper
import com.github.l34130.mise.core.command.MiseCommandLineNotFoundException
import com.github.l34130.mise.core.notification.MiseNotificationServiceUtils
import com.github.l34130.mise.core.run.MiseRunConfigurationSettingsEditor
import com.github.l34130.mise.core.setting.MiseSettings
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.configurations.RunnerSettings
import com.intellij.openapi.components.service
import com.intellij.openapi.options.SettingsEditor
import com.jetbrains.python.run.AbstractPythonRunConfiguration
import com.jetbrains.python.run.PythonRunConfigurationExtension
import org.jdom.Element

class PythonRunConfigurationExtension : PythonRunConfigurationExtension() {
override fun isApplicableFor(runConfiguration: AbstractPythonRunConfiguration<*>): Boolean = true

override fun isEnabledFor(
runConfiguration: AbstractPythonRunConfiguration<*>,
runnerSettings: RunnerSettings?,
): Boolean = true

override fun writeExternal(
runConfiguration: AbstractPythonRunConfiguration<*>,
element: Element,
) {
MiseRunConfigurationSettingsEditor.writeExternal(runConfiguration, element)
}

override fun readExternal(
runConfiguration: AbstractPythonRunConfiguration<*>,
element: Element,
) {
MiseRunConfigurationSettingsEditor.readExternal(runConfiguration, element)
}

override fun getEditorTitle(): String = MiseRunConfigurationSettingsEditor.EDITOR_TITLE

override fun <P : AbstractPythonRunConfiguration<*>> createEditor(configuration: P): SettingsEditor<P> =
MiseRunConfigurationSettingsEditor(configuration.project)

override fun patchCommandLine(
runConfiguration: AbstractPythonRunConfiguration<*>,
runnerSettings: RunnerSettings?,
cmdLine: GeneralCommandLine,
runnerId: String,
) {
val project = runConfiguration.project
val projectState = project.service<MiseSettings>().state
val runConfigState = MiseRunConfigurationSettingsEditor.getMiseRunConfigurationState(runConfiguration)

val (workDir, configEnvironment) =
when {
projectState.useMiseDirEnv -> project.basePath to projectState.miseConfigEnvironment
runConfigState?.useMiseDirEnv == true -> {
val pythonWorkDir = runConfiguration.workingDirectory ?: runConfiguration.projectPathOnTarget
(pythonWorkDir ?: runConfiguration.project.basePath) to runConfigState.miseConfigEnvironment
}
else -> null to null
}

val envVars =
MiseCommandLineHelper
.getEnvVars(workDir, configEnvironment)
.fold(
onSuccess = { envVars -> envVars },
onFailure = {
if (it !is MiseCommandLineNotFoundException) {
MiseNotificationServiceUtils.notifyException("Failed to load environment variables", it)
}
emptyMap()
},
)

cmdLine.withEnvironment(envVars)
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include(
"modules/products/gradle",
"modules/products/idea",
"modules/products/nodejs",
"modules/products/pycharm",
"modules/products/rider",
"modules/products/toml",
)
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/mise-pycharm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<idea-plugin require-restart="false">
<extensions defaultExtensionNs="Pythonid">
<runConfigurationExtension implementation="com.github.l34130.mise.python.run.PythonRunConfigurationExtension"
id="misePython"/>
<pythonCommandLineTargetEnvironmentProvider implementation="com.github.l34130.mise.python.run.PythonCommandLineTargetEnvironmentProvider"/>
</extensions>
</idea-plugin>
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<depends optional="true" config-file="mise-gradle.xml">com.intellij.gradle</depends>
<depends optional="true" config-file="mise-idea.xml">com.intellij.java</depends>
<depends optional="true" config-file="mise-javascript.xml">JavaScript</depends>
<depends optional="true" config-file="mise-pycharm.xml">com.intellij.modules.python</depends>
<depends optional="true" config-file="mise-rider.xml">com.intellij.modules.rider</depends>
<depends optional="true" config-file="mise-toml.xml">org.toml.lang</depends>
</idea-plugin>

0 comments on commit c90208b

Please sign in to comment.