Skip to content

Commit

Permalink
Add settings-include-dsl Gradle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisCAD committed Nov 7, 2023
1 parent 40626b9 commit 9179e00
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 1 deletion.
16 changes: 16 additions & 0 deletions buildSrc/src/main/kotlin/gradle-plugin.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import gradle.kotlin.dsl.accessors._3446ec0516f3690bb745eb6b3e74e05f.signing
import publishing.trySignAll

plugins {
id("com.gradle.plugin-publish")
`java-gradle-plugin`
`maven-publish`
signing
}

dependencies {
compileOnly(gradleKotlinDsl())
}

java { withSourcesJar() }
signing { trySignAll() }
27 changes: 27 additions & 0 deletions gradle-plugins/settings-include-dsl/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
`gradle-plugin`
`kotlin-dsl`
}

group = "org.splitties.incubator"

gradlePlugin {
plugins {
create(project.name) {
id = "org.splitties.incubator.settings-include-dsl"
displayName = "Settings include DSL"
description = "For Gradle projects with nested modules where you call include a lot."
implementationClass = "org.splitties.incubator.gradle.SettingsIncludeDslPlugin"
}
}
}

kotlin {
jvmToolchain(8)
}

pluginBundle {
website = "https://github.com/LouisCAD/LibPublishingHelpers"
vcsUrl = "https://github.com/LouisCAD/LibPublishingHelpers.git"
tags = listOf("kotlin-dsl", "kotlin")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.splitties.incubator.gradle

import org.gradle.api.initialization.Settings

class ModuleParentScope(
private val settings: Settings,
private val name: String,
private val parent: ModuleParentScope? = null
) {

operator fun String.invoke(block: (ModuleParentScope.() -> Unit)? = null) {
check(startsWith(':').not())
val moduleName = ":$this"
val projectName = "$parentalPath$moduleName"
settings.include(projectName)
block?.let { buildNode ->
ModuleParentScope(
settings = settings,
name = moduleName,
parent = this@ModuleParentScope
).buildNode()
}
}

private val parentalPath: String =
generateSequence(this) { it.parent }
.map { it.name }.toList().reversed().joinToString("")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.splitties.incubator.gradle

import org.gradle.api.Plugin
import org.gradle.api.initialization.Settings

class SettingsIncludeDslPlugin : Plugin<Settings> {
override fun apply(target: Settings) = Unit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.gradle.kotlin.dsl

import org.gradle.api.initialization.Settings
import org.splitties.incubator.gradle.ModuleParentScope

inline fun Settings.include(block: ModuleParentScope.() -> Unit) {
ModuleParentScope(settings = this, name = "").block()
}
38 changes: 37 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
rootProject.name = "LibPublishingHelpers"
include("lib-publishing-helpers")

dependencyResolutionManagement {
repositories {
Expand All @@ -12,3 +11,40 @@ val versionOfThisProject: String = file("version.txt").bufferedReader().use { it
gradle.beforeProject {
version = versionOfThisProject
}

include {
"lib-publishing-helpers"()
"gradle-plugins" {
"settings-include-dsl"()
}
}

//region include DSL
class ModuleParentScope(
private val name: String,
private val parent: ModuleParentScope? = null
) {

operator fun String.invoke(block: (ModuleParentScope.() -> Unit)? = null) {
check(startsWith(':').not())
val moduleName = ":$this"
val projectName = "$parentalPath$moduleName"
include(projectName)
block?.let { buildNode ->
ModuleParentScope(
name = moduleName,
parent = this@ModuleParentScope
).buildNode()
}
}

private val parentalPath: String =
generateSequence(this) { it.parent }
.map { it.name }.toList().reversed().joinToString("")

}

inline fun include(block: ModuleParentScope.() -> Unit) {
ModuleParentScope("").block()
}
//endregion

0 comments on commit 9179e00

Please sign in to comment.