Implement patch cleanup

This commit is contained in:
Kyle Wood 2020-12-22 21:52:01 -08:00
parent 4e08628fb5
commit 5979956f48
2 changed files with 103 additions and 44 deletions

View file

@ -1,4 +1,3 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation
import org.gradle.api.publish.maven.MavenPom
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
@ -110,36 +109,16 @@ val isSnapshot = version.toString().endsWith("-SNAPSHOT")
publishing {
publications {
register<MavenPublication>("shadow") {
groupId = project.group.toString()
artifactId = "io.papermc.paperweight.gradle.plugin"
version = project.version.toString()
project.shadow.component(this)
artifact(project.tasks.named("sourcesJar"))
for (artifact in artifacts) {
if (artifact.classifier == "all") {
artifact.classifier = null
}
}
withoutBuildIdentifier()
pom {
pomConfig()
}
pluginConfig(project.version.toString())
}
register<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()
from(components["java"])
withoutBuildIdentifier()
pom {
pomConfig()
}
standardConfig(project.version.toString())
}
register<MavenPublication>("shadowLocal") {
pluginConfig(localVersion())
}
register<MavenPublication>("mavenLocal") {
standardConfig(localVersion())
}
repositories {
@ -156,6 +135,50 @@ publishing {
}
}
tasks.withType(PublishToMavenRepository::class).configureEach {
onlyIf {
!publication.name.endsWith("Local")
}
}
tasks.withType(PublishToMavenLocal::class).configureEach {
onlyIf {
publication.name.endsWith("Local")
}
}
fun MavenPublication.standardConfig(versionName: String) {
groupId = project.group.toString()
artifactId = project.name
version = versionName
from(components["java"])
withoutBuildIdentifier()
pom {
pomConfig()
}
}
fun MavenPublication.pluginConfig(versionName: String) {
groupId = project.group.toString()
artifactId = "io.papermc.paperweight.gradle.plugin"
version = versionName
project.shadow.component(this)
artifact(project.tasks.named("sourcesJar"))
for (artifact in artifacts) {
if (artifact.classifier == "all") {
artifact.classifier = null
}
}
withoutBuildIdentifier()
pom {
pomConfig()
}
}
fun MavenPom.pomConfig() {
val repoUrl = "https://github.com/DemonWav/paperweight"
@ -193,3 +216,11 @@ fun MavenPom.pomConfig() {
developerConnection.set(connection)
}
}
fun localVersion(): String {
return if (isSnapshot) {
project.version.toString().substringBefore('-') + "-LOCAL-SNAPSHOT"
} else {
project.version.toString() + "-LOCAL"
}
}

View file

@ -2,6 +2,11 @@ package io.papermc.paperweight.tasks
import io.papermc.paperweight.util.Git
import io.papermc.paperweight.util.file
import java.io.File
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.Executors
import java.util.concurrent.Future
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Console
@ -30,7 +35,7 @@ abstract class RebuildPaperPatches : ControllableOutputTask() {
@TaskAction
fun run() {
val what =inputDir.file.name
val what = inputDir.file.name
val patchFolder = patchDir.file
if (!patchFolder.exists()) {
patchFolder.mkdirs()
@ -61,10 +66,11 @@ abstract class RebuildPaperPatches : ControllableOutputTask() {
}
Git(inputDir.file)("format-patch", "--zero-commit", "--full-index", "--no-signature", "--no-stat", "-N", "-o", patchFolder.absolutePath, if (server.get()) "base" else "upstream/upstream").executeSilently()
Git(patchFolder)("add", "-A", ".").executeSilently()
val patchDirGit = Git(patchFolder)
patchDirGit("add", "-A", ".").executeSilently()
if (filterPatches.get()) {
cleanupPatches()
cleanupPatches(patchDirGit)
}
if (printOutput.get()) {
@ -72,25 +78,47 @@ abstract class RebuildPaperPatches : ControllableOutputTask() {
}
}
private fun cleanupPatches() {
private fun cleanupPatches(git: Git) {
val patchFiles = patchDir.file.listFiles { f -> f.name.endsWith(".patch") } ?: emptyArray()
if (patchFiles.isEmpty()) {
return
}
patchFiles.sortBy { it.name }
for (patch in patchFiles) {
if (printOutput.get()) {
println(patch.name)
val noChangesPatches = ConcurrentLinkedQueue<File>()
val futures = mutableListOf<Future<*>>()
// Calling out to git over and over again for each `git diff --staged` command is really slow from the JVM
// so to mitigate this we do it parallel
val executor = Executors.newWorkStealingPool()
try {
for (patch in patchFiles) {
futures += executor.submit {
val hasNoChanges = git("diff", "--staged", patch.name).getText().lineSequence()
.filter { it.startsWith('+') || it.startsWith('-') }
.filterNot { it.startsWith("+++") || it.startsWith("---") }
.all { it.startsWith("+index") || it.startsWith("-index") }
if (hasNoChanges) {
noChangesPatches += patch
}
}
}
// TODO implement patch cleanup
//
// diffs=$($gitcmd diff --staged "$patch" | grep --color=none -E "^(\+|\-)" | grep --color=none -Ev "(\-\-\- a|\+\+\+ b|^.index)")
//
// if [ "x$diffs" == "x" ] ; then
// $gitcmd reset HEAD "$patch" >/dev/null
// $gitcmd checkout -- "$patch" >/dev/null
// fi
futures.forEach { it.get() }
} finally {
executor.shutdownNow()
}
if (noChangesPatches.isNotEmpty()) {
git("reset", "HEAD", *noChangesPatches.map { it.name }.toTypedArray()).executeSilently()
git("checkout", "--", *noChangesPatches.map { it.name }.toTypedArray()).executeSilently()
}
if (printOutput.get()) {
for (patch in patchFiles) {
println(patch.name)
}
}
}
}