-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
com.starter.library.multiplatform
plugin to easily config…
…ure multiplatform libraries (#211)
- Loading branch information
1 parent
d630474
commit a7ea4cd
Showing
22 changed files
with
534 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
plugins { | ||
id 'java-gradle-plugin' | ||
id 'com.starter.library.kotlin' | ||
id 'pl.droidsonroids.jacoco.testkit' version '1.0.7' | ||
id 'com.starter.publishing' | ||
} | ||
|
||
repositories { | ||
gradlePluginPortal() | ||
} | ||
|
||
dependencies { | ||
api 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32' | ||
implementation project(":jvm") | ||
implementation project(":quality") | ||
implementation project(":config") | ||
|
||
testImplementation project(":testing") | ||
} | ||
|
||
jacoco { | ||
toolVersion = "0.8.6" | ||
} | ||
test { | ||
useJUnitPlatform() | ||
} | ||
|
||
gradlePlugin { | ||
plugins { | ||
kotlinLibrary { | ||
id = 'com.starter.library.multiplatform' | ||
displayName = 'Kotlin Multiplatform Library Plugin' | ||
implementationClass = 'com.project.starter.modules.plugins.MultiplatformLibraryPlugin' | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...main/kotlin/com/project/starter/modules/extensions/MultiplatfromLibraryConfigExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.project.starter.modules.extensions | ||
|
||
open class MultiplatfromLibraryConfigExtension |
33 changes: 33 additions & 0 deletions
33
multiplatform/src/main/kotlin/com/project/starter/modules/internal/MultiplatformCoverage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.project.starter.modules.internal | ||
|
||
import com.project.starter.config.getByType | ||
import org.gradle.api.Project | ||
import org.gradle.api.tasks.testing.Test | ||
import org.gradle.testing.jacoco.plugins.JacocoPluginExtension | ||
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension | ||
import org.gradle.testing.jacoco.tasks.JacocoReport | ||
|
||
internal fun Project.configureMultiplatformCoverage() { | ||
pluginManager.apply("jacoco") | ||
|
||
tasks.withType(Test::class.java) { | ||
it.extensions.getByType<JacocoTaskExtension>().apply { | ||
isIncludeNoLocationClasses = true | ||
excludes = listOf("jdk.internal.*") | ||
} | ||
} | ||
|
||
extensions.configure(JacocoPluginExtension::class.java) { | ||
it.toolVersion = "0.8.6" | ||
} | ||
tasks.register("jacocoTestReport", JacocoReport::class.java) { | ||
it.dependsOn(":$path:jvmTest") | ||
it.classDirectories.setFrom(buildDir.resolve("classes/kotlin/jvm/main")) | ||
it.sourceDirectories.setFrom(files("src/commonMain/kotlin", "src/jvmMain/kotlin")) | ||
it.executionData.setFrom(buildDir.resolve("jacoco/jvmTest.exec")) | ||
it.reports.apply { | ||
xml.isEnabled = true | ||
html.isEnabled = true | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
multiplatform/src/main/kotlin/com/project/starter/modules/internal/Repositories.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.project.starter.modules.internal | ||
|
||
import org.gradle.api.Project | ||
|
||
internal fun Project.configureRepositories(): Unit = with(repositories) { | ||
google { repository -> | ||
repository.mavenContent { content -> | ||
val googleLibraries = listOf( | ||
"com\\.android.*", | ||
"androidx.*", | ||
"android.arch.*", | ||
"com\\.google.*" | ||
) | ||
googleLibraries.forEach(content::includeGroupByRegex) | ||
} | ||
} | ||
mavenCentral() | ||
} |
29 changes: 29 additions & 0 deletions
29
...latform/src/main/kotlin/com/project/starter/modules/plugins/MultiplatformLibraryPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.project.starter.modules.plugins | ||
|
||
import com.project.starter.modules.extensions.MultiplatfromLibraryConfigExtension | ||
import com.project.starter.modules.internal.configureMultiplatformCoverage | ||
import com.project.starter.modules.tasks.ProjectCoverageTask.Companion.registerProjectCoverageTask | ||
import com.project.starter.modules.tasks.ProjectTestTask.Companion.registerProjectTestTask | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
|
||
class MultiplatformLibraryPlugin : Plugin<Project> { | ||
|
||
override fun apply(target: Project) = with(target) { | ||
pluginManager.apply("org.jetbrains.kotlin.multiplatform") | ||
pluginManager.apply("com.starter.quality") | ||
pluginManager.apply(ConfigurationPlugin::class.java) | ||
|
||
extensions.create("projectConfig", MultiplatfromLibraryConfigExtension::class.java) | ||
|
||
registerProjectTestTask { | ||
it.dependsOn("allTests") | ||
} | ||
|
||
configureMultiplatformCoverage() | ||
registerProjectCoverageTask { projectCoverage -> | ||
projectCoverage.dependsOn("jacocoTestReport") | ||
} | ||
Unit | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
multiplatform/src/test/kotlin/com/project/starter/modules/MultiplatformLibraryPluginTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package com.project.starter.modules | ||
|
||
import com.project.starter.WithGradleProjectTest | ||
import com.project.starter.commit | ||
import com.project.starter.kotlinClass | ||
import com.project.starter.kotlinTestClass | ||
import com.project.starter.setupGit | ||
import com.project.starter.tag | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.io.TempDir | ||
import java.io.File | ||
|
||
internal class MultiplatformLibraryPluginTest : WithGradleProjectTest() { | ||
|
||
lateinit var rootBuildScript: File | ||
lateinit var module1Root: File | ||
lateinit var module2Root: File | ||
|
||
@TempDir | ||
lateinit var origin: File | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
rootDirectory.apply { | ||
mkdirs() | ||
resolve("settings.gradle").writeText("""include ":module1", ":module2" """) | ||
|
||
rootBuildScript = resolve("build.gradle") | ||
module1Root = resolve("module1") { | ||
resolve("build.gradle") { | ||
writeText( | ||
""" | ||
plugins { | ||
id('com.starter.library.multiplatform') | ||
} | ||
kotlin { | ||
jvm() | ||
} | ||
dependencies { | ||
"jvmTestImplementation"("junit:junit:4.13.2") | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
resolve("src/commonMain/kotlin/ValidKotlinFile1.kt") { | ||
writeText(kotlinClass("ValidKotlinFile1")) | ||
} | ||
resolve("src/jvmTest/kotlin/JvmTest1.kt") { | ||
writeText(kotlinTestClass("JvmTest1")) | ||
} | ||
} | ||
module2Root = resolve("module2") { | ||
resolve("build.gradle") { | ||
writeText( | ||
""" | ||
plugins { | ||
id('com.starter.library.multiplatform') | ||
} | ||
kotlin { | ||
ios() | ||
jvm() | ||
} | ||
dependencies { | ||
"jvmTestImplementation"("junit:junit:4.13.2") | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
resolve("src/commonMain/kotlin/ValidKotlinFile2.kt") { | ||
writeText(kotlinClass("ValidKotlinFile2")) | ||
} | ||
resolve("src/jvmTest/kotlin/JvmTest2.kt") { | ||
writeText(kotlinTestClass("JvmTest2")) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `kotlin library plugin compiles 'src_main_kotlin' classes`() { | ||
val result = runTask("assemble") | ||
|
||
assertThat(result.task(":module1:assemble")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) | ||
assertThat(result.task(":module2:assemble")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) | ||
} | ||
|
||
@Test | ||
fun `projectTest runs tests for all modules`() { | ||
val result = runTask("projectTest") | ||
|
||
assertThat(result.task(":module1:allTests")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) | ||
assertThat(result.task(":module2:allTests")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) | ||
} | ||
|
||
@Test | ||
fun `projectCoverage runs coverage for all modules`() { | ||
val result = runTask("projectCoverage") | ||
|
||
assertThat(result.task(":module1:jvmTest")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) | ||
assertThat(result.task(":module2:jvmTest")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) | ||
assertThat(module1Root.resolve("build/reports/jacoco/jacocoTestReport")).isDirectoryContaining { | ||
it.name == "jacocoTestReport.xml" | ||
} | ||
} | ||
|
||
@Test | ||
fun `configures quality plugin by default`() { | ||
val qualityEnabled = runTask("projectCodeStyle") | ||
|
||
assertThat(qualityEnabled.task(":module1:projectCodeStyle")?.outcome).isNotNull() | ||
assertThat(qualityEnabled.task(":module2:projectCodeStyle")?.outcome).isNotNull() | ||
} | ||
|
||
@Test | ||
fun `configures versioning plugin by default`() { | ||
val git = setupGit(origin) | ||
git.tag("v1.2.2") | ||
git.commit("random commit") | ||
|
||
val versioningEnabled = runTask("currentVersion") | ||
|
||
assertThat(versioningEnabled.output).contains("version: 1.3.0-SNAPSHOT") | ||
} | ||
|
||
@Test | ||
fun `does not configure versioning plugin if disabled using configuration plugin`() { | ||
//language=groovy | ||
val versioningScript = | ||
""" | ||
plugins { | ||
id('com.starter.config') | ||
} | ||
commonConfig { | ||
versioningPlugin { | ||
enabled false | ||
} | ||
} | ||
""".trimIndent() | ||
rootBuildScript.writeText(versioningScript) | ||
|
||
val versioningDisabled = runTask("currentVersion", shouldFail = true) | ||
|
||
assertThat(versioningDisabled.output).contains("Task 'currentVersion' not found ") | ||
} | ||
} |
Oops, something went wrong.