Skip to content

Commit 119e3bf

Browse files
committed
create paper-checkstyle plugin
1 parent c8cac5e commit 119e3bf

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.papermc.paperweight.checkstyle
2+
3+
data class JavadocTag(val tag: String, val appliesTo: String, val prefix: String) {
4+
fun toOptionString(): String {
5+
return "$tag:$appliesTo:$prefix"
6+
}
7+
}
8+
9+
fun PaperCheckstyleTask.setCustomJavadocTags(tags: Iterable<JavadocTag>) {
10+
configProperties = (configProperties ?: emptyMap()).toMutableMap().apply {
11+
this["custom_javadoc_tags"] = tags.joinToString("|") { it.toOptionString() }
12+
}
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.papermc.paperweight.checkstyle
2+
3+
import io.papermc.paperweight.core.extension.PaperCheckstyleExt
4+
import io.papermc.paperweight.util.constants.*
5+
import org.gradle.api.Plugin
6+
import org.gradle.api.Project
7+
import org.gradle.api.plugins.quality.CheckstyleExtension
8+
import org.gradle.kotlin.dsl.*
9+
10+
abstract class PaperCheckstyle : Plugin<Project> {
11+
12+
override fun apply(target: Project) = with(target) {
13+
val ext = extensions.create(PAPER_CHECKSTYLE_EXTENSION, PaperCheckstyleExt::class)
14+
plugins.apply(PaperCheckstylePlugin::class.java)
15+
16+
extensions.configure(CheckstyleExtension::class.java) {
17+
toolVersion = "10.21.0"
18+
configDirectory.set(ext.projectLocalCheckstyleConfig)
19+
}
20+
21+
tasks.withType(PaperCheckstyleTask::class.java) {
22+
rootPath.set(project.rootDir.path)
23+
directoriesToSkip.set(ext.directoriesToSkip)
24+
typeUseAnnotations.set(ext.typeUseAnnotations)
25+
}
26+
Unit
27+
}
28+
}
29+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.papermc.paperweight.checkstyle
2+
3+
import org.gradle.api.plugins.quality.Checkstyle
4+
import org.gradle.api.plugins.quality.CheckstylePlugin
5+
6+
abstract class PaperCheckstylePlugin : CheckstylePlugin() {
7+
8+
override fun getTaskType(): Class<Checkstyle> {
9+
@Suppress("UNCHECKED_CAST")
10+
return PaperCheckstyleTask::class.java as Class<Checkstyle>
11+
}
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.papermc.paperweight.checkstyle
2+
3+
import org.gradle.api.plugins.quality.Checkstyle
4+
import org.gradle.api.provider.Property
5+
import org.gradle.api.provider.SetProperty
6+
import org.gradle.api.tasks.Input
7+
import org.gradle.api.tasks.TaskAction
8+
import java.nio.file.Paths
9+
import kotlin.io.path.relativeTo
10+
11+
abstract class PaperCheckstyleTask : Checkstyle() {
12+
13+
@get:Input
14+
abstract val rootPath: Property<String>
15+
16+
@get:Input
17+
abstract val directoriesToSkip: SetProperty<String>
18+
19+
@get:Input
20+
abstract val typeUseAnnotations: SetProperty<String>
21+
22+
@TaskAction
23+
override fun run() {
24+
val existingProperties = configProperties?.toMutableMap() ?: mutableMapOf()
25+
existingProperties["type_use_annotations"] = typeUseAnnotations.get().joinToString("|")
26+
configProperties = existingProperties
27+
exclude {
28+
if (it.isDirectory) return@exclude false
29+
val absPath = it.file.toPath().toAbsolutePath().relativeTo(Paths.get(rootPath.get()))
30+
val parentPath = (absPath.parent?.toString() + "/")
31+
directoriesToSkip.get().any { pkg -> parentPath == pkg }
32+
}
33+
if (!source.isEmpty) {
34+
super.run()
35+
}
36+
}
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.papermc.paperweight.core.extension
2+
3+
import javax.inject.Inject
4+
import org.gradle.api.file.DirectoryProperty
5+
import org.gradle.api.file.ProjectLayout
6+
import org.gradle.api.provider.SetProperty
7+
8+
@Suppress("LeakingThis")
9+
abstract class PaperCheckstyleExt {
10+
11+
@get:Inject
12+
abstract val layout: ProjectLayout
13+
14+
abstract val typeUseAnnotations: SetProperty<String>
15+
abstract val directoriesToSkip: SetProperty<String>
16+
abstract val projectLocalCheckstyleConfig: DirectoryProperty
17+
18+
init {
19+
projectLocalCheckstyleConfig.convention(layout.projectDirectory.dir(".checkstyle"))
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=io.papermc.paperweight.checkstyle.PaperCheckstyle

paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/constants/constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package io.papermc.paperweight.util.constants
2525
import org.gradle.api.Task
2626

2727
const val PAPERWEIGHT_EXTENSION = "paperweight"
28+
const val PAPER_CHECKSTYLE_EXTENSION = "paperCheckstyle"
2829
const val PAPERWEIGHT_DEBUG = "paperweight.debug"
2930
fun paperweightDebug(): Boolean = System.getProperty(PAPERWEIGHT_DEBUG, "false") == "true"
3031
const val PAPERWEIGHT_VERBOSE_APPLY_PATCHES = "paperweight.verboseApplyPatches"

0 commit comments

Comments
 (0)