Skip to content

Commit

Permalink
Created examples module.
Browse files Browse the repository at this point in the history
  • Loading branch information
smyrgeorge committed Jul 4, 2024
1 parent 895cc35 commit d4d25fe
Show file tree
Hide file tree
Showing 24 changed files with 788 additions and 173 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ $RECYCLE.BIN/
### Gradle ###
.gradle
**/build/
!src/**/build/

# Ignore Gradle GUI config
gradle-app.setting
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ docker compose up -d
Then run the `main` method.

```shell
./build/bin/macosArm64/releaseExecutable/sqlx4k.kexe
./examples/build/bin/macosArm64/releaseExecutable/examples.kexe
```

## Examples

See `Main.kt` file for more examples.
See `Main.kt` file for more examples (examples module).

```kotlin
// Initialize the connection pool.
Expand Down Expand Up @@ -173,13 +173,13 @@ codesign -s - -v -f --entitlements =(echo -n '<?xml version="1.0" encoding="UTF-
<key>com.apple.security.get-task-allow</key>
<true/>
</dict>
</plist>') ./build/bin/macosArm64/releaseExecutable/sqlx4k.kexe
</plist>') ./examples/build/bin/macosArm64/releaseExecutable/examples.kexe
```

Then run the tool:

```shell
leaks -atExit -- ./build/bin/macosArm64/releaseExecutable/sqlx4k.kexe
leaks -atExit -- ./examples/build/bin/macosArm64/releaseExecutable/examples.kexe
```

Sample output:
Expand Down
146 changes: 3 additions & 143 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,151 +1,11 @@
import com.vanniktech.maven.publish.SonatypeHost
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import java.lang.System.getenv
group = "io.github.smyrgeorge"
version = "0.3.0"

plugins {
// https://plugins.gradle.org/plugin/org.jetbrains.kotlin.multiplatform
kotlin("multiplatform") version "2.0.0"
// https://github.com/vanniktech/gradle-maven-publish-plugin
id("com.vanniktech.maven.publish") version "0.28.0"
kotlin("multiplatform") version "2.0.0" apply false
}

group = "io.github.smyrgeorge"
version = "0.2.0"

repositories {
mavenCentral()
}

private val os = DefaultNativePlatform.getCurrentOperatingSystem()
private val arch = DefaultNativePlatform.getCurrentArchitecture()

fun projectFile(path: String): String =
projectDir.resolve(path).absolutePath

private val exeExt: String
get() = when {
os.isWindows -> ".exe"
else -> ""
}

private val cargo: String
get() = when {
os.isWindows -> getenv("USERPROFILE")
else -> getenv("HOME")
}?.let(::File)
?.resolve(".cargo/bin/cargo$exeExt")
?.takeIf { it.exists() }
?.absolutePath
?: throw GradleException("Rust cargo binary is required to build project but it wasn't found.")

val chosenTargets = (properties["targets"] as? String)?.split(",")
?: listOf("macosArm64", "macosX64", "linuxArm64", "linuxX64")

kotlin {
fun KotlinNativeTarget.rust(target: String) {
compilations.getByName("main").cinterops {
create("librust_lib") {
val cargo = tasks.create("cargo-$target") {
exec {
executable = cargo
args(
"build",
"--manifest-path", projectFile("rust_lib/Cargo.toml"),
"--package", "rust_lib",
"--lib",
"--target=$target",
"--release"
)
}
}

tasks.getByName(interopProcessingTaskName) {
dependsOn(cargo)
}

definitionFile.set(projectDir.resolve("src/nativeInterop/cinterop/$target.def"))
}
}

// TODO: remove when we start building proper tests.
binaries.executable {
entryPoint = "main"
baseName = "sqlx4k"
linkerOpts += projectFile(
path = when {
os.isWindows -> "rust_lib/target/$target/release/rust_lib.lib"
else -> "rust_lib/target/$target/release/librust_lib.a"
}
)
}

}

val availableTargets = mapOf(
Pair("macosArm64") { macosArm64 { rust("aarch64-apple-darwin") } },
Pair("macosX64") { macosX64 { rust("x86_64-apple-darwin") } },
Pair("linuxArm64") { linuxArm64 { rust("aarch64-unknown-linux-gnu") } },
Pair("linuxX64") { linuxX64 { rust("x86_64-unknown-linux-gnu") } },
)
chosenTargets.forEach {
println("Enabling target $it")
availableTargets[it]?.invoke()
}

applyDefaultHierarchyTemplate()

sourceSets {
configureEach {
languageSettings.progressiveMode = true
}
val nativeMain by getting {
dependencies {
// https://github.com/Kotlin/kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
}
}
}
}

mavenPublishing {
coordinates(
groupId = group as String,
artifactId = name,
version = version as String
)

pom {
name = "sqlx4k"
description = "A small non-blocking database driver written in Kotlin for the Native platform."
url = "https://github.com/smyrgeorge/sqlx4k"

licenses {
license {
name = "MIT License"
url = "https://github.com/smyrgeorge/sqlx4k/blob/main/LICENSE"
}
}

developers {
developer {
id = "smyrgeorge"
name = "Yorgos S."
email = "[email protected]"
url = "https://smyrgeorge.github.io/"
}
}

scm {
url = "https://github.com/smyrgeorge/sqlx4k"
connection = "scm:git:https://github.com/smyrgeorge/sqlx4k.git"
developerConnection = "scm:git:[email protected]:smyrgeorge/sqlx4k.git"
}
}

// Configure publishing to Maven Central
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

// Enable GPG signing for all publications
signAllPublications()
}
28 changes: 28 additions & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
kotlin("multiplatform")
}

repositories {
mavenCentral()
}

group = rootProject.group
version = rootProject.version

kotlin {
macosArm64 { binaries { executable() } }
macosX64 { binaries { executable() } }
linuxArm64 { binaries { executable() } }
linuxX64 { binaries { executable() } }

applyDefaultHierarchyTemplate()
sourceSets {
val nativeMain by getting {
dependencies {
implementation(project(":sqlx4k"))
// // https://github.com/Kotlin/kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.time.measureTime

@Suppress("unused")
fun main() {
runBlocking {
suspend fun <A, B> Iterable<A>.mapParallel(
context: CoroutineContext = Dispatchers.IO,
f: suspend (A) -> B
): List<B> = withContext(context) { map { async { f(it) } }.awaitAll() }
// @Suppress("unused")
// suspend fun <A, B> Iterable<A>.mapParallel(
// context: CoroutineContext = Dispatchers.IO,
// f: suspend (A) -> B
// ): List<B> = withContext(context) { map { async { f(it) } }.awaitAll() }

suspend fun <A> Iterable<A>.forEachParallel(
context: CoroutineContext = Dispatchers.IO,
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
rootProject.name = "sqlx4k"

include("sqlx4k")
include("examples")
133 changes: 133 additions & 0 deletions sqlx4k/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import com.vanniktech.maven.publish.SonatypeHost
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import java.lang.System.getenv

plugins {
kotlin("multiplatform")
// https://github.com/vanniktech/gradle-maven-publish-plugin
id("com.vanniktech.maven.publish") version "0.28.0"
}

repositories {
mavenCentral()
}

group = rootProject.group
version = rootProject.version

private val os = DefaultNativePlatform.getCurrentOperatingSystem()
private val arch = DefaultNativePlatform.getCurrentArchitecture()

private val exeExt: String
get() = when {
os.isWindows -> ".exe"
else -> ""
}

private val cargo: String
get() = when {
os.isWindows -> getenv("USERPROFILE")
else -> getenv("HOME")
}?.let(::File)
?.resolve(".cargo/bin/cargo$exeExt")
?.takeIf { it.exists() }
?.absolutePath
?: throw GradleException("Rust cargo binary is required to build project but it wasn't found.")

val chosenTargets = (properties["targets"] as? String)?.split(",")
?: listOf("macosArm64", "macosX64", "linuxArm64", "linuxX64")

kotlin {
fun KotlinNativeTarget.rust(target: String) {
compilations.getByName("main").cinterops {
create("librust_lib") {
val cargo = tasks.create("cargo-$target") {
exec {
executable = cargo
args(
"build",
"--manifest-path", projectDir.resolve("rust_lib/Cargo.toml").absolutePath,
"--package", "rust_lib",
"--lib",
"--target=$target",
"--release"
)
}
}

tasks.getByName(interopProcessingTaskName) {
dependsOn(cargo)
}

definitionFile.set(projectDir.resolve("src/nativeInterop/cinterop/$target.def"))
}
}
}

val availableTargets = mapOf(
Pair("macosArm64") { macosArm64 { rust("aarch64-apple-darwin") } },
Pair("macosX64") { macosX64 { rust("x86_64-apple-darwin") } },
Pair("linuxArm64") { linuxArm64 { rust("aarch64-unknown-linux-gnu") } },
Pair("linuxX64") { linuxX64 { rust("x86_64-unknown-linux-gnu") } },
)
chosenTargets.forEach {
println("Enabling target $it")
availableTargets[it]?.invoke()
}

applyDefaultHierarchyTemplate()
sourceSets {
configureEach {
languageSettings.progressiveMode = true
}
val nativeMain by getting {
dependencies {
// https://github.com/Kotlin/kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
}
}
}
}

mavenPublishing {
coordinates(
groupId = group as String,
artifactId = name,
version = version as String
)

pom {
name = "sqlx4k"
description = "A small non-blocking database driver written in Kotlin for the Native platform."
url = "https://github.com/smyrgeorge/sqlx4k"

licenses {
license {
name = "MIT License"
url = "https://github.com/smyrgeorge/sqlx4k/blob/main/LICENSE"
}
}

developers {
developer {
id = "smyrgeorge"
name = "Yorgos S."
email = "[email protected]"
url = "https://smyrgeorge.github.io/"
}
}

scm {
url = "https://github.com/smyrgeorge/sqlx4k"
connection = "scm:git:https://github.com/smyrgeorge/sqlx4k.git"
developerConnection = "scm:git:[email protected]:smyrgeorge/sqlx4k.git"
}
}

// Configure publishing to Maven Central
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

// Enable GPG signing for all publications
signAllPublications()
}
File renamed without changes.
Loading

0 comments on commit d4d25fe

Please sign in to comment.