diff --git a/main.go b/main.go index 5ed38fb..68d7500 100644 --- a/main.go +++ b/main.go @@ -159,19 +159,25 @@ func BuildProcess() { log.Fatalf("Error resolving source path: %v", err) } + // Detect the build directory dynamically + buildDir, err := spitfire.ResolveBuildDir(sourcePath) + if err != nil { + log.Fatalf("Error resolving build directory: %v", err) + } + if all { spitfire.DownloadSource(sourcePath, sourceRepo) spitfire.DiscardChanges(sourcePath) spitfire.CleanBuild(sourcePath) spitfire.UpdateRepo(sourcePath) - - // Pass patchesRepo to ApplyPatches - if err := spitfire.ApplyPatches(sourcePath, patchesRepo); err != nil { + if err := spitfire.ApplyPatches(sourcePath, patchesRepo, "pre-compile-patches", filepath.Join(sourcePath, "patcher")); err != nil { log.Fatalf("Error during patch application: %v", err) } - spitfire.Configure(sourcePath) spitfire.Build(sourcePath) + if err := spitfire.ApplyPatches(buildDir, patchesRepo, "post-compile-patches", filepath.Join(sourcePath, "patcher")); err != nil { + log.Fatalf("Error during patch application: %v", err) + } if run { spitfire.RunProject(sourcePath) } @@ -185,15 +191,16 @@ func BuildProcess() { fmt.Println("Mozilla repository updated.") } else if patches { spitfire.DownloadSource(sourcePath, sourceRepo) - - // Pass patchesRepo to ApplyPatches - if err := spitfire.ApplyPatches(sourcePath, patchesRepo); err != nil { + if err := spitfire.ApplyPatches(sourcePath, patchesRepo, "pre-compile-patches", filepath.Join(sourcePath, "patcher")); err != nil { log.Fatalf("Error during patch application: %v", err) } fmt.Println("Patches updated.") } else if buildFlag { spitfire.Configure(sourcePath) spitfire.Build(sourcePath) + if err := spitfire.ApplyPatches(buildDir, patchesRepo, "post-compile-patches", filepath.Join(sourcePath, "patcher")); err != nil { + log.Fatalf("Error during patch application: %v", err) + } if run { spitfire.RunProject(sourcePath) } diff --git a/spitfire/patch.go b/spitfire/patch.go index b43c240..371bf1e 100644 --- a/spitfire/patch.go +++ b/spitfire/patch.go @@ -8,12 +8,14 @@ import ( ) // ApplyPatches handles cloning, updating, and applying patches -func ApplyPatches(sourcePath string, patchesRepo string) error { - // Define the patches repository and clone directory - patchesCloneDir := filepath.Join(sourcePath, "patches") +func ApplyPatches(sourcePath string, patchesRepo string, patchesPath string, patchesCloneDir string) error { + // Define the full patches path + fullPatchesPath := filepath.Join(patchesCloneDir, patchesPath) // Cleaned path without double slashes + fmt.Printf("Source Path: %s\n", sourcePath) fmt.Printf("Patches Clone Directory: %s\n", patchesCloneDir) fmt.Printf("Patches Repository URL: %s\n", patchesRepo) + fmt.Printf("Patches Path: %s\n", fullPatchesPath) // Check if the patches directory already exists if _, err := os.Stat(patchesCloneDir); os.IsNotExist(err) { @@ -26,34 +28,15 @@ func ApplyPatches(sourcePath string, patchesRepo string) error { return fmt.Errorf("failed to clone patches repository: %v", err) } fmt.Println("Patches repository cloned successfully.") - } else { - // If the directory exists, fetch and pull the latest changes - fmt.Println("Patches directory already exists. Fetching latest changes.") - cmdFetch := exec.Command("git", "fetch", "--all") - cmdFetch.Dir = patchesCloneDir - cmdFetch.Stdout = os.Stdout - cmdFetch.Stderr = os.Stderr - if err := cmdFetch.Run(); err != nil { - return fmt.Errorf("failed to fetch updates for patches repository: %v", err) - } - - cmdReset := exec.Command("git", "reset", "--hard", "origin/main") - cmdReset.Dir = patchesCloneDir - cmdReset.Stdout = os.Stdout - cmdReset.Stderr = os.Stderr - if err := cmdReset.Run(); err != nil { - return fmt.Errorf("failed to reset patches repository to latest state: %v", err) - } - fmt.Println("Patches repository updated successfully.") } - // Verify the directory exists - if _, err := os.Stat(patchesCloneDir); os.IsNotExist(err) { - return fmt.Errorf("patches directory not found after update: %s", patchesCloneDir) + // Verify the patches directory exists + if _, err := os.Stat(fullPatchesPath); os.IsNotExist(err) { + return fmt.Errorf("patches path not found: %s", fullPatchesPath) } - // Apply the patches using `go run apply.go` - applyCmd := exec.Command("run.sh", "--path "+sourcePath) + // Apply the patches using `run.sh` + applyCmd := exec.Command("./run.sh", "--path", sourcePath, "--patches", fullPatchesPath) applyCmd.Dir = patchesCloneDir // Run from the patches directory applyCmd.Stdout = os.Stdout applyCmd.Stderr = os.Stderr diff --git a/spitfire/resolvebuilddir.go b/spitfire/resolvebuilddir.go new file mode 100644 index 0000000..1cc917b --- /dev/null +++ b/spitfire/resolvebuilddir.go @@ -0,0 +1,25 @@ +package spitfire + +import ( + "fmt" + "path/filepath" +) + +// ResolveBuildDir detects the build directory dynamically +func ResolveBuildDir(sourcePath string) (string, error) { + // The expected build directory pattern + globPattern := filepath.Join(sourcePath, "obj-*") + + // Find matching directories + matches, err := filepath.Glob(globPattern) + if err != nil { + return "", fmt.Errorf("error resolving build directory: %v", err) + } + if len(matches) == 0 { + return "", fmt.Errorf("build directory not found under %s", sourcePath) + } + full := filepath.Join(matches[0], "dist", "bin") + + // Return the first match (assumes one build directory exists) + return full, nil +}