Skip to content

Commit

Permalink
Added patch applier, made the patcher maker apply patches to the patc…
Browse files Browse the repository at this point in the history
…hes to fix the patches to keep the source files from hiding from us.
  • Loading branch information
Mr-Zombii committed Jun 1, 2024
1 parent f88bcc6 commit 2d0472c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 40 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
bin
*.exe
.idea
.idea
modifiedsources
src
patches
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ func main() {
createSources(os.Args[2], "temp_src")
createPatches(os.Args[2])
os.RemoveAll("temp_src")
//case "-p":
// if _, err := os.Stat("patches"); errors.Is(err, os.ErrNotExist) {
// fmt.Println("You must have a dir named \"patches\" containing all the patches.")
// os.Exit(1)
// }
// if _, err := os.Stat("src"); errors.Is(err, os.ErrNotExist) {
// fmt.Println("You must have a dir named \"src\" containing all the generated for create source code from cosmic reach.")
// os.Exit(1)
// }
// applyPatches("src", "patches")
case "-p":
if _, err := os.Stat("patches"); errors.Is(err, os.ErrNotExist) {
fmt.Println("You must have a dir named \"patches\" containing all the patches.")
os.Exit(1)
}
if _, err := os.Stat("src"); errors.Is(err, os.ErrNotExist) {
fmt.Println("You must have a dir named \"src\" containing all the generated for create source code from cosmic reach.")
os.Exit(1)
}
applyPatches("src", "patches")
default:
printHelp()
}
Expand Down
28 changes: 12 additions & 16 deletions patchApplier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"io/fs"
"os"
Expand All @@ -9,7 +10,7 @@ import (
)

func applyPatches(srcDir string, patchesDir string) {
applyPatchesRecurse(srcDir, patchesDir)
applyPatchesRecurse(srcDir, srcDir)
}

func applyPatchesRecurse(name string, srcDir string) {
Expand All @@ -22,21 +23,16 @@ func applyPatchesRecurse(name string, srcDir string) {
applyPatchesRecurse(FName, srcDir)
} else {
patchFile := strings.ReplaceAll(FName, srcDir+"/", "patches/") + ".patch"
originalFile := strings.ReplaceAll(FName, srcDir+"/", "src/")
fmt.Printf("git diff --no-index --default-prefix -u --output \"%s\" \"%s\" \"%s\"\n",
patchFile,
originalFile,
FName, // modified
)
exec.Command("git", "diff", "--no-index", "--default-prefix", "-u", "--output",
patchFile,
originalFile,
FName, // modified
).Run()

f, _ := os.ReadFile(patchFile)
if len(f) == 0 {
os.RemoveAll(patchFile)
if _, err := os.Stat(patchFile); !errors.Is(err, os.ErrNotExist) {
fmt.Printf("git apply --ignore-whitespace \"%s\"\n",
patchFile,
)
CMD := exec.Command("git", "apply", "--ignore-whitespace",
patchFile,
)
CMD.Stdout = os.Stdout
CMD.Stdin = os.Stdin
CMD.Run()
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions patchCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (
"strings"
)

func mkPatchRecurse(name string, srcDir string) {
func mkPatchRecurse(name string, srcDir string, oldSrcDir string) {
entries, _ := os.ReadDir(name)

for _, entry := range entries {
FName := name + "/" + entry.Name()
if entry.IsDir() {
os.MkdirAll(strings.ReplaceAll(FName, srcDir+"/", "patches/"), fs.ModePerm)
mkPatchRecurse(FName, srcDir)
mkPatchRecurse(FName, srcDir, oldSrcDir)
} else {
patchFile := strings.ReplaceAll(FName, srcDir+"/", "patches/") + ".patch"
originalFile := strings.ReplaceAll(FName, srcDir+"/", "src/")
originalFile := strings.ReplaceAll(FName, srcDir+"/", oldSrcDir)
fmt.Printf("git diff --no-index --default-prefix -u --output \"%s\" \"%s\" \"%s\"\n",
patchFile,
originalFile,
Expand All @@ -35,6 +35,11 @@ func mkPatchRecurse(name string, srcDir string) {
f, _ := os.ReadFile(patchFile)
if len(f) == 0 {
os.RemoveAll(patchFile)
} else {
patchedContent := string(f)
patchedContent = strings.ReplaceAll(patchedContent, "temp_src_mod", "src")
patchedContent = strings.ReplaceAll(patchedContent, "temp_src", "src")
os.WriteFile(patchFile, []byte(patchedContent), fs.ModePerm)
}
}
}
Expand All @@ -55,7 +60,7 @@ func createPatches(version string) {
recursiveSort("temp_ext", "temp_src_mod")
os.RemoveAll("temp_ext")

mkPatchRecurse("temp_src_mod", "temp_src_mod")
mkPatchRecurse("temp_src_mod", "temp_src_mod", "temp_src/")
os.RemoveAll("temp_src_mod")
deleteEmptyDirs("patches")
}
7 changes: 0 additions & 7 deletions scripts/build.bat

This file was deleted.

2 changes: 0 additions & 2 deletions scripts/build.sh

This file was deleted.

0 comments on commit 2d0472c

Please sign in to comment.