Skip to content

Commit

Permalink
Move gradle configuration into convention plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Nov 30, 2023
1 parent b42cf3c commit 8a1b4b5
Show file tree
Hide file tree
Showing 22 changed files with 385 additions and 196 deletions.
66 changes: 1 addition & 65 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,69 +1,5 @@
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
import java.io.ByteArrayOutputStream

plugins {
kotlin("multiplatform").version(libs.versions.kotlin).apply(false)
alias(libs.plugins.dokka).apply(false)
alias(libs.plugins.publish).apply(false)
alias(libs.plugins.kotlinApiDump)
}

val VERSION_NAME: String by project


fun getPublishVersion(): String {
// Call gradle with -PinferVersion to set the dynamic version name.
// Otherwise, we skip it to save time.
if (!project.hasProperty("inferVersion")) return VERSION_NAME

val stdout = ByteArrayOutputStream()
project.exec {
commandLine = listOf("git", "tag", "--points-at", "master")
standardOutput = stdout
}
val tag = String(stdout.toByteArray()).trim()
if (tag.isNotEmpty()) return tag

val buildNumber = System.getenv("GITHUB_RUN_NUMBER") ?: "0"
return "$VERSION_NAME.$buildNumber-SNAPSHOT"
}



subprojects {
project.setProperty("VERSION_NAME", getPublishVersion())

if (name != "graalvm") {
tasks.withType<KotlinJvmCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
}
}
tasks.withType<JavaCompile>().configureEach {
options.release.set(8)
}
}

pluginManager.withPlugin("com.vanniktech.maven.publish") {
apply(plugin = "org.jetbrains.dokka")

tasks.named<DokkaTask>("dokkaHtml") {
outputDirectory.set(rootProject.rootDir.resolve("docs/api"))
pluginsMapConfiguration.set(
mapOf(
"org.jetbrains.dokka.base.DokkaBase" to """{
"footerMessage": "Copyright &copy; 2017 AJ Alt"
}"""
)
)
dokkaSourceSets.configureEach {
reportUndocumented.set(false)
skipDeprecated.set(true)
}
}
}
alias(libs.plugins.kotlinBinaryCompatibilityValidator)
}

apiValidation {
Expand Down
14 changes: 14 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation(libs.kotlin.gradle.plugin)
implementation(libs.publish)
implementation(libs.dokka)
}
9 changes: 9 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rootProject.name = "buildSrc"

dependencyResolutionManagement {
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}
11 changes: 11 additions & 0 deletions buildSrc/src/main/kotlin/mordant-js-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
kotlin("multiplatform")
}

kotlin {
js {
// We have different code paths on browsers and node, so we run tests on both
nodejs()
browser()
}
}
22 changes: 22 additions & 0 deletions buildSrc/src/main/kotlin/mordant-jvm-sample-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id("mordant-kotlin-conventions")
application
}

kotlin {
// For some reason MainKt isn't present in the jar unless we add withJava
jvm { withJava() }

sourceSets {
val jvmMain by getting {
dependencies {
implementation(project(":mordant"))
}
}
}
}

application {
mainClass.set("com.github.ajalt.mordant.samples.MainKt")
applicationDefaultJvmArgs = listOf("-Dfile.encoding=utf-8")
}
17 changes: 17 additions & 0 deletions buildSrc/src/main/kotlin/mordant-kotlin-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
kotlin("multiplatform")
}

tasks.withType<KotlinJvmCompile>().configureEach {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + "-opt-in=kotlin.RequiresOptIn"
}
compilerOptions.jvmTarget.set(JvmTarget.JVM_1_8)
}

tasks.withType<JavaCompile>().configureEach {
options.release.set(8)
}
9 changes: 9 additions & 0 deletions buildSrc/src/main/kotlin/mordant-mpp-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("mordant-kotlin-conventions")
id("mordant-js-conventions")
id("mordant-native-conventions")
}

kotlin {
jvm()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugins {
id("mordant-jvm-sample-conventions")
id("mordant-native-sample-conventions")
}
18 changes: 18 additions & 0 deletions buildSrc/src/main/kotlin/mordant-native-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
kotlin("multiplatform")
}

kotlin {
macosX64()
macosArm64()
linuxX64()
mingwX64()

applyDefaultHierarchyTemplate()

sourceSets {
val posixMain by creating { dependsOn(nativeMain.get()) }
linuxMain.get().dependsOn(posixMain)
macosMain.get().dependsOn(posixMain)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
id("mordant-native-conventions")
}

kotlin.targets.filterIsInstance<KotlinNativeTarget>().forEach { target ->
target.binaries.executable {
entryPoint = "com.github.ajalt.mordant.samples.main"
}
}
46 changes: 46 additions & 0 deletions buildSrc/src/main/kotlin/mordant-publishing-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@file:Suppress("PropertyName")

import org.jetbrains.dokka.gradle.DokkaTask
import org.gradle.kotlin.dsl.provideDelegate
import java.io.ByteArrayOutputStream

plugins {
id("com.vanniktech.maven.publish")
id("org.jetbrains.dokka")
}

val VERSION_NAME: String by project

fun getPublishVersion(): String {
// Call gradle with -PinferVersion to set the dynamic version name.
// Otherwise, we skip it to save time.
if (!project.hasProperty("inferVersion")) return VERSION_NAME

val stdout = ByteArrayOutputStream()
project.exec {
commandLine = listOf("git", "tag", "--points-at", "master")
standardOutput = stdout
}
val tag = String(stdout.toByteArray()).trim()
if (tag.isNotEmpty()) return tag

val buildNumber = System.getenv("GITHUB_RUN_NUMBER") ?: "0"
return "$VERSION_NAME.$buildNumber-SNAPSHOT"
}

project.setProperty("VERSION_NAME", getPublishVersion())

tasks.named<DokkaTask>("dokkaHtml") {
outputDirectory.set(rootProject.rootDir.resolve("docs/api"))
pluginsMapConfiguration.set(
mapOf(
"org.jetbrains.dokka.base.DokkaBase" to """{
"footerMessage": "Copyright &copy; 2017 AJ Alt"
}"""
)
)
dokkaSourceSets.configureEach {
reportUndocumented.set(false)
skipDeprecated.set(true)
}
}
11 changes: 7 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ jna-core = "net.java.dev.jna:jna:5.13.0"
graalvm-svm = "org.graalvm.nativeimage:svm:23.1.0"

# used in tests
kotest = "io.kotest:kotest-assertions-core:5.7.2"
kotest = "io.kotest:kotest-assertions-core:5.7.2"
systemrules = "com.github.stefanbirkner:system-rules:1.19.0"
r8 = "com.android.tools:r8:8.1.56"

# build logic
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version="1.9.10"}
publish = { module = "com.vanniktech:gradle-maven-publish-plugin", version="0.25.3"}

[plugins]
dokka = "org.jetbrains.dokka:1.9.10"
publish = "com.vanniktech.maven.publish:0.25.3"
graalvm-nativeimage = "org.graalvm.buildtools.native:0.9.27"
kotlinApiDump = "org.jetbrains.kotlinx.binary-compatibility-validator:0.13.2"
kotlinBinaryCompatibilityValidator = "org.jetbrains.kotlinx.binary-compatibility-validator:0.13.2"
21 changes: 2 additions & 19 deletions mordant/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
plugins {
kotlin("multiplatform")
alias(libs.plugins.publish)
id("mordant-mpp-conventions")
id("mordant-publishing-conventions")
}

kotlin {
jvm()
js(IR) {
nodejs()
browser()
}

macosX64()
macosArm64()
linuxX64()
mingwX64()

applyDefaultHierarchyTemplate()

sourceSets {
val commonMain by getting {
dependencies {
Expand All @@ -43,9 +30,5 @@ kotlin {
api(libs.systemrules)
}
}

val posixMain by creating { dependsOn(nativeMain.get()) }
linuxMain.get().dependsOn(posixMain)
macosMain.get().dependsOn(posixMain)
}
}
9 changes: 0 additions & 9 deletions samples/build.gradle.kts

This file was deleted.

34 changes: 1 addition & 33 deletions samples/detection/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
kotlin("multiplatform")
application
}

kotlin {
jvm {
withJava()
}

macosX64()
linuxX64()
mingwX64()

targets.filterIsInstance<KotlinNativeTarget>().forEach { target ->
target.binaries.executable {
entryPoint = "com.github.ajalt.mordant.samples.main"
}
}

sourceSets {
val commonMain by getting {
dependencies {
implementation(project(":mordant"))
}
}
}
}

application {
mainClass.set("com.github.ajalt.mordant.samples.MainKt")
applicationDefaultJvmArgs = listOf("-Dfile.encoding=utf-8")
id("mordant-mpp-sample-conventions")
}
13 changes: 1 addition & 12 deletions samples/markdown/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
plugins {
application
kotlin("jvm")
}

application {
mainClass.set("com.github.ajalt.mordant.samples.MainKt")
applicationDefaultJvmArgs = listOf("-Dfile.encoding=utf-8")
}

dependencies {
api(kotlin("stdlib"))
api(project(":mordant"))
id("mordant-mpp-sample-conventions")
}
14 changes: 1 addition & 13 deletions samples/progress/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
plugins {
application
kotlin("jvm")
id("mordant-jvm-sample-conventions")
}

application {
mainClass.set("com.github.ajalt.mordant.samples.MainKt")
applicationDefaultJvmArgs = listOf("-Dfile.encoding=utf-8")
}

dependencies {
api(kotlin("stdlib"))
api(project(":mordant"))
}

Loading

0 comments on commit 8a1b4b5

Please sign in to comment.