Skip to content

Commit

Permalink
move to convention plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Dec 31, 2024
1 parent c130533 commit 567e547
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 167 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ Shane Freeder
Spottedleaf
Maddy Miller
me4502

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.nio.file.Path
import kotlin.random.Random

plugins {
id("io.papermc.paperweight.core") version "2.0.0-beta.10" apply false
id("io.papermc.paperweight.core") apply false
}

subprojects {
Expand Down
12 changes: 12 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation("io.papermc.paperweight.core:io.papermc.paperweight.core.gradle.plugin:2.0.0-beta.10")
}
31 changes: 31 additions & 0 deletions buildSrc/src/main/kotlin/ChangedFilesSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.gradle.api.provider.ValueSource
import org.gradle.api.provider.ValueSourceParameters
import org.gradle.process.ExecOperations
import java.io.ByteArrayOutputStream
import javax.inject.Inject

abstract class ChangedFilesSource: ValueSource<Set<String>, ValueSourceParameters.None> {

@get:Inject
abstract val exec: ExecOperations

private fun run(vararg args: String): String {
val out = ByteArrayOutputStream()
exec.exec {
commandLine(*args)
standardOutput = out
}

return String(out.toByteArray(), Charsets.UTF_8).trim()
}

override fun obtain(): Set<String> {
val remoteName = run("git", "remote", "-v").split("\n").filter {
it.contains("PaperMC/Paper", ignoreCase = true)
}.take(1).map { it.split("\t")[0] }.singleOrNull() ?: "origin"
run("git", "fetch", remoteName, "main", "-q")
val mergeBase = run("git", "merge-base", "HEAD", "$remoteName/main")
val changedFiles = run("git", "diff", "--name-only", mergeBase).split("\n").filter { it.endsWith(".java") }.toSet()
return changedFiles
}
}
50 changes: 50 additions & 0 deletions buildSrc/src/main/kotlin/CollectDiffedDataTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io.papermc.paperweight.tasks.BaseTask
import io.papermc.paperweight.util.cacheDir
import io.papermc.paperweight.util.deleteForcefully
import io.papermc.paperweight.util.path
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import kotlin.io.path.writeText

abstract class CollectDiffedDataTask : BaseTask() {

@get:Input
abstract val uncheckedFiles: SetProperty<String>

@get:Input
abstract val specialUsers: SetProperty<String>

@get:Input
abstract val changedFiles: SetProperty<String>

@get:Input
abstract val gitUser: Property<String>

@get:OutputFile
abstract val changedFilesTxt: RegularFileProperty

@get:OutputFile
abstract val filesToRemoveFromUncheckedTxt: RegularFileProperty

override fun init() {
changedFilesTxt.convention(layout.cacheDir("diffed-files").file("changed-files.txt"))
filesToRemoveFromUncheckedTxt.convention(layout.cacheDir("diffed-files").file("files-to-remove-from-unchecked.txt"))
}

@TaskAction
fun run() {
changedFilesTxt.path.deleteForcefully()
filesToRemoveFromUncheckedTxt.path.deleteForcefully()
if (gitUser.get() in specialUsers.get()) {
changedFilesTxt.path.writeText(changedFiles.get().joinToString("\n"))
filesToRemoveFromUncheckedTxt.path.writeText(changedFiles.get().intersect(uncheckedFiles.get()).joinToString("\n"))
} else {
changedFilesTxt.path.writeText(changedFiles.get().minus(uncheckedFiles.get()).joinToString("\n"))
filesToRemoveFromUncheckedTxt.path.writeText("")
}
}
}
53 changes: 53 additions & 0 deletions buildSrc/src/main/kotlin/CustomCheckstyleTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import io.papermc.paperweight.util.path
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.plugins.quality.Checkstyle
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import java.nio.file.Paths
import kotlin.io.path.readLines
import kotlin.io.path.relativeTo

abstract class CustomCheckstyleTask : Checkstyle() {

@get:Input
abstract val rootPath: Property<String>

@get:InputFile
abstract val changedFilesTxt: RegularFileProperty

@get:Input
@get:Optional
abstract val runForAll: Property<Boolean>

@get:InputFile
abstract val filesToRemoveFromUncheckedTxt: RegularFileProperty

@get:Input
abstract val typeUseAnnotations: SetProperty<String>

@TaskAction
override fun run() {
val diffedFiles = changedFilesTxt.path.readLines().filterNot { it.isBlank() }.toSet()
val existingProperties = configProperties?.toMutableMap() ?: mutableMapOf()
existingProperties["type_use_annotations"] = typeUseAnnotations.get().joinToString("|")
configProperties = existingProperties
include { fileTreeElement ->
if (fileTreeElement.isDirectory || runForAll.getOrElse(false)) {
return@include true
}
val absPath = fileTreeElement.file.toPath().toAbsolutePath().relativeTo(Paths.get(rootPath.get()))
return@include diffedFiles.contains(absPath.toString())
}
if (!source.isEmpty) {
super.run()
}
val uncheckedFiles = filesToRemoveFromUncheckedTxt.path.readLines().filterNot { it.isBlank() }.toSet()
if (uncheckedFiles.isNotEmpty()) {
error("Remove the following files from unchecked-files.txt: ${uncheckedFiles.joinToString("\n\t", prefix = "\n")}")
}
}
}
11 changes: 11 additions & 0 deletions buildSrc/src/main/kotlin/JavadocTag.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
data class JavadocTag(val tag: String, val appliesTo: String, val prefix: String) {
fun toOptionString(): String {
return "$tag:$appliesTo:$prefix"
}
}

fun CustomCheckstyleTask.setCustomJavadocTags(tags: Iterable<JavadocTag>) {
configProperties = (configProperties ?: emptyMap()).toMutableMap().apply {
this["custom_javadoc_tags"] = tags.joinToString("|") { it.toOptionString() }
}
}
50 changes: 50 additions & 0 deletions buildSrc/src/main/kotlin/checkstyle-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io.papermc.paperweight.util.convention
import java.nio.file.Path


abstract class CustomCheckstylePlugin : CheckstylePlugin() {

override fun getTaskType(): Class<Checkstyle> {
@Suppress("UNCHECKED_CAST")
return CustomCheckstyleTask::class.java as Class<Checkstyle>
}
}

apply {
plugin(CustomCheckstylePlugin::class.java)
}

// config dir for checkstyle extras
val checkstyleExtraConfigDir = objects.directoryProperty().convention(rootProject, Path.of(".checkstyle"))
val localCheckstyleConfigDir = objects.directoryProperty().convention(project, Path.of(".checkstyle"))

extensions.configure<CheckstyleExtension>() {
toolVersion = "10.21.0"
configDirectory = localCheckstyleConfigDir
}

val gitUserProvider: Provider<String> = if (System.getenv("CI") == "true") {
providers.environmentVariable("GIT_USER").map { it.trim() }
} else {
providers.exec { commandLine("git", "config", "--get", "user.name") }.standardOutput.asText.map { it.trim() }
}
val changedFilesSource = providers.of(ChangedFilesSource::class) {}

val collectDiffedData = tasks.register<CollectDiffedDataTask>("collectDiffedData") {
uncheckedFiles.set(providers.fileContents(localCheckstyleConfigDir.file("unchecked-files.txt")).asText.map { it.split("\n").toSet() })
specialUsers.set(providers.fileContents(checkstyleExtraConfigDir.file("users-who-can-update.txt")).asText.map { it.split("\n").toSet() })
changedFiles.set(changedFilesSource)
gitUser.set(gitUserProvider)
}

val typeUseAnnotationsProvider: Provider<Set<String>> = providers.fileContents(checkstyleExtraConfigDir.file("type-use-annotations.txt"))
.asText.map { it.trim().split("\n").toSet() }

tasks.withType<CustomCheckstyleTask> {
rootPath = project.rootDir.path
changedFilesTxt = collectDiffedData.flatMap { it.changedFilesTxt }
runForAll = providers.gradleProperty("runCheckstyleForAll").map { it.toBoolean() }
filesToRemoveFromUncheckedTxt = collectDiffedData.flatMap { it.filesToRemoveFromUncheckedTxt }
typeUseAnnotations = typeUseAnnotationsProvider
}

Loading

0 comments on commit 567e547

Please sign in to comment.