Skip to content

Commit 948beb1

Browse files
committed
move source AT rebuilding to its own task
1 parent c8cac5e commit 948beb1

File tree

29 files changed

+357
-322
lines changed

29 files changed

+357
-322
lines changed

paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/CoreTasks.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import io.papermc.paperweight.core.tasks.ImportLibraryFiles
2828
import io.papermc.paperweight.core.tasks.IndexLibraryFiles
2929
import io.papermc.paperweight.core.tasks.SetupMinecraftSources
3030
import io.papermc.paperweight.core.tasks.SetupPaperScript
31+
import io.papermc.paperweight.core.tasks.patching.ProcessNewSourceATs
3132
import io.papermc.paperweight.core.util.coreExt
3233
import io.papermc.paperweight.tasks.*
3334
import io.papermc.paperweight.tasks.mache.DecompileJar
@@ -40,6 +41,8 @@ import org.gradle.api.Project
4041
import org.gradle.api.provider.Property
4142
import org.gradle.api.tasks.TaskContainer
4243
import org.gradle.kotlin.dsl.*
44+
import org.gradle.kotlin.dsl.provideDelegate
45+
import org.gradle.kotlin.dsl.registering
4346

4447
class CoreTasks(
4548
val project: Project,
@@ -153,6 +156,16 @@ class CoreTasks(
153156

154157
root.set(project.rootProject.layout.projectDirectory)
155158
}
159+
160+
val processNewSourceATs by project.tasks.registering(ProcessNewSourceATs::class) {
161+
description = "Processes new source ATs"
162+
163+
base.set(layout.cache.resolve(BASE_PROJECT).resolve("sources"))
164+
input.set(layout.projectDirectory.dir("src/minecraft/java"))
165+
atFile.set(project.coreExt.paper.additionalAts.fileExists(project))
166+
ats.jstClasspath.from(project.configurations.named(MACHE_MINECRAFT_CONFIG))
167+
ats.jst.from(project.configurations.named(JST_CONFIG))
168+
}
156169
}
157170

158171
// Setup Paper's Minecraft patching tasks

paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,6 @@ class MinecraftPatchingTasks(
206206
input.set(outputSrc)
207207
patches.set(sourcePatchDir)
208208
gitFilePatches.set(this@MinecraftPatchingTasks.gitFilePatches)
209-
210-
ats.jstClasspath.from(project.configurations.named(MACHE_MINECRAFT_CONFIG))
211-
ats.jst.from(project.configurations.named(JST_CONFIG))
212-
atFile.set(additionalAts.fileExists(project))
213-
atFileOut.set(additionalAts.fileExists(project))
214209
}
215210

216211
val rebuildResourcePatches = tasks.register<RebuildFilePatches>(rebuildResourcePatchesName) {

paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/SetupMinecraftSources.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import codechicken.diffpatch.util.archiver.ArchiveFormat
2828
import io.papermc.paperweight.core.util.ApplySourceATs
2929
import io.papermc.paperweight.tasks.*
3030
import io.papermc.paperweight.util.*
31+
import io.papermc.paperweight.util.constants.*
3132
import java.nio.file.Path
3233
import java.util.function.Predicate
3334
import kotlin.io.path.*
@@ -81,7 +82,7 @@ abstract class SetupMinecraftSources : JavaLauncherTask() {
8182
val git: Git
8283
if (outputPath.resolve(".git/HEAD").isRegularFile()) {
8384
git = Git.open(outputPath.toFile())
84-
git.reset().setRef("ROOT").setMode(ResetCommand.ResetType.HARD).call()
85+
git.reset().setRef(MACHE_TAG_ROOT).setMode(ResetCommand.ResetType.HARD).call()
8586
} else {
8687
outputPath.createDirectories()
8788

@@ -90,10 +91,10 @@ abstract class SetupMinecraftSources : JavaLauncherTask() {
9091
.setInitialBranch("main")
9192
.call()
9293

93-
val rootIdent = PersonIdent("ROOT", "noreply+automated@papermc.io")
94-
git.commit().setMessage("ROOT").setAllowEmpty(true).setAuthor(rootIdent).setSign(false).call()
95-
git.tagDelete().setTags("ROOT").call()
96-
git.tag().setName("ROOT").setTagger(rootIdent).setSigned(false).call()
94+
val rootIdent = PersonIdent(MACHE_TAG_ROOT, "noreply+automated@papermc.io")
95+
git.commit().setMessage(MACHE_TAG_ROOT).setAllowEmpty(true).setAuthor(rootIdent).setSign(false).call()
96+
git.tagDelete().setTags(MACHE_TAG_ROOT).call()
97+
git.tag().setName(MACHE_TAG_ROOT).setTagger(rootIdent).setSigned(false).call()
9798
}
9899

99100
if (macheOld.isPresent) {
@@ -130,7 +131,7 @@ abstract class SetupMinecraftSources : JavaLauncherTask() {
130131
println("Setup git repo...")
131132
if (!macheOld.isPresent) {
132133
// skip this if we are diffing against old, since it would be a commit without mache patches
133-
commitAndTag(git, "Vanilla")
134+
commitAndTag(git, MACHE_TAG_VANILLA)
134135
}
135136

136137
if (!mache.isEmpty) {
@@ -147,7 +148,7 @@ abstract class SetupMinecraftSources : JavaLauncherTask() {
147148
.build()
148149
.operate()
149150

150-
commitAndTag(git, "Mache")
151+
commitAndTag(git, MACHE_TAG_PATCHES)
151152

152153
if (result.exit != 0) {
153154
throw Exception("Failed to apply ${result.summary.failedMatches} mache patches")
@@ -165,13 +166,13 @@ abstract class SetupMinecraftSources : JavaLauncherTask() {
165166
atFile.path,
166167
temporaryDir.toPath(),
167168
)
168-
commitAndTag(git, "ATs", "paper ATs")
169+
commitAndTag(git, MACHE_TAG_ATS, "paper ATs")
169170
}
170171

171172
if (libraryImports.isPresent) {
172173
libraryImports.path.copyRecursivelyTo(outputPath)
173174

174-
commitAndTag(git, "Imports", "paper Imports")
175+
commitAndTag(git, MACHE_TAG_IMPORTS, "paper Imports")
175176
}
176177

177178
git.close()

paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFeaturePatches.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ package io.papermc.paperweight.core.tasks.patching
2525
import io.papermc.paperweight.PaperweightException
2626
import io.papermc.paperweight.tasks.*
2727
import io.papermc.paperweight.util.*
28+
import io.papermc.paperweight.util.constants.*
2829
import java.nio.file.Path
2930
import kotlin.io.path.*
31+
import kotlin.io.path.createDirectories
3032
import org.gradle.api.file.DirectoryProperty
3133
import org.gradle.api.provider.Property
3234
import org.gradle.api.tasks.Input
@@ -86,7 +88,7 @@ abstract class ApplyFeaturePatches : ControllableOutputTask() {
8688
if (git("checkout", "main").runSilently(silenceErr = true) != 0) {
8789
git("checkout", "-b", "main").runSilently(silenceErr = true)
8890
}
89-
git("reset", "--hard", "file").executeSilently(silenceErr = true)
91+
git("reset", "--hard", MACHE_TAG_FILE).executeSilently(silenceErr = true)
9092
git("gc").runSilently(silenceErr = true)
9193

9294
applyGitPatches(git, "server repo", repoPath, patches.path, printOutput.get(), verbose.get())

paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFilePatches.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import codechicken.diffpatch.util.LoggingOutputStream
2828
import codechicken.diffpatch.util.PatchMode
2929
import io.papermc.paperweight.tasks.*
3030
import io.papermc.paperweight.util.*
31+
import io.papermc.paperweight.util.constants.*
3132
import java.io.PrintStream
3233
import java.nio.file.Path
3334
import java.time.Instant
@@ -202,8 +203,8 @@ abstract class ApplyFilePatches : BaseTask() {
202203
.setAllowEmpty(true)
203204
.setSign(false)
204205
.call()
205-
git.tagDelete().setTags("file").call()
206-
git.tag().setName("file").setTagger(ident).setSigned(false).call()
206+
git.tagDelete().setTags(MACHE_TAG_FILE).call()
207+
git.tag().setName(MACHE_TAG_FILE).setTagger(ident).setSigned(false).call()
207208
git.close()
208209
}
209210

paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/FixupFilePatches.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package io.papermc.paperweight.core.tasks.patching
2424

2525
import io.papermc.paperweight.tasks.*
2626
import io.papermc.paperweight.util.*
27+
import io.papermc.paperweight.util.constants.*
2728
import org.gradle.api.file.DirectoryProperty
2829
import org.gradle.api.provider.Property
2930
import org.gradle.api.tasks.Input
@@ -44,7 +45,7 @@ abstract class FixupFilePatches : BaseTask() {
4445
fun run() {
4546
val git = Git(repo)
4647
git("add", ".").executeOut()
47-
git("commit", "--fixup", "file").executeOut()
48+
git("commit", "--fixup", MACHE_TAG_FILE).executeOut()
4849
git("-c", "sequence.editor=:", "rebase", "-i", "--autosquash", upstream.get()).executeOut()
4950
}
5051
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* paperweight is a Gradle plugin for the PaperMC project.
3+
*
4+
* Copyright (c) 2023 Kyle Wood (DenWav)
5+
* Contributors
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation;
10+
* version 2.1 only, no later versions.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20+
* USA
21+
*/
22+
23+
package io.papermc.paperweight.core.tasks.patching
24+
25+
import io.papermc.paperweight.PaperweightException
26+
import io.papermc.paperweight.core.util.ApplySourceATs
27+
import io.papermc.paperweight.tasks.*
28+
import io.papermc.paperweight.util.*
29+
import io.papermc.paperweight.util.constants.*
30+
import java.nio.file.Path
31+
import kotlin.io.path.*
32+
import org.cadixdev.at.AccessTransformSet
33+
import org.cadixdev.at.io.AccessTransformFormats
34+
import org.cadixdev.bombe.type.signature.MethodSignature
35+
import org.gradle.api.file.DirectoryProperty
36+
import org.gradle.api.file.RegularFileProperty
37+
import org.gradle.api.tasks.InputDirectory
38+
import org.gradle.api.tasks.InputFile
39+
import org.gradle.api.tasks.Nested
40+
import org.gradle.api.tasks.Optional
41+
import org.gradle.api.tasks.TaskAction
42+
import org.gradle.api.tasks.UntrackedTask
43+
import org.gradle.kotlin.dsl.*
44+
45+
@UntrackedTask(because = "Always process when requested")
46+
abstract class ProcessNewSourceATs : JavaLauncherTask() {
47+
48+
@get:InputDirectory
49+
abstract val input: DirectoryProperty
50+
51+
@get:InputDirectory
52+
abstract val base: DirectoryProperty
53+
54+
@get:Optional
55+
@get:InputFile
56+
abstract val atFile: RegularFileProperty
57+
58+
@get:Nested
59+
val ats: ApplySourceATs = objects.newInstance()
60+
61+
@TaskAction
62+
fun run() {
63+
val inputDir = input.convertToPath()
64+
val baseDir = base.convertToPath()
65+
66+
// find ATs, cleanup comment, apply to base
67+
val newATs = handleAts(baseDir, inputDir, atFile)
68+
69+
// save work and jump to AT commit
70+
val git = Git(inputDir)
71+
git("stash", "push").executeSilently(silenceErr = true)
72+
git("checkout", MACHE_TAG_ATS).executeSilently(silenceErr = true)
73+
74+
// apply new ATs to source
75+
newATs.forEach { (path, ats) ->
76+
val source = inputDir.resolve(path)
77+
applyNewATs(source, ats)
78+
}
79+
80+
// commit new ATs
81+
git("add", ".").executeSilently(silenceErr = true)
82+
git("commit", "--amend", "--no-edit").executeSilently(silenceErr = true)
83+
84+
// clean up tree: rebasing drops the old AT commit and replaces it with the new one
85+
git("switch", "-").executeSilently(silenceErr = true)
86+
git("rebase", MACHE_TAG_ATS).executeOut()
87+
88+
git("stash", "pop").executeSilently(silenceErr = true)
89+
}
90+
91+
private fun handleAts(
92+
baseDir: Path,
93+
inputDir: Path,
94+
atFile: RegularFileProperty
95+
): MutableList<Pair<String, AccessTransformSet>> {
96+
val oldAts = AccessTransformFormats.FML.read(atFile.path)
97+
val newATs = mutableListOf<Pair<String, AccessTransformSet>>()
98+
99+
baseDir.walk()
100+
.map { it.relativeTo(baseDir).invariantSeparatorsPathString }
101+
.filter { it.endsWith(".java") }
102+
.forEach {
103+
val ats = AccessTransformSet.create()
104+
val source = inputDir.resolve(it)
105+
val decomp = baseDir.resolve(it)
106+
val className = it.replace(".java", "")
107+
if (findATInSource(source, ats, className)) {
108+
applyNewATs(decomp, ats)
109+
newATs.add(it to ats)
110+
}
111+
oldAts.merge(ats)
112+
}
113+
114+
AccessTransformFormats.FML.writeLF(
115+
atFile.path,
116+
oldAts,
117+
"# This file is auto generated, any changes may be overridden!\n# See CONTRIBUTING.md on how to add access transformers.\n"
118+
)
119+
120+
return newATs
121+
}
122+
123+
private fun applyNewATs(decomp: Path, newAts: AccessTransformSet) {
124+
if (newAts.classes.isEmpty()) {
125+
return
126+
}
127+
128+
val at = temporaryDir.toPath().resolve("ats.cfg").createParentDirectories()
129+
AccessTransformFormats.FML.writeLF(at, newAts)
130+
ats.run(
131+
launcher.get(),
132+
decomp,
133+
decomp,
134+
at,
135+
temporaryDir.toPath().resolve("jst_work").cleanDir(),
136+
singleFile = true,
137+
)
138+
}
139+
140+
private fun findATInSource(source: Path, newAts: AccessTransformSet, className: String): Boolean {
141+
if (!source.exists()) {
142+
// source was added via lib imports
143+
return false
144+
}
145+
146+
val sourceLines = source.readLines()
147+
var foundNew = false
148+
val fixedLines = ArrayList<String>(sourceLines.size)
149+
sourceLines.forEach { line ->
150+
if (!line.contains("// Paper-AT: ")) {
151+
fixedLines.add(line)
152+
return@forEach
153+
}
154+
155+
foundNew = true
156+
157+
val split = line.split("// Paper-AT: ")
158+
val at = split[1]
159+
try {
160+
val atClass = newAts.getOrCreateClass(className)
161+
val parts = at.split(" ")
162+
val accessTransform = atFromString(parts[0])
163+
val name = parts[1]
164+
val index = name.indexOf('(')
165+
if (index == -1) {
166+
atClass.mergeField(name, accessTransform)
167+
} else {
168+
atClass.mergeMethod(MethodSignature.of(name.substring(0, index), name.substring(index)), accessTransform)
169+
}
170+
logger.lifecycle("Found new AT in $className: $at -> $accessTransform")
171+
} catch (ex: Exception) {
172+
throw PaperweightException("Found invalid AT '$at' in class $className")
173+
}
174+
175+
fixedLines.add(split[0].trimEnd())
176+
}
177+
178+
if (foundNew) {
179+
source.writeText(fixedLines.joinToString("\n", postfix = "\n"), Charsets.UTF_8)
180+
}
181+
182+
return foundNew
183+
}
184+
}

0 commit comments

Comments
 (0)