[kotlin] Added missing dependency on TOML plugin to avoid exceptions if it is disabled

This change moves the corresponding function to the new Kotlin Gradle wizard to only disable that part of the plugin if TOML is disabled. This still means that if TOML is disabled, then the Kotlin Gradle Wizard will no longer be available.

KTIJ-31608


(cherry picked from commit b502241bf2ef62ea93a618c0567e3560d8de91b2)

IJ-CR-147988

GitOrigin-RevId: 41f53d72a537a15534be18b7064821df3005aaa5
This commit is contained in:
Frederik Haselmeier
2024-10-29 23:15:27 +00:00
committed by intellij-monorepo-bot
parent d6647ed568
commit da441b45a2
5 changed files with 34 additions and 37 deletions
@@ -40,6 +40,5 @@
<orderEntry type="module" module-name="intellij.platform.vcs.core" />
<orderEntry type="module" module-name="intellij.platform.core.ui" />
<orderEntry type="module" module-name="kotlin.code-insight.inspections.shared" />
<orderEntry type="module" module-name="intellij.toml.core" />
</component>
</module>
@@ -28,9 +28,7 @@ import com.intellij.platform.ide.progress.withModalProgress
import com.intellij.platform.util.progress.reportSequentialProgress
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.util.childrenOfType
import org.gradle.util.GradleVersion
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.Nls
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.config.ApiVersion
@@ -39,7 +37,6 @@ import org.jetbrains.kotlin.idea.base.projectStructure.ModuleSourceRootGroup
import org.jetbrains.kotlin.idea.base.projectStructure.toModuleGroup
import org.jetbrains.kotlin.idea.compiler.configuration.IdeKotlinVersion
import org.jetbrains.kotlin.idea.configuration.*
import org.jetbrains.kotlin.idea.core.util.toPsiFile
import org.jetbrains.kotlin.idea.extensions.gradle.KotlinGradleConstants.GRADLE_PLUGIN_ID
import org.jetbrains.kotlin.idea.extensions.gradle.KotlinGradleConstants.GROUP_ID
import org.jetbrains.kotlin.idea.facet.getRuntimeLibraryVersion
@@ -55,9 +52,6 @@ import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.tools.projectWizard.compatibility.KotlinGradleCompatibilityStore
import org.jetbrains.plugins.gradle.settings.GradleSettings
import org.toml.lang.psi.TomlFile
import org.toml.lang.psi.TomlInlineTable
import org.toml.lang.psi.TomlTable
abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
@@ -651,8 +645,6 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
@NonNls
const val CLASSPATH = "classpath \"$GROUP_ID:$GRADLE_PLUGIN_ID:\$kotlin_version\""
private const val KOTLIN_GRADLE_PLUGIN_ID = "org.jetbrains.kotlin:kotlin-gradle-plugin"
private fun getAllConfigurableKotlinVersions(): List<IdeKotlinVersion> {
return KotlinGradleCompatibilityStore.allKotlinVersions()
}
@@ -688,31 +680,6 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
return remainingKotlinVersions.maxOrNull()
}
/**
* Returns if there is a Kotlin Gradle Plugin version defined in the Gradle version catalog
* and that version is used in the build script of the buildSrc folder.
*/
@ApiStatus.Internal
fun usesVersionCatalogVersionInBuildSrc(project: Project): Boolean {
val buildSrcModule = project.modules.firstOrNull { it.name.endsWith(".buildSrc") } ?: return false
val buildSrcBuildFile = buildSrcModule.getBuildScriptPsiFile() ?: return false
val gradleFolder = project.guessProjectDir()?.findChild("gradle") ?: return false
val tomlFile = gradleFolder.findChild("libs.versions.toml")?.toPsiFile(project) as? TomlFile ?: return false
val libraryTable = tomlFile.childrenOfType<TomlTable>().firstOrNull {
it.header.key?.text == "libraries"
} ?: return false
val gradlePluginKey = libraryTable.entries.firstOrNull { entry ->
val entryValue = entry.value as? TomlInlineTable ?: return@firstOrNull false
val moduleEntry = entryValue.entries.firstOrNull { it.key.text == "module" } ?: return@firstOrNull false
moduleEntry.value?.text?.contains(KOTLIN_GRADLE_PLUGIN_ID) == true
}?.key?.text ?: return false
return buildSrcBuildFile.text.contains(gradlePluginKey)
}
/**
* Returns the defined Kotlin version in the pluginManagement block in the settings.gradle file for the [module].
* Returns null if the version is not defined in the settings.gradle file.
@@ -26,5 +26,6 @@
<orderEntry type="module" module-name="kotlin.base.project-structure" />
<orderEntry type="module" module-name="kotlin.base.util" />
<orderEntry type="module" module-name="kotlin.gradle.gradle-tooling.impl" />
<orderEntry type="module" module-name="intellij.toml.core" />
</component>
</module>
@@ -2,6 +2,7 @@
<dependencies>
<plugin id="org.jetbrains.plugins.gradle"/>
<plugin id="org.toml.lang"/>
<module name="kotlin.project-wizard.idea"/>
<module name="kotlin.gradle.code-insight-common"/>
<module name="kotlin.gradle.gradle-java"/>
@@ -22,6 +22,7 @@ import com.intellij.openapi.observable.util.and
import com.intellij.openapi.observable.util.bindBooleanStorage
import com.intellij.openapi.observable.util.equalsTo
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.project.modules
import com.intellij.openapi.projectRoots.JavaSdk
import com.intellij.openapi.ui.ValidationInfo
@@ -29,6 +30,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.findDocument
import com.intellij.openapi.vfs.findPsiFile
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.util.childrenOfType
import com.intellij.ui.UIBundle
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.bindSelected
@@ -38,8 +40,10 @@ import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.idea.base.projectStructure.ModuleSourceRootGroup
import org.jetbrains.kotlin.idea.base.projectStructure.ModuleSourceRootMap
import org.jetbrains.kotlin.idea.compiler.configuration.IdeKotlinVersion
import org.jetbrains.kotlin.idea.core.util.toPsiFile
import org.jetbrains.kotlin.idea.gradleCodeInsightCommon.GradleBuildScriptSupport
import org.jetbrains.kotlin.idea.gradleCodeInsightCommon.KotlinWithGradleConfigurator
import org.jetbrains.kotlin.idea.gradleCodeInsightCommon.getBuildScriptPsiFile
import org.jetbrains.kotlin.idea.gradleJava.kotlinGradlePluginVersion
import org.jetbrains.kotlin.tools.projectWizard.*
import org.jetbrains.kotlin.tools.projectWizard.BuildSystemKotlinNewProjectWizard.Companion.DEFAULT_KOTLIN_VERSION
@@ -64,9 +68,14 @@ import org.jetbrains.plugins.gradle.service.project.wizard.AbstractGradleModuleB
import org.jetbrains.plugins.gradle.service.project.wizard.GradleNewProjectWizardStep
import org.jetbrains.plugins.gradle.util.GradleConstants.KOTLIN_DSL_SCRIPT_NAME
import org.jetbrains.plugins.gradle.util.GradleConstants.KOTLIN_DSL_SETTINGS_FILE_NAME
import org.toml.lang.psi.TomlFile
import org.toml.lang.psi.TomlInlineTable
import org.toml.lang.psi.TomlTable
private const val GENERATE_SINGLE_MODULE_PROPERTY_NAME: String = "NewProjectWizard.generateSingleModule"
private const val KOTLIN_GRADLE_PLUGIN_ID = "org.jetbrains.kotlin:kotlin-gradle-plugin"
private val MIN_GRADLE_VERSION_BUILD_SRC = GradleVersion.version("8.2")
private class GradleKotlinModuleBuilder : AbstractGradleModuleBuilder()
@@ -295,9 +304,29 @@ internal class GradleKotlinNewProjectWizard : BuildSystemKotlinNewProjectWizard
return KotlinWithGradleConfigurator.getPluginManagementVersion(parentModule)?.parsedVersion
}
private fun usesVersionCatalogVersion(project: Project): Boolean {
/**
* Returns if there is a Kotlin Gradle Plugin version defined in the Gradle version catalog
* and that version is used in the build script of the buildSrc folder.
*/
private fun usesVersionCatalogVersionInBuildSrc(project: Project): Boolean {
if (isCreatingNewRootModule()) return false
return KotlinWithGradleConfigurator.usesVersionCatalogVersionInBuildSrc(project)
val buildSrcModule = project.modules.firstOrNull { it.name.endsWith(".buildSrc") } ?: return false
val buildSrcBuildFile = buildSrcModule.getBuildScriptPsiFile() ?: return false
val gradleFolder = project.guessProjectDir()?.findChild("gradle") ?: return false
val tomlFile = gradleFolder.findChild("libs.versions.toml")?.toPsiFile(project) as? TomlFile ?: return false
val libraryTable = tomlFile.childrenOfType<TomlTable>().firstOrNull {
it.header.key?.text == "libraries"
} ?: return false
val gradlePluginKey = libraryTable.entries.firstOrNull { entry ->
val entryValue = entry.value as? TomlInlineTable ?: return@firstOrNull false
val moduleEntry = entryValue.entries.firstOrNull { it.key.text == "module" } ?: return@firstOrNull false
moduleEntry.value?.text?.contains(KOTLIN_GRADLE_PLUGIN_ID) == true
}?.key?.text ?: return false
return buildSrcBuildFile.text.contains(gradlePluginKey)
}
override fun setupProject(project: Project) {
@@ -320,7 +349,7 @@ internal class GradleKotlinNewProjectWizard : BuildSystemKotlinNewProjectWizard
withKotlinJvmPlugin(kotlinVersionToUse.takeUnless {
pluginManagementVersion != null || // uses the version from pluginManagement
parentKotlinVersion != null || // uses the version from parent
usesVersionCatalogVersion(project) // uses the version from the version catalog
usesVersionCatalogVersionInBuildSrc(project) // uses the version from the version catalog
})
withKotlinTest()