diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/CoreTasks.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/CoreTasks.kt index 057511ca4..3f2891080 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/CoreTasks.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/CoreTasks.kt @@ -28,6 +28,7 @@ import io.papermc.paperweight.core.tasks.ImportLibraryFiles import io.papermc.paperweight.core.tasks.IndexLibraryFiles import io.papermc.paperweight.core.tasks.SetupMinecraftSources import io.papermc.paperweight.core.tasks.SetupPaperScript +import io.papermc.paperweight.core.tasks.patching.ProcessNewSourceATs import io.papermc.paperweight.core.util.coreExt import io.papermc.paperweight.tasks.* import io.papermc.paperweight.tasks.mache.DecompileJar @@ -40,6 +41,8 @@ import org.gradle.api.Project import org.gradle.api.provider.Property import org.gradle.api.tasks.TaskContainer import org.gradle.kotlin.dsl.* +import org.gradle.kotlin.dsl.provideDelegate +import org.gradle.kotlin.dsl.registering class CoreTasks( val project: Project, @@ -153,6 +156,16 @@ class CoreTasks( root.set(project.rootProject.layout.projectDirectory) } + + val processNewSourceATs by project.tasks.registering(ProcessNewSourceATs::class) { + description = "Processes new source ATs" + + base.set(layout.cache.resolve(BASE_PROJECT).resolve("sources")) + input.set(layout.projectDirectory.dir("src/minecraft/java")) + atFile.set(project.coreExt.paper.additionalAts.fileExists(project)) + ats.jstClasspath.from(project.configurations.named(MACHE_MINECRAFT_CONFIG)) + ats.jst.from(project.configurations.named(JST_CONFIG)) + } } // Setup Paper's Minecraft patching tasks diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt index 5fe4bf9af..d2b04268d 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt @@ -206,11 +206,6 @@ class MinecraftPatchingTasks( input.set(outputSrc) patches.set(sourcePatchDir) gitFilePatches.set(this@MinecraftPatchingTasks.gitFilePatches) - - ats.jstClasspath.from(project.configurations.named(MACHE_MINECRAFT_CONFIG)) - ats.jst.from(project.configurations.named(JST_CONFIG)) - atFile.set(additionalAts.fileExists(project)) - atFileOut.set(additionalAts.fileExists(project)) } val rebuildResourcePatches = tasks.register(rebuildResourcePatchesName) { diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/SetupMinecraftSources.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/SetupMinecraftSources.kt index 673970b2c..e1ccec282 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/SetupMinecraftSources.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/SetupMinecraftSources.kt @@ -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", "noreply+automated@papermc.io") - 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, "noreply+automated@papermc.io") + 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() diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFeaturePatches.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFeaturePatches.kt index 1e5a51c5b..2bcd3af1a 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFeaturePatches.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFeaturePatches.kt @@ -25,8 +25,10 @@ package io.papermc.paperweight.core.tasks.patching import io.papermc.paperweight.PaperweightException 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 kotlin.io.path.createDirectories import org.gradle.api.file.DirectoryProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.Input @@ -86,7 +88,7 @@ abstract class ApplyFeaturePatches : ControllableOutputTask() { if (git("checkout", "main").runSilently(silenceErr = true) != 0) { git("checkout", "-b", "main").runSilently(silenceErr = true) } - git("reset", "--hard", "file").executeSilently(silenceErr = true) + git("reset", "--hard", MACHE_TAG_FILE).executeSilently(silenceErr = true) git("gc").runSilently(silenceErr = true) applyGitPatches(git, "server repo", repoPath, patches.path, printOutput.get(), verbose.get()) diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFilePatches.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFilePatches.kt index 7f692b392..efdfa7671 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFilePatches.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ApplyFilePatches.kt @@ -28,6 +28,7 @@ import codechicken.diffpatch.util.LoggingOutputStream import codechicken.diffpatch.util.PatchMode import io.papermc.paperweight.tasks.* import io.papermc.paperweight.util.* +import io.papermc.paperweight.util.constants.* import java.io.PrintStream import java.nio.file.Path import java.time.Instant @@ -202,8 +203,8 @@ abstract class ApplyFilePatches : BaseTask() { .setAllowEmpty(true) .setSign(false) .call() - git.tagDelete().setTags("file").call() - git.tag().setName("file").setTagger(ident).setSigned(false).call() + git.tagDelete().setTags(MACHE_TAG_FILE).call() + git.tag().setName(MACHE_TAG_FILE).setTagger(ident).setSigned(false).call() git.close() } diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/FixupFilePatches.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/FixupFilePatches.kt index 52a7cdecb..6180c3695 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/FixupFilePatches.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/FixupFilePatches.kt @@ -24,6 +24,7 @@ package io.papermc.paperweight.core.tasks.patching import io.papermc.paperweight.tasks.* import io.papermc.paperweight.util.* +import io.papermc.paperweight.util.constants.* import org.gradle.api.file.DirectoryProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.Input @@ -44,7 +45,7 @@ abstract class FixupFilePatches : BaseTask() { fun run() { val git = Git(repo) git("add", ".").executeOut() - git("commit", "--fixup", "file").executeOut() + git("commit", "--fixup", MACHE_TAG_FILE).executeOut() git("-c", "sequence.editor=:", "rebase", "-i", "--autosquash", upstream.get()).executeOut() } } diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ProcessNewSourceATs.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ProcessNewSourceATs.kt new file mode 100644 index 000000000..dcfb9932c --- /dev/null +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/ProcessNewSourceATs.kt @@ -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> { + val oldAts = AccessTransformFormats.FML.read(atFile.path) + val newATs = mutableListOf>() + + 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(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 + } +} diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/RebuildFilePatches.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/RebuildFilePatches.kt index 68c016b1d..b764f8324 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/RebuildFilePatches.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/RebuildFilePatches.kt @@ -25,23 +25,17 @@ package io.papermc.paperweight.core.tasks.patching import codechicken.diffpatch.cli.DiffOperation import codechicken.diffpatch.util.LogLevel import codechicken.diffpatch.util.LoggingOutputStream -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.io.PrintStream 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.provider.Property import org.gradle.api.tasks.* import org.gradle.api.tasks.options.Option import org.gradle.kotlin.dsl.* -import org.intellij.lang.annotations.Language @UntrackedTask(because = "Always rebuild patches") abstract class RebuildFilePatches : JavaLauncherTask() { @@ -62,14 +56,6 @@ abstract class RebuildFilePatches : JavaLauncherTask() { @get:OutputDirectory abstract val patches: DirectoryProperty - @get:Optional - @get:InputFile - abstract val atFile: RegularFileProperty - - @get:Optional - @get:OutputFile - abstract val atFileOut: RegularFileProperty - @get:Input abstract val contextLines: Property @@ -77,9 +63,6 @@ abstract class RebuildFilePatches : JavaLauncherTask() { @get:Input abstract val gitFilePatches: Property - @get:Nested - val ats: ApplySourceATs = objects.newInstance() - override fun init() { super.init() contextLines.convention(3) @@ -95,28 +78,8 @@ abstract class RebuildFilePatches : JavaLauncherTask() { val git = Git(inputDir) git("stash", "push").executeSilently(silenceErr = true) - git("checkout", "file").executeSilently(silenceErr = true) + git("checkout", MACHE_TAG_FILE).executeSilently(silenceErr = true) - val filesWithNewAts = if (!ats.jst.isEmpty) { - handleAts( - baseDir, - inputDir, - atFile, - atFileOut, - ) - } else { - emptyList() - } - - if (filesWithNewAts.isNotEmpty()) { - git("status").executeOut() - git("diff").executeOut() - // we removed the comment, we need to commit this - git("add", ".").executeOut() - git("commit", "--amend", "--no-edit").executeOut() - } - - // rebuild patches val result = if (gitFilePatches.get()) { rebuildWithGit(git, patchDir) } else { @@ -124,29 +87,10 @@ abstract class RebuildFilePatches : JavaLauncherTask() { } git("switch", "-").executeSilently(silenceErr = true) - if (filesWithNewAts.isNotEmpty()) { - try { - // we need to rebase, so that the new file commit is part of the tree again. - // for that we use GIT_SEQUENCE_EDITOR to drop the first commit - // and then execs for all the files that remove the papter - // todo detect if sed is not present (windows) and switch out sed for something else - @Language("Shell Script") - val sequenceEditor = "sed -i -e 0,/pick/{s/pick/drop/}" - val execs = filesWithNewAts - .map { "sed -i -e 's|// Paper-AT:.*||g' $it && ((git add $it && git commit --amend --no-edit) || true)" } - .flatMap { listOf("--exec", it) }.toTypedArray() - git.withEnv( - mapOf("GIT_SEQUENCE_EDITOR" to sequenceEditor) - )("rebase", "-i", "file", "--strategy-option=theirs", *execs).executeSilently() - } catch (e: Exception) { - // TODO better message to inform the user on what to do - throw RuntimeException("Encountered conflicts while rebuilding file patches.", e) - } - } git("stash", "pop").runSilently(silenceErr = true) val patchDirGit = Git(patchDir) - patchDirGit("add", "-A", ".").executeSilently() + patchDirGit("add", "-A", ".").executeSilently(silenceErr = true) logger.lifecycle("Rebuilt $result patches") } @@ -202,103 +146,4 @@ abstract class RebuildFilePatches : JavaLauncherTask() { .operate() return result.summary.changedFiles } - - private fun handleAts( - baseDir: Path, - inputDir: Path, - atFile: RegularFileProperty, - atFileOut: RegularFileProperty, - ): MutableList { - val oldAts = if (atFile.isPresent) { - AccessTransformFormats.FML.read(atFile.path) - } else { - AccessTransformSet.create() - } - - // handle AT - val filesWithNewAts = mutableListOf() - 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 (handleATInSource(source, ats, className)) { - handleATInBase(decomp, ats) - filesWithNewAts.add(it) - } - oldAts.merge(ats) - } - - if (atFileOut.isPresent) { - AccessTransformFormats.FML.writeLF( - atFileOut.path, - oldAts, - "# This file is auto generated, any changes may be overridden!\n# See CONTRIBUTING.md on how to add access transformers." - ) - } - - return filesWithNewAts - } - - private fun handleATInBase(decomp: Path, newAts: AccessTransformSet) { - if (newAts.classes.isEmpty()) { - return - } - - val at = temporaryDir.toPath().resolve("ats.cfg").createParentDirectories() - AccessTransformFormats.FML.writeLF(at, newAts) - println("OLD: " + decomp.readText()) - ats.run( - launcher.get(), - decomp, - decomp, - at, - temporaryDir.toPath().resolve("jst_work"), - singleFile = true, - ) - println("NEW: " + decomp.readText()) - } - - private fun handleATInSource(source: Path, newAts: AccessTransformSet, className: String): Boolean { - val sourceLines = source.readLines() - var foundNew = false - val fixedLines = ArrayList(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]) - } - - if (foundNew) { - source.writeText(fixedLines.joinToString("\n", postfix = "\n"), Charsets.UTF_8) - } - - return foundNew - } } diff --git a/paperweight-core/src/test/kotlin/io/papermc/paperweight/FunctionalTest.kt b/paperweight-core/src/test/kotlin/io/papermc/paperweight/FunctionalTest.kt index 936982aaf..6f77ad8e2 100644 --- a/paperweight-core/src/test/kotlin/io/papermc/paperweight/FunctionalTest.kt +++ b/paperweight-core/src/test/kotlin/io/papermc/paperweight/FunctionalTest.kt @@ -23,6 +23,7 @@ package io.papermc.paperweight import io.papermc.paperweight.util.* +import io.papermc.paperweight.util.constants.* import java.net.URL import java.nio.file.Path import java.nio.file.Paths @@ -42,7 +43,7 @@ class FunctionalTest { val debug = true - @Disabled + @Disabled("only used for debugging") @Test fun setupCleanTestRepo() { val projectDir = Path.of("F:\\Projects\\paperweight\\test").cleanFile().createDirectories() @@ -90,34 +91,6 @@ class FunctionalTest { tempDir.resolve("fake-patches/sources/Test.java.patch").readText() ) - // add AT to source -> patch and AT file is updated - println("adding at to source") - modifyFile(tempDir.resolve("test-server/src/minecraft/java/Test.java")) { - it.replace( - "\"2\";", - "\"2\"; // Woo" - ).replace("public final String getTest2() {", "public String getTest2() {// Paper-AT: public-f getTest2()Ljava/lang/String;") - } - - Git(tempDir.resolve("test-server/src/minecraft/java")).let { git -> - git("add", ".").executeSilently() - git("commit", "--fixup", "file").executeSilently() - git("rebase", "--autosquash", "upstream/main").executeSilently() - } - - println("\nrunning rebuildPatches again\n") - val rebP2 = gradleRunner - .withArguments("rebuildPatches", "--stacktrace", "-Dfake=true") - .withDebug(debug) - .build() - - assertEquals(rebP2.task(":test-server:rebuildPatches")?.outcome, TaskOutcome.SUCCESS) - assertEquals( - testResource.resolve("fake-patches/expected/Test.java.patch").readText(), - tempDir.resolve("fake-patches/sources/Test.java.patch").readText() - ) - assertEquals(testResource.resolve("build-data/expected.at").readText(), tempDir.resolve("build-data/fake.at").readText()) - // feature patch println("\nmodifying feature patch\n") modifyFile(tempDir.resolve("test-server/src/minecraft/java/Test.java")) { diff --git a/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/tasks/patching/ProcessNewSourceATsTest.kt b/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/tasks/patching/ProcessNewSourceATsTest.kt new file mode 100644 index 000000000..c5a064c01 --- /dev/null +++ b/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/tasks/patching/ProcessNewSourceATsTest.kt @@ -0,0 +1,70 @@ +/* + * 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.tasks.TaskTest +import io.papermc.paperweight.util.constants.MACHE_TAG_ATS +import java.nio.file.Path +import org.gradle.kotlin.dsl.register +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir + +class ProcessNewSourceATsTest : TaskTest() { + private lateinit var task: ProcessNewSourceATs + + @BeforeEach + fun setup() { + val project = setupProject() + project.plugins.apply("java") + task = project.tasks.register("processNewSourceATs", ProcessNewSourceATs::class).get() + } + + @Test + @Disabled("need to set jst") + fun `should process source access transformers`(@TempDir tempDir: Path) { + val testResource = Path.of("src/test/resources/process_new_source_ats") + val testInput = testResource.resolve("input") + + val source = setupDir(tempDir, testInput, "source").toFile() + setupGitRepo(source, "main", MACHE_TAG_ATS) + setupGitRepo(tempDir.toFile(), "main", "dum") + setupGitHook(source) + val base = setupDir(tempDir, testInput, "base").toFile() + val atFile = testInput.resolve("ats.at").toFile() + + task.base.set(base) + task.input.set(source) + task.atFile.set(atFile) + + task.ats.jst.setFrom() // TODO fix + + task.run() + + val testOutput = testResource.resolve("output") + compareDir(tempDir, testOutput, "base") + compareDir(tempDir, testOutput, "source") + compareFile(tempDir, testOutput, "ats.at") + } +} diff --git a/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/tasks/patching/RebuildFilePatchesTest.kt b/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/tasks/patching/RebuildFilePatchesTest.kt index dcc491c59..0bbb41c7f 100644 --- a/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/tasks/patching/RebuildFilePatchesTest.kt +++ b/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/tasks/patching/RebuildFilePatchesTest.kt @@ -23,11 +23,11 @@ package io.papermc.paperweight.core.tasks.patching import io.papermc.paperweight.tasks.* +import io.papermc.paperweight.util.constants.* import java.nio.file.Path import kotlin.test.BeforeTest import kotlin.test.Test import org.gradle.kotlin.dsl.* -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.io.CleanupMode import org.junit.jupiter.api.io.TempDir @@ -41,8 +41,6 @@ class RebuildFilePatchesTest : TaskTest() { task = project.tasks.register("rebuildPatches", RebuildFilePatches::class).get() } - // TODO - @Disabled("worker api not available here") @Test fun `should rebuild patches`(@TempDir(cleanup = CleanupMode.ON_SUCCESS) tempDir: Path) { println("running in $tempDir") @@ -50,19 +48,15 @@ class RebuildFilePatchesTest : TaskTest() { val testInput = testResource.resolve("input") val source = setupDir(tempDir, testInput, "source").toFile() - setupGitRepo(source, "main", "file") + setupGitRepo(source, "main", MACHE_TAG_FILE) setupGitRepo(tempDir.toFile(), "main", "dum") setupGitHook(source) val base = setupDir(tempDir, testInput, "base").toFile() val patches = tempDir.resolve("patches").toFile() - val atFile = testInput.resolve("ats.at").toFile() - val atFileOut = tempDir.resolve("ats.at").toFile() task.input.set(source) task.base.set(base) task.patches.set(patches) - task.atFile.set(atFile) - task.atFileOut.set(atFileOut) task.verbose.set(true) task.run() @@ -71,6 +65,5 @@ class RebuildFilePatchesTest : TaskTest() { compareDir(tempDir, testOutput, "base") compareDir(tempDir, testOutput, "source") compareDir(tempDir, testOutput, "patches") - compareFile(tempDir, testOutput, "ats.at") } } diff --git a/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/util/ApplySourceATsTest.kt b/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/util/ApplySourceATsTest.kt deleted file mode 100644 index d42a662dd..000000000 --- a/paperweight-core/src/test/kotlin/io/papermc/paperweight/core/util/ApplySourceATsTest.kt +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.util - -import io.papermc.paperweight.tasks.* - -class ApplySourceATsTest : TaskTest() { - /* TODO: re-add a test for this - private lateinit var task: ApplySourceAT - - private val workerExecutor: WorkerExecutor = mockk() - private val workQueue: WorkQueue = mockk() - - @BeforeTest - fun setup() { - val project = setupProject() - task = project.tasks.register("applySourceAT", ApplySourceAT::class).get() - mockkObject(task) - - every { task.worker } returns workerExecutor - every { workerExecutor.processIsolation(any()) } returns workQueue - every { workQueue.submit(ApplySourceATWorker::class, any()) } answers { - val action = object : ApplySourceATWorker() { - override fun getParameters(): Params { - return mockk().also { - every { it.inputJar.get() } returns task.inputJar.get() - every { it.atFile.get() } returns task.atFile.get() - every { it.outputJar.get() } returns task.outputJar.get() - every { it.minecraftClasspath } returns task.minecraftClasspath - } - } - } - action.execute() - } - } - - @Test - fun `should apply source access transformers`(@TempDir tempDir: Path) { - val testResource = Path.of("src/test/resources/apply_source_at") - val testInput = testResource.resolve("input") - - val inputJar = createZip(tempDir, testInput, "Test.jar", "Test.java", "Unrelated.java") - val atFile = testInput.resolve("ats.at").toFile() - val outputJar = tempDir.resolve("output.jar") - - task.inputJar.set(inputJar.toFile()) - task.atFile.set(atFile) - task.outputJar.set(outputJar.toFile()) - - task.run() - - val testOutput = testResource.resolve("output") - val expectedJar = createZip(tempDir, testOutput, "expected.jar", "Test.java", "Unrelated.java") - compareZip(outputJar, expectedJar) - } - */ -} diff --git a/paperweight-core/src/test/resources/functional_test/fake-patches/expected/0001-Feature.patch b/paperweight-core/src/test/resources/functional_test/fake-patches/expected/0001-Feature.patch index 7d91209a3..b9b8c1de0 100644 --- a/paperweight-core/src/test/resources/functional_test/fake-patches/expected/0001-Feature.patch +++ b/paperweight-core/src/test/resources/functional_test/fake-patches/expected/0001-Feature.patch @@ -5,12 +5,12 @@ Subject: [PATCH] Feature diff --git a/Test.java b/Test.java -index 51f6794c111f68e72cc4054c01f0c8896afaf292..d047a159f76d162bc24b030a575252202e57080c 100644 +index bf731c042bf7f59b60378df1d61302e1769a6a77..13e57904b05e4b98b8b105511254c2f6c1b1924d 100644 --- a/Test.java +++ b/Test.java @@ -13,4 +13,8 @@ public class Test { - public String getTest2() { - return this.test + "2"; // Woo + public final String getTest2() { + return this.test + "2"; } + + public String feature() { diff --git a/paperweight-lib/src/test/resources/rebuild_patches/input/ats.at b/paperweight-core/src/test/resources/process_new_source_ats/input/ats.at similarity index 88% rename from paperweight-lib/src/test/resources/rebuild_patches/input/ats.at rename to paperweight-core/src/test/resources/process_new_source_ats/input/ats.at index c46fe9b71..8130cbdcf 100644 --- a/paperweight-lib/src/test/resources/rebuild_patches/input/ats.at +++ b/paperweight-core/src/test/resources/process_new_source_ats/input/ats.at @@ -1,3 +1,3 @@ # This file is auto generated, any changes may be overridden! # See CONTRIBUTING.md on how to add access transformers. -public Test dum + diff --git a/paperweight-lib/src/test/resources/rebuild_patches/output/base/Test.java b/paperweight-core/src/test/resources/process_new_source_ats/input/base/Test.java similarity index 84% rename from paperweight-lib/src/test/resources/rebuild_patches/output/base/Test.java rename to paperweight-core/src/test/resources/process_new_source_ats/input/base/Test.java index fd852a162..671b5f49e 100644 --- a/paperweight-lib/src/test/resources/rebuild_patches/output/base/Test.java +++ b/paperweight-core/src/test/resources/process_new_source_ats/input/base/Test.java @@ -1,7 +1,7 @@ public class Test { public int dum; - public String test; + private final String test; public Test(String test) { this.test = test; diff --git a/paperweight-lib/src/test/resources/apply_source_at/input/Unrelated.java b/paperweight-core/src/test/resources/process_new_source_ats/input/base/Unrelated.java similarity index 100% rename from paperweight-lib/src/test/resources/apply_source_at/input/Unrelated.java rename to paperweight-core/src/test/resources/process_new_source_ats/input/base/Unrelated.java diff --git a/paperweight-core/src/test/resources/process_new_source_ats/input/source/Test.java b/paperweight-core/src/test/resources/process_new_source_ats/input/source/Test.java new file mode 100644 index 000000000..ac6fad0a9 --- /dev/null +++ b/paperweight-core/src/test/resources/process_new_source_ats/input/source/Test.java @@ -0,0 +1,13 @@ +public class Test { + + public int dum; + private final String test; + + public Test(String test) { + this.test = test; + } + + public String getTest() {// Paper-AT: public-f getTest()Ljava/lang/String; + return test; + } +} diff --git a/paperweight-lib/src/test/resources/apply_source_at/output/Unrelated.java b/paperweight-core/src/test/resources/process_new_source_ats/input/source/Unrelated.java similarity index 100% rename from paperweight-lib/src/test/resources/apply_source_at/output/Unrelated.java rename to paperweight-core/src/test/resources/process_new_source_ats/input/source/Unrelated.java diff --git a/paperweight-lib/src/test/resources/rebuild_patches/output/ats.at b/paperweight-core/src/test/resources/process_new_source_ats/output/ats.at similarity index 60% rename from paperweight-lib/src/test/resources/rebuild_patches/output/ats.at rename to paperweight-core/src/test/resources/process_new_source_ats/output/ats.at index 3b4280360..0c17e0099 100644 --- a/paperweight-lib/src/test/resources/rebuild_patches/output/ats.at +++ b/paperweight-core/src/test/resources/process_new_source_ats/output/ats.at @@ -1,5 +1,3 @@ # This file is auto generated, any changes may be overridden! # See CONTRIBUTING.md on how to add access transformers. -private+f Test getTest()Ljava/lang/String; -public Test dum -public-f Test test +public-f Test getTest()Ljava/lang/String; diff --git a/paperweight-lib/src/test/resources/apply_source_at/output/Test.java b/paperweight-core/src/test/resources/process_new_source_ats/output/source/Test.java similarity index 81% rename from paperweight-lib/src/test/resources/apply_source_at/output/Test.java rename to paperweight-core/src/test/resources/process_new_source_ats/output/source/Test.java index 869744fb8..a761c51b1 100644 --- a/paperweight-lib/src/test/resources/apply_source_at/output/Test.java +++ b/paperweight-core/src/test/resources/process_new_source_ats/output/source/Test.java @@ -7,7 +7,7 @@ public Test(String test) { this.test = test; } - private final String getTest() { + public String getTest() { return test; } } diff --git a/paperweight-core/src/test/resources/process_new_source_ats/output/source/Unrelated.java b/paperweight-core/src/test/resources/process_new_source_ats/output/source/Unrelated.java new file mode 100644 index 000000000..c821a33ff --- /dev/null +++ b/paperweight-core/src/test/resources/process_new_source_ats/output/source/Unrelated.java @@ -0,0 +1,3 @@ +class Unrealted { + +} diff --git a/paperweight-lib/src/test/resources/apply_source_at/input/Test.java b/paperweight-core/src/test/resources/rebuild_patches/input/base/Test.java similarity index 100% rename from paperweight-lib/src/test/resources/apply_source_at/input/Test.java rename to paperweight-core/src/test/resources/rebuild_patches/input/base/Test.java diff --git a/paperweight-lib/src/test/resources/rebuild_patches/output/source/Test.java b/paperweight-core/src/test/resources/rebuild_patches/input/source/Test.java similarity index 71% rename from paperweight-lib/src/test/resources/rebuild_patches/output/source/Test.java rename to paperweight-core/src/test/resources/rebuild_patches/input/source/Test.java index a2d35e435..bb72c175b 100644 --- a/paperweight-lib/src/test/resources/rebuild_patches/output/source/Test.java +++ b/paperweight-core/src/test/resources/rebuild_patches/input/source/Test.java @@ -1,13 +1,13 @@ public class Test { public int dum; - public String test; + private final String test; public Test(String test) { this.test = test; } - private final String getTest() { + public String getTest() { return test + "Test"; // Test } } diff --git a/paperweight-lib/src/test/resources/rebuild_patches/input/base/Test.java b/paperweight-core/src/test/resources/rebuild_patches/output/base/Test.java similarity index 100% rename from paperweight-lib/src/test/resources/rebuild_patches/input/base/Test.java rename to paperweight-core/src/test/resources/rebuild_patches/output/base/Test.java diff --git a/paperweight-lib/src/test/resources/rebuild_patches/output/patches/Test.java.patch b/paperweight-core/src/test/resources/rebuild_patches/output/patches/Test.java.patch similarity index 77% rename from paperweight-lib/src/test/resources/rebuild_patches/output/patches/Test.java.patch rename to paperweight-core/src/test/resources/rebuild_patches/output/patches/Test.java.patch index aaa46dd44..58d2f2fdb 100644 --- a/paperweight-lib/src/test/resources/rebuild_patches/output/patches/Test.java.patch +++ b/paperweight-core/src/test/resources/rebuild_patches/output/patches/Test.java.patch @@ -3,7 +3,7 @@ @@ -8,6 +_,6 @@ } - private final String getTest() { + public String getTest() { - return test; + return test + "Test"; // Test } diff --git a/paperweight-lib/src/test/resources/rebuild_patches/input/source/Test.java b/paperweight-core/src/test/resources/rebuild_patches/output/source/Test.java similarity index 52% rename from paperweight-lib/src/test/resources/rebuild_patches/input/source/Test.java rename to paperweight-core/src/test/resources/rebuild_patches/output/source/Test.java index 1e61ad8fc..bb72c175b 100644 --- a/paperweight-lib/src/test/resources/rebuild_patches/input/source/Test.java +++ b/paperweight-core/src/test/resources/rebuild_patches/output/source/Test.java @@ -1,13 +1,13 @@ public class Test { public int dum; - public String test;// Paper-AT: public-f test + private final String test; public Test(String test) { this.test = test; } - private final String getTest() {// Paper-AT: private+f getTest()Ljava/lang/String; + public String getTest() { return test + "Test"; // Test } } diff --git a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/constants/constants.kt b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/constants/constants.kt index a35d44da8..ed864814a 100644 --- a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/constants/constants.kt +++ b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/constants/constants.kt @@ -26,7 +26,7 @@ import org.gradle.api.Task const val PAPERWEIGHT_EXTENSION = "paperweight" const val PAPERWEIGHT_DEBUG = "paperweight.debug" -fun paperweightDebug(): Boolean = System.getProperty(PAPERWEIGHT_DEBUG, "false") == "true" +fun paperweightDebug(): Boolean = true const val PAPERWEIGHT_VERBOSE_APPLY_PATCHES = "paperweight.verboseApplyPatches" const val MC_LIBRARY_URL = "https://libraries.minecraft.net/" @@ -117,6 +117,14 @@ const val DOWNLOAD_SERVICE_NAME = "paperweightDownloadService" private const val MACHE_PATH = "$PAPER_PATH/mache" const val BASE_PROJECT = "$MACHE_PATH/base" +// when changing these, also change it in the post-rewrite.sh! +const val MACHE_TAG_ROOT = "ROOT" +const val MACHE_TAG_VANILLA = "Vanilla" +const val MACHE_TAG_PATCHES = "Mache" +const val MACHE_TAG_ATS = "ATs" +const val MACHE_TAG_IMPORTS = "Imports" +const val MACHE_TAG_FILE = "file" + fun Task.paperTaskOutput(ext: String? = null) = paperTaskOutput(name, ext) fun paperTaskOutput(name: String, ext: String? = null) = "$TASK_CACHE/$name" + (ext?.let { ".$it" } ?: "") diff --git a/paperweight-lib/src/main/resources/post-rewrite.sh b/paperweight-lib/src/main/resources/post-rewrite.sh index 8918da877..963ba0a27 100644 --- a/paperweight-lib/src/main/resources/post-rewrite.sh +++ b/paperweight-lib/src/main/resources/post-rewrite.sh @@ -1,11 +1,28 @@ -#!/bin/sh -input=$(cat) # SP [SP ] LF -fileCommit=$(git rev-list -n 1 file) # current commit tagged as "file" -oldObject=$(echo $input | cut -d' ' -f1) # -newObject=$(echo $input | cut -d' ' -f2) # - -if [ $oldObject = $fileCommit ]; then - git tag -d file > /dev/null # delete old tag (silent) - git tag file "$newObject" --no-sign - echo "Updated tag 'file' from $oldObject to $newObject" -fi +#!/bin/bash +tags=("ATs" "Imports" "file") +declare -A tagCommits + +# get the current commit for each tag +for tag in "${tags[@]}"; do + tagCommits["$tag"]=$(git rev-list -n 1 "$tag") +done + +while IFS= read -r line; do + # SP [SP ] LF + oldObject=$(echo "$line" | cut -d' ' -f1) + newObject=$(echo "$line" | cut -d' ' -f2) + + for tag in "${tags[@]}"; do + if [ "$oldObject" = "${tagCommits[$tag]}" ]; then + # delete old tag (silent) and add new tag + git tag -d "$tag" > /dev/null + git tag "$tag" "$newObject" --no-sign + echo "Updated tag '$tag' from $oldObject to $newObject" + fi + done + + # if we have updated the file tag, we can stop, no tags after that + if [ "$oldObject" = "${tagCommits["file"]}" ] || [ "$newObject" = "${tagCommits["file"]}" ]; then + break + fi +done diff --git a/paperweight-lib/src/test/resources/apply_source_at/input/ats.at b/paperweight-lib/src/test/resources/apply_source_at/input/ats.at deleted file mode 100644 index 693b10bf2..000000000 --- a/paperweight-lib/src/test/resources/apply_source_at/input/ats.at +++ /dev/null @@ -1,5 +0,0 @@ -# This file is auto generated, any changes may be overridden! -# See CONTRIBUTING.md on how to add access transformers. -public-f Test test -public+f Test dum -private+f Test getTest()Ljava/lang/String;