Skip to content

Commit

Permalink
Fix AlwaysUpToDate
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaKR93 committed May 28, 2024
1 parent e6586da commit 75f8807
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import org.plazmamc.alwaysuptodate.tasks.PurpurUpdateTask

class AlwaysUpToDate : Plugin<Project> {

override fun apply(target: Project) {
override fun apply(target: Project) = with(target) {

target.extensions.create("alwaysUpToDate", AlwaysUpToDateExtension::class.java)
target.tasks.register("updatePaper", PaperUpdateTask::class.java)
target.tasks.register("updatePurpur", PurpurUpdateTask::class.java)
target.tasks.register("checkPaperCommit", CheckPaperCommitTask::class.java)
target.tasks.register("checkPurpurCommit", CheckPurpurCommitTask::class.java)
extensions.create("alwaysUpToDate", AlwaysUpToDateExtension::class.java)

arrayOf(
"updatePaper" to PaperUpdateTask::class.java,
"updatePurpur" to PurpurUpdateTask::class.java,
"checkPaperCommit" to CheckPaperCommitTask::class.java,
"checkPurpurCommit" to CheckPurpurCommitTask::class.java,
).forEach { tasks.register(it.first, it.second) }

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ import org.plazmamc.alwaysuptodate.AlwaysUpToDateException
import org.plazmamc.alwaysuptodate.AlwaysUpToDateExtension
import org.plazmamc.alwaysuptodate.utils.pathIO
import paper.libs.com.google.gson.JsonObject
import java.net.URI
import java.net.URI.create

abstract class CheckPaperCommitTask : Task() {

private val property = project.extensions["alwaysUpToDate"] as AlwaysUpToDateExtension

@TaskAction
fun check() {
println(project.checkCommit(
project.property(property.paperRepoName.get()).toString(),
project.property(property.paperBranchName.get()).toString(),
property.paperCommitName.get()
))
}
fun check() = println(project.checkCommit(
project.property(property.paperRepoName.get()).toString(),
project.property(property.paperBranchName.get()).toString(),
property.paperCommitName.get()
))

}

Expand All @@ -32,41 +30,32 @@ abstract class CheckPurpurCommitTask : Task() {
private val property = project.extensions["alwaysUpToDate"] as AlwaysUpToDateExtension

@TaskAction
fun check() {
println(project.checkCommit(
project.property(property.purpurRepoName.get()).toString(),
project.property(property.purpurBranchName.get()).toString(),
property.purpurCommitName.get()
))
}
fun check() = println(project.checkCommit(
project.property(property.purpurRepoName.get()).toString(),
project.property(property.purpurBranchName.get()).toString(),
property.purpurCommitName.get()
))

}

fun Project.getLatest(repository: String, branch: String) : String {
val regex = "[a-z0-9]{40}\trefs/heads/$branch".toRegex()
val temp = Git(project.pathIO)("ls-remote", repository).readText()

return temp?.lines()?.first(regex::matches)?.split("\t")?.first()
fun Project.getLatest(repository: String, branch: String) =
Git(project.pathIO)("ls-remote", repository).readText()
?.lines()?.first("[a-z0-9]{40}\trefs/heads/$branch".toRegex()::matches)?.split("\t")?.first()
?: throw AlwaysUpToDateException("Failed to get latest commit of $repository")
}

fun Project.checkCommit(repository: String, branch: String, propertyName: String) : Boolean {
val latestCommit = project.getLatest(repository, branch)
val currentCommit = project.properties[propertyName] as String

return currentCommit == latestCommit
}
fun Project.checkCommit(repository: String, branch: String, propertyName: String) =
project.getLatest(repository, branch) == project.properties[propertyName] as String

fun Project.createCompareComment(repository: String, branch: String, before: String, clear: Boolean = false) {
val builder = StringBuilder()
val rawRepo = URI.create(repository).path.substring(1)
val rawRepo = create(repository).path.substring(1)

if (!clear) builder.append(project.file("compare.txt").readText())
if (clear) builder.append("\n\nUpstream has released updates that appear to apply and compile correctly.")
else builder.append(project.file("compare.txt").readText())
builder.append("\n\n[${rawRepo.split("/").last()} Changes]\n")

gson.fromJson<JsonObject>(URI.create("https://api.github.com/repos/$rawRepo/compare/$before...$branch").toURL().readText())["commits"].asJsonArray.forEach {
val commit = it.asJsonObject
builder.append("$rawRepo@${commit["sha"].asString.subSequence(0, 7)}: ${commit["commit"].asJsonObject["message"].asString.split("\n")[0]}\n")
gson.fromJson<JsonObject>(create("https://api.github.com/repos/$rawRepo/compare/$before...$branch").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") }
}
project.file("compare.txt").writeText(builder.toString())
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,43 @@ abstract class PaperUpdateTask : Task() {

private val property = project.extensions["alwaysUpToDate"] as AlwaysUpToDateExtension

override fun init() {
outputs.upToDateWhen {
project.checkCommit(
project.property(property.paperRepoName.get()).toString(),
project.property(property.paperBranchName.get()).toString(),
property.paperCommitName.get()
)
}
override fun init() = outputs.upToDateWhen {
project.checkCommit(
project.property(property.paperRepoName.get()).toString(),
project.property(property.paperBranchName.get()).toString(),
property.paperCommitName.get()
)
}

@TaskAction
fun update() {
if (project.checkCommit(
project.property(property.paperRepoName.get()).toString(),
project.property(property.purpurBranchName.get()).toString(),
fun update() = with(project) {
if (
checkCommit(
property(property.paperRepoName.get()).toString(),
property(property.paperBranchName.get()).toString(),
property.purpurCommitName.get()
)) return
)
) return

project.createCompareComment(
project.property(property.paperRepoName.get()).toString(),
project.property(property.paperBranchName.get()).toString(),
project.property(property.paperCommitName.get()).toString(),
createCompareComment(
property(property.paperRepoName.get()).toString(),
property(property.paperBranchName.get()).toString(),
property(property.paperCommitName.get()).toString(),
true
)

updatePaperCommit(
project.property(property.paperRepoName.get()).toString(),
project.property(property.paperBranchName.get()).toString(),
project.file("gradle.properties")
property(property.paperRepoName.get()).toString(),
property(property.paperBranchName.get()).toString(),
file("gradle.properties")
)
}

}

fun updatePaperCommit(repo: String, branch: String, properties: File, regexRule: String = "paperCommit = ") {
val latestCommit = Git(properties.parentFile.toPath())("ls-remote", repo).readText()?.lines()
?.filterNot { "[a-z0-9]{40}\trefs/heads/$branch".toRegex().matches(it) }?.first()?.split("\t")?.first()
?: throw AlwaysUpToDateException("Failed to get latest Paper commit")

properties.writeText(properties.readText().replace("$regexRule.*".toRegex(), "$regexRule$latestCommit"))
}
fun updatePaperCommit(repo: String, branch: String, properties: File, regexRule: String = "paperCommit = ") =
(Git(properties.parentFile.toPath())("ls-remote", repo).readText()?.lines()
?.filterNot("[a-z0-9]{40}\trefs/heads/$branch".toRegex()::matches)?.first()?.split("\t")?.first()
?: throw AlwaysUpToDateException("Failed to get latest Paper commit")).let {
properties.writeText(properties.readText().replace("$regexRule.*".toRegex(), "$regexRule$it"))
}
Loading

0 comments on commit 75f8807

Please sign in to comment.