Cotton code
101 lines · 4.1 KB · KotlinRaw
import org.jetbrains.kotlin.gradle.dsl.JvmTargetimport org.gradle.api.tasks.PathSensitivityimport org.gradle.api.tasks.testing.Testplugins {    kotlin("jvm")    kotlin("plugin.serialization")}val repoRoot = rootProject.projectDir.parentFile.parentFile// The phone's routes are the core's: crates/cotton-ffi/src/phone/routes.rs declares// ROUTES once, and Route.patterns is generated from it on every build, never typed.val routesSource = repoRoot.resolve("crates/cotton-ffi/src/phone/routes.rs")val coreRoutesDir = layout.buildDirectory.dir("generated/coreRoutes/kotlin")val generateCoreRoutes = tasks.register("generateCoreRoutes") {    inputs.file(routesSource).withPathSensitivity(PathSensitivity.NONE)    outputs.dir(coreRoutesDir)    doLast {        val body = routesSource.readText().substringAfter("pub const ROUTES").substringAfter("[").substringBefore("];")        val routes = Regex("\"(/[^\"]*)\"").findAll(body).map { it.groupValues[1] }.toList()        check(routes.isNotEmpty()) { "ROUTES was empty or moved in $routesSource" }        val out = coreRoutesDir.get().asFile.resolve("com/cottondb/android/navigation/CoreRoutes.kt")        out.parentFile.mkdirs()        out.writeText(buildString {            appendLine("// Generated from crates/cotton-ffi/src/phone/routes.rs by :core:generateCoreRoutes.")            appendLine("package com.cottondb.android.navigation")            appendLine()            appendLine("internal val CORE_ROUTES: List<String> = listOf(")            routes.forEach { appendLine("    \"$it\",") }            appendLine(")")        })    }}kotlin.sourceSets["main"].kotlin.srcDir(generateCoreRoutes)// The shell kit's legal lines, packed as `shell/lines/legal.json` for LegalDocuments.val shellLegal = tasks.register<Sync>("shellLegalLines") {    from(repoRoot.resolve("crates/cotton-core/kits/shell/lines/legal.json"))    into(layout.buildDirectory.dir("generated/shellLines/shell/lines"))}sourceSets["main"].resources.srcDir(files(layout.buildDirectory.dir("generated/shellLines")).builtBy(shellLegal))tasks.withType<Test>().configureEach {    systemProperty("cotton.repoRoot", rootProject.projectDir.parentFile.parentFile.absolutePath)}tasks.named<Test>("test") {    exclude("**/NativeSmokeTest.class")}val nativeLibrary = providers.gradleProperty("cotton.nativeLibraryDir").map { directory ->    file(directory).resolve("libcotton_android_jni.so")}tasks.register<Test>("nativeSmoke") {    description = "Runs real JNI against a temporary client container on the host JVM."    group = "verification"    testClassesDirs = sourceSets["test"].output.classesDirs    classpath = sourceSets["test"].runtimeClasspath    include("**/NativeSmokeTest.class")    systemProperty("cotton.nativeSmoke", "true")    // Keep the task out of date when the host JNI binary changes, while leaving the    // ordinary JVM test task independent of an optional local native build. Missing    // properties and files remain explicit errors below instead of Gradle validation noise.    inputs.files(nativeLibrary.map { library ->        if (library.isFile) listOf(library) else emptyList()    })        .withPropertyName("nativeLibrary")        .withPathSensitivity(PathSensitivity.NONE)    doFirst {        val library = nativeLibrary.orNull            ?: error("Build scripts/build-android-core.sh --host, then pass -Pcotton.nativeLibraryDir=<directory>")        check(library.isFile) {            "No host JNI library exists in ${library.parent}"        }        systemProperty("java.library.path", library.parent)    }}kotlin {    jvmToolchain(21)    compilerOptions {        jvmTarget.set(JvmTarget.JVM_17)    }}java {    sourceCompatibility = JavaVersion.VERSION_17    targetCompatibility = JavaVersion.VERSION_17}dependencies {    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1")    testImplementation(kotlin("test"))    testImplementation("junit:junit:4.13.2")    testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2")}