Skip to content

Commit

Permalink
Migrate to sweet-spi
Browse files Browse the repository at this point in the history
  • Loading branch information
whyoleg committed Jan 19, 2025
1 parent 186a420 commit 4c9c8a8
Show file tree
Hide file tree
Showing 21 changed files with 54 additions and 122 deletions.
2 changes: 2 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ dependencies {
implementation(libs.kotlinx.kover.gradle.plugin)
implementation(libs.android.gradle.plugin)
implementation(libs.maven.publish.gradle.plugin)
implementation(libs.ksp.gradle.plugin)
implementation(libs.sweetspi.gradle.plugin)
implementation(libs.apache.commons.compress)
implementation("testtool:plugin")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2023-2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

import dev.whyoleg.sweetspi.gradle.*

plugins {
kotlin("multiplatform")
id("com.google.devtools.ksp")
id("dev.whyoleg.sweetspi")
}

kotlin {
withSweetSpi()
}
11 changes: 0 additions & 11 deletions cryptography-core/api/cryptography-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,9 @@ public final class dev/whyoleg/cryptography/CryptographyProvider$Companion {
public final fun getDefault ()Ldev/whyoleg/cryptography/CryptographyProvider;
}

public final class dev/whyoleg/cryptography/CryptographyProvider$Registry {
public static final field INSTANCE Ldev/whyoleg/cryptography/CryptographyProvider$Registry;
public final fun getRegisteredProviders ()Lkotlin/sequences/Sequence;
public final fun registerProvider (Ldev/whyoleg/cryptography/CryptographyProvider;)V
public final fun registerProvider (Lkotlin/Lazy;)V
}

public abstract interface annotation class dev/whyoleg/cryptography/CryptographyProviderApi : java/lang/annotation/Annotation {
}

public abstract interface class dev/whyoleg/cryptography/CryptographyProviderContainer {
public abstract fun getProvider ()Lkotlin/Lazy;
}

public abstract interface annotation class dev/whyoleg/cryptography/DelicateCryptographyApi : java/lang/annotation/Annotation {
}

Expand Down
12 changes: 4 additions & 8 deletions cryptography-core/api/cryptography-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -735,14 +735,6 @@ abstract class dev.whyoleg.cryptography/CryptographyProvider { // dev.whyoleg.cr
final val Default // dev.whyoleg.cryptography/CryptographyProvider.Companion.Default|{}Default[0]
final fun <get-Default>(): dev.whyoleg.cryptography/CryptographyProvider // dev.whyoleg.cryptography/CryptographyProvider.Companion.Default.<get-Default>|<get-Default>(){}[0]
}

final object Registry { // dev.whyoleg.cryptography/CryptographyProvider.Registry|null[0]
final val registeredProviders // dev.whyoleg.cryptography/CryptographyProvider.Registry.registeredProviders|{}registeredProviders[0]
final fun <get-registeredProviders>(): kotlin.sequences/Sequence<dev.whyoleg.cryptography/CryptographyProvider> // dev.whyoleg.cryptography/CryptographyProvider.Registry.registeredProviders.<get-registeredProviders>|<get-registeredProviders>(){}[0]

final fun registerProvider(dev.whyoleg.cryptography/CryptographyProvider) // dev.whyoleg.cryptography/CryptographyProvider.Registry.registerProvider|registerProvider(dev.whyoleg.cryptography.CryptographyProvider){}[0]
final fun registerProvider(kotlin/Lazy<dev.whyoleg.cryptography/CryptographyProvider>) // dev.whyoleg.cryptography/CryptographyProvider.Registry.registerProvider|registerProvider(kotlin.Lazy<dev.whyoleg.cryptography.CryptographyProvider>){}[0]
}
}

final class dev.whyoleg.cryptography/CryptographyAlgorithmNotFoundException : dev.whyoleg.cryptography/CryptographyException { // dev.whyoleg.cryptography/CryptographyAlgorithmNotFoundException|null[0]
Expand Down Expand Up @@ -820,3 +812,7 @@ final object dev.whyoleg.cryptography.algorithms/SHA3_384 : dev.whyoleg.cryptogr
final object dev.whyoleg.cryptography.algorithms/SHA3_512 : dev.whyoleg.cryptography/CryptographyAlgorithmId<dev.whyoleg.cryptography.algorithms/Digest> // dev.whyoleg.cryptography.algorithms/SHA3_512|null[0]

final object dev.whyoleg.cryptography.algorithms/SHA512 : dev.whyoleg.cryptography/CryptographyAlgorithmId<dev.whyoleg.cryptography.algorithms/Digest> // dev.whyoleg.cryptography.algorithms/SHA512|null[0]

// Targets: [js]
final val dev.whyoleg.cryptography/init_dev_whyoleg_cryptography // dev.whyoleg.cryptography/init_dev_whyoleg_cryptography|{}init_dev_whyoleg_cryptography[0]
final fun <get-init_dev_whyoleg_cryptography>(): dynamic // dev.whyoleg.cryptography/init_dev_whyoleg_cryptography.<get-init_dev_whyoleg_cryptography>|<get-init_dev_whyoleg_cryptography>(){}[0]
14 changes: 1 addition & 13 deletions cryptography-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*/

import ckbuild.*
import org.jetbrains.kotlin.gradle.*

plugins {
id("ckbuild.multiplatform-library")
id("ckbuild.multiplatform-sweetspi")
}

description = "cryptography-kotlin core API"
Expand All @@ -17,18 +17,6 @@ kotlin {
nativeTargets()
wasmTargets()

@OptIn(ExperimentalKotlinGradlePluginApi::class)
applyDefaultHierarchyTemplate {
common {
group("nonJvm") {
withJs()
withWasmJs()
withWasmWasi()
group("native")
}
}
}

sourceSets.commonMain.dependencies {
api(projects.cryptographyBigint)
api(projects.cryptographyRandom)
Expand Down
24 changes: 4 additions & 20 deletions cryptography-core/src/commonMain/kotlin/CryptographyProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

package dev.whyoleg.cryptography

import dev.whyoleg.sweetspi.*

@Service
@SubclassOptInRequired(CryptographyProviderApi::class)
public abstract class CryptographyProvider {
public abstract val name: String
Expand All @@ -12,30 +15,11 @@ public abstract class CryptographyProvider {
public open fun <A : CryptographyAlgorithm> get(identifier: CryptographyAlgorithmId<A>): A =
getOrNull(identifier) ?: throw IllegalStateException("Algorithm not found: $identifier")

@CryptographyProviderApi
public object Registry {
private val providers = initProviders().toMutableList()
public val registeredProviders: Sequence<CryptographyProvider>
get() = providers.toList().asSequence().map(Lazy<CryptographyProvider>::value)

public fun registerProvider(provider: CryptographyProvider) {
providers.add(lazyOf(provider))
}

public fun registerProvider(provider: Lazy<CryptographyProvider>) {
providers.add(provider)
}
}

public companion object {
public val Default: CryptographyProvider by lazy {
@OptIn(CryptographyProviderApi::class)
checkNotNull(Registry.registeredProviders.firstOrNull()) {
checkNotNull(ServiceLoader.load<CryptographyProvider>().firstOrNull()) {
"No providers registered. Please provide a dependency or register provider explicitly"
}
}
}
}

// used only on JVM for ServiceLoader
internal expect fun initProviders(): List<Lazy<CryptographyProvider>>
20 changes: 0 additions & 20 deletions cryptography-core/src/jvmMain/kotlin/CryptographyProvider.jvm.kt

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions cryptography-providers/apple/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.gradle.*

plugins {
id("ckbuild.multiplatform-library")
id("ckbuild.multiplatform-sweetspi")
id("ckbuild.multiplatform-provider-tests")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ package dev.whyoleg.cryptography.providers.apple
import dev.whyoleg.cryptography.*
import dev.whyoleg.cryptography.algorithms.*
import dev.whyoleg.cryptography.providers.apple.algorithms.*
import dev.whyoleg.sweetspi.*

private val defaultProvider = lazy { AppleCryptographyProvider }
@ServiceProvider
internal val defaultProvider by lazy { AppleCryptographyProvider }

public val CryptographyProvider.Companion.Apple: CryptographyProvider by defaultProvider
public val CryptographyProvider.Companion.Apple: CryptographyProvider get() = defaultProvider

internal object AppleCryptographyProvider : CryptographyProvider() {
override val name: String get() = "Apple"
Expand All @@ -37,8 +39,3 @@ internal object AppleCryptographyProvider : CryptographyProvider() {
else -> null
} as A?
}

@Suppress("DEPRECATION")
@OptIn(ExperimentalStdlibApi::class)
@EagerInitialization
private val initHook = CryptographyProvider.Registry.registerProvider(defaultProvider)
1 change: 1 addition & 0 deletions cryptography-providers/jdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.gradle.*

plugins {
id("ckbuild.multiplatform-library")
id("ckbuild.multiplatform-sweetspi")
id("ckbuild.multiplatform-provider-tests")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import dev.whyoleg.cryptography.*
import dev.whyoleg.cryptography.algorithms.*
import dev.whyoleg.cryptography.providers.jdk.algorithms.*
import dev.whyoleg.cryptography.random.*
import dev.whyoleg.sweetspi.*
import java.security.*
import java.util.concurrent.*

private val defaultProvider = lazy { CryptographyProvider.Companion.JDK() }
@ServiceProvider
internal val defaultProvider by lazy { CryptographyProvider.Companion.JDK() }

public val CryptographyProvider.Companion.JDK: CryptographyProvider by defaultProvider
public val CryptographyProvider.Companion.JDK: CryptographyProvider get() = defaultProvider

@Suppress("FunctionName")
public fun CryptographyProvider.Companion.JDK(
Expand Down Expand Up @@ -95,7 +97,3 @@ internal class JdkCryptographyProvider(
}
} as A?
}

internal class JdkCryptographyProviderContainer : CryptographyProviderContainer {
override val provider: Lazy<CryptographyProvider> get() = defaultProvider
}

This file was deleted.

1 change: 1 addition & 0 deletions cryptography-providers/openssl3/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.gradle.tasks.*

plugins {
id("ckbuild.multiplatform-library")
id("ckbuild.multiplatform-sweetspi")
id("ckbuild.use-openssl")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import dev.whyoleg.cryptography.algorithms.*
import dev.whyoleg.cryptography.algorithms.RSA
import dev.whyoleg.cryptography.providers.openssl3.algorithms.*
import dev.whyoleg.cryptography.providers.openssl3.internal.cinterop.*
import dev.whyoleg.sweetspi.*
import kotlinx.cinterop.*

private val defaultProvider = lazy { Openssl3CryptographyProvider }
@ServiceProvider
internal val defaultProvider by lazy { Openssl3CryptographyProvider }

public val CryptographyProvider.Companion.Openssl3: CryptographyProvider by defaultProvider
public val CryptographyProvider.Companion.Openssl3: CryptographyProvider get() = defaultProvider

internal object Openssl3CryptographyProvider : CryptographyProvider() {
override val name: String = "OpenSSL3 (${OpenSSL_version(OPENSSL_VERSION_STRING)?.toKString() ?: "unknown"})"
Expand Down Expand Up @@ -47,8 +49,3 @@ internal object Openssl3CryptographyProvider : CryptographyProvider() {
else -> null
} as A?
}

@Suppress("DEPRECATION")
@OptIn(ExperimentalStdlibApi::class)
@EagerInitialization
private val initHook = CryptographyProvider.Registry.registerProvider(defaultProvider)
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ final val dev.whyoleg.cryptography.providers.webcrypto/WebCrypto // dev.whyoleg.
final fun (dev.whyoleg.cryptography/CryptographyProvider.Companion).<get-WebCrypto>(): dev.whyoleg.cryptography/CryptographyProvider // dev.whyoleg.cryptography.providers.webcrypto/WebCrypto.<get-WebCrypto>|<get-WebCrypto>@dev.whyoleg.cryptography.CryptographyProvider.Companion(){}[0]

// Targets: [js]
final val dev.whyoleg.cryptography.providers.webcrypto/initHook // dev.whyoleg.cryptography.providers.webcrypto/initHook|{}initHook[0]
final fun <get-initHook>(): dynamic // dev.whyoleg.cryptography.providers.webcrypto/initHook.<get-initHook>|<get-initHook>(){}[0]
final val dev.whyoleg.cryptography.providers.webcrypto/init_dev_whyoleg_cryptography_providers_webcrypto // dev.whyoleg.cryptography.providers.webcrypto/init_dev_whyoleg_cryptography_providers_webcrypto|{}init_dev_whyoleg_cryptography_providers_webcrypto[0]
final fun <get-init_dev_whyoleg_cryptography_providers_webcrypto>(): dynamic // dev.whyoleg.cryptography.providers.webcrypto/init_dev_whyoleg_cryptography_providers_webcrypto.<get-init_dev_whyoleg_cryptography_providers_webcrypto>|<get-init_dev_whyoleg_cryptography_providers_webcrypto>(){}[0]
1 change: 1 addition & 0 deletions cryptography-providers/webcrypto/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.gradle.*

plugins {
id("ckbuild.multiplatform-library")
id("ckbuild.multiplatform-sweetspi")
id("ckbuild.multiplatform-provider-tests")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ package dev.whyoleg.cryptography.providers.webcrypto
import dev.whyoleg.cryptography.*
import dev.whyoleg.cryptography.algorithms.*
import dev.whyoleg.cryptography.providers.webcrypto.algorithms.*
import dev.whyoleg.sweetspi.*

internal val defaultProvider = lazy { WebCryptoCryptographyProvider }
@ServiceProvider
internal val defaultProvider by lazy { WebCryptoCryptographyProvider }

public val CryptographyProvider.Companion.WebCrypto: CryptographyProvider by defaultProvider
public val CryptographyProvider.Companion.WebCrypto: CryptographyProvider get() = defaultProvider

internal object WebCryptoCryptographyProvider : CryptographyProvider() {
override val name: String get() = "WebCrypto"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,3 @@
*/

package dev.whyoleg.cryptography.providers.webcrypto

import dev.whyoleg.cryptography.*

// declaration should be public for EagerInitialization to work
// Deprecated to make it `internal`ish
@Suppress("DEPRECATION")
@OptIn(ExperimentalStdlibApi::class, ExperimentalJsExport::class)
@EagerInitialization
@JsExport
@Deprecated("", level = DeprecationLevel.HIDDEN)
public val initHook: dynamic = CryptographyProvider.Registry.registerProvider(defaultProvider)
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,3 @@
*/

package dev.whyoleg.cryptography.providers.webcrypto

import dev.whyoleg.cryptography.*

@Suppress("DEPRECATION")
@OptIn(ExperimentalStdlibApi::class)
@EagerInitialization
internal val initHook = CryptographyProvider.Registry.registerProvider(defaultProvider)
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[versions]

kotlin = "2.0.20"
ksp = "2.0.20-1.0.24"
sweetspi = "0.1.2"

# kotlin/kotlinx
kotlin-dokka = "1.9.20"
Expand Down Expand Up @@ -30,6 +32,8 @@ kotlinx-bcv-gradle-plugin = { module = "org.jetbrains.kotlinx:binary-compatibili
kotlinx-kover-gradle-plugin = { module = "org.jetbrains.kotlinx:kover-gradle-plugin", version.ref = "kotlinx-kover" }
android-gradle-plugin = { module = "com.android.tools.build:gradle", version.ref = "android" }
maven-publish-gradle-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "maven-publish" }
ksp-gradle-plugin = { module = "com.google.devtools.ksp:symbol-processing-gradle-plugin", version.ref = "ksp" }
sweetspi-gradle-plugin = { module = "dev.whyoleg.sweetspi:sweetspi-gradle-plugin", version.ref = "sweetspi" }

apache-commons-compress = { module = "org.apache.commons:commons-compress", version.ref = "apache-commons-compress" }

Expand Down Expand Up @@ -63,3 +67,5 @@ kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref
kotlin-plugin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlin-dokka = { id = "org.jetbrains.dokka", version.ref = "kotlin-dokka" }
android-library = { id = "com.android.library", version.ref = "android" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
sweetspi = { id = "dev.whyoleg.sweetspi", version.ref = "sweetspi" }

0 comments on commit 4c9c8a8

Please sign in to comment.