Skip to content

Instantly share code, notes, and snippets.

@fuzzyweapon
Last active July 5, 2018 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzzyweapon/4fe110160eac136c531f8fe1f8c70660 to your computer and use it in GitHub Desktop.
Save fuzzyweapon/4fe110160eac136c531f8fe1f8c70660 to your computer and use it in GitHub Desktop.
Gradle - Kotlin DSLisms
// Sub-project or project build.gradle.kts
// This is for KotlinJVM and KotlinJVM multiplatform build.gradle.kts
java.sourceSets["main"].withConvention(KotlinSourceSet::class) {
kotlin.srcDir(file("src"))
}
java.sourceSets["test"].withConvention(KotlinSourceSet::class) {
kotlin.srcDir(file("test"))
}
// Another pattern for centralizing configuration. Adapted from groovy source below, untested.
// https://github.com/JetBrains/kotlin/blob/master/libraries/commonConfiguration.gradle
val jvmProjectConfiguration by extra { proj: Project ->
project(proj.path) {
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
javaParameters = true
verbose = true
}
}
}
}
// In sub-project build.gradle.kts file apply with...
val jvmProjectConfiguration: (proj: Project) -> Unit by rootProject.extra
jvmProjectConfiguration(project)
// build.gradle.kts
// I still haven't figured out a good and easy way of discovering extension types for various plugins.
// I can find things like Java's extension types while using a Kotlin plugin that applies a Java plugin though.
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
javaParameters = true
verbose = true
}
}
// build.gradle.kts-es
// Adhoc task creation.
tasks.create("task name") {
doLast {
println("hello world")
}
}
// settings.gradle.kts
// These are helpers for including multiplatform and single platform kotlin projects that do or do not follow the standard
// project layout.
// The one thing they do not support is a flag for allowing nested directories to generate container projects.
// Instead, currently they default to taking something like /libs/albatross/srcContainer/src which would normally be
// ':libs:albatross:srcContainer' where srcContainer represents something like common, JS, or JVM and instead registering
// it as ':albatross-srcContainer' so that everything ends up being a simple sub-project with minimal container projects.
fun includeModule(name: String, dir: String) {
include("$name")
project(":$name").projectDir = file(dir)
}
val File.isValid: Boolean
get() = this.exists() and this.isDirectory
fun multiModule(dir: String, name: String = "") {
val fileDir = file(dir)
if (fileDir.isValid) {
fileDir.listFiles().forEach { file ->
if (file.isValid) {
val subProjectName = if (name.isEmpty())
"${fileDir.name}:${file.name}"
else
"$name:${file.name}"
includeModule(subProjectName, file.path)
}
}
}
}
fun module(dir: String, name: String = "") {
var name = name
val fileDir = file(dir)
if (name.isEmpty())
name = fileDir.name
includeModule(name, fileDir.path)
}
// Usage
val multiModules = listOf("acorn-core", "tools/test-utils", "acorn-utils", "tools/acorn-texturepacker")
multiModules.forEach { multiModule(it) }
val modules = listOf("acorn-game", "acorn-spine")
modules.forEach { module(it) }
// build.gradle.kts
// These have to do with wrapper generation from within the project itself.
// Prevents needing to store .gradle files in VCS
tasks.withType<Wrapper> {
gradleVersion = "4.7"
distributionType = Wrapper.DistributionType.ALL
}
// Unsure of what a Kotlin DSL wrapper does. It might be legacy from early working Kotlin DSL days, but I found it interesting.
// https://github.com/sureshg/kotlin-starter/blob/master/build.gradle.kts
/**
* Generate Gradle Kotlin DSL wrapper.
*/
task<Wrapper>("wrapper") {
description = "Generate Gradle Kotlin DSL wrapper ${wrapperVersion.bold}"
distributionType = Wrapper.DistributionType.ALL
distributionUrl = gradleKotlinDslUrl(wrapperVersion)
doFirst {
println(description)
}
}
// build.gradle.kts
/**
* Note that this pattern doesn't always lead to predictable results as there seems to be edge case bugs.
* For instance, this did not work well with multiplatform kotlin projects in which there are dependencies between two
* multiplatform projects.
---------
* Example would be :acorn-core:jvm with a dependency on :acorn-utils:jvm...
*
* First, the compiler had troubles whenever the stdlib was not directly depended on explicitly in the :acorn-utils:jvm
* build.gradle.kts.
*
* Second, the multiplatform plugins had issues with detecting actual statements in :acorn-utils:jvm for :acorn-utils:common
* used in :acorn-core:jvm.
*
* It's highly possible there are workarounds for this. It's also worth nothing that moving :acorn-utils to the root directory
* had no effect on these problems, though the mechanism for declaring/including them had not changed (that is setting their
* project directories directly after including them as if they were following a different project layout from the norm).
*
* It's also worth noting that Intellij had /no/ problems resolving anything itself that the compiler had issue with /except/
* when trying to use expectedBy in the dependencies block, even when a platform plugin was applied within perceived scope.
*/
fun kotlinCommonProjects(): List<Project> {
return subprojects.filter { project ->
project.plugins.hasPlugin("org.jetbrains.kotlin.platform.common")
}
}
kotlinJvmProjects().forEach {
project(it.path) {
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
javaParameters = true
verbose = true
}
}
java.sourceSets["main"].withConvention(KotlinSourceSet::class) {
kotlin.srcDir(file("src"))
}
java.sourceSets["test"].withConvention(KotlinSourceSet::class) {
kotlin.srcDir(file("test"))
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit"))
}
kotlin {
experimental.coroutines = Coroutines.ENABLE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment