From 3a5423ad964b999143b0090c0820f26d77f91547 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Sat, 1 May 2021 00:42:57 -0400 Subject: [PATCH] re-write applyPatches in kotlin --- buildSrc/src/main/kotlin/task/ApplyPatches.kt | 50 ++++++++++++++++++- mappings/scripts/apply.sh | 47 ----------------- 2 files changed, 49 insertions(+), 48 deletions(-) delete mode 100755 mappings/scripts/apply.sh diff --git a/buildSrc/src/main/kotlin/task/ApplyPatches.kt b/buildSrc/src/main/kotlin/task/ApplyPatches.kt index 7649f7cb..7dabccc2 100644 --- a/buildSrc/src/main/kotlin/task/ApplyPatches.kt +++ b/buildSrc/src/main/kotlin/task/ApplyPatches.kt @@ -60,6 +60,53 @@ internal fun Project.createApplyPatchesTask( return false; } + fun applyPatchesYarn(): Boolean { + val projectDir = Paths.get("$rootDir/Yatopia-Server_yarn").toFile() + val importDir = Paths.get("$rootDir/mappings/work/Yatopia-Server_yarn_unpatched").toFile() + logger.lifecycle(">>> Resetting subproject $name") + if (projectDir.exists()) { + ensureSuccess(gitCmd("fetch", "origin", dir = projectDir)) + ensureSuccess(gitCmd("reset", "--hard", "origin/master", dir = projectDir)) + } else { + ensureSuccess(gitCmd("clone", importDir.toString(), projectDir.toString(), printOut = true)) + } + logger.lifecycle(">>> Done resetting subproject $name") + + projectDir.mkdirs() + val applyName = "mappedPatches" + val name = "Yatopia-Server_yarn" + val patchDir: Path = Paths.get("$rootDir/mappedPatches") + if (Files.notExists(patchDir)) return true + + + val patchPaths = Files.newDirectoryStream(patchDir) + .map { it.toFile() } + .filter { it.name.endsWith(".patch") } + .sorted() + .takeIf { it.isNotEmpty() } ?: return true + val patches = patchPaths.map { it.absolutePath }.toTypedArray() + + logger.lifecycle(">>> Applying $applyName patches to $name") + + gitCmd("am", "--abort") + + //Cursed Apply Mode that makes fixing stuff a lot easier + if (checkCursed(project)) { + for (patch in patches) { + val gitCommand = arrayListOf("am", "--3way", "--ignore-whitespace", + "--rerere-autoupdate", "--whitespace=fix", "--reject", "-C0", patch) + if (gitCmd(*gitCommand.toTypedArray(), dir = projectDir, printOut = true).exitCode != 0) { + gitCmd("add", ".", dir = projectDir, printOut = true) + gitCmd("am", "--continue", dir = projectDir, printOut = true) + } + } + } else { + val gitCommand = arrayListOf("am", "--3way", "--ignore-whitespace", + "--rerere-autoupdate", "--whitespace=fix", *patches) + } + return false; + } + doLast { for ((name, subproject) in toothpick.subprojects) { val (sourceRepo, projectDir, patchesDir) = subproject @@ -102,6 +149,7 @@ internal fun Project.createApplyPatchesTask( bashCmd("rm -fr patches/server/*-Mapped-Patches.patch") bashCmd("bash mappings/scripts/init.sh", printOut = true) - bashCmd("bash mappings/scripts/apply.sh", printOut = true) + if (applyPatchesYarn()) {} + } } diff --git a/mappings/scripts/apply.sh b/mappings/scripts/apply.sh deleted file mode 100755 index f3ef6847..00000000 --- a/mappings/scripts/apply.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash -# set -e -cd "$(dirname "$0")" -cd .. -basedir="$(cd .. && pwd -P)" - -gitcmd="git -c commit.gpgsign=false" - -applyPatch(){ - local what="$1" - local what_name="$(basename "$what")" - local target="$2" - local branch="$3" - local patch_folder="$4" - - cd "$basedir/$what" - $gitcmd branch -f upstream "$branch" >/dev/null - - cd "$basedir" - if [ ! -d "$basedir/$target" ]; then - echo "doing clone" - $gitcmd clone "$what" "$target" - fi - cd "$basedir/$target" - - echo "Resetting $target to $what_name..." - $gitcmd remote rm upstream > /dev/null 2>&1 - $gitcmd remote add upstream "$basedir/$what" >/dev/null 2>&1 - $gitcmd checkout master 2>/dev/null || $gitcmd checkout -b master - - $gitcmd fetch upstream >/dev/null 2>&1 - $gitcmd reset --hard upstream/upstream - echo " Applying patches to $target..." - $gitcmd am --abort >/dev/null 2>&1 - if [ -z "$(ls "$basedir/$patch_folder/"*.patch||true)" ];then - echo " no patches for $target" - elif $gitcmd am --3way --ignore-whitespace "$basedir/$patch_folder/"*.patch; then - echo " Patches applied cleanly to $target" - else - echo " Something did not apply cleanly to $target." - echo " Please review above details and finish the apply then" - echo " save the changes with rebuildPatches.sh" - return 1 - fi -} - -(applyPatch mappings/work/Yatopia-Server_yarn_unpatched Yatopia-Server_yarn HEAD mappedPatches)