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

View file

@ -3,6 +3,7 @@ package com.jozufozu.gradle.nullability
import org.gradle.api.Project
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.SourceSet
import org.gradle.kotlin.dsl.register
open class PackageInfosExtension(private val project: Project) {
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.
val sourceSetName = sourceSet.name
val taskName = sourceSet.getTaskName("generate", "PackageInfos")
val task = project.tasks.register(taskName, GeneratePackageInfosTask::class.java) {
val task = project.tasks.register<GeneratePackageInfosTask>(taskName) {
group = "flywheel"
description = "Generates package-info files for $sourceSetName packages."
@ -31,7 +32,7 @@ open class PackageInfosExtension(private val project: Project) {
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"
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.javadoc.Javadoc
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.*
import org.gradle.language.jvm.tasks.ProcessResources
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
@ -65,17 +64,17 @@ open class PlatformExtension(val project: Project) {
fun compileWithCommonSourceSets() {
project.tasks.apply {
withType(JavaCompile::class.java).configureEach {
withType<JavaCompile>().configureEach {
JarTaskSet.excludeDuplicatePackageInfos(this)
}
sources.forEach {
val commonSourceSet = commonSourceSets.named(it.name).get()
named(it.compileJavaTaskName, JavaCompile::class.java).configure {
named<JavaCompile>(it.compileJavaTaskName).configure {
source(commonSourceSet.allJava)
}
named(it.processResourcesTaskName, ProcessResources::class.java).configure {
named<ProcessResources>(it.processResourcesTaskName).configure {
from(commonSourceSet.resources)
}
}
@ -87,20 +86,20 @@ open class PlatformExtension(val project: Project) {
val extraSourceSets = sources.filter { it.name != "main" }.toList()
val commonSources = sources.map { commonSourceSets.named(it.name).get() }
named("jar", Jar::class.java).configure {
named<Jar>("jar").configure {
extraSourceSets.forEach { from(it.output) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
named("javadoc", Javadoc::class.java).configure {
named<Javadoc>("javadoc").configure {
commonSources.forEach { source(it.allJava) }
extraSourceSets.forEach { source(it.allJava) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
named("sourcesJar", Jar::class.java).configure {
named<Jar>("sourcesJar").configure {
commonSources.forEach { from(it.allJava) }
extraSourceSets.forEach { from(it.allJava) }
@ -110,12 +109,12 @@ open class PlatformExtension(val project: Project) {
}
fun publishMod() {
val remapJar = project.tasks.named("remapJar", RemapJarTask::class.java)
val remapSourcesJar = project.tasks.named("remapSourcesJar", RemapSourcesJarTask::class.java)
val javadocJar = project.tasks.named("javadocJar", Jar::class.java)
val remapJar = project.tasks.named<RemapJarTask>("remapJar")
val remapSourcesJar = project.tasks.named<RemapSourcesJarTask>("remapSourcesJar")
val javadocJar = project.tasks.named<Jar>("javadocJar")
project.the<PublishingExtension>().publications {
register("modMaven", MavenPublication::class.java) {
register<MavenPublication>("modMaven") {
artifact(remapJar)
artifact(remapSourcesJar)
artifact(javadocJar)
@ -126,7 +125,7 @@ open class PlatformExtension(val project: Project) {
fun publishRemap(artifactId: String, jarSet: JarTaskSet) {
project.the<PublishingExtension>().publications {
register("${jarSet.name}RemapMaven", MavenPublication::class.java) {
register<MavenPublication>("${jarSet.name}RemapMaven") {
artifact(jarSet.remapJar)
artifact(jarSet.remapSources)
artifact(jarSet.javadocJar)
@ -137,7 +136,7 @@ open class PlatformExtension(val project: Project) {
fun publish(artifactId: String, jarSet: JarTaskSet) {
project.the<PublishingExtension>().publications {
register("${jarSet.name}Maven", MavenPublication::class.java) {
register<MavenPublication>("${jarSet.name}Maven") {
artifact(jarSet.jar)
artifact(jarSet.sources)
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.jvm.tasks.Jar
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.kotlin.dsl.assign
import org.gradle.kotlin.dsl.maven
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.*
import org.gradle.language.jvm.tasks.ProcessResources
@ -113,36 +110,36 @@ class SubprojectPlugin: Plugin<Project> {
project.tasks.apply {
// make builds reproducible
withType(AbstractArchiveTask::class.java).configureEach {
withType<AbstractArchiveTask>().configureEach {
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
}
// module metadata is often broken on multi-platform projects
withType(GenerateModuleMetadata::class.java).configureEach {
withType<GenerateModuleMetadata>().configureEach {
enabled = false
}
withType(JavaCompile::class.java).configureEach {
withType<JavaCompile>().configureEach {
options.encoding = "UTF-8"
options.release = Integer.parseInt(java_version)
options.compilerArgs.add("-Xdiags:verbose")
}
withType(Jar::class.java).configureEach {
withType<Jar>().configureEach {
from("${project.rootDir}/LICENSE.md") {
into("META-INF")
}
}
withType(Javadoc::class.java).configureEach {
withType<Javadoc>().configureEach {
options.optionFiles(project.rootProject.file("javadoc-options.txt"))
options.encoding = "UTF-8"
}
val replaceProperties = processResourcesExpandProperties.associateWith { project.property(it) as String }
withType(ProcessResources::class.java).configureEach {
withType<ProcessResources>().configureEach {
inputs.properties(replaceProperties)
filesMatching(processResourcesExpandFiles) {