Skip to content

Commit

Permalink
Build
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaKR93 committed Jun 2, 2024
1 parent 4f2deab commit 5557065
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ "ver/*", "dev/*", "feat/**/*" ]
paths:
- "**.patch"
- "*.patch"
- "gradle.properties"
workflow_dispatch:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import io.papermc.paperweight.util.Git
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.plazmamc.alwaysuptodate.utils.*
import org.plazmamc.alwaysuptodate.tasks.CheckUpstreamCommit
import org.plazmamc.alwaysuptodate.tasks.SimpleUpstreamUpdateTask
import org.plazmamc.alwaysuptodate.tasks.CreateCompareComment
import org.plazmamc.alwaysuptodate.tasks.PurpurUpdateTask
import org.gradle.api.tasks.TaskProvider
import org.plazmamc.alwaysuptodate.tasks.*
import org.plazmamc.alwaysuptodate.utils.CheckGitTask
import org.plazmamc.alwaysuptodate.utils.configureTask
import org.plazmamc.alwaysuptodate.utils.flatten
import org.plazmamc.alwaysuptodate.utils.registerTask

class AlwaysUpToDate : Plugin<Project> {

Expand All @@ -23,16 +24,16 @@ class AlwaysUpToDate : Plugin<Project> {
fun generateTasks(
upstream: String,
provider: AlwaysUpToDateExtension.() -> Pair<Pair<Property<String>, Property<String>>, Property<String>>
) {
registerTask<CheckUpstreamCommit>("check$upstream", "Check if the $upstream commit is up to date") {
): Triple<TaskProvider<CheckUpstreamCommit>, TaskProvider<CreateCompareComment>, TaskProvider<CreateCompareComment>> {
val i = registerTask<CheckUpstreamCommit>("check$upstream", "Check if the $upstream commit is up to date") {
dependsOn(git)
val (repo, ref, commitProperty) = extension.provider().flatten()
this.repo.set(repo)
this.ref.set(ref)
this.commitPropertyName.set(commitProperty)
}

registerTask<CreateCompareComment>("compare$upstream", "Create a comment comparing the $upstream commit") {
val j = registerTask<CreateCompareComment>("compare$upstream", "Create a comment comparing the $upstream commit") {
dependsOn(git)
val (repo, ref, commitProperty) = extension.provider().flatten()
this.repo.set(repo)
Expand All @@ -41,29 +42,33 @@ class AlwaysUpToDate : Plugin<Project> {
this.clear.set(false)
}

registerTask<CreateCompareComment>("cleanCompare$upstream", "Create a comment comparing the $upstream commit") {
val k = registerTask<CreateCompareComment>(
"cleanCompare$upstream",
"Create a comment comparing the $upstream commit"
) {
dependsOn(git)
val (repo, ref, commitProperty) = extension.provider().flatten()
this.repo.set(repo)
this.ref.set(ref)
this.commitPropertyName.set(commitProperty)
this.clear.set(true)
}

return (i to j to k).flatten()
}

generateTasks("Paper") { paperRepo to paperRef to paperCommitName }
generateTasks("Purpur") { purpurRepo to purpurRef to purpurCommitName }
val (checkPaper, comparePaper, cleanComparePaper) = generateTasks("Paper") { paperRepo to paperRef to paperCommitName }
val (checkPurpur, comparePurpur, cleanComparePurpur) = generateTasks("Purpur") { purpurRepo to purpurRef to purpurCommitName }

registerTask<SimpleUpstreamUpdateTask>("updateUpstream", "Update the upstream commit") {
dependsOn(git)
repo.convention(extension.paperRepo)
ref.convention(extension.paperRef)
commitPropertyName.convention(extension.paperCommitName)
workDir.set(layout.projectDirectory)
regex.convention("paperCommit = ")
}

registerTask<PurpurUpdateTask>("updateImplementation", "Update the implementation commit") {
registerTask<PurpurUpdateTask>("updateImplementation", "Update the implementation") {
dependsOn(git)
workDir.set(layout.projectDirectory)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package org.plazmamc.alwaysuptodate.tasks
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.plazmamc.alwaysuptodate.AlwaysUpToDateException
import org.plazmamc.alwaysuptodate.AlwaysUpToDateExtension
import org.plazmamc.alwaysuptodate.utils.*
import org.plazmamc.alwaysuptodate.utils.extension
import org.plazmamc.alwaysuptodate.utils.flatten
import org.plazmamc.alwaysuptodate.utils.git
import org.plazmamc.alwaysuptodate.utils.property

abstract class CheckUpstreamCommit : Task() {

Expand All @@ -19,8 +21,13 @@ abstract class CheckUpstreamCommit : Task() {
@get:Input
abstract val commitPropertyName: Property<String>

@TaskAction
fun check() = with(project) { println(checkCommitFor { repo to ref to commitPropertyName }) }
override fun init(): Unit = with(project) {
outputs.upToDateWhen { checkCommitFor { repo to ref to commitPropertyName } }

doLast {
println(checkCommitFor { repo to ref to commitPropertyName })
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ abstract class CreateCompareComment : Task() {
builder.append("\n\n[${rawRepo.split("/").last()} Changes]\n")

gson.fromJson<JsonObject>(
URI.create("https://api.github.com/repos/$rawRepo/compare/${property { commitPropertyName }}...${ref.get()}")
.toURL().readText())["commits"].asJsonArray.forEach { obj ->
obj.asJsonObject.let { builder.append("$rawRepo@${it["sha"].asString.subSequence(0, 7)}: ${it["commit"].asJsonObject["message"].asString.split("\n")[0]}\n") }
URI.create("https://api.github.com/repos/$rawRepo/compare/${property { commitPropertyName }}...${ref.get()}").toURL().readText()
)["commits"].asJsonArray.forEach { obj ->
obj.asJsonObject.let {
builder.append("$rawRepo@${it["sha"].asString.subSequence(0, 7)}: ${it["commit"].asJsonObject["message"].asString.split("\n")[0]}\n")
}
}
file("compare.txt").writeText(builder.toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
import org.plazmamc.alwaysuptodate.utils.addCommit
import java.io.File

abstract class GenerateMergedAPIPatch : Task() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.plazmamc.alwaysuptodate.tasks

import io.papermc.paperweight.patcher.tasks.CheckoutRepo
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.Git
import io.papermc.paperweight.util.cache
import io.papermc.paperweight.util.path
import io.papermc.paperweight.util.set
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Provider
Expand All @@ -12,8 +15,10 @@ import org.gradle.api.tasks.TaskProvider
import org.plazmamc.alwaysuptodate.utils.*
import java.io.File
import java.nio.file.Path
import java.util.Calendar
import kotlin.io.path.*
import java.util.*
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.name
import kotlin.io.path.walk

@Deprecated("It will soon be changed to be available for other upstreams.")
abstract class PurpurUpdateTask : Task() {
Expand All @@ -35,7 +40,7 @@ abstract class PurpurUpdateTask : Task() {
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
""".trimIndent()
private val pufferfishAuthor = "--author=Kevin Raneri <[email protected]>"
private val pufferfishAuthor = "Kevin Raneri <[email protected]>"
private val purpurHeader = """
PurpurMC
Copyright (C) ${Calendar.getInstance().get(Calendar.YEAR)} PurpurMC
Expand All @@ -58,7 +63,7 @@ abstract class PurpurUpdateTask : Task() {
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
""".trimIndent()
private val purpurAuthor = "--author=granny <[email protected]>"
private val purpurAuthor = "granny <[email protected]>"

@get:Internal
abstract val workDir: DirectoryProperty
Expand All @@ -70,28 +75,29 @@ abstract class PurpurUpdateTask : Task() {
abstract val pufferfishDir: DirectoryProperty

override fun init(): Unit = with(project) {
outputs.upToDateWhen { checkCommitFor { purpurRepo to purpurRef to purpurCommitName } }

val wd = layout.cache.resolve("alwaysUpToDate/update/purpur").also { it.toFile().deleteRecursively() }
workDir.set(wd)

val compare = configureTask<CreateCompareComment>("createPurpurCompareComment", "Create Purpur Compare Comment") {
clear.convention(true)
repo.convention(extension.purpurRepo)
ref.convention(extension.purpurRef)
commitPropertyName.convention(extension.purpurCommitName)
}
val compare =
configureTask<CreateCompareComment>("createPurpurCompareComment", "Create Purpur Compare Comment") {
clear.convention(true)
repo.convention(extension.purpurRepo)
ref.convention(extension.purpurRef)
commitPropertyName.convention(extension.purpurCommitName)
}

val paper = dependsOn<SimpleUpstreamUpdateTask>("updatePaper", "Update Paper") {
outputs.upToDateWhen { checkCommitFor { repo to ref to paperCommitName } }
dependsOn(compare)
repo.convention(extension.paperRepo)
ref.convention(extension.paperRef)
regex.convention("paperCommit = ")
commitPropertyName.convention(extension.paperCommitName)
workDir.set(layout.projectDirectory)
}

fun checkout(name: String, repo: Provider<String>, ref: Provider<String>, regex: String, block: CheckoutRepo.() -> Unit): Pair<TaskProvider<out Task>, Directory> {
fun checkout(
name: String, repo: Provider<String>, ref: Provider<String>, regex: String, block: CheckoutRepo.() -> Unit
): Pair<TaskProvider<out Task>, Directory> {
val updatePaper = configureTask<SimpleUpstreamUpdateTask>("update${name}Paper", "Update $name's Paper") {
this.repo.convention(extension.paperRepo)
this.ref.convention(extension.paperRef)
Expand All @@ -113,8 +119,14 @@ abstract class PurpurUpdateTask : Task() {
return updatePaper to checkout.flatMap { it.outputDir }.get()
}

val (checkoutPufferfish, pufferfish) = checkout("Pufferfish", extension.pufferfishRepo, extension.pufferfishRef, "paperRef=") { onlyIf { extension { usePufferfish } } }
val (checkoutPurpur, purpur) = checkout("Purpur", extension.purpurRepo, extension.purpurRef, "paperCommit = ") { dependsOn(checkoutPufferfish) }
val (checkoutPufferfish, pufferfish) =
checkout("Pufferfish", extension.pufferfishRepo, extension.pufferfishRef, "paperRef=") {
onlyIf { extension { usePufferfish } }
}
val (checkoutPurpur, purpur) =
checkout("Purpur", extension.purpurRepo, extension.purpurRef, "paperCommit = ") {
dependsOn(checkoutPufferfish)
}

pufferfishDir.set(pufferfish)
purpurDir.set(purpur)
Expand All @@ -139,36 +151,40 @@ abstract class PurpurUpdateTask : Task() {

dependsOn(preparePurpur, preparePufferfish)

val serverPatch = configureTask<GenerateMergedServerPatch>("generateMergedServerPatches", "Generate Merged Server Patch") {
dependsOn(preparePurpur)
if (!extension { usePufferfish }) {
workDir.convention(purpur.dir("Purpur-Server"))
commitTitle.convention("Purpur Server Changes")
license.convention(purpurHeader)
author.convention(purpurAuthor)
return@configureTask
}
val serverPatch =
configureTask<GenerateMergedServerPatch>("generateMergedServerPatches", "Generate Merged Server Patch") {
dependsOn(preparePurpur)
if (!extension { usePufferfish }) {
workDir.convention(purpur.dir("Purpur-Server"))
commitTitle.convention("Purpur Server Changes")
license.convention(purpurHeader)
author.convention(purpurAuthor)
return@configureTask
}

dependsOn(preparePufferfish)
workDir.convention(pufferfish.dir("pufferfish-server"))
commitTitle.convention("Pufferfish Server Changes")
license.convention(pufferfishHeader)
author.convention(pufferfishAuthor)
dependsOn(preparePufferfish)
workDir.convention(pufferfish.dir("pufferfish-server"))
commitTitle.convention("Pufferfish Server Changes")
license.convention(pufferfishHeader)
author.convention(pufferfishAuthor)

doLast {
val dotGit = pufferfish.dir("pufferfish-server/.git").path.toFile()
doLast {
val dotGit = pufferfish.dir("pufferfish-server/.git").path.toFile()

purpur.path.resolve("Purpur-Server").let {
val purpurDotGit = it.resolve(".git").toFile().also(File::deleteRecursively)
purpur.path.resolve("Purpur-Server").let {
val purpurDotGit = it.resolve(".git").toFile().also(File::deleteRecursively)

copySource(it)
dotGit.copyRecursively(purpurDotGit, overwrite = true)
Git(it).addCommit("Purpur Server Changes\n\n$purpurHeader", purpurAuthor)
copySource(it)
dotGit.copyRecursively(purpurDotGit, overwrite = true)
Git(it).addCommit("Purpur Server Changes\n\n$purpurHeader", "--author=$purpurAuthor")
}
}
}
}

val pufferfishAPIChanges = configureTask<GenerateMergedAPIPatch>("generateMergedPufferfishAPIPatch", "Generate Merged Pufferfish API Patch") {
val pufferfishAPIChanges = configureTask<GenerateMergedAPIPatch>(
"generateMergedPufferfishAPIPatch",
"Generate Merged Pufferfish API Patch"
) {
dependsOn(preparePufferfish)
inputDir.convention(pufferfish.dir(".gradle/caches/paperweight/upstreams/paper/Paper-API/.git"))
workDir.convention(pufferfish.dir("pufferfish-api"))
Expand All @@ -177,7 +193,10 @@ abstract class PurpurUpdateTask : Task() {
author.convention(pufferfishAuthor)
}

val apiPatch = configureTask<GenerateMergedAPIPatch>("generateMergedAPIPatches", "Generate Merged API Patches") {
val apiPatch = configureTask<GenerateMergedAPIPatch>(
"generateMergedAPIPatches",
"Generate Merged API Patches"
) {
dependsOn(preparePurpur)
if (extension { usePufferfish }) dependsOn(pufferfishAPIChanges)

Expand All @@ -204,19 +223,21 @@ abstract class PurpurUpdateTask : Task() {

val patches = purpur.resolve("patches")
with(layout.projectDirectory.path.resolve("patches")) {
patches.resolve("server").copyPatch(resolve("server"),
patches.resolve("server").copyPatch( resolve("server"),
if (pufferfish == null) "" else "Pufferfish-Server-Changes",
"Purpur-Server-Changes"
)

patches.resolve("api").copyPatch(resolve("api"),
patches.resolve("api").copyPatch( resolve("api"),
if (pufferfish == null) "" else "Pufferfish-API-Changes",
"Purpur-API-Changes"
)
}

file("gradle.properties").let {
it.writeText(it.readText().replace("purpurCommit = .*".toRegex(), "purpurCommit = ${Git(purpur).revParse()}"))
it.writeText(
it.readText().replace("purpurCommit = .*".toRegex(), "purpurCommit = ${Git(purpur).revParse()}")
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.plazmamc.alwaysuptodate.AlwaysUpToDateException
import org.plazmamc.alwaysuptodate.utils.*
import org.plazmamc.alwaysuptodate.utils.dependsOn
import org.plazmamc.alwaysuptodate.utils.extension

abstract class SimpleUpstreamUpdateTask : Task() {

Expand All @@ -19,25 +20,15 @@ abstract class SimpleUpstreamUpdateTask : Task() {
@get:Input
abstract val ref: Property<String>

@get:Input
@get:Optional
abstract val commitPropertyName: Property<String>

@get:InputDirectory
abstract val workDir: DirectoryProperty

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

override fun init(): Unit = with(project) {
outputs.upToDateWhen {
if (commitPropertyName.orNull != null)
checkCommitFor { repo to ref to (commitPropertyName) }
else
false
}

dependsOn<CreateCompareComment>("createCompareComment", "Create Paper Compare Comment") {
onlyIf { !this@SimpleUpstreamUpdateTask.state.upToDate }
clear.convention(false)
repo.convention(extension.paperRepo)
ref.convention(extension.paperRef)
Expand All @@ -49,9 +40,9 @@ abstract class SimpleUpstreamUpdateTask : Task() {
fun update() = (Git(workDir.path)("ls-remote", repo.get()).readText()?.lines()
?.filterNot("[a-z0-9]{40}\trefs/heads/${ref.get()}".toRegex()::matches)?.first()?.split("\t")?.first()
?: throw AlwaysUpToDateException("Failed to get latest commit")).let { commit ->
workDir.file("gradle.properties").path.toFile().let {
it.writeText(it.readText().replace("${regex.get()}.*".toRegex(), "${regex.get()}$commit"))
}
workDir.file("gradle.properties").path.toFile().let {
it.writeText(it.readText().replace("${regex.get()}.*".toRegex(), "${regex.get()}$commit"))
}
}

}
Loading

0 comments on commit 5557065

Please sign in to comment.