Overextending

- Use cool kotlin dsl extension methods in plugins where possible
This commit is contained in:
Jozufozu 2024-05-03 17:43:10 -07:00
parent f25d66680e
commit 4094093825
4 changed files with 30 additions and 34 deletions

View file

@ -11,6 +11,7 @@ import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.jvm.tasks.Jar import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.register
class JarTaskSet( class JarTaskSet(
val project: Project, val project: Project,
@ -73,7 +74,7 @@ class JarTaskSet(
val devlibs = buildDirectory.dir("devlibs/${name}") val devlibs = buildDirectory.dir("devlibs/${name}")
val libs = buildDirectory.dir("libs/${name}") val libs = buildDirectory.dir("libs/${name}")
val jarTask = project.tasks.register("${name}Jar", Jar::class.java) { val jarTask = project.tasks.register<Jar>("${name}Jar") {
group = BUILD_GROUP group = BUILD_GROUP
destinationDirectory.set(devlibs) destinationDirectory.set(devlibs)
@ -82,14 +83,14 @@ class JarTaskSet(
} }
excludeDuplicatePackageInfos(this) excludeDuplicatePackageInfos(this)
} }
val remapJarTask = project.tasks.register("${name}RemapJar", RemapJarTask::class.java) { val remapJarTask = project.tasks.register<RemapJarTask>("${name}RemapJar") {
dependsOn(jarTask) dependsOn(jarTask)
group = LOOM_GROUP group = LOOM_GROUP
destinationDirectory.set(libs) destinationDirectory.set(libs)
inputFile.set(jarTask.flatMap { it.archiveFile }) inputFile.set(jarTask.flatMap { it.archiveFile })
} }
val sourcesTask = project.tasks.register("${name}SourcesJar", Jar::class.java) { val sourcesTask = project.tasks.register<Jar>("${name}SourcesJar") {
group = BUILD_GROUP group = BUILD_GROUP
destinationDirectory.set(devlibs) destinationDirectory.set(devlibs)
archiveClassifier.set(SOURCES_CLASSIFIER) archiveClassifier.set(SOURCES_CLASSIFIER)
@ -99,7 +100,7 @@ class JarTaskSet(
} }
excludeDuplicatePackageInfos(this) excludeDuplicatePackageInfos(this)
} }
val remapSourcesTask = project.tasks.register("${name}RemapSourcesJar", RemapSourcesJarTask::class.java) { val remapSourcesTask = project.tasks.register<RemapSourcesJarTask>("${name}RemapSourcesJar") {
dependsOn(sourcesTask) dependsOn(sourcesTask)
group = LOOM_GROUP group = LOOM_GROUP
destinationDirectory.set(libs) destinationDirectory.set(libs)
@ -107,11 +108,9 @@ class JarTaskSet(
inputFile.set(sourcesTask.flatMap { it.archiveFile }) inputFile.set(sourcesTask.flatMap { it.archiveFile })
} }
val javadocTask = project.tasks.register("${name}Javadoc", Javadoc::class.java) { val javadocTask = project.tasks.register<Javadoc>("${name}Javadoc") {
group = BUILD_GROUP group = BUILD_GROUP
setDestinationDir(buildDirectory.dir("docs/${name}-javadoc").get().asFile) setDestinationDir(buildDirectory.dir("docs/${name}-javadoc").get().asFile)
options.encoding = "UTF-8"
options.optionFiles(project.rootProject.file("javadoc-options.txt"))
for (set in sourceSetSet) { for (set in sourceSetSet) {
source(set.allJava) source(set.allJava)
@ -119,7 +118,7 @@ class JarTaskSet(
} }
excludeDuplicatePackageInfos(this) excludeDuplicatePackageInfos(this)
} }
val javadocJarTask = project.tasks.register("${name}JavadocJar", Jar::class.java) { val javadocJarTask = project.tasks.register<Jar>("${name}JavadocJar") {
dependsOn(javadocTask) dependsOn(javadocTask)
group = BUILD_GROUP group = BUILD_GROUP
destinationDirectory.set(libs) destinationDirectory.set(libs)

View file

@ -3,6 +3,7 @@ package com.jozufozu.gradle.nullability
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.tasks.Delete import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.SourceSet
import org.gradle.kotlin.dsl.register
open class PackageInfosExtension(private val project: Project) { open class PackageInfosExtension(private val project: Project) {
fun sources(vararg sourceSets: SourceSet) { fun sources(vararg sourceSets: SourceSet) {
@ -16,7 +17,7 @@ open class PackageInfosExtension(private val project: Project) {
// otherwise it'll just be whatever the last source set is in the list. // otherwise it'll just be whatever the last source set is in the list.
val sourceSetName = sourceSet.name val sourceSetName = sourceSet.name
val taskName = sourceSet.getTaskName("generate", "PackageInfos") val taskName = sourceSet.getTaskName("generate", "PackageInfos")
val task = project.tasks.register(taskName, GeneratePackageInfosTask::class.java) { val task = project.tasks.register<GeneratePackageInfosTask>(taskName) {
group = "flywheel" group = "flywheel"
description = "Generates package-info files for $sourceSetName packages." description = "Generates package-info files for $sourceSetName packages."
@ -31,7 +32,7 @@ open class PackageInfosExtension(private val project: Project) {
finalizedBy(task) finalizedBy(task)
} }
val cleanTask = project.tasks.register(sourceSet.getTaskName("clean", "PackageInfos"), Delete::class.java) { val cleanTask = project.tasks.register<Delete>(sourceSet.getTaskName("clean", "PackageInfos")) {
group = "flywheel" group = "flywheel"
delete(project.file("src/$sourceSetName/generatedPackageInfos")) delete(project.file("src/$sourceSetName/generatedPackageInfos"))
} }

View file

@ -12,8 +12,7 @@ import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.compile.JavaCompile import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.jvm.tasks.Jar import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.provideDelegate import org.gradle.kotlin.dsl.*
import org.gradle.kotlin.dsl.the
import org.gradle.language.jvm.tasks.ProcessResources import org.gradle.language.jvm.tasks.ProcessResources
import kotlin.properties.ReadWriteProperty import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
@ -65,17 +64,17 @@ open class PlatformExtension(val project: Project) {
fun compileWithCommonSourceSets() { fun compileWithCommonSourceSets() {
project.tasks.apply { project.tasks.apply {
withType(JavaCompile::class.java).configureEach { withType<JavaCompile>().configureEach {
JarTaskSet.excludeDuplicatePackageInfos(this) JarTaskSet.excludeDuplicatePackageInfos(this)
} }
sources.forEach { sources.forEach {
val commonSourceSet = commonSourceSets.named(it.name).get() val commonSourceSet = commonSourceSets.named(it.name).get()
named(it.compileJavaTaskName, JavaCompile::class.java).configure { named<JavaCompile>(it.compileJavaTaskName).configure {
source(commonSourceSet.allJava) source(commonSourceSet.allJava)
} }
named(it.processResourcesTaskName, ProcessResources::class.java).configure { named<ProcessResources>(it.processResourcesTaskName).configure {
from(commonSourceSet.resources) from(commonSourceSet.resources)
} }
} }
@ -87,20 +86,20 @@ open class PlatformExtension(val project: Project) {
val extraSourceSets = sources.filter { it.name != "main" }.toList() val extraSourceSets = sources.filter { it.name != "main" }.toList()
val commonSources = sources.map { commonSourceSets.named(it.name).get() } val commonSources = sources.map { commonSourceSets.named(it.name).get() }
named("jar", Jar::class.java).configure { named<Jar>("jar").configure {
extraSourceSets.forEach { from(it.output) } extraSourceSets.forEach { from(it.output) }
JarTaskSet.excludeDuplicatePackageInfos(this) JarTaskSet.excludeDuplicatePackageInfos(this)
} }
named("javadoc", Javadoc::class.java).configure { named<Javadoc>("javadoc").configure {
commonSources.forEach { source(it.allJava) } commonSources.forEach { source(it.allJava) }
extraSourceSets.forEach { source(it.allJava) } extraSourceSets.forEach { source(it.allJava) }
JarTaskSet.excludeDuplicatePackageInfos(this) JarTaskSet.excludeDuplicatePackageInfos(this)
} }
named("sourcesJar", Jar::class.java).configure { named<Jar>("sourcesJar").configure {
commonSources.forEach { from(it.allJava) } commonSources.forEach { from(it.allJava) }
extraSourceSets.forEach { from(it.allJava) } extraSourceSets.forEach { from(it.allJava) }
@ -110,12 +109,12 @@ open class PlatformExtension(val project: Project) {
} }
fun publishMod() { fun publishMod() {
val remapJar = project.tasks.named("remapJar", RemapJarTask::class.java) val remapJar = project.tasks.named<RemapJarTask>("remapJar")
val remapSourcesJar = project.tasks.named("remapSourcesJar", RemapSourcesJarTask::class.java) val remapSourcesJar = project.tasks.named<RemapSourcesJarTask>("remapSourcesJar")
val javadocJar = project.tasks.named("javadocJar", Jar::class.java) val javadocJar = project.tasks.named<Jar>("javadocJar")
project.the<PublishingExtension>().publications { project.the<PublishingExtension>().publications {
register("modMaven", MavenPublication::class.java) { register<MavenPublication>("modMaven") {
artifact(remapJar) artifact(remapJar)
artifact(remapSourcesJar) artifact(remapSourcesJar)
artifact(javadocJar) artifact(javadocJar)
@ -126,7 +125,7 @@ open class PlatformExtension(val project: Project) {
fun publishRemap(artifactId: String, jarSet: JarTaskSet) { fun publishRemap(artifactId: String, jarSet: JarTaskSet) {
project.the<PublishingExtension>().publications { project.the<PublishingExtension>().publications {
register("${jarSet.name}RemapMaven", MavenPublication::class.java) { register<MavenPublication>("${jarSet.name}RemapMaven") {
artifact(jarSet.remapJar) artifact(jarSet.remapJar)
artifact(jarSet.remapSources) artifact(jarSet.remapSources)
artifact(jarSet.javadocJar) artifact(jarSet.javadocJar)
@ -137,7 +136,7 @@ open class PlatformExtension(val project: Project) {
fun publish(artifactId: String, jarSet: JarTaskSet) { fun publish(artifactId: String, jarSet: JarTaskSet) {
project.the<PublishingExtension>().publications { project.the<PublishingExtension>().publications {
register("${jarSet.name}Maven", MavenPublication::class.java) { register<MavenPublication>("${jarSet.name}Maven") {
artifact(jarSet.jar) artifact(jarSet.jar)
artifact(jarSet.sources) artifact(jarSet.sources)
artifact(jarSet.javadocJar) artifact(jarSet.javadocJar)

View file

@ -13,10 +13,7 @@ import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.jvm.tasks.Jar import org.gradle.jvm.tasks.Jar
import org.gradle.jvm.toolchain.JavaLanguageVersion import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.kotlin.dsl.assign import org.gradle.kotlin.dsl.*
import org.gradle.kotlin.dsl.maven
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.the
import org.gradle.language.jvm.tasks.ProcessResources import org.gradle.language.jvm.tasks.ProcessResources
@ -113,36 +110,36 @@ class SubprojectPlugin: Plugin<Project> {
project.tasks.apply { project.tasks.apply {
// make builds reproducible // make builds reproducible
withType(AbstractArchiveTask::class.java).configureEach { withType<AbstractArchiveTask>().configureEach {
isPreserveFileTimestamps = false isPreserveFileTimestamps = false
isReproducibleFileOrder = true isReproducibleFileOrder = true
} }
// module metadata is often broken on multi-platform projects // module metadata is often broken on multi-platform projects
withType(GenerateModuleMetadata::class.java).configureEach { withType<GenerateModuleMetadata>().configureEach {
enabled = false enabled = false
} }
withType(JavaCompile::class.java).configureEach { withType<JavaCompile>().configureEach {
options.encoding = "UTF-8" options.encoding = "UTF-8"
options.release = Integer.parseInt(java_version) options.release = Integer.parseInt(java_version)
options.compilerArgs.add("-Xdiags:verbose") options.compilerArgs.add("-Xdiags:verbose")
} }
withType(Jar::class.java).configureEach { withType<Jar>().configureEach {
from("${project.rootDir}/LICENSE.md") { from("${project.rootDir}/LICENSE.md") {
into("META-INF") into("META-INF")
} }
} }
withType(Javadoc::class.java).configureEach { withType<Javadoc>().configureEach {
options.optionFiles(project.rootProject.file("javadoc-options.txt")) options.optionFiles(project.rootProject.file("javadoc-options.txt"))
options.encoding = "UTF-8" options.encoding = "UTF-8"
} }
val replaceProperties = processResourcesExpandProperties.associateWith { project.property(it) as String } val replaceProperties = processResourcesExpandProperties.associateWith { project.property(it) as String }
withType(ProcessResources::class.java).configureEach { withType<ProcessResources>().configureEach {
inputs.properties(replaceProperties) inputs.properties(replaceProperties)
filesMatching(processResourcesExpandFiles) { filesMatching(processResourcesExpandFiles) {