diff --git a/build.gradle.kts b/build.gradle.kts index 31cf5b9ed..c2b7b4376 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -34,11 +34,13 @@ plugins { mcdev groovy idea - id("org.jetbrains.intellij.platform") version "2.0.0-beta6" + id("org.jetbrains.intellij.platform") version "2.0.0-beta8" id("org.cadixdev.licenser") id("org.jlleitschuh.gradle.ktlint") version "10.3.0" } +val ideaVersion: String by project +val ideaVersionNoEapSnapshot = ideaVersion.removeSuffix("-EAP-SNAPSHOT") val ideaVersionName: String by project val coreVersion: String by project @@ -93,6 +95,17 @@ repositories { } } +configurations.all { + resolutionStrategy.eachDependency { + // For some reason dependency versions do not include the EAP-SNAPSHOT suffix anymore since beta7 + // This works around the issue for now + // TODO remove when no longer needed + if (requested.version == ideaVersionNoEapSnapshot) { + useVersion(ideaVersion) + } + } +} + dependencies { // Add tools.jar for the JDI API implementation(files(Jvm.current().toolsJar)) @@ -126,7 +139,7 @@ dependencies { bundledPlugin("com.intellij.gradle") bundledPlugin("org.intellij.groovy") // For some reason the Kotlin plugin can't be resolved... -// bundledPlugin("org.jetbrains.kotlin") + bundledPlugin("org.jetbrains.kotlin") bundledPlugin("ByteCodeViewer") bundledPlugin("com.intellij.properties") bundledPlugin("org.toml.lang") @@ -136,7 +149,7 @@ dependencies { instrumentationTools() - testFramework(TestFrameworkType.Platform.JUnit5) + testFramework(TestFrameworkType.JUnit5) testFramework(TestFrameworkType.Plugin.Java) pluginVerifier() diff --git a/gradle.properties b/gradle.properties index 27193437e..d9708ef15 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,7 +21,7 @@ # suppress inspection "UnusedProperty" for whole file kotlin.code.style=official -ideaVersion = 242.16677.21-EAP-SNAPSHOT +ideaVersion = 242.19890.14-EAP-SNAPSHOT ideaVersionName = 2024.2 coreVersion = 1.7.6 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index daed1929c..c3528e8b3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -18,7 +18,7 @@ jflex-skeleton = "org.jetbrains.idea:jflex:1.7.0-c1fdf11" grammarKit = "org.jetbrains.idea:grammar-kit:1.5.1" # Gradle Tooling -gradleToolingExtension = "com.jetbrains.intellij.gradle:gradle-tooling-extension:242.16677.21-EAP-SNAPSHOT" +gradleToolingExtension = "com.jetbrains.intellij.gradle:gradle-tooling-extension:242.19890.14-EAP-SNAPSHOT" annotations = "org.jetbrains:annotations:24.0.0" groovy = "org.codehaus.groovy:groovy:3.0.19" diff --git a/src/main/kotlin/creator/buildsystem/GradleFile.kt b/src/main/kotlin/creator/buildsystem/GradleFile.kt index 4f3facc42..793c6b76b 100644 --- a/src/main/kotlin/creator/buildsystem/GradleFile.kt +++ b/src/main/kotlin/creator/buildsystem/GradleFile.kt @@ -20,20 +20,20 @@ package com.demonwav.mcdev.creator.buildsystem -// import com.demonwav.mcdev.util.childrenOfType +import com.demonwav.mcdev.util.childrenOfType import com.demonwav.mcdev.util.mapFirstNotNull import com.intellij.openapi.extensions.ExtensionPointName import com.intellij.openapi.project.Project import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.PsiFile import org.jetbrains.annotations.ApiStatus.Internal -// import org.jetbrains.kotlin.psi.KtBinaryExpression -// import org.jetbrains.kotlin.psi.KtBlockExpression -// import org.jetbrains.kotlin.psi.KtCallExpression -// import org.jetbrains.kotlin.psi.KtFile -// import org.jetbrains.kotlin.psi.KtNameReferenceExpression -// import org.jetbrains.kotlin.psi.KtPsiFactory -// import org.jetbrains.kotlin.psi.KtScriptInitializer +import org.jetbrains.kotlin.psi.KtBinaryExpression +import org.jetbrains.kotlin.psi.KtBlockExpression +import org.jetbrains.kotlin.psi.KtCallExpression +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtNameReferenceExpression +import org.jetbrains.kotlin.psi.KtPsiFactory +import org.jetbrains.kotlin.psi.KtScriptInitializer import org.jetbrains.plugins.groovy.lang.psi.GroovyFile import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock @@ -136,88 +136,88 @@ class GroovyGradleFile(override val psi: GroovyFile) : GradleFile { } } -// class KotlinGradleFile(override val psi: KtFile) : GradleFile { -// override fun addRepositories(project: Project, repositories: List) { -// val script = psi.script?.blockExpression ?: return -// val reposBlock = findOrCreateKotlinBlock(project, script, "repositories") -// val elementFactory = KtPsiFactory(project) -// for (repo in repositories) { -// if (BuildSystemType.GRADLE !in repo.buildSystems) { -// continue -// } -// val mavenBlock = elementFactory.createExpression("maven {\n}") as KtCallExpression -// val mavenLambda = mavenBlock.lambdaArguments[0].getLambdaExpression()!!.bodyExpression!! -// if (repo.id.isNotBlank()) { -// val idStatement = elementFactory.createAssignment("name = ${makeStringLiteral(repo.id)}") -// mavenLambda.addBefore(idStatement, mavenLambda.rBrace) -// } -// val urlStatement = elementFactory.createAssignment("url = uri(${makeStringLiteral(repo.url)})") -// mavenLambda.addBefore(urlStatement, mavenLambda.rBrace) -// reposBlock.addBefore(mavenBlock, reposBlock.rBrace) -// } -// } -// -// override fun addDependencies(project: Project, dependencies: List) { -// val script = psi.script?.blockExpression ?: return -// val depsBlock = findOrCreateKotlinBlock(project, script, "dependencies") -// val elementFactory = KtPsiFactory(project) -// for (dep in dependencies) { -// val gradleConfig = dep.gradleConfiguration ?: continue -// val stmt = elementFactory.createExpression( -// "$gradleConfig(\"${escapeGString(dep.groupId)}:${ -// escapeGString(dep.artifactId) -// }:${escapeGString(dep.version)}\")", -// ) -// depsBlock.addBefore(stmt, depsBlock.rBrace) -// } -// } -// -// override fun addPlugins(project: Project, plugins: List) { -// val script = psi.script?.blockExpression ?: return -// val pluginsBlock = findOrCreateKotlinBlock(project, script, "plugins", first = true) -// val elementFactory = KtPsiFactory(project) -// for (plugin in plugins) { -// val stmt = elementFactory.createExpression(makePluginStatement(plugin, true)) -// pluginsBlock.addBefore(stmt, pluginsBlock.rBrace) -// } -// } -// -// private fun findKotlinBlock(element: KtBlockExpression, name: String): KtBlockExpression? { -// return element.childrenOfType() -// .flatMap { it.childrenOfType() } -// .mapFirstNotNull { call -> -// if ((call.calleeExpression as? KtNameReferenceExpression)?.getReferencedName() == name) { -// call.lambdaArguments.firstOrNull()?.getLambdaExpression()?.bodyExpression -// } else { -// null -// } -// } -// } -// -// private fun findOrCreateKotlinBlock( -// project: Project, -// element: KtBlockExpression, -// name: String, -// first: Boolean = false, -// ): KtBlockExpression { -// findKotlinBlock(element, name)?.let { return it } -// val block = KtPsiFactory(project).createExpression("$name {\n}") -// val addedBlock = if (first) { -// element.addAfter(block, element.lBrace) -// } else { -// element.addBefore(block, element.rBrace) -// } -// return (addedBlock as KtCallExpression).lambdaArguments.first().getLambdaExpression()!!.bodyExpression!! -// } -// -// private fun KtPsiFactory.createAssignment(text: String): KtBinaryExpression { -// return this.createBlock(text).firstStatement as KtBinaryExpression -// } -// -// class Type : GradleFile.Type { -// override fun createGradleFile(psiFile: PsiFile) = (psiFile as? KtFile)?.let(::KotlinGradleFile) -// } -// } +class KotlinGradleFile(override val psi: KtFile) : GradleFile { + override fun addRepositories(project: Project, repositories: List) { + val script = psi.script?.blockExpression ?: return + val reposBlock = findOrCreateKotlinBlock(project, script, "repositories") + val elementFactory = KtPsiFactory(project) + for (repo in repositories) { + if (BuildSystemType.GRADLE !in repo.buildSystems) { + continue + } + val mavenBlock = elementFactory.createExpression("maven {\n}") as KtCallExpression + val mavenLambda = mavenBlock.lambdaArguments[0].getLambdaExpression()!!.bodyExpression!! + if (repo.id.isNotBlank()) { + val idStatement = elementFactory.createAssignment("name = ${makeStringLiteral(repo.id)}") + mavenLambda.addBefore(idStatement, mavenLambda.rBrace) + } + val urlStatement = elementFactory.createAssignment("url = uri(${makeStringLiteral(repo.url)})") + mavenLambda.addBefore(urlStatement, mavenLambda.rBrace) + reposBlock.addBefore(mavenBlock, reposBlock.rBrace) + } + } + + override fun addDependencies(project: Project, dependencies: List) { + val script = psi.script?.blockExpression ?: return + val depsBlock = findOrCreateKotlinBlock(project, script, "dependencies") + val elementFactory = KtPsiFactory(project) + for (dep in dependencies) { + val gradleConfig = dep.gradleConfiguration ?: continue + val stmt = elementFactory.createExpression( + "$gradleConfig(\"${escapeGString(dep.groupId)}:${ + escapeGString(dep.artifactId) + }:${escapeGString(dep.version)}\")", + ) + depsBlock.addBefore(stmt, depsBlock.rBrace) + } + } + + override fun addPlugins(project: Project, plugins: List) { + val script = psi.script?.blockExpression ?: return + val pluginsBlock = findOrCreateKotlinBlock(project, script, "plugins", first = true) + val elementFactory = KtPsiFactory(project) + for (plugin in plugins) { + val stmt = elementFactory.createExpression(makePluginStatement(plugin, true)) + pluginsBlock.addBefore(stmt, pluginsBlock.rBrace) + } + } + + private fun findKotlinBlock(element: KtBlockExpression, name: String): KtBlockExpression? { + return element.childrenOfType() + .flatMap { it.childrenOfType() } + .mapFirstNotNull { call -> + if ((call.calleeExpression as? KtNameReferenceExpression)?.getReferencedName() == name) { + call.lambdaArguments.firstOrNull()?.getLambdaExpression()?.bodyExpression + } else { + null + } + } + } + + private fun findOrCreateKotlinBlock( + project: Project, + element: KtBlockExpression, + name: String, + first: Boolean = false, + ): KtBlockExpression { + findKotlinBlock(element, name)?.let { return it } + val block = KtPsiFactory(project).createExpression("$name {\n}") + val addedBlock = if (first) { + element.addAfter(block, element.lBrace) + } else { + element.addBefore(block, element.rBrace) + } + return (addedBlock as KtCallExpression).lambdaArguments.first().getLambdaExpression()!!.bodyExpression!! + } + + private fun KtPsiFactory.createAssignment(text: String): KtBinaryExpression { + return this.createBlock(text).firstStatement as KtBinaryExpression + } + + class Type : GradleFile.Type { + override fun createGradleFile(psiFile: PsiFile) = (psiFile as? KtFile)?.let(::KotlinGradleFile) + } +} private fun makeStringLiteral(str: String): String { return "\"${escapeGString(str)}\"" diff --git a/src/main/resources/META-INF/mcdev-kotlin.xml b/src/main/resources/META-INF/mcdev-kotlin.xml index 3bf7d07c7..e49b5a467 100644 --- a/src/main/resources/META-INF/mcdev-kotlin.xml +++ b/src/main/resources/META-INF/mcdev-kotlin.xml @@ -20,6 +20,6 @@ - +