-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c9eaa7
commit 3188afd
Showing
8 changed files
with
139 additions
and
220 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
135 changes: 26 additions & 109 deletions
135
paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/ApplyMachePatches.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 |
---|---|---|
@@ -1,126 +1,43 @@ | ||
package io.papermc.paperweight.tasks.mache | ||
|
||
import io.papermc.paperweight.tasks.softspoon.ApplyPatches | ||
import io.papermc.paperweight.util.* | ||
import io.papermc.paperweight.util.constants.* | ||
import io.papermc.paperweight.util.patches.* | ||
import io.papermc.paperweight.util.patches.NativePatcher | ||
import javax.inject.Inject | ||
import kotlin.io.path.createDirectory | ||
import kotlin.io.path.deleteRecursively | ||
import kotlin.io.path.exists | ||
import kotlin.io.path.inputStream | ||
import kotlin.io.path.relativeTo | ||
import org.gradle.api.DefaultTask | ||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.lib.PersonIdent | ||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.file.ProjectLayout | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.internal.file.FileOperations | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.* | ||
import org.gradle.process.ExecOperations | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.tasks.CacheableTask | ||
import org.gradle.api.tasks.Classpath | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.UntrackedTask | ||
|
||
@UntrackedTask(because = "Always apply patches") | ||
abstract class ApplyMachePatches : DefaultTask() { | ||
|
||
@get:Classpath | ||
abstract val mache: ConfigurableFileCollection | ||
|
||
@get:InputFile | ||
abstract val inputFile: RegularFileProperty | ||
|
||
@get:Internal | ||
abstract val useNativeDiff: Property<Boolean> | ||
@CacheableTask | ||
abstract class ApplyMachePatches : ApplyPatches() { | ||
|
||
@get:Internal | ||
abstract val patchExecutable: Property<String> | ||
abstract override val patches: DirectoryProperty | ||
|
||
@get:OutputFile | ||
abstract val outputJar: RegularFileProperty | ||
|
||
@get:OutputFile | ||
abstract val failedPatchesJar: RegularFileProperty | ||
|
||
@get:Inject | ||
abstract val exec: ExecOperations | ||
|
||
@get:Inject | ||
abstract val files: FileOperations | ||
|
||
@get:Inject | ||
abstract val layout: ProjectLayout | ||
|
||
init { | ||
run { | ||
useNativeDiff.convention(false) | ||
patchExecutable.convention("patch") | ||
} | ||
} | ||
@get:Classpath | ||
abstract val mache: ConfigurableFileCollection | ||
|
||
@TaskAction | ||
fun run() { | ||
val patchesFolder = layout.cache.resolve(PATCHES_FOLDER).ensureClean() | ||
override fun setup() { | ||
// prepare for patches for patching | ||
val patchesFolder = patches.convertToPath().ensureClean() | ||
|
||
mache.singleFile.toPath().openZip().use { zip -> | ||
zip.getPath("patches").copyRecursivelyTo(patchesFolder) | ||
} | ||
|
||
val out = outputJar.convertToPath().ensureClean() | ||
val failed = failedPatchesJar.convertToPath().ensureClean() | ||
|
||
val tempInDir = out.resolveSibling(".tmp_applyPatches_input").ensureClean() | ||
tempInDir.createDirectory() | ||
val tempOutDir = out.resolveSibling(".tmp_applyPatches_output").ensureClean() | ||
tempOutDir.createDirectory() | ||
val tempFailedPatchDir = out.resolveSibling(".tmp_applyPatches_failed").ensureClean() | ||
tempFailedPatchDir.createDirectory() | ||
|
||
try { | ||
files.sync { | ||
from(files.zipTree(inputFile)) | ||
into(tempInDir) | ||
} | ||
|
||
val result = createPatcher().applyPatches(tempInDir, patchesFolder, tempOutDir, tempFailedPatchDir) | ||
|
||
out.writeZipStream { zos -> | ||
failed.writeZipStream { failedZos -> | ||
inputFile.convertToPath().readZipStream { zis, zipEntry -> | ||
if (zipEntry.name.endsWith(".java")) { | ||
val patchedFile = tempOutDir.resolve(zipEntry.name) | ||
if (patchedFile.exists()) { | ||
patchedFile.inputStream().buffered().use { input -> | ||
copyEntry(input, zos, zipEntry) | ||
} | ||
} | ||
val failedPatch = tempFailedPatchDir.resolve(zipEntry.name) | ||
if (failedPatch.exists()) { | ||
failedPatch.inputStream().buffered().use { input -> | ||
copyEntry(input, failedZos, zipEntry) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (result is PatchFailure) { | ||
result.failures | ||
.map { "Patch failed: ${it.patch.relativeTo(patchesFolder)}: ${it.details}" } | ||
.forEach { logger.error(it) } | ||
throw Exception("Failed to apply ${result.failures.size} patches") | ||
} | ||
} finally { | ||
tempInDir.deleteRecursively() | ||
tempOutDir.deleteRecursively() | ||
tempFailedPatchDir.deleteRecursively() | ||
} | ||
} | ||
|
||
internal open fun createPatcher(): Patcher { | ||
return if (useNativeDiff.get()) { | ||
NativePatcher(exec, patchExecutable.get()) | ||
} else { | ||
JavaPatcher() | ||
} | ||
override fun commit() { | ||
val macheIdent = PersonIdent("Mache", "[email protected]") | ||
val git = Git.open(output.convertToPath().toFile()) | ||
git.add().addFilepattern(".").call() | ||
git.tag().setName("mache").setTagger(macheIdent).setSigned(false).call() | ||
git.commit() | ||
.setMessage("Mache") | ||
.setAuthor(macheIdent) | ||
.setSign(false) | ||
.call() | ||
} | ||
} |
55 changes: 0 additions & 55 deletions
55
paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/SetupSources.kt
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/SetupVanilla.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,60 @@ | ||
package io.papermc.paperweight.tasks.mache | ||
|
||
import io.papermc.paperweight.tasks.* | ||
import io.papermc.paperweight.util.* | ||
import java.nio.file.Path | ||
import java.util.function.Predicate | ||
import kotlin.io.path.* | ||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.lib.PersonIdent | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.CacheableTask | ||
import org.gradle.api.tasks.InputFile | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.UntrackedTask | ||
|
||
@CacheableTask | ||
abstract class SetupVanilla : BaseTask() { | ||
|
||
@get:InputFile | ||
abstract val inputFile: RegularFileProperty | ||
|
||
@get:Internal | ||
abstract val predicate: Property<Predicate<Path>> | ||
|
||
@get:OutputDirectory | ||
abstract val outputDir: DirectoryProperty | ||
|
||
@TaskAction | ||
fun run() { | ||
val path = outputDir.convertToPath().ensureClean() | ||
|
||
// copy initial sources | ||
inputFile.convertToPath().openZip().walk() | ||
.filter(predicate.get()) | ||
.forEach { | ||
val target = path.resolve(it.toString().substring(1)) | ||
target.parent.createDirectories() | ||
it.copyTo(target, true) | ||
} | ||
|
||
// setup git repo | ||
val vanillaIdent = PersonIdent("Vanilla", "[email protected]") | ||
|
||
val git = Git.init() | ||
.setDirectory(path.toFile()) | ||
.setInitialBranch("main") | ||
.call() | ||
git.add().addFilepattern(".").call() | ||
git.commit() | ||
.setMessage("Vanilla") | ||
.setAuthor(vanillaIdent) | ||
.setSign(false) | ||
.call() | ||
git.tag().setName("vanilla").setTagger(vanillaIdent).setSigned(false).call() | ||
} | ||
} |
Oops, something went wrong.