Skip to content

Commit

Permalink
feat&fix: rsproto.targetSourceSet is added to the gradle plugin for c…
Browse files Browse the repository at this point in the history
…ustom sourceSets destination & fix kmp projects with ability to have jvm/android-only projects.
  • Loading branch information
y9vad9 committed Feb 29, 2024
1 parent 23faece commit bf9feb7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions build-conventions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ kotlin {
dependencies {
api(libs.kotlin.plugin)
api(libs.vanniktech.maven.publish)
api(libs.android.plugin)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import org.gradle.kotlin.dsl.property
/**
* Class representing the extension for generating Protobuf code.
*/
public open class RSProtoExtension(private val objects: ObjectFactory) {
public open class RSProtoExtension(objects: ObjectFactory) {
/**
* If you have custom project structure that does not have commonMain / main sourceSets,
* you should specify your main source set in the [targetSourceSet].
*/
public val targetSourceSet: Property<String?> = objects.property<String?>()
.convention(null)

/**
* Contains the path to the folder where the Proto definition files are located.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package org.timemates.rsproto.plugin

import org.timemates.rsproto.codegen.CodeGenerator
import okio.FileSystem
import okio.Path.Companion.toOkioPath
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.register
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.timemates.rsproto.codegen.CodeGenerator
import java.io.File
import kotlin.math.log

public class RSocketProtoGeneratorPlugin : Plugin<Project> {
override fun apply(target: Project) {
val extension = target.extensions.create<RSProtoExtension>("rsproto", target.objects)
val generationOutputPath = target.layout.buildDirectory.file(extension.generationOutputPath)

val generationTask = target.tasks.create("generateProto") {
val generateProto = target.tasks.create("generateProto") {
group = "rsproto"

val generationOutputPath = target.layout.buildDirectory.file(extension.generationOutputPath)

inputs.dir(extension.protoSourcePath)
outputs.dir(generationOutputPath)

Expand All @@ -45,8 +45,25 @@ public class RSocketProtoGeneratorPlugin : Plugin<Project> {
}
}

target.tasks.withType<KotlinCompile> {
mustRunAfter(generationTask)
target.afterEvaluate {
val allSourceSets = target.extensions.getByType<KotlinMultiplatformExtension>()
.sourceSets

val commonSourceSet = allSourceSets
.findByName(KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME)
val mainSourceSet = allSourceSets.findByName("main")

val sourceSet = if (extension.targetSourceSet.getOrNull() != null)
allSourceSets.getByName(extension.targetSourceSet.get())
else commonSourceSet ?: mainSourceSet

sourceSet
?.kotlin
?.srcDirs(generateProto.outputs)
?: error(SOURCE_SET_NOT_FOUND)
}
}
}
}

private const val SOURCE_SET_NOT_FOUND =
"Unable to obtain source set: you should have commonMain/main or custom one that is set up in the [rsproto.targetSourceSet]"
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ accompanist-systemUiController = { module = "com.google.accompanist:accompanist-
# Plugin classpaths
kotlin-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
vanniktech-maven-publish = { module = "com.vanniktech.maven.publish:com.vanniktech.maven.publish.gradle.plugin", version.require = "0.25.3" }
android-plugin = { module = "com.android.tools.build:gradle", version.ref = "android-gradle-plugin" }

[plugins]
# Build Conventions
Expand Down

0 comments on commit bf9feb7

Please sign in to comment.