Skip to content

Commit 6d321a9

Browse files
DominicGBauerDominicGBauer
and
DominicGBauer
authored
chore: add ktlint for linting and formatting (#75)
* chore: add klint for linting and formatting * docs: update README --------- Co-authored-by: DominicGBauer <[email protected]>
1 parent 2b35be4 commit 6d321a9

File tree

75 files changed

+1266
-1056
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1266
-1056
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*.{kt,kts}]
2+
ktlint_code_style = ktlint_official
3+
ktlint_standard_property-naming = disabled

PowerSync/build.gradle.kts

+39-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import co.touchlab.faktory.artifactmanager.ArtifactManager
22
import co.touchlab.faktory.capitalized
3-
import co.touchlab.skie.configuration.SuspendInterop
43
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
54
import java.net.URL
65
import java.security.MessageDigest
@@ -10,14 +9,15 @@ plugins {
109
alias(libs.plugins.kmmbridge)
1110
alias(libs.plugins.skie)
1211
alias(libs.plugins.mavenPublishPlugin)
12+
alias(libs.plugins.kotlinter)
1313
id("com.powersync.plugins.sonatype")
1414
}
1515

1616
kotlin {
1717
listOf(
1818
iosX64(),
1919
iosArm64(),
20-
iosSimulatorArm64()
20+
iosSimulatorArm64(),
2121
).forEach {
2222
it.binaries.framework {
2323
export(project(":core"))
@@ -40,16 +40,6 @@ kotlin {
4040
}
4141
}
4242

43-
skie {
44-
features {
45-
group {
46-
// We turn this off as the suspend interop feature results in
47-
// threading issues when implementing SDK in Swift
48-
SuspendInterop.Enabled(false)
49-
}
50-
}
51-
}
52-
5343
kmmbridge {
5444
artifactManager.set(SonatypePortalPublishArtifactManager(project, repositoryName = null))
5545
artifactManager.finalizeValue()
@@ -70,19 +60,22 @@ class SonatypePortalPublishArtifactManager(
7060
val project: Project,
7161
private val publicationName: String = "KMMBridgeFramework",
7262
artifactSuffix: String = "kmmbridge",
73-
private val repositoryName: String?,
63+
private val repositoryName: String?
7464
) : ArtifactManager {
7565
private val group: String = project.group.toString().replace(".", "/")
7666
private val kmmbridgeArtifactId =
77-
"${project.name}-${artifactSuffix}"
67+
"${project.name}-$artifactSuffix"
7868
private val LIBRARY_VERSION: String by project
69+
7970
// This is the URL that will be added to Package.swift in Github package so that
8071
// KMMBridge is downloaded when a user includes the package in XCode
8172
private val MAVEN_CENTRAL_PACKAGE_ZIP_URL = "https://repo1.maven.org/maven2/com/powersync/${kmmbridgeArtifactId.lowercase()}/${LIBRARY_VERSION}/${kmmbridgeArtifactId.lowercase()}-${LIBRARY_VERSION}.zip"
8273

83-
override fun deployArtifact(project: Project, zipFilePath: File, version: String): String {
84-
return MAVEN_CENTRAL_PACKAGE_ZIP_URL
85-
}
74+
override fun deployArtifact(
75+
project: Project,
76+
zipFilePath: File,
77+
version: String
78+
): String = MAVEN_CENTRAL_PACKAGE_ZIP_URL
8679

8780
override fun configure(
8881
project: Project,
@@ -91,12 +84,14 @@ class SonatypePortalPublishArtifactManager(
9184
kmmPublishTask: TaskProvider<Task>
9285
) {
9386
project.extensions.getByType<PublishingExtension>().publications.create(
94-
publicationName, MavenPublication::class.java
87+
publicationName,
88+
MavenPublication::class.java,
9589
) {
9690
this.version = version
97-
val archiveProvider = project.tasks.named("zipXCFramework", Zip::class.java).flatMap {
98-
it.archiveFile
99-
}
91+
val archiveProvider =
92+
project.tasks.named("zipXCFramework", Zip::class.java).flatMap {
93+
it.archiveFile
94+
}
10095
artifact(archiveProvider) {
10196
extension = "zip"
10297
}
@@ -136,11 +131,14 @@ class SonatypePortalPublishArtifactManager(
136131

137132
// Either the user has supplied a correct name, or we use the default. If neither is found, fail.
138133
val publicationNameCap =
139-
publishingExtension.publications.getByName(
140-
publicationName
141-
).name.capitalized()
142-
143-
return publishingExtension.repositories.filterIsInstance<MavenArtifactRepository>()
134+
publishingExtension.publications
135+
.getByName(
136+
publicationName,
137+
).name
138+
.capitalized()
139+
140+
return publishingExtension.repositories
141+
.filterIsInstance<MavenArtifactRepository>()
144142
.map { repo ->
145143
val repositoryName = repo.name.capitalized()
146144
val publishTaskName =
@@ -175,26 +173,27 @@ abstract class UpdatePackageSwiftChecksumTask : DefaultTask() {
175173
}
176174

177175
// Compute the checksum
178-
val checksum = zipFile.inputStream().use { input ->
179-
val digest = MessageDigest.getInstance("SHA-256")
180-
val buffer = ByteArray(8192)
181-
var bytes = input.read(buffer)
182-
while (bytes >= 0) {
183-
digest.update(buffer, 0, bytes)
184-
bytes = input.read(buffer)
176+
val checksum =
177+
zipFile.inputStream().use { input ->
178+
val digest = MessageDigest.getInstance("SHA-256")
179+
val buffer = ByteArray(8192)
180+
var bytes = input.read(buffer)
181+
while (bytes >= 0) {
182+
digest.update(buffer, 0, bytes)
183+
bytes = input.read(buffer)
184+
}
185+
digest.digest().joinToString("") { "%02x".format(it) }
185186
}
186-
digest.digest().joinToString("") { "%02x".format(it) }
187-
}
188187

189188
// Update Package.swift
190189
val packageSwiftFile = project.rootProject.file("Package.swift")
191-
val updatedContent = packageSwiftFile.readText().replace(
192-
Regex("let remoteKotlinChecksum = \"[a-f0-9]+\""),
193-
"let remoteKotlinChecksum = \"$checksum\""
194-
)
190+
val updatedContent =
191+
packageSwiftFile.readText().replace(
192+
Regex("let remoteKotlinChecksum = \"[a-f0-9]+\""),
193+
"let remoteKotlinChecksum = \"$checksum\"",
194+
)
195195
packageSwiftFile.writeText(updatedContent)
196196

197197
println("Updated Package.swift with new checksum: $checksum")
198198
}
199199
}
200-
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
@file:Suppress("ktlint:standard:no-empty-file")
2+
13
// This is required to build the iOS framework
2-
package com.powersync
4+
5+
package com.powersync

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Demo applications are located in the [`demos/`](./demos) directory. See their re
3737
- [demos/hello-powersync](./demos/hello-powersync/README.md): A minimal example demonstrating the use of the PowerSync Kotlin Multiplatform SDK and the Supabase connector.
3838

3939
- [demos/supabase-todolist](./demos/supabase-todolist/README.md): A simple to-do list application demonstrating the use of the PowerSync Kotlin Multiplatform SDK and the Supabase connector.
40-
-
4140
- [demos/android-supabase-todolist](./demos/android-supabase-todolist/README.md): A simple to-do list application demonstrating the use of the PowerSync Kotlin Multiplatform SDK and the Supabase connector in an Android application.
4241

4342
## Current Limitations / Future work
@@ -100,6 +99,11 @@ cocoapods {
10099

101100
Note: The `linkOnly` attribute is set to `true` and framework is set to `isStatic = true` to ensure that the `powersync-sqlite-core` binaries are only statically linked.
102101

102+
## Formatting and Linting
103+
104+
This repo uses [ktlint](https://pinterest.github.io/ktlint/) to handle formatting and linting. If you would like the IDE to automatically format your code and show linting errors install the [ktlint plugin](https://plugins.jetbrains.com/plugin/15057-ktlint). Then in Settings go to Tools -> Ktlint -> Select Distract free (recommended) mode.
105+
It will automatically use the rules set in the `.editorconfig` file.
106+
103107
## Getting Started
104108

105109
Our [full SDK reference](https://docs.powersync.com/client-sdk-references/kotlin-multiplatform-alpha#getting-started) contains everything you need to know to get started implementing PowerSync in your project.

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
alias(libs.plugins.grammarKitComposer) apply false
1313
alias(libs.plugins.mavenPublishPlugin) apply false
1414
alias(libs.plugins.downloadPlugin) apply false
15+
alias(libs.plugins.kotlinter) apply false
1516
}
1617

1718
// Having different versions of this lead to the issue mentioned here
@@ -38,7 +39,6 @@ allprojects {
3839
}
3940
}
4041

41-
4242
configurations.configureEach {
4343
exclude(group = "com.jetbrains.rd")
4444
exclude(group = "com.github.jetbrains", module = "jetCheck")

compose/build.gradle.kts

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55
alias(libs.plugins.androidLibrary)
66
alias(libs.plugins.jetbrainsCompose)
77
alias(libs.plugins.compose.compiler)
8+
alias(libs.plugins.kotlinter)
89
id("com.powersync.plugins.sonatype")
910
}
1011

@@ -33,13 +34,19 @@ kotlin {
3334

3435
android {
3536
namespace = "com.powersync.compose"
36-
compileSdk = libs.versions.android.compileSdk.get().toInt()
37+
compileSdk =
38+
libs.versions.android.compileSdk
39+
.get()
40+
.toInt()
3741
defaultConfig {
38-
minSdk = libs.versions.android.minSdk.get().toInt()
42+
minSdk =
43+
libs.versions.android.minSdk
44+
.get()
45+
.toInt()
3946
}
4047
kotlin {
4148
jvmToolchain(17)
4249
}
4350
}
4451

45-
setupGithubRepository()
52+
setupGithubRepository()

compose/src/androidMain/kotlin/com/powersync/compose/DatabaseDriverFactory.compose.android.kt

-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import androidx.compose.runtime.remember
55
import androidx.compose.ui.platform.LocalContext
66
import com.powersync.DatabaseDriverFactory
77

8-
98
@Composable
109
public actual fun rememberDatabaseDriverFactory(): DatabaseDriverFactory {
1110
val context = LocalContext.current
1211
return remember {
1312
DatabaseDriverFactory(context)
1413
}
15-
1614
}

compose/src/iosMain/kotlin/com/powersync/compose/DatabaseDriverFactory.compose.ios.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.remember
55
import com.powersync.DatabaseDriverFactory
66

7-
87
@Composable
9-
public actual fun rememberDatabaseDriverFactory(): DatabaseDriverFactory {
10-
return remember {
8+
public actual fun rememberDatabaseDriverFactory(): DatabaseDriverFactory =
9+
remember {
1110
DatabaseDriverFactory()
1211
}
13-
}

connectors/supabase/build.gradle.kts

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55
alias(libs.plugins.kotlinMultiplatform)
66
alias(libs.plugins.kotlinSerialization)
77
alias(libs.plugins.androidLibrary)
8+
alias(libs.plugins.kotlinter)
89
id("com.powersync.plugins.sonatype")
910
}
1011

@@ -37,13 +38,19 @@ kotlin {
3738

3839
android {
3940
namespace = "com.powersync.connector.supabase"
40-
compileSdk = libs.versions.android.compileSdk.get().toInt()
41+
compileSdk =
42+
libs.versions.android.compileSdk
43+
.get()
44+
.toInt()
4145
defaultConfig {
42-
minSdk = libs.versions.android.minSdk.get().toInt()
46+
minSdk =
47+
libs.versions.android.minSdk
48+
.get()
49+
.toInt()
4350
}
4451
kotlin {
4552
jvmToolchain(17)
4653
}
4754
}
4855

49-
setupGithubRepository()
56+
setupGithubRepository()

connectors/supabase/src/commonMain/kotlin/com/powersync/connector/supabase/SupabaseConnector.kt

+21-18
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,42 @@ import kotlinx.coroutines.flow.StateFlow
2121
*/
2222
public class SupabaseConnector(
2323
public val supabaseClient: SupabaseClient,
24-
public val powerSyncEndpoint: String
24+
public val powerSyncEndpoint: String,
2525
) : PowerSyncBackendConnector() {
26-
2726
public constructor(
2827
supabaseUrl: String,
2928
supabaseKey: String,
30-
powerSyncEndpoint: String
29+
powerSyncEndpoint: String,
3130
) : this(
32-
supabaseClient = createSupabaseClient(supabaseUrl, supabaseKey) {
33-
install(Auth)
34-
install(Postgrest)
35-
},
36-
powerSyncEndpoint = powerSyncEndpoint
31+
supabaseClient =
32+
createSupabaseClient(supabaseUrl, supabaseKey) {
33+
install(Auth)
34+
install(Postgrest)
35+
},
36+
powerSyncEndpoint = powerSyncEndpoint,
3737
)
3838

3939
init {
4040
require(supabaseClient.pluginManager.getPluginOrNull(Auth) != null) { "The Auth plugin must be installed on the Supabase client" }
41-
require(supabaseClient.pluginManager.getPluginOrNull(Postgrest) != null) { "The Postgrest plugin must be installed on the Supabase client" }
41+
require(
42+
supabaseClient.pluginManager.getPluginOrNull(Postgrest) != null,
43+
) { "The Postgrest plugin must be installed on the Supabase client" }
4244
}
4345

44-
public suspend fun login(email: String, password: String) {
46+
public suspend fun login(
47+
email: String,
48+
password: String,
49+
) {
4550
supabaseClient.auth.signInWith(Email) {
4651
this.email = email
4752
this.password = password
4853
}
4954
}
5055

51-
public suspend fun signUp(email: String, password: String) {
56+
public suspend fun signUp(
57+
email: String,
58+
password: String,
59+
) {
5260
supabaseClient.auth.signUpWith(Email) {
5361
this.email = email
5462
this.password = password
@@ -59,9 +67,7 @@ public class SupabaseConnector(
5967
supabaseClient.auth.signOut()
6068
}
6169

62-
public fun session(): UserSession? {
63-
return supabaseClient.auth.currentSessionOrNull()
64-
}
70+
public fun session(): UserSession? = supabaseClient.auth.currentSessionOrNull()
6571

6672
public val sessionStatus: StateFlow<SessionStatus> = supabaseClient.auth.sessionStatus
6773

@@ -84,7 +90,7 @@ public class SupabaseConnector(
8490
return PowerSyncCredentials(
8591
endpoint = powerSyncEndpoint,
8692
token = session.accessToken, // Use the access token to authenticate against PowerSync
87-
userId = session.user!!.id
93+
userId = session.user!!.id,
8894
)
8995
}
9096

@@ -95,12 +101,10 @@ public class SupabaseConnector(
95101
* If this call throws an error, it is retried periodically.
96102
*/
97103
override suspend fun uploadData(database: PowerSyncDatabase) {
98-
99104
val transaction = database.getNextCrudTransaction() ?: return
100105

101106
var lastEntry: CrudEntry? = null
102107
try {
103-
104108
for (entry in transaction.crud) {
105109
lastEntry = entry
106110

@@ -131,7 +135,6 @@ public class SupabaseConnector(
131135
}
132136

133137
transaction.complete(null)
134-
135138
} catch (e: Exception) {
136139
println("Data upload error - retrying last entry: ${lastEntry!!}, $e")
137140
throw e

0 commit comments

Comments
 (0)