Skip to content

Commit

Permalink
Separate Kotlin bindings into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer committed Oct 16, 2024
1 parent 568e32b commit 31863f2
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 44 deletions.
18 changes: 18 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,27 @@ steps:
./gradlew detektMain detektTest
agents:
queue: android
- label: ":kotlin: Publish `rs.wordpress.api:bindings`"
key: "publish-wp-api-kotlin-bindings"
plugins: [$CI_TOOLKIT]
command: |
echo "--- :rust: Installing Rust"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -v -y
source "$$HOME/.cargo/env"
echo "--- :package: Installing Rust Toolchains"
make setup-rust
echo "--- :kotlin: Publishing `rs.wordpress.api:bindings`"
.buildkite/publish-wp-api-kotlin-bindings.sh
agents:
queue: android
- label: ":kotlin: Publish `rs.wordpress.api:kotlin`"
key: "publish-wp-api-kotlin"
plugins: [$CI_TOOLKIT]
depends_on:
- "publish-wp-api-kotlin-bindings"
command: |
echo "--- :rust: Installing Rust"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -v -y
Expand Down
11 changes: 11 additions & 0 deletions .buildkite/publish-wp-api-kotlin-bindings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -euo pipefail

cd ./native/kotlin
./gradlew \
:api:bindings:prepareToPublishToS3 $(prepare_to_publish_to_s3_params) \
:api:bindings:publish

# Add meta-data for the published version so we can use it in subsequent steps
buildkite-agent meta-data set "PUBLISHED_WP_API_BINDINGS_VERSION" < ./api/bindings/build/published-version.txt
4 changes: 4 additions & 0 deletions .buildkite/publish-wp-api-kotlin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

set -euo pipefail

# Retrieve data from previous steps
PUBLISHED_WP_API_BINDINGS_VERSION=$(buildkite-agent meta-data get "PUBLISHED_WP_API_BINDINGS_VERSION")

cd ./native/kotlin
./gradlew \
-PwpApiBindingsVersion="$PUBLISHED_WP_API_BINDINGS_VERSION" \
:api:kotlin:prepareToPublishToS3 $(prepare_to_publish_to_s3_params) \
:api:kotlin:publish

Expand Down
78 changes: 78 additions & 0 deletions native/kotlin/api/bindings/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
plugins {
alias(libs.plugins.rustAndroid)
alias(libs.plugins.kotlinJvm)
alias(libs.plugins.publishToS3)
id("java-library")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

dependencies {
implementation(libs.jna)
implementation(libs.kotlinx.coroutines.core)
}

sourceSets {
main {
java {
srcDir("${layout.buildDirectory.get()}/generated/source/uniffi/java/uniffi")
}
}
}

val generateUniFFIBindingsTask = tasks.register<Exec>("generateUniFFIBindings") {
val cargoProjectRoot = rootProject.ext.get("cargoProjectRoot")
val uniffiGeneratedPath = "${layout.buildDirectory.get()}/generated/source/uniffi/java"
val nativeLibraryPath = rootProject.ext.get("nativeLibraryPath")!!
val rustModuleName = rootProject.ext.get("rustModuleName")

dependsOn(rootProject.tasks.named("cargoBuildLibraryRelease"))
workingDir(project.rootDir)
commandLine(
"cargo",
"run",
"--release",
"--bin",
"wp_uniffi_bindgen",
"generate",
"--library",
nativeLibraryPath,
"--out-dir",
uniffiGeneratedPath,
"--language",
"kotlin"
)
outputs.dir(uniffiGeneratedPath)
// Re-generate if the interface definition changes.
inputs.file(nativeLibraryPath)
// Re-generate if our uniffi-bindgen tooling changes.
inputs.dir("$cargoProjectRoot/wp_uniffi_bindgen/")
// Re-generate if our uniffi-bindgen version changes.
inputs.file("$cargoProjectRoot/Cargo.lock")
// Re-generate if the module source code changes
inputs.dir("$cargoProjectRoot/$rustModuleName/")
}

tasks.named("compileKotlin").configure {
dependsOn(generateUniFFIBindingsTask)
}

project.afterEvaluate {
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])

groupId = "rs.wordpress.api"
artifactId = "bindings"
// version is set by "publish-to-s3" plugin
}
}
}
}
51 changes: 7 additions & 44 deletions native/kotlin/api/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
plugins {
alias(libs.plugins.rustAndroid)
alias(libs.plugins.kotlinJvm)
alias(libs.plugins.kotlinSerialization)
alias(libs.plugins.publishToS3)
Expand Down Expand Up @@ -70,53 +69,17 @@ dependencies {
implementation(libs.okhttp)
implementation(libs.jna)
implementation(libs.kotlinx.coroutines.core)
}

sourceSets {
main {
java {
srcDir("${layout.buildDirectory.get()}/generated/source/uniffi/java/uniffi")
if (project.hasProperty("wpApiBindingsVersion")) {
api("rs.wordpress.api:bindings:${project.properties["wpApiBindingsVersion"]}") {
exclude(group = "net.java.dev.jna")
}
} else {
api(project(":api:bindings")) {
exclude(group = "net.java.dev.jna")
}
}
}

val generateUniFFIBindingsTask = tasks.register<Exec>("generateUniFFIBindings") {
val cargoProjectRoot = rootProject.ext.get("cargoProjectRoot")
val uniffiGeneratedPath = "${layout.buildDirectory.get()}/generated/source/uniffi/java"
val nativeLibraryPath = rootProject.ext.get("nativeLibraryPath")!!
val rustModuleName = rootProject.ext.get("rustModuleName")

dependsOn(rootProject.tasks.named("cargoBuildLibraryRelease"))
workingDir(project.rootDir)
commandLine(
"cargo",
"run",
"--release",
"--bin",
"wp_uniffi_bindgen",
"generate",
"--library",
nativeLibraryPath,
"--out-dir",
uniffiGeneratedPath,
"--language",
"kotlin"
)
outputs.dir(uniffiGeneratedPath)
// Re-generate if the interface definition changes.
inputs.file(nativeLibraryPath)
// Re-generate if our uniffi-bindgen tooling changes.
inputs.dir("$cargoProjectRoot/wp_uniffi_bindgen/")
// Re-generate if our uniffi-bindgen version changes.
inputs.file("$cargoProjectRoot/Cargo.lock")
// Re-generate if the module source code changes
inputs.dir("$cargoProjectRoot/$rustModuleName/")
}


tasks.named("compileKotlin").configure {
dependsOn(generateUniFFIBindingsTask)
}
tasks.named("processIntegrationTestResources").configure {
dependsOn(rootProject.tasks.named("copyDesktopJniLibs"))
dependsOn(rootProject.tasks.named("copyTestCredentials"))
Expand Down
1 change: 1 addition & 0 deletions native/kotlin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencyResolutionManagement {
}
}

include(":api:bindings")
include(":api:kotlin")
include(":api:android")
include(":example:composeApp")

0 comments on commit 31863f2

Please sign in to comment.