Skip to content

Commit

Permalink
feat: Add com.starter.library.multiplatform plugin to easily config…
Browse files Browse the repository at this point in the history
…ure multiplatform libraries (#211)
  • Loading branch information
mateuszkwiecinski authored Apr 15, 2021
1 parent d630474 commit a7ea4cd
Show file tree
Hide file tree
Showing 22 changed files with 534 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ internal class AndroidQualityPluginTest : WithGradleProjectTest() {
id('kotlin')
}
repositories.jcenter()
""".trimIndent()
)
}
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java-gradle-plugin'
id "org.jmailen.kotlinter" version "3.4.0"
id 'org.jetbrains.kotlin.jvm' version '1.4.32'
id 'org.jetbrains.kotlin.jvm' version '1.5.0-RC'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class KotlinLibraryPlugin : Plugin<Project> {
}

configureKotlinCoverage()
registerProjectCoverageTask {
it.dependsOn("jacocoTestReport")
registerProjectCoverageTask { projectCoverage ->
projectCoverage.dependsOn("jacocoTestReport")
}
withExtension<KotlinLibraryConfigExtension> { config ->
val javaFilesAllowed = config.javaFilesAllowed ?: rootConfig.javaFilesAllowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ internal class KotlinLibraryPluginTest : WithGradleProjectTest() {
@Test
fun `configures versioning plugin by default`() {
val git = setupGit(origin)
git.tag("release-1.2.2")
git.tag("v1.2.2")
git.commit("random commit")

val versioningEnabled = runTask("currentVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ internal class KotlinVersioningPluginTest : WithGradleProjectTest() {
}
}
git = setupGit(origin)
git.tag("release-1.1.0")
git.tag("v1.1.0")
}

@Test
fun `sets version to all projects`() {
git.commit("features in 1.2.0")
git.tag("release-1.2.0")
git.tag("v1.2.0")

val modules = listOf(":module1", ":module2", "")

Expand Down
36 changes: 36 additions & 0 deletions multiplatform/build.gradle
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'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.project.starter.modules.extensions

open class MultiplatfromLibraryConfigExtension
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
}
}
}
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()
}
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
}
}
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 ")
}
}
Loading

0 comments on commit a7ea4cd

Please sign in to comment.