-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor DokkatooExampleProjects convention plugin so that the `gradl…
…e.properties` files can be configured using a DSL
- Loading branch information
Showing
8 changed files
with
243 additions
and
47 deletions.
There are no files selected for viewing
2 changes: 0 additions & 2 deletions
2
buildSrc/src/main/kotlin/buildsrc/conventions/dokkatoo-example-projects-base.gradle.kts
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
25 changes: 25 additions & 0 deletions
25
buildSrc/src/main/kotlin/buildsrc/settings/DokkatooExampleProjectsSettings.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,25 @@ | ||
package buildsrc.settings | ||
|
||
import buildsrc.tasks.UpdateDokkatooExampleGradleProperties.GradlePropertiesSpec | ||
import buildsrc.utils.adding | ||
import buildsrc.utils.domainObjectContainer | ||
import javax.inject.Inject | ||
import org.gradle.api.NamedDomainObjectContainer | ||
import org.gradle.api.model.ObjectFactory | ||
import org.gradle.api.plugins.ExtensionAware | ||
|
||
/** | ||
* Settings for the [buildsrc.conventions.Dokkatoo_example_projects_gradle] convention plugin | ||
*/ | ||
abstract class DokkatooExampleProjectsSettings @Inject constructor( | ||
private val objects: ObjectFactory, | ||
) : ExtensionAware { | ||
|
||
val gradleProperties: NamedDomainObjectContainer<GradlePropertiesSpec> = | ||
// create an extension so Gradle will generate DSL accessors | ||
extensions.adding("gradleProperties", objects.domainObjectContainer()) | ||
|
||
companion object { | ||
const val EXTENSION_NAME = "dokkatooExampleProjects" | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
buildSrc/src/main/kotlin/buildsrc/settings/MavenPublishTestSettings.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,17 @@ | ||
package buildsrc.settings | ||
|
||
import org.gradle.api.attributes.Attribute | ||
import org.gradle.api.file.Directory | ||
import org.gradle.api.plugins.ExtensionAware | ||
import org.gradle.api.provider.Provider | ||
|
||
/** | ||
* Settings for the [buildsrc.conventions.Maven_publish_test_gradle] convention plugin. | ||
*/ | ||
abstract class MavenPublishTestSettings( | ||
val testMavenRepo: Provider<Directory> | ||
) : ExtensionAware { | ||
companion object { | ||
val attribute = Attribute.of("maven-publish-test", String::class.java) | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
buildSrc/src/main/kotlin/buildsrc/tasks/UpdateDokkatooExampleGradleProperties.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,83 @@ | ||
package buildsrc.tasks | ||
|
||
import javax.inject.Inject | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.Named | ||
import org.gradle.api.NamedDomainObjectContainer | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.file.RegularFile | ||
import org.gradle.api.model.ObjectFactory | ||
import org.gradle.api.plugins.ExtensionAware | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.tasks.* | ||
|
||
/** | ||
* Utility for updating the `gradle.properties` of projects used in automated tests. | ||
*/ | ||
@CacheableTask | ||
abstract class UpdateDokkatooExampleGradleProperties @Inject constructor( | ||
@get:Internal | ||
val objects: ObjectFactory | ||
) : DefaultTask(), ExtensionAware { | ||
|
||
@get:Nested | ||
abstract val gradleProperties: NamedDomainObjectContainer<GradlePropertiesSpec> | ||
|
||
@get:Internal // tracked by testMavenRepoPath (don't care about the directory contents, only the path) | ||
abstract val testMavenRepo: DirectoryProperty | ||
|
||
@get:Input | ||
protected val testMavenRepoPath: Provider<String> = | ||
testMavenRepo.asFile.map { it.invariantSeparatorsPath } | ||
|
||
@TaskAction | ||
fun update() { | ||
gradleProperties.forEach { spec -> | ||
|
||
val content = buildString { | ||
appendLine("# DO NOT EDIT - Generated by ${this@UpdateDokkatooExampleGradleProperties.path}") | ||
appendLine() | ||
if (spec.enableTestMavenRepo.get()) { | ||
appendLine("testMavenRepo=${testMavenRepoPath.get()}") | ||
appendLine() | ||
} | ||
spec.content.orNull?.takeIf { it.isNotBlank() }?.let { content -> | ||
appendLine(content) | ||
appendLine() | ||
} | ||
} | ||
|
||
spec.gradlePropertiesFile.get().asFile.writeText(content) | ||
} | ||
} | ||
|
||
/** | ||
* Represents a `gradle.properties` file that should be automatically generated, | ||
* into the file [gradleProperties]. | ||
*/ | ||
abstract class GradlePropertiesSpec( | ||
private val name: String | ||
) : Named { | ||
|
||
/** The generated `gradle.properties` file */ | ||
@get:OutputFile | ||
val gradlePropertiesFile: Provider<RegularFile> = destinationDir.file("gradle.properties") | ||
|
||
/** The directory that the `gradle.properties` will be generated into */ | ||
@get:Internal | ||
abstract val destinationDir: DirectoryProperty | ||
|
||
/** Optional additional content to append to the `gradle.properties` file */ | ||
@get:Input | ||
@get:Optional | ||
abstract val content: Property<String> | ||
|
||
/** Whether the project-local Maven repo (that contains Dokkatoo snapshots) should be added */ | ||
@get:Input | ||
abstract val enableTestMavenRepo: Property<Boolean> | ||
|
||
@Input | ||
override fun getName(): String = name | ||
} | ||
} |
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,30 @@ | ||
package buildsrc.utils | ||
|
||
|
||
/** | ||
* Title case the first char of a string. | ||
* | ||
* (Custom implementation because [toUpperCase]/[uppercase] is deprecated. | ||
* Can be removed when Gradle is updated) | ||
*/ | ||
internal fun String.uppercaseFirstChar(): String = mapFirstChar(Character::toTitleCase) | ||
|
||
|
||
internal fun String.lowercaseFirstChar(): String = mapFirstChar(Character::toLowerCase) | ||
|
||
|
||
private inline fun String.mapFirstChar( | ||
transform: (Char) -> Char | ||
): String = if (isNotEmpty()) transform(this[0]) + substring(1) else this | ||
|
||
|
||
/** | ||
* Filters all non-alphanumeric characters and converts the result into camelCase. | ||
*/ | ||
internal fun String.toAlphaNumericCamelCase(): String = | ||
map { if (it.isLetterOrDigit()) it else ' '} | ||
.joinToString("") | ||
.split(" ") | ||
.filter { it.isNotBlank() } | ||
.joinToString("") { it.uppercaseFirstChar() } | ||
.lowercaseFirstChar() |