Hi. This forum was a great help so far, so I have a hope
I’m trying to write a basic plugin that will perform code generation for my kotlin project.
Here’s the code:
private open class GenerateFromJsonTask : DefaultTask() {
@TaskAction
fun generate() {
CodeGenerator().apply {
baseDirectoryName = "${project.layout.buildDirectory.get()}/generated/fromSchema"
basePackageName = "com.example"
generate(File("my_schema.yml"))
}
}
}
open class GenerateFromJsonPlugin : Plugin<Project> {
override fun apply(project: Project) {
val genCodeTask = project.tasks.create("generateCode", GenerateFromJsonTask::class.java)
project.plugins.apply("org.jetbrains.kotlin.jvm")
}
}
And it does generate the code. My problem is: I need to add the generated code directory as a source to my project so I could use it.
But how to do it? I’ve tried all the variants of sourceSets for JavaPlugin and Kotlin*Plugins, but it didn’t work.
Please direct me to the right doc or help with a code snippet.
this is my buildSrc/build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
`kotlin-dsl`
`java-gradle-plugin`
}
gradlePlugin {
plugins {
register("json-schema-codegen") {
id = "json-schema-codegen"
implementationClass = "com.example.gradle.GenerateFromJsonPlugin"
}
}
}
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(gradleApi())
implementation(kotlin("gradle-plugin", "1.9.23"))
implementation("net.pwall.json:json-kotlin-schema-codegen:0.107")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
}
}