Skip to content

Commit

Permalink
improve caching
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Oct 21, 2023
1 parent 3188afd commit 5def8a3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ open class SoftSpoonTasks(
group = "mache"
description = "Setup vanilla source dir."

mache.from(project.configurations.named(MACHE_CONFIG))
patches.set(layout.cache.resolve(PATCHES_FOLDER))

inputFile.set(macheDecompileJar.flatMap { it.outputJar })
predicate.set { Files.isRegularFile(it) && it.toString().endsWith(".java")}
outputDir.set(layout.cache.resolve(BASE_PROJECT).resolve("sources"))
Expand All @@ -81,22 +84,11 @@ open class SoftSpoonTasks(
outputDir.set(layout.cache.resolve(BASE_PROJECT).resolve("resources"))
}

val applyMachePatches by tasks.registering(ApplyMachePatches::class) {
group = "mache"
description = "Applies patches to the vanilla sources"

mache.from(project.configurations.named(MACHE_CONFIG))

input.set(setupMacheSources.flatMap { it.outputDir })
output.set(layout.cache.resolve(BASE_PROJECT).resolve("sources"))
patches.set(layout.cache.resolve(PATCHES_FOLDER))
}

val applySourcePatches by tasks.registering(ApplyPatches::class) {
group = "softspoon"
description = "Applies patches to the vanilla sources"

input.set(applyMachePatches.flatMap { it.output })
input.set(setupMacheSources.flatMap { it.outputDir })
output.set(project.ext.serverProject.map { it.layout.projectDirectory.dir("src/vanilla/java") })
patches.set(project.layout.projectDirectory.dir("patches/sources"))
}
Expand All @@ -105,7 +97,7 @@ open class SoftSpoonTasks(
group = "softspoon"
description = "Applies patches to the vanilla sources"

input.set(applyMachePatches.flatMap { it.output })
input.set(setupMacheSources.flatMap { it.outputDir })
output.set(project.ext.serverProject.map { it.layout.projectDirectory.dir("src/vanilla/java") })
patches.set(project.layout.projectDirectory.dir("patches/sources"))
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,60 @@ package io.papermc.paperweight.tasks.mache

import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.patches.*
import io.papermc.paperweight.util.patches.JavaPatcher
import io.papermc.paperweight.util.patches.NativePatcher
import io.papermc.paperweight.util.patches.Patcher
import java.nio.file.Path
import java.util.function.Predicate
import javax.inject.Inject
import kotlin.io.path.*
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.PersonIdent
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.ProjectLayout
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
import org.gradle.api.tasks.*
import org.gradle.process.ExecOperations

@CacheableTask
abstract class SetupVanilla : BaseTask() {

@get:PathSensitive(PathSensitivity.NONE)
@get:InputFile
abstract val inputFile: RegularFileProperty

@get:Internal
@get:Input
abstract val predicate: Property<Predicate<Path>>

@get:OutputDirectory
abstract val outputDir: DirectoryProperty

@get:Internal
abstract val patches: DirectoryProperty

@get:Optional
@get:Classpath
abstract val mache: ConfigurableFileCollection

@get:Internal
abstract val useNativeDiff: Property<Boolean>

@get:Internal
abstract val patchExecutable: Property<String>

@get:Inject
abstract val exec: ExecOperations

init {
run {
useNativeDiff.convention(false)
patchExecutable.convention("patch")
}
}

@TaskAction
fun run() {
val path = outputDir.convertToPath().ensureClean()
Expand Down Expand Up @@ -56,5 +83,44 @@ abstract class SetupVanilla : BaseTask() {
.setSign(false)
.call()
git.tag().setName("vanilla").setTagger(vanillaIdent).setSigned(false).call()

if (patches.isPresent()) {
// prepare for patches for patching
val patchesFolder = patches.convertToPath().ensureClean()

mache.singleFile.toPath().openZip().use { zip ->
zip.getPath("patches").copyRecursivelyTo(patchesFolder)
}

// patch
val result = createPatcher().applyPatches(path, patches.convertToPath(), path, path)

val macheIdent = PersonIdent("Mache", "mache@automated.papermc.io")
git.add().addFilepattern(".").call()
git.tag().setName("mache").setTagger(macheIdent).setSigned(false).call()
git.commit()
.setMessage("Mache")
.setAuthor(macheIdent)
.setSign(false)
.call()

if (result is PatchFailure) {
result.failures
.map { "Patch failed: ${it.patch.relativeTo(patches.get().path)}: ${it.details}" }
.forEach { logger.error(it) }
git.close()
throw Exception("Failed to apply ${result.failures.size} patches")
}
}

git.close()
}

internal open fun createPatcher(): Patcher {
return if (useNativeDiff.get()) {
NativePatcher(exec, patchExecutable.get())
} else {
JavaPatcher()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ import org.gradle.api.provider.Property
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.UntrackedTask
import org.gradle.process.ExecOperations

@UntrackedTask(because = "Always apply patches")
abstract class ApplyPatches : DefaultTask() {

@get:PathSensitive(PathSensitivity.NONE)
@get:InputDirectory
abstract val input: DirectoryProperty

@get:OutputDirectory
abstract val output: DirectoryProperty

@get:PathSensitive(PathSensitivity.NONE)
@get:InputDirectory
abstract val patches: DirectoryProperty

Expand Down Expand Up @@ -66,7 +70,8 @@ abstract class ApplyPatches : DefaultTask() {

open fun setup() {
output.convertToPath().ensureClean()
Git.cloneRepository().setBranch("main").setURI("file://" + input.convertToPath().toString()).setDirectory(output.convertToPath().toFile()).call()
Git.cloneRepository().setBranch("main").setURI("file://" + input.convertToPath().toString()).setDirectory(output.convertToPath().toFile())
.call().close()
}

open fun commit() {
Expand All @@ -79,6 +84,7 @@ abstract class ApplyPatches : DefaultTask() {
.setSign(false)
.call()
git.tag().setName("file").setTagger(ident).setSigned(false).call()
git.close()
}

internal open fun createPatcher(): Patcher {
Expand Down

0 comments on commit 5def8a3

Please sign in to comment.