Added genref and messages modules
This commit is contained in:
1 parent
ff6dc22d00
commit
1d45c1b718
30 files changed
+1395
No files matched your search
@@ -0,0 +1,42 @@
|
||||
.gradle
|
||||
build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
@@ -0,0 +1,34 @@
|
||||
Instruction for kotlin v. 1.8.21
|
||||
<h3>Step 1 - configure build.gradle</h3>
|
||||
<p>Add <b>Kotlin 1.8.21</b> and <b>KSP</b> plugins:</p>
|
||||
|
||||
```kotlin
|
||||
kotlin("jvm") version "1.8.21"
|
||||
id("com.google.devtools.ksp") version "1.8.21-1.0.11"
|
||||
```
|
||||
|
||||
Add <b>CarcadeX GenRef</b> and <b>KotlinBukkitAPI</b> to project:</p>
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("br.com.devsrsouza.kotlinbukkitapi:architecture:1.0.0-SNAPSHOT")
|
||||
compileOnly("io.github.iredtea:carcadex-genref:[LATEST_VERSION]")
|
||||
ksp("io.github.iredtea:carcadex-genref:[LATEST_VERSION]")
|
||||
}
|
||||
```
|
||||
|
||||
<h3>Step 2 - create plugin functions</h3>
|
||||
|
||||
```kotlin
|
||||
@Plugin
|
||||
fun KotlinPlugin.start() {
|
||||
// Plugin enable logic
|
||||
}
|
||||
|
||||
|
||||
//Optional
|
||||
@OnDisable
|
||||
fun KotlinPlugin.stop() {
|
||||
// Disable enable logic
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,116 @@
|
||||
plugins {
|
||||
id("java")
|
||||
kotlin("jvm")
|
||||
id("maven-publish")
|
||||
id("signing")
|
||||
id("com.google.devtools.ksp") version "1.8.21-1.0.11"
|
||||
}
|
||||
|
||||
group = "tech.carcadex"
|
||||
val globalArtifactId = "kotlinbukkitkit-genref"
|
||||
val globalName = "kotlinbukkitkit-genref"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven {
|
||||
name = "spigotmc-repo"
|
||||
url = uri("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT")
|
||||
implementation(project(":architecture"))
|
||||
implementation("com.google.devtools.ksp:symbol-processing-api:1.8.21-1.0.11")
|
||||
implementation("com.squareup:kotlinpoet:1.10.1")
|
||||
implementation("com.squareup:kotlinpoet-ksp:1.10.1")
|
||||
|
||||
}
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(8)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
sourceSets.main {
|
||||
kotlin.srcDir("build/generated/ksp/main/kotlin")
|
||||
}
|
||||
sourceSets.test {
|
||||
kotlin.srcDir("build/generated/ksp/test/kotlin")
|
||||
}
|
||||
}
|
||||
|
||||
java {
|
||||
withJavadocJar()
|
||||
withSourcesJar()
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
artifactId = globalArtifactId
|
||||
groupId = "tech.carcadex"
|
||||
version = version
|
||||
from(components["java"])
|
||||
versionMapping {
|
||||
usage("java-api") {
|
||||
fromResolutionOf("runtimeClasspath")
|
||||
}
|
||||
usage("java-runtime") {
|
||||
fromResolutionResult()
|
||||
}
|
||||
}
|
||||
pom {
|
||||
artifactId = globalArtifactId
|
||||
groupId = "tech.carcadex"
|
||||
version = version
|
||||
name.set(globalArtifactId)
|
||||
description.set(globalArtifactId)
|
||||
url.set("https://github.com/iRedTea/CarcadeX")
|
||||
|
||||
licenses {
|
||||
license {
|
||||
name.set("GNU General Public License v3.0")
|
||||
url.set("https://www.gnu.org/licenses/gpl-3.0.en.html")
|
||||
}
|
||||
}
|
||||
developers {
|
||||
developer {
|
||||
id.set("itzRedTea")
|
||||
name.set("Red Tea")
|
||||
email.set("red__tea@outlook.com")
|
||||
}
|
||||
}
|
||||
scm {
|
||||
connection.set("scm:https://github.com/CarcadeX/$globalName.git")
|
||||
developerConnection.set("git@github.com:CarcadeX/$globalName.git")
|
||||
url.set("https://github.com/iRedTea/CarcadeX")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
val releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
|
||||
val snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
|
||||
url = uri(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl)
|
||||
credentials {
|
||||
username = "itzRedTea"
|
||||
password = project.findProperty("password").toString()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
signing {
|
||||
useGpgCmd()
|
||||
sign(publishing.publications.getByName("maven"))
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package me.redtea.carcadex.genref.plugin
|
||||
|
||||
import com.google.devtools.ksp.KspExperimental
|
||||
import com.google.devtools.ksp.getAnnotationsByType
|
||||
import com.google.devtools.ksp.processing.CodeGenerator
|
||||
import com.google.devtools.ksp.processing.KSPLogger
|
||||
import com.google.devtools.ksp.symbol.KSFile
|
||||
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
|
||||
import com.google.devtools.ksp.symbol.KSVisitorVoid
|
||||
import com.squareup.kotlinpoet.*
|
||||
import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview
|
||||
import com.squareup.kotlinpoet.ksp.writeTo
|
||||
import me.redtea.carcadex.genref.plugin.annotations.OnDisable
|
||||
import me.redtea.carcadex.genref.plugin.annotations.Plugin
|
||||
import tech.carcadex.kotlinbukkitkit.architecture.KotlinPlugin
|
||||
|
||||
@OptIn(KotlinPoetKspPreview::class)
|
||||
class PluginVisitor(private val codeGenerator: CodeGenerator,
|
||||
private val logger: KSPLogger
|
||||
) : KSVisitorVoid() {
|
||||
|
||||
var funcs: MutableList<FunSpec> = mutableListOf()
|
||||
|
||||
override fun visitFile(file: KSFile, data: Unit) {
|
||||
file.declarations.filter { it is KSFunctionDeclaration }.forEach { it.accept(this, Unit) }
|
||||
|
||||
val packageName = file.packageName.asString()
|
||||
val className = file.fileName.replace(
|
||||
".kt",
|
||||
""
|
||||
) + "Plugin"
|
||||
|
||||
val fileSpec = FileSpec.builder(packageName, className).apply {
|
||||
addProperty(
|
||||
PropertySpec.builder(
|
||||
"plugin",
|
||||
ClassName(packageName, className)
|
||||
)
|
||||
.mutable(true)
|
||||
.setter(FunSpec.builder("set()").addModifiers(KModifier.PRIVATE).build())
|
||||
.addModifiers(
|
||||
listOf(
|
||||
KModifier.LATEINIT
|
||||
)
|
||||
)
|
||||
.build()
|
||||
)
|
||||
addType(
|
||||
TypeSpec.Companion.classBuilder(className).primaryConstructor(
|
||||
FunSpec.constructorBuilder().build()
|
||||
)
|
||||
.addFunctions(funcs)
|
||||
.superclass(KotlinPlugin::class)
|
||||
.build()
|
||||
)
|
||||
}.build()
|
||||
try {
|
||||
fileSpec.writeTo(codeGenerator, true)
|
||||
} catch (ignored: FileAlreadyExistsException) {
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(KspExperimental::class)
|
||||
override fun visitFunctionDeclaration(function: KSFunctionDeclaration, data: Unit) {
|
||||
if (function.getAnnotationsByType(OnDisable::class).any()) disable(function)
|
||||
|
||||
if(function.getAnnotationsByType(Plugin::class).any()) enable(function)
|
||||
|
||||
|
||||
function.returnType!!.accept(this, Unit)
|
||||
}
|
||||
|
||||
fun disable(function: KSFunctionDeclaration) {
|
||||
funcs.add(FunSpec.builder("onPluginDisable")
|
||||
.addCode(CodeBlock.of("""
|
||||
${function.simpleName.getShortName()}()
|
||||
""".trimIndent()))
|
||||
.returns(Unit::class)
|
||||
.addModifiers(KModifier.OVERRIDE)
|
||||
.build())
|
||||
|
||||
}
|
||||
|
||||
fun enable(function: KSFunctionDeclaration) {
|
||||
funcs.add(FunSpec.builder("onPluginEnable")
|
||||
.addCode(CodeBlock.of("""
|
||||
plugin = this
|
||||
${function.simpleName.getShortName()}()
|
||||
|
||||
""".trimIndent()))
|
||||
.returns(Unit::class)
|
||||
.addModifiers(KModifier.OVERRIDE)
|
||||
.build())
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package me.redtea.carcadex.genref.plugin.annotations
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class OnDisable()
|
||||
@@ -0,0 +1,5 @@
|
||||
package me.redtea.carcadex.genref.plugin.annotations
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class Plugin()
|
||||
@@ -0,0 +1,29 @@
|
||||
package me.redtea.carcadex.genref.processor
|
||||
|
||||
import com.google.devtools.ksp.KspExperimental
|
||||
import com.google.devtools.ksp.isAnnotationPresent
|
||||
import com.google.devtools.ksp.processing.CodeGenerator
|
||||
import com.google.devtools.ksp.processing.KSPLogger
|
||||
import com.google.devtools.ksp.processing.Resolver
|
||||
import com.google.devtools.ksp.processing.SymbolProcessor
|
||||
import com.google.devtools.ksp.symbol.KSAnnotated
|
||||
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
|
||||
import com.google.devtools.ksp.validate
|
||||
import me.redtea.carcadex.genref.plugin.PluginVisitor
|
||||
import me.redtea.carcadex.genref.plugin.annotations.Plugin
|
||||
|
||||
class GenRefProcessor(private val codeGenerator: CodeGenerator,
|
||||
private val logger: KSPLogger) : SymbolProcessor {
|
||||
@OptIn(KspExperimental::class)
|
||||
override fun process(resolver: Resolver): List<KSAnnotated> {
|
||||
val files = resolver.getAllFiles().filter { it.declarations.any { it is KSFunctionDeclaration && it.validate()
|
||||
&& it.isAnnotationPresent(Plugin::class)} }
|
||||
|
||||
files.let { files -> files.forEach {
|
||||
it.accept(PluginVisitor(codeGenerator, logger), Unit)
|
||||
}
|
||||
}
|
||||
return files.filter { it.validate() }.toList()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package me.redtea.carcadex.genref.processor
|
||||
|
||||
import com.google.devtools.ksp.processing.SymbolProcessor
|
||||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
|
||||
import com.google.devtools.ksp.processing.SymbolProcessorProvider
|
||||
|
||||
class ProcessorProvider : SymbolProcessorProvider {
|
||||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
|
||||
return GenRefProcessor(environment.codeGenerator, environment.logger)
|
||||
}
|
||||
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
me.redtea.carcadex.genref.processor.ProcessorProvider
|
||||
Reference in new issue
Block a user