From da441b45a2d4fe3c374170648c7853841d67682d Mon Sep 17 00:00:00 2001 From: Frederik Haselmeier Date: Tue, 29 Oct 2024 12:22:52 +0100 Subject: [PATCH] [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 --- .../kotlin.gradle.code-insight-common.iml | 1 - .../KotlinWithGradleConfigurator.kt | 33 ----------------- .../gradle/kotlin.project-wizard.gradle.iml | 1 + .../kotlin.project-wizard.gradle.xml | 1 + .../gradle/GradleKotlinNewProjectWizard.kt | 35 +++++++++++++++++-- 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/plugins/kotlin/gradle/code-insight-common/kotlin.gradle.code-insight-common.iml b/plugins/kotlin/gradle/code-insight-common/kotlin.gradle.code-insight-common.iml index 0cd5be5c9554..efd181b501ca 100644 --- a/plugins/kotlin/gradle/code-insight-common/kotlin.gradle.code-insight-common.iml +++ b/plugins/kotlin/gradle/code-insight-common/kotlin.gradle.code-insight-common.iml @@ -40,6 +40,5 @@ - \ No newline at end of file diff --git a/plugins/kotlin/gradle/code-insight-common/src/org/jetbrains/kotlin/idea/gradleCodeInsightCommon/KotlinWithGradleConfigurator.kt b/plugins/kotlin/gradle/code-insight-common/src/org/jetbrains/kotlin/idea/gradleCodeInsightCommon/KotlinWithGradleConfigurator.kt index d4b2a2522886..9829a4aa83cf 100644 --- a/plugins/kotlin/gradle/code-insight-common/src/org/jetbrains/kotlin/idea/gradleCodeInsightCommon/KotlinWithGradleConfigurator.kt +++ b/plugins/kotlin/gradle/code-insight-common/src/org/jetbrains/kotlin/idea/gradleCodeInsightCommon/KotlinWithGradleConfigurator.kt @@ -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 { 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().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. diff --git a/plugins/kotlin/project-wizard/gradle/kotlin.project-wizard.gradle.iml b/plugins/kotlin/project-wizard/gradle/kotlin.project-wizard.gradle.iml index f964a6c55249..ffc29ee257bd 100644 --- a/plugins/kotlin/project-wizard/gradle/kotlin.project-wizard.gradle.iml +++ b/plugins/kotlin/project-wizard/gradle/kotlin.project-wizard.gradle.iml @@ -26,5 +26,6 @@ + \ No newline at end of file diff --git a/plugins/kotlin/project-wizard/gradle/resources/kotlin.project-wizard.gradle.xml b/plugins/kotlin/project-wizard/gradle/resources/kotlin.project-wizard.gradle.xml index 4e2a7e0fa2c2..d35836a606d3 100644 --- a/plugins/kotlin/project-wizard/gradle/resources/kotlin.project-wizard.gradle.xml +++ b/plugins/kotlin/project-wizard/gradle/resources/kotlin.project-wizard.gradle.xml @@ -2,6 +2,7 @@ + diff --git a/plugins/kotlin/project-wizard/gradle/src/org/jetbrains/kotlin/tools/projectWizard/gradle/GradleKotlinNewProjectWizard.kt b/plugins/kotlin/project-wizard/gradle/src/org/jetbrains/kotlin/tools/projectWizard/gradle/GradleKotlinNewProjectWizard.kt index 0b6a98ed7de9..dd207aedf499 100644 --- a/plugins/kotlin/project-wizard/gradle/src/org/jetbrains/kotlin/tools/projectWizard/gradle/GradleKotlinNewProjectWizard.kt +++ b/plugins/kotlin/project-wizard/gradle/src/org/jetbrains/kotlin/tools/projectWizard/gradle/GradleKotlinNewProjectWizard.kt @@ -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().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()