Skip to content

Commit

Permalink
KSP gradle plugin single target
Browse files Browse the repository at this point in the history
  • Loading branch information
programadorthi committed Nov 15, 2024
1 parent 9394150 commit 7ec2b89
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ksp/core-annotations/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
../../ksp-annotations/build
build
6 changes: 3 additions & 3 deletions ksp/core-annotations/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POM_NAME=Ksp Processor
POM_ARTIFACT_ID=ksp-processor
POM_DESCRIPTION=The KSP routing processor
POM_NAME=Ksp Core Annotations
POM_ARTIFACT_ID=ksp-core-annotations
POM_DESCRIPTION=Core annotations to generate basic behaviors
2 changes: 1 addition & 1 deletion ksp/core-processor/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
../../ksp-processor/build
build
6 changes: 3 additions & 3 deletions ksp/core-processor/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POM_NAME=Ksp Processor
POM_ARTIFACT_ID=ksp-processor
POM_DESCRIPTION=The KSP routing processor
POM_NAME=Ksp Core Processor
POM_ARTIFACT_ID=ksp-core-processor
POM_DESCRIPTION=Ksp core processor to generate basic behaviors
1 change: 1 addition & 0 deletions ksp/gradle-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
41 changes: 41 additions & 0 deletions ksp/gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import com.android.build.gradle.internal.tasks.factory.dependsOn

plugins {
kotlin("jvm")
id("java-gradle-plugin")
id("com.gradle.plugin-publish") version "1.2.1"
id("org.jetbrains.kotlinx.kover")
alias(libs.plugins.maven.publish)
}

dependencies {
implementation(libs.plugin.kotlin)
}

gradlePlugin {
website.set("https://github.com/programadorthi/kotlin-routing")
vcsUrl.set("https://github.com/programadorthi/kotlin-routing")

plugins {
create("kotlin-routing-plugin") {
id = "dev.programadorthi.routing"
implementationClass = "dev.programadorthi.routing.gradle.KotlinRoutingGradlePlugin"
displayName = "Kotlin Routing Gradle Plugin"
description = "Gradle Plugin for Kotlin Routing"
tags.set(listOf("router", "kotlin", "kotlin-mpp", "ktor", "routing", "navigation"))
}
}
}

val versionRegister = tasks.register<Exec>("version-register") {
commandLine(
"find", "./src",
"-type", "f",
"-exec",
"sed", "-i",
"''", """s/<version>/${providers.gradleProperty("version").get()}/g""",
"{}", "+"
)
}

tasks.named("processResources").dependsOn(versionRegister)
3 changes: 3 additions & 0 deletions ksp/gradle-plugin/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_NAME=Ksp Gradle Plugin
POM_ARTIFACT_ID=ksp-gradle-plugin
POM_DESCRIPTION=The KSP Gradle Plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dev.programadorthi.routing.gradle

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinSingleTargetExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import java.util.Locale

class KotlinRoutingGradlePlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
checkNotNull(plugins.findPlugin("com.google.devtools.ksp")) {
"KSP plugin not found. Please, apply ksp plugin before routing plugin"
}

val kex = kotlinExtension
if (kex is KotlinSingleTargetExtension<*>) {
dependencies.add("implementation", ANNOTATIONS)
dependencies.add("ksp", PROCESSOR)
return@with
}

if (kex is KotlinMultiplatformExtension) {
kex.targets.configureEach { kTarget ->
kTarget.compilations.configureEach { compilation ->
println(">>>> $kTarget -> $compilation")
}
if (kTarget.platformType.name == "common") {
dependencies.add("kspCommonMainMetadata", PROCESSOR)
return@configureEach
}
val capitalizedTargetName =
kTarget.targetName.replaceFirstChar {
if (it.isLowerCase()) {
it.titlecase(Locale.US)
} else {
it.toString()
}
}
dependencies.add("ksp$capitalizedTargetName", PROCESSOR)

if (kTarget.compilations.any { it.name == "test" }) {
dependencies.add("ksp${capitalizedTargetName}Test", PROCESSOR)
}
}
}
}
}

private companion object {
// Version will be replaced by Gradle Exec Task
private const val ANNOTATIONS = "dev.programadorthi.routing:ksp-core-annotations:<version>"
private const val PROCESSOR = "dev.programadorthi.routing:ksp-core-processor:<version>"
}
}
14 changes: 10 additions & 4 deletions samples/ksp-sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
plugins {
alias(libs.plugins.ksp)
kotlin("jvm")
alias(libs.plugins.ksp)
//id("dev.programadorthi.routing") version "0.0.99"
}

dependencies {
implementation(projects.core)
implementation(projects.ksp.coreAnnotations)
implementation(projects.ksp.coreProcessor)
ksp(projects.ksp.coreProcessor)
}

configurations.all {
resolutionStrategy.dependencySubstitution {
substitute(module("dev.programadorthi.routing:core"))
.using(project(":core"))
.because("KSP gradle plugin have maven central dependencies")
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ include(":events")
include(":events-resources")
include(":ksp:core-annotations")
include(":ksp:core-processor")
include(":ksp:gradle-plugin")
include(":resources")
include(":sessions")
include(":status-pages")
Expand Down

0 comments on commit 7ec2b89

Please sign in to comment.