Skip to content

Commit

Permalink
CI
Browse files Browse the repository at this point in the history
  • Loading branch information
SalomonBrys committed Nov 22, 2023
1 parent aa0905d commit f4986b3
Show file tree
Hide file tree
Showing 9 changed files with 4,079 additions and 73 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
create-staging-repository:
uses: kosi-libs/kodein-internal-github-actions/create-nexus-staging-repository.yml@main
uses: kosi-libs/kodein-internal-github-actions/.github/workflows/create-nexus-staging-repository.yml@main
secrets: inherit

build-upload:
Expand All @@ -29,15 +29,15 @@ jobs:
drop-or-release-staging-repository:
needs: [create-staging-repository, build-upload]
if: ${{ always() && needs.create-staging-repository.result == 'success' }}
uses: kosi-libs/kodein-internal-github-actions/create-nexus-staging-repository.yml@main
uses: kosi-libs/kodein-internal-github-actions/.github/workflows/drop-or-release-nexus-staging-repository.yml@main
secrets: inherit
with:
repository-id: ${{ needs.create-staging-repository.outputs.repository-id }}
build-upload-result: ${{ needs.build-upload.result }}

gradle-plugin:
runs-on: ubuntu-latest
needs: [build-upload, drop-or-release]
needs: [build-upload, drop-or-release-staging-repository]
steps:
- name: Setup
uses: kosi-libs/kodein-internal-github-actions/setup@main
Expand Down
20 changes: 15 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ Built in collaboration with https://www.deezer.com/[Deezer].
[source,kotlin,subs="verbatim,attributes"]
----
plugins {
kotlin("multiplatform")
id("org.kodein.mock.mockmp") version "{version}"
}
kotlin {
// Your Koltin/Multiplatform configuration
}
mockmp {
usesHelper = true
installWorkaround()
}
----

Expand Down Expand Up @@ -672,11 +678,6 @@ repositories {
mavenCentral()
}
mockmp {
// OPTIONAL!
usesHelper = true //<2>
}
kotlin {
jvm()
ios()
Expand All @@ -692,9 +693,18 @@ kotlin {
}
}
}
mockmp {
// OPTIONAL!
usesHelper = true //<2>
// REQUIRED!
installWorkaround() // <3>
}
----
<1> Applying the MocKMP plugin.
<2> Requesting the *optional* `test-helper` dependency
<3> *Must* be called *after* the `kotlin` configuration and *must be the last line* of the `mockmp` block.

The plugin takes care of:

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +25,89 @@ class MocKMPGradlePlugin : Plugin<Project> {

@Suppress("PropertyName")
class Extension(
private val project: Project,
var usesHelper: Boolean = false,
var throwErrors: Boolean = false,
var public: Boolean = false,
var targetSourceSet: SourceSetTarget = SourceSetTarget.CommonTest_Via_JVM
) {
val CommonMain get() = SourceSetTarget.CommonMain
val CommonTest_Via_JVM get() = SourceSetTarget.CommonTest_Via_JVM
}

private val Project.kotlinExtension get() = project.extensions.findByType<KotlinMultiplatformExtension>() ?: throw GradleException("Could not find Kotlin/Multiplatform plugin")
internal var workaroundInstalled = false

private fun addKSPDependency(project: Project, kspConfiguration: String) {
project.dependencies {
kspConfiguration("org.kodein.mock:mockmp-processor:${BuildConfig.VERSION}")
fun installWorkaround() {
execute(project, this)
workaroundInstalled = true
}
}

private fun addRuntimeDependencies(project: Project, sourceSet: KotlinSourceSet, ext: Extension) {
sourceSet.dependencies {
implementation("org.kodein.mock:mockmp-runtime:${BuildConfig.VERSION}")
if (ext.usesHelper) {
implementation(project.provider {
val isJunit5 = project.tasks.withType<KotlinJvmTest>().any { it.testFramework is JUnitPlatformTestFramework }
if (isJunit5) "org.kodein.mock:mockmp-test-helper-junit5:${BuildConfig.VERSION}"
else "org.kodein.mock:mockmp-test-helper:${BuildConfig.VERSION}"
})
private companion object {
private val Project.kotlinExtension get() = project.extensions.findByType<KotlinMultiplatformExtension>() ?: throw GradleException("Could not find Kotlin/Multiplatform plugin")

private fun addKSPDependency(project: Project, kspConfiguration: String) {
project.dependencies {
kspConfiguration("org.kodein.mock:mockmp-processor:${BuildConfig.VERSION}")
}
}
}

private fun configureKsp(project: Project, ext: Extension) {
val ksp = project.extensions.getByName<com.google.devtools.ksp.gradle.KspExtension>("ksp")
if (ext.throwErrors) {
ksp.arg("org.kodein.mock.errors", "throw")
}
if (ext.public) {
ksp.arg("org.kodein.mock.visibility", "public")
private fun addRuntimeDependencies(project: Project, sourceSet: KotlinSourceSet, ext: Extension) {
sourceSet.dependencies {
implementation("org.kodein.mock:mockmp-runtime:${BuildConfig.VERSION}")
if (ext.usesHelper) {
implementation(project.provider {
val isJunit5 = project.tasks.withType<KotlinJvmTest>().any { it.testFramework is JUnitPlatformTestFramework }
if (isJunit5) "org.kodein.mock:mockmp-test-helper-junit5:${BuildConfig.VERSION}"
else "org.kodein.mock:mockmp-test-helper:${BuildConfig.VERSION}"
})
}
}
}
}

private fun executeOnTests(project: Project, ext: Extension) {
val kotlin = project.kotlinExtension

val jvmTarget = kotlin.targets.firstOrNull { it.platformType == KotlinPlatformType.jvm }
?: kotlin.targets.firstOrNull { it.platformType == KotlinPlatformType.androidJvm }
?: throw GradleException("Could not find JVM or Android target")

when (jvmTarget.platformType) {
KotlinPlatformType.jvm -> addKSPDependency(project, "ksp${jvmTarget.name.replaceFirstChar { it.titlecase() }}Test")
KotlinPlatformType.androidJvm -> addKSPDependency(project, "ksp${jvmTarget.name.replaceFirstChar { it.titlecase() }}TestDebug")
else -> error("Unsupported platform type ${jvmTarget.platformType}")
private fun configureKsp(project: Project, ext: Extension) {
val ksp = project.extensions.getByName<com.google.devtools.ksp.gradle.KspExtension>("ksp")
if (ext.throwErrors) {
ksp.arg("org.kodein.mock.errors", "throw")
}
if (ext.public) {
ksp.arg("org.kodein.mock.visibility", "public")
}
}

project.afterEvaluate {
val commonTest = kotlin.sourceSets.getByName("commonTest")
private fun executeOnTests(project: Project, ext: Extension) {
val kotlin = project.kotlinExtension

addRuntimeDependencies(project, commonTest, ext)
val jvmTarget = kotlin.targets.firstOrNull { it.platformType == KotlinPlatformType.jvm }
?: kotlin.targets.firstOrNull { it.platformType == KotlinPlatformType.androidJvm }
?: throw GradleException("Could not find JVM or Android target")

// Adding KSP JVM result to COMMON source set
when (jvmTarget.platformType) {
KotlinPlatformType.jvm -> commonTest.kotlin.srcDir("${project.buildDir}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}Test/kotlin")
KotlinPlatformType.androidJvm -> {
commonTest.kotlin.srcDir("${project.buildDir}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}DebugUnitTest/kotlin")
commonTest.kotlin.srcDir("${project.buildDir}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}UnitTestDebug/kotlin")
}
KotlinPlatformType.jvm -> addKSPDependency(project, "ksp${jvmTarget.name.replaceFirstChar { it.titlecase() }}Test")
KotlinPlatformType.androidJvm -> addKSPDependency(project, "ksp${jvmTarget.name.replaceFirstChar { it.titlecase() }}TestDebug")
else -> error("Unsupported platform type ${jvmTarget.platformType}")
}

configureKsp(project, ext)
project.afterEvaluate {
val commonTest = kotlin.sourceSets.getByName("commonTest")

addRuntimeDependencies(project, commonTest, ext)

val buildDir = project.layout.buildDirectory.asFile

// Adding KSP JVM result to COMMON source set
when (jvmTarget.platformType) {
KotlinPlatformType.jvm -> {
commonTest.kotlin.srcDir(provider { "${buildDir.get()}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}Test/kotlin" })
}
KotlinPlatformType.androidJvm -> {
commonTest.kotlin.srcDir(provider { "${buildDir.get()}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}DebugUnitTest/kotlin" })
commonTest.kotlin.srcDir(provider { "${buildDir.get()}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}UnitTestDebug/kotlin" })
}
else -> error("Unsupported platform type ${jvmTarget.platformType}")
}

configureKsp(project, ext)

// For some reason, the simple fact of calling `project.tasks.withType<KotlinCompile<*>>()`
// breaks KSP if not set in a deep level of afterEvaluate :(
afterEvaluate { afterEvaluate {
// Adding KSP JVM as a dependency to all Kotlin compilations
project.tasks.withType<KotlinCompile<*>>().all {
if (name.startsWith("compile") && name.contains("TestKotlin")) {
when (jvmTarget.platformType) {
Expand All @@ -108,27 +117,45 @@ class MocKMPGradlePlugin : Plugin<Project> {
}
}
}
} }
}
}

private fun executeOnMain(project: Project, ext: Extension) {
addKSPDependency(project, "kspCommonMainMetadata")
addRuntimeDependencies(project, project.kotlinExtension.sourceSets["commonMain"], ext)
configureKsp(project, ext)
}

private fun execute(project: Project, ext: Extension) {
when (ext.targetSourceSet) {
SourceSetTarget.CommonMain -> executeOnMain(project, ext)
SourceSetTarget.CommonTest_Via_JVM -> executeOnTests(project, ext)
}
}
}

private fun executeOnMain(project: Project, ext: Extension) {
addKSPDependency(project, "kspCommonMainMetadata")
addRuntimeDependencies(project, project.kotlinExtension.sourceSets["commonMain"], ext)
configureKsp(project, ext)
private class MocKMPGradlePluginInstallMissing
: Exception(
"""
Please add the following code AFTER your kotlin AND your optional mockmp configuration block:
mockmp.installWorkaround()
This is now required because of this KSP issue:
https://github.com/google/ksp/issues/1524
You can have a look at the plugin documentation for its usage information:
https://github.com/kosi-libs/MocKMP#with-the-official-plugin
""".trimIndent()) {
}

override fun apply(target: Project) {
target.plugins.apply("com.google.devtools.ksp")

val ext = Extension()
val ext = Extension(target)
target.extensions.add("mockmp", ext)

target.afterEvaluate {
when (ext.targetSourceSet) {
SourceSetTarget.CommonMain -> executeOnMain(target, ext)
SourceSetTarget.CommonTest_Via_JVM -> executeOnTests(target, ext)
}
if (!ext.workaroundInstalled) throw MocKMPGradlePluginInstallMissing()
}
}

Expand Down
1 change: 1 addition & 0 deletions tests-projects/tests-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ kotlin {

mockmp {
usesHelper = true
installWorkaround()
}

// Showing tests in Gradle command line
Expand Down
22 changes: 19 additions & 3 deletions tests-projects/tests-jvm-junit4/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@ repositories {
}

kotlin {
applyDefaultHierarchyTemplate()

jvm()
ios()

iosSimulatorArm64()
iosX64()

js(IR) {
browser()
binaries.library()
}

sourceSets {
val commonMain by getting {
commonMain {
kotlin.srcDir("$rootDir/../../tests/tests-junit4/src/commonMain/kotlin")
dependencies {
implementation(libs.datetime)
}
}
val commonTest by getting {
commonTest {
kotlin.srcDir("$rootDir/../../tests/tests-junit4/src/commonTest/kotlin")
dependencies {
implementation(libs.coroutines.test)
Expand All @@ -31,11 +40,18 @@ kotlin {
implementation(kodeinGlobals.kotlin.test.junit)
}
}

val jsTest by getting {
dependencies {
implementation(kodeinGlobals.kotlin.test)
}
}
}
}

mockmp {
usesHelper = true
installWorkaround()
}

// Showing tests in Gradle command line
Expand Down
Loading

0 comments on commit f4986b3

Please sign in to comment.