-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(script): add support for 'mavenId()' in 'class'
- Loading branch information
Showing
5 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
idea-plugin/src/main/kotlin/com/itangcent/idea/utils/MavenHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.itangcent.idea.utils | ||
|
||
import com.intellij.openapi.module.ModuleUtilCore | ||
import com.intellij.psi.PsiClass | ||
import com.itangcent.annotation.script.ScriptTypeName | ||
import com.itangcent.common.utils.safe | ||
import org.jetbrains.idea.maven.project.MavenProjectsManager | ||
import org.jetbrains.plugins.gradle.service.project.data.ExternalProjectDataCache | ||
|
||
|
||
/** | ||
* @author tangcent | ||
* @date 2024/08/11 | ||
*/ | ||
object MavenHelper { | ||
|
||
fun getMavenId(psiClass: PsiClass): MavenIdData? { | ||
return safe { | ||
getMavenIdByMaven(psiClass) | ||
} ?: safe { | ||
getMavenIdByGradle(psiClass) | ||
} | ||
} | ||
|
||
private fun getMavenIdByMaven(psiClass: PsiClass): MavenIdData? { | ||
val project = psiClass.project | ||
val module = ModuleUtilCore.findModuleForPsiElement(psiClass) | ||
?: return null | ||
|
||
val mavenProjectsManager = MavenProjectsManager.getInstance(project) | ||
val mavenProject = mavenProjectsManager.findProject(module) ?: return null | ||
val mavenId = mavenProject.mavenId | ||
return MavenIdData( | ||
groupId = mavenId.groupId!!, | ||
artifactId = mavenId.artifactId!!, | ||
version = mavenId.version!! | ||
) | ||
} | ||
|
||
private fun getMavenIdByGradle(psiClass: PsiClass): MavenIdData? { | ||
val project = psiClass.project | ||
val projectPath = project.basePath ?: return null | ||
val externalProject = ExternalProjectDataCache.getInstance(project).getRootExternalProject(projectPath) | ||
?: return null | ||
|
||
return MavenIdData( | ||
groupId = externalProject.group, | ||
artifactId = externalProject.name, | ||
version = externalProject.version | ||
) | ||
} | ||
} | ||
|
||
@ScriptTypeName("MavenId") | ||
class MavenIdData( | ||
val groupId: String, | ||
val artifactId: String, | ||
val version: String | ||
) { | ||
fun maven(): String { | ||
return """ | ||
<dependency> | ||
<groupId>$groupId</groupId> | ||
<artifactId>$artifactId</artifactId> | ||
<version>$version</version> | ||
</dependency> | ||
""".trimIndent() | ||
} | ||
|
||
fun gradle(): String { | ||
return """ | ||
implementation group: '$groupId', name: '$artifactId', version: '$version' | ||
""".trimIndent() | ||
} | ||
|
||
fun gradleShort(): String { | ||
return """ | ||
implementation '$groupId:$artifactId:$version' | ||
""".trimIndent() | ||
} | ||
|
||
fun gradleKotlin(): String { | ||
return """ | ||
implementation("$groupId:$artifactId:$version") | ||
""".trimIndent() | ||
} | ||
|
||
fun sbt(): String { | ||
return """ | ||
libraryDependencies += "$groupId" % "$artifactId" % "$version" | ||
""".trimIndent() | ||
} | ||
|
||
fun ivy(): String { | ||
return """ | ||
<dependency org="$groupId" name="$artifactId" rev="$version" /> | ||
""".trimIndent() | ||
} | ||
|
||
override fun toString(): String { | ||
return "$groupId:$artifactId:$version" | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is MavenIdData) return false | ||
|
||
if (groupId != other.groupId) return false | ||
if (artifactId != other.artifactId) return false | ||
if (version != other.version) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = groupId.hashCode() | ||
result = 31 * result + artifactId.hashCode() | ||
result = 31 * result + version.hashCode() | ||
return result | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
idea-plugin/src/test/kotlin/com/itangcent/idea/utils/MavenHelperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.itangcent.idea.utils | ||
|
||
import com.intellij.psi.PsiClass | ||
import com.itangcent.testFramework.PluginContextLightCodeInsightFixtureTestCase | ||
import org.junit.jupiter.api.Assertions | ||
|
||
/** | ||
* @author tangcent | ||
* @date 2024/08/11 | ||
*/ | ||
class MavenHelperTest : PluginContextLightCodeInsightFixtureTestCase() { | ||
|
||
private lateinit var userCtrlPsiClass: PsiClass | ||
|
||
override fun beforeBind() { | ||
super.beforeBind() | ||
userCtrlPsiClass = loadClass("api/UserCtrl.java")!! | ||
} | ||
|
||
fun testGetMavenId() { | ||
val mavenId = MavenHelper.getMavenId(userCtrlPsiClass) | ||
Assertions.assertNull(mavenId) | ||
} | ||
|
||
fun testMavenIdData() { | ||
val mavenIdData = MavenIdData("com.itangcent", "intellij-idea-test", "1.0.0") | ||
Assertions.assertEquals("com.itangcent", mavenIdData.groupId) | ||
Assertions.assertEquals("intellij-idea-test", mavenIdData.artifactId) | ||
Assertions.assertEquals("1.0.0", mavenIdData.version) | ||
|
||
Assertions.assertEquals( | ||
""" | ||
<dependency> | ||
<groupId>com.itangcent</groupId> | ||
<artifactId>intellij-idea-test</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
""".trimIndent(), mavenIdData.maven() | ||
) | ||
|
||
Assertions.assertEquals( | ||
""" | ||
implementation group: 'com.itangcent', name: 'intellij-idea-test', version: '1.0.0' | ||
""".trimIndent(), mavenIdData.gradle() | ||
) | ||
|
||
Assertions.assertEquals( | ||
""" | ||
implementation 'com.itangcent:intellij-idea-test:1.0.0' | ||
""".trimIndent(), mavenIdData.gradleShort() | ||
) | ||
|
||
Assertions.assertEquals( | ||
""" | ||
implementation("com.itangcent:intellij-idea-test:1.0.0") | ||
""".trimIndent(), mavenIdData.gradleKotlin() | ||
) | ||
|
||
Assertions.assertEquals( | ||
""" | ||
<dependency org="com.itangcent" name="intellij-idea-test" rev="1.0.0" /> | ||
""".trimIndent(), mavenIdData.ivy() | ||
) | ||
|
||
Assertions.assertEquals( | ||
""" | ||
libraryDependencies += "com.itangcent" % "intellij-idea-test" % "1.0.0" | ||
""".trimIndent(), mavenIdData.sbt() | ||
) | ||
} | ||
} |