Skip to content

Commit

Permalink
Update EAP build and restore Kotlin support
Browse files Browse the repository at this point in the history
  • Loading branch information
RedNesto committed Jul 5, 2024
1 parent d4b39e6 commit 01f85cf
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 96 deletions.
19 changes: 16 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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")
Expand All @@ -136,7 +149,7 @@ dependencies {

instrumentationTools()

testFramework(TestFrameworkType.Platform.JUnit5)
testFramework(TestFrameworkType.JUnit5)
testFramework(TestFrameworkType.Plugin.Java)

pluginVerifier()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
180 changes: 90 additions & 90 deletions src/main/kotlin/creator/buildsystem/GradleFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<BuildRepository>) {
// 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<BuildDependency>) {
// 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<GradlePlugin>) {
// 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<KtScriptInitializer>()
// .flatMap { it.childrenOfType<KtCallExpression>() }
// .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<BuildRepository>) {
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<BuildDependency>) {
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<GradlePlugin>) {
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<KtScriptInitializer>()
.flatMap { it.childrenOfType<KtCallExpression>() }
.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)}\""
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/mcdev-kotlin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

<idea-plugin>
<extensions defaultExtensionNs="com.demonwav.minecraft-dev">
<!-- <gradleFileType implementation="com.demonwav.mcdev.creator.buildsystem.KotlinGradleFile$Type" />-->
<gradleFileType implementation="com.demonwav.mcdev.creator.buildsystem.KotlinGradleFile$Type" />
</extensions>
</idea-plugin>

0 comments on commit 01f85cf

Please sign in to comment.