Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KRPC-71 Type-safe project accessors #106

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import util.KOTLINX_RPC_PREFIX
import util.isPublicModule

plugins {
`java-platform`
`maven-publish`
Expand All @@ -11,7 +14,7 @@ plugins {
dependencies {
constraints {
rootProject.subprojects.filter {
it.name.startsWith(KOTLINX_RPC_PREFIX) && it != project
it.isPublicModule && it != project
}.forEach { project ->
api(project)
}
Expand All @@ -20,9 +23,9 @@ dependencies {

publishing {
publications {
create<MavenPublication>("kotlinxRpcPlatform") {
create<MavenPublication>("bom") {
groupId = project.group.toString()
artifactId = project.name
artifactId = "$KOTLINX_RPC_PREFIX-${project.name}"
version = project.version.toString()

from(components["javaPlatform"])
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ apiValidation {
listOf(
"codegen-tests-jvm",
"codegen-tests-mpp",
"kotlinx-rpc-runtime-test",
"kotlinx-rpc-utils",
"runtime-test",
"utils",
)
)

Expand Down
6 changes: 5 additions & 1 deletion compiler-plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

rootProject.name = "kotlinx-rpc-compiler-plugin"
rootProject.name = "compiler-plugin"

enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

pluginManagement {
includeBuild("../gradle-conventions")
Expand All @@ -13,3 +15,5 @@ plugins {
id("settings-conventions")
id("compiler-specific-modules")
}

includeRootAsPublic()
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package util

import org.gradle.api.Project
import org.gradle.kotlin.dsl.extra

/**
* See gradle-settings-conventions/src/main/kotlin/includePublic.kt
*/
val Project.isPublicModule: Boolean get() {
return extra.has("isPublicModule") && extra["isPublicModule"] == true
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ package util
import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.provider.Property
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.kotlin.dsl.maven

const val KOTLINX_RPC_PREFIX = "kotlinx-rpc"

fun MavenPublication.setPublicArtifactId(project: Project) {
artifactId = "$KOTLINX_RPC_PREFIX-$artifactId"

project.logger.info("Altered artifactId for $name publication: $artifactId")
}

infix fun <T> Property<T>.by(value: T) {
set(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private fun isIncluded(targetName: String, kotlinVersion: String, lookupTable: M
}

private fun KotlinMultiplatformExtension.configureTargets(
project: Project,
kotlinVersion: String,
targetsLookup: Map<String, String>,
jvm: Boolean = true,
Expand Down Expand Up @@ -95,6 +96,12 @@ private fun KotlinMultiplatformExtension.configureTargets(
}.also { targets.add(it) }
}

targets.forEach { target ->
target.mavenPublication {
setPublicArtifactId(project)
}
}

return targets
}

Expand All @@ -120,7 +127,7 @@ fun Project.configureKotlin(
}

kotlin {
val includedTargets = configureTargets(kotlinVersion, lookupTable, jvm, js, native)
val includedTargets = configureTargets(project, kotlinVersion, lookupTable, jvm, js, native)

configureDetekt(includedTargets)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import util.by
import util.configureRepository
import util.getSensitiveProperty
import util.*

val isGradlePlugin = project.properties["kotlinx.rpc.gradle.plugin"] == "true"
val publishingExtension = project.extensions.findByType<PublishingExtension>()
val globalRootDir: String by extra

if (name.startsWith("kotlinx-rpc")) { // only public modules
if (isPublicModule) {
if (publishingExtension == null) {
apply(plugin = "maven-publish")
}
Expand Down Expand Up @@ -48,6 +46,9 @@ fun PublishingExtension.configurePublication() {
if (javadocJar != null) {
publication.artifact(javadocJar)
}

publication.setPublicArtifactId(project)

logger.info("Project ${project.name} -> Publication configured: ${publication.name}")
}

Expand Down
6 changes: 3 additions & 3 deletions gradle-plugin/gradle-plugin-all/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
description = "kotlinx.rpc Gradle Plugin"

dependencies {
implementation(project(":kotlinx-rpc-gradle-plugin-api"))
implementation(project(":kotlinx-rpc-gradle-plugin-platform"))
implementation(projects.gradlePluginApi)
implementation(projects.gradlePluginPlatform)

compileOnly(libs.kotlin.gradle.plugin)
}

gradlePlugin {
plugins {
create("kotlinxRpcPlugin") {
create("plugin") {
id = "org.jetbrains.kotlinx.rpc.plugin"

displayName = "kotlinx.rpc Gradle Plugin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import org.jetbrains.kotlin.gradle.utils.loadPropertyFromResources
object RPCPluginConst {
const val GROUP_ID = "org.jetbrains.kotlinx"

const val COMPILER_PLUGIN_MODULE = "${GROUP_ID}:kotlinx-rpc-compiler-plugin"
const val KSP_PLUGIN_MODULE = "${GROUP_ID}:kotlinx-rpc-ksp-plugin"
const val COMPILER_PLUGIN_MODULE = "${GROUP_ID}:compiler-plugin"
const val KSP_PLUGIN_MODULE = "${GROUP_ID}:ksp-plugin"

const val KSP_PLUGIN_ID = "com.google.devtools.ksp"

Expand Down
4 changes: 2 additions & 2 deletions gradle-plugin/gradle-plugin-platform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
description = "kotlinx.rpc Platform Plugin"

dependencies {
implementation(project(":kotlinx-rpc-gradle-plugin-api"))
implementation(projects.gradlePluginApi)
}

gradlePlugin {
plugins {
create("kotlinxRpcPlatform") {
create("platform") {
id = "org.jetbrains.kotlinx.rpc.platform"

displayName = "kotlinx.rpc Platform Plugin"
Expand Down
4 changes: 3 additions & 1 deletion gradle-plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

rootProject.name = "kotlinx-rpc-gradle-plugin"
rootProject.name = "gradle-plugin"

enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

pluginManagement {
includeBuild("../gradle-conventions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ val kotlinVersion = extra[CSM.KOTLIN_VERSION_EXTRA] as? String
// IMPORTANT: it is expected that the root submodule is already included to the project,
// otherwise the exception will be thrown
fun includeCSM(dir: File, files: Array<File>) {
val rootProjectDirName = dir.name
val rootProjectName = dir.name

val submodules = files.filter {
it.isDirectory && it.name.startsWith(rootProjectDirName)
it.isDirectory && it.name.startsWith(rootProjectName)
}.toMutableSet()

val core = submodules.singleOrNull { it.name == "$rootProjectDirName-core" }
val core = submodules.singleOrNull { it.name == "$rootProjectName-core" }
if (core == null) {
error("Compiler Specific Module $rootProjectDirName should have `-core` module defined")
error("Compiler Specific Module $rootProjectName should have `-core` module defined")
}
val compilerSubmodules = submodules - core

Expand All @@ -64,9 +64,9 @@ fun includeCSM(dir: File, files: Array<File>) {
.replace(File.separator, ":")
.takeIf { it.isNotEmpty() }?.let { ":$it" } ?: ""

includePublic("$basePath:$rootProjectDirName-core")
includePublic("$basePath:$rootProjectName-core")

val prefix = "$rootProjectDirName-"
val prefix = "$rootProjectName-"

val currentCompilerModuleDirName = compilerSubmodules
.map { it.name to CompilerModuleSemVer(it.name, prefix) }
Expand All @@ -76,13 +76,12 @@ fun includeCSM(dir: File, files: Array<File>) {
kotlinVersion.startsWith(semVer.version)
}?.first
?: error("""
Unable to find compiler specific submodule for $rootProjectDirName and Kotlin $kotlinVersion.
Unable to find compiler specific submodule for $rootProjectName and Kotlin $kotlinVersion.
Available modules: ${compilerSubmodules.joinToString { it.name }}
""".trimIndent())

includePublic("$basePath:$currentCompilerModuleDirName")

val rootProjectName = "$KOTLINX_RPC_PREFIX$rootProjectDirName"
gradle.projectsLoaded {
if (rootProject.name == rootProjectName) {
return@projectsLoaded
Expand Down
40 changes: 19 additions & 21 deletions gradle-settings-conventions/src/main/kotlin/includePublic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@
@file:Suppress("unused", "detekt.MissingPackageDeclaration")

import org.gradle.api.initialization.Settings
import java.io.File
import java.io.File.separator

const val KOTLINX_RPC_PREFIX = "kotlinx-rpc-"
import org.gradle.kotlin.dsl.extra

/**
* Includes a project by the given Gradle path and sets proper public name.
*
* Adds mandatory "kotlinx-rpc-" prefix to the project name, keeping the path as is.
* Example:
* ```kotlin
* // this declaration
* includePublic(":bom")
*
* // is the short form for this
* include(":kotlinx-rpc-bom")
* project(":kotlinx-rpc-bom").projectDir = file("./bom")
* ```
* Includes a project by the given Gradle path and marks it as a public.
*/
fun Settings.includePublic(projectPath: String) {
require(projectPath.startsWith(":")) { "Project name should start with a colon: $projectPath" }
include(projectPath)

val fullProjectName = projectPath.replace(":", ":$KOTLINX_RPC_PREFIX")
val fullProjectPath = "." + projectPath.replace(":", separator)
val projectName = projectPath.substringAfterLast(":")
gradle.rootProject {
allprojects {
if (name == projectName) {
extra["isPublicModule"] = true
}
}
}
}

include(fullProjectName)
project(fullProjectName).projectDir = File(fullProjectPath)
/**
* Marks root project as public
*/
fun Settings.includeRootAsPublic() {
gradle.rootProject {
extra["isPublicModule"] = true
}
}
6 changes: 5 additions & 1 deletion ksp-plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

rootProject.name = "kotlinx-rpc-ksp-plugin"
rootProject.name = "ksp-plugin"

enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

pluginManagement {
includeBuild("../gradle-conventions")
Expand All @@ -12,3 +14,5 @@ pluginManagement {
plugins {
id("settings-conventions")
}

includeRootAsPublic()
File renamed without changes.
26 changes: 13 additions & 13 deletions runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ kotlin {
sourceSets {
commonMain {
dependencies {
api(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-api"))
api(projects.runtime.runtimeApi)

implementation(project(":kotlinx-rpc-utils"))
implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-serialization"))
implementation(projects.utils)
implementation(projects.runtime.runtimeSerialization)

api(libs.coroutines.core)
implementation(libs.serialization.core)
implementation(libs.kotlin.reflect)

implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-logging"))
implementation(projects.runtime.runtimeLogging)
}
}
commonTest {
dependencies {
implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-client"))
implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-server"))
implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-serialization"))
implementation(projects.runtime.runtimeClient)
implementation(projects.runtime.runtimeServer)
implementation(projects.runtime.runtimeSerialization)

implementation(libs.kotlin.test)
implementation(libs.coroutines.test)
Expand All @@ -49,10 +49,10 @@ kotlin {

val jvmTest by getting {
dependencies {
implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-test"))
implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-serialization:kotlinx-rpc-runtime-serialization-json"))
implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-serialization:kotlinx-rpc-runtime-serialization-cbor"))
implementation(project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-serialization:kotlinx-rpc-runtime-serialization-protobuf"))
implementation(projects.runtime.runtimeTest)
implementation(projects.runtime.runtimeSerialization.runtimeSerializationJson)
implementation(projects.runtime.runtimeSerialization.runtimeSerializationCbor)
implementation(projects.runtime.runtimeSerialization.runtimeSerializationProtobuf)

implementation(libs.logback.classic)
}
Expand Down Expand Up @@ -92,10 +92,10 @@ tasks.create("moveToGold") {
}
}

evaluationDependsOn(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-test")
evaluationDependsOn(":runtime:runtime-test")

// otherwise it complains and fails the build on 1.8.*
val jsProductionLibraryCompileSync: TaskProvider<Task> = project(":kotlinx-rpc-runtime:kotlinx-rpc-runtime-test")
val jsProductionLibraryCompileSync: TaskProvider<Task> = project(":runtime:runtime-test")
.tasks.named("jsProductionLibraryCompileSync")

tasks.named("jsBrowserTest") {
Expand Down
Loading