-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move source AT rebuilding to its own task
- Loading branch information
1 parent
c8cac5e
commit 948beb1
Showing
29 changed files
with
357 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import codechicken.diffpatch.util.archiver.ArchiveFormat | |
import io.papermc.paperweight.core.util.ApplySourceATs | ||
import io.papermc.paperweight.tasks.* | ||
import io.papermc.paperweight.util.* | ||
import io.papermc.paperweight.util.constants.* | ||
import java.nio.file.Path | ||
import java.util.function.Predicate | ||
import kotlin.io.path.* | ||
|
@@ -81,7 +82,7 @@ abstract class SetupMinecraftSources : JavaLauncherTask() { | |
val git: Git | ||
if (outputPath.resolve(".git/HEAD").isRegularFile()) { | ||
git = Git.open(outputPath.toFile()) | ||
git.reset().setRef("ROOT").setMode(ResetCommand.ResetType.HARD).call() | ||
git.reset().setRef(MACHE_TAG_ROOT).setMode(ResetCommand.ResetType.HARD).call() | ||
} else { | ||
outputPath.createDirectories() | ||
|
||
|
@@ -90,10 +91,10 @@ abstract class SetupMinecraftSources : JavaLauncherTask() { | |
.setInitialBranch("main") | ||
.call() | ||
|
||
val rootIdent = PersonIdent("ROOT", "[email protected]") | ||
git.commit().setMessage("ROOT").setAllowEmpty(true).setAuthor(rootIdent).setSign(false).call() | ||
git.tagDelete().setTags("ROOT").call() | ||
git.tag().setName("ROOT").setTagger(rootIdent).setSigned(false).call() | ||
val rootIdent = PersonIdent(MACHE_TAG_ROOT, "[email protected]") | ||
git.commit().setMessage(MACHE_TAG_ROOT).setAllowEmpty(true).setAuthor(rootIdent).setSign(false).call() | ||
git.tagDelete().setTags(MACHE_TAG_ROOT).call() | ||
git.tag().setName(MACHE_TAG_ROOT).setTagger(rootIdent).setSigned(false).call() | ||
} | ||
|
||
if (macheOld.isPresent) { | ||
|
@@ -130,7 +131,7 @@ abstract class SetupMinecraftSources : JavaLauncherTask() { | |
println("Setup git repo...") | ||
if (!macheOld.isPresent) { | ||
// skip this if we are diffing against old, since it would be a commit without mache patches | ||
commitAndTag(git, "Vanilla") | ||
commitAndTag(git, MACHE_TAG_VANILLA) | ||
} | ||
|
||
if (!mache.isEmpty) { | ||
|
@@ -147,7 +148,7 @@ abstract class SetupMinecraftSources : JavaLauncherTask() { | |
.build() | ||
.operate() | ||
|
||
commitAndTag(git, "Mache") | ||
commitAndTag(git, MACHE_TAG_PATCHES) | ||
|
||
if (result.exit != 0) { | ||
throw Exception("Failed to apply ${result.summary.failedMatches} mache patches") | ||
|
@@ -165,13 +166,13 @@ abstract class SetupMinecraftSources : JavaLauncherTask() { | |
atFile.path, | ||
temporaryDir.toPath(), | ||
) | ||
commitAndTag(git, "ATs", "paper ATs") | ||
commitAndTag(git, MACHE_TAG_ATS, "paper ATs") | ||
} | ||
|
||
if (libraryImports.isPresent) { | ||
libraryImports.path.copyRecursivelyTo(outputPath) | ||
|
||
commitAndTag(git, "Imports", "paper Imports") | ||
commitAndTag(git, MACHE_TAG_IMPORTS, "paper Imports") | ||
} | ||
|
||
git.close() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
...ht-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ProcessNewSourceATs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
* paperweight is a Gradle plugin for the PaperMC project. | ||
* | ||
* Copyright (c) 2023 Kyle Wood (DenWav) | ||
* Contributors | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 only, no later versions. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | ||
* USA | ||
*/ | ||
|
||
package io.papermc.paperweight.core.tasks.patching | ||
|
||
import io.papermc.paperweight.PaperweightException | ||
import io.papermc.paperweight.core.util.ApplySourceATs | ||
import io.papermc.paperweight.tasks.* | ||
import io.papermc.paperweight.util.* | ||
import io.papermc.paperweight.util.constants.* | ||
import java.nio.file.Path | ||
import kotlin.io.path.* | ||
import org.cadixdev.at.AccessTransformSet | ||
import org.cadixdev.at.io.AccessTransformFormats | ||
import org.cadixdev.bombe.type.signature.MethodSignature | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.tasks.InputDirectory | ||
import org.gradle.api.tasks.InputFile | ||
import org.gradle.api.tasks.Nested | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.UntrackedTask | ||
import org.gradle.kotlin.dsl.* | ||
|
||
@UntrackedTask(because = "Always process when requested") | ||
abstract class ProcessNewSourceATs : JavaLauncherTask() { | ||
|
||
@get:InputDirectory | ||
abstract val input: DirectoryProperty | ||
|
||
@get:InputDirectory | ||
abstract val base: DirectoryProperty | ||
|
||
@get:Optional | ||
@get:InputFile | ||
abstract val atFile: RegularFileProperty | ||
|
||
@get:Nested | ||
val ats: ApplySourceATs = objects.newInstance() | ||
|
||
@TaskAction | ||
fun run() { | ||
val inputDir = input.convertToPath() | ||
val baseDir = base.convertToPath() | ||
|
||
// find ATs, cleanup comment, apply to base | ||
val newATs = handleAts(baseDir, inputDir, atFile) | ||
|
||
// save work and jump to AT commit | ||
val git = Git(inputDir) | ||
git("stash", "push").executeSilently(silenceErr = true) | ||
git("checkout", MACHE_TAG_ATS).executeSilently(silenceErr = true) | ||
|
||
// apply new ATs to source | ||
newATs.forEach { (path, ats) -> | ||
val source = inputDir.resolve(path) | ||
applyNewATs(source, ats) | ||
} | ||
|
||
// commit new ATs | ||
git("add", ".").executeSilently(silenceErr = true) | ||
git("commit", "--amend", "--no-edit").executeSilently(silenceErr = true) | ||
|
||
// clean up tree: rebasing drops the old AT commit and replaces it with the new one | ||
git("switch", "-").executeSilently(silenceErr = true) | ||
git("rebase", MACHE_TAG_ATS).executeOut() | ||
|
||
git("stash", "pop").executeSilently(silenceErr = true) | ||
} | ||
|
||
private fun handleAts( | ||
baseDir: Path, | ||
inputDir: Path, | ||
atFile: RegularFileProperty | ||
): MutableList<Pair<String, AccessTransformSet>> { | ||
val oldAts = AccessTransformFormats.FML.read(atFile.path) | ||
val newATs = mutableListOf<Pair<String, AccessTransformSet>>() | ||
|
||
baseDir.walk() | ||
.map { it.relativeTo(baseDir).invariantSeparatorsPathString } | ||
.filter { it.endsWith(".java") } | ||
.forEach { | ||
val ats = AccessTransformSet.create() | ||
val source = inputDir.resolve(it) | ||
val decomp = baseDir.resolve(it) | ||
val className = it.replace(".java", "") | ||
if (findATInSource(source, ats, className)) { | ||
applyNewATs(decomp, ats) | ||
newATs.add(it to ats) | ||
} | ||
oldAts.merge(ats) | ||
} | ||
|
||
AccessTransformFormats.FML.writeLF( | ||
atFile.path, | ||
oldAts, | ||
"# This file is auto generated, any changes may be overridden!\n# See CONTRIBUTING.md on how to add access transformers.\n" | ||
) | ||
|
||
return newATs | ||
} | ||
|
||
private fun applyNewATs(decomp: Path, newAts: AccessTransformSet) { | ||
if (newAts.classes.isEmpty()) { | ||
return | ||
} | ||
|
||
val at = temporaryDir.toPath().resolve("ats.cfg").createParentDirectories() | ||
AccessTransformFormats.FML.writeLF(at, newAts) | ||
ats.run( | ||
launcher.get(), | ||
decomp, | ||
decomp, | ||
at, | ||
temporaryDir.toPath().resolve("jst_work").cleanDir(), | ||
singleFile = true, | ||
) | ||
} | ||
|
||
private fun findATInSource(source: Path, newAts: AccessTransformSet, className: String): Boolean { | ||
if (!source.exists()) { | ||
// source was added via lib imports | ||
return false | ||
} | ||
|
||
val sourceLines = source.readLines() | ||
var foundNew = false | ||
val fixedLines = ArrayList<String>(sourceLines.size) | ||
sourceLines.forEach { line -> | ||
if (!line.contains("// Paper-AT: ")) { | ||
fixedLines.add(line) | ||
return@forEach | ||
} | ||
|
||
foundNew = true | ||
|
||
val split = line.split("// Paper-AT: ") | ||
val at = split[1] | ||
try { | ||
val atClass = newAts.getOrCreateClass(className) | ||
val parts = at.split(" ") | ||
val accessTransform = atFromString(parts[0]) | ||
val name = parts[1] | ||
val index = name.indexOf('(') | ||
if (index == -1) { | ||
atClass.mergeField(name, accessTransform) | ||
} else { | ||
atClass.mergeMethod(MethodSignature.of(name.substring(0, index), name.substring(index)), accessTransform) | ||
} | ||
logger.lifecycle("Found new AT in $className: $at -> $accessTransform") | ||
} catch (ex: Exception) { | ||
throw PaperweightException("Found invalid AT '$at' in class $className") | ||
} | ||
|
||
fixedLines.add(split[0].trimEnd()) | ||
} | ||
|
||
if (foundNew) { | ||
source.writeText(fixedLines.joinToString("\n", postfix = "\n"), Charsets.UTF_8) | ||
} | ||
|
||
return foundNew | ||
} | ||
} |
Oops, something went wrong.