Builder/spitfire/patch.go
2025-04-13 19:21:04 +02:00

77 lines
2.5 KiB
Go

package spitfire
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
// ApplyPatches handles cloning, updating, and applying patches
func ApplyPatches(sourcePath string, patchesRepo string, patchesPath string, patchesCloneDir string, skipUpdateRepo bool) error {
// Define the full patches path
fullPatchesPath := filepath.Join(patchesCloneDir, patchesPath)
// Print configuration with consistent style
fmt.Printf("🔧 Config:\n")
fmt.Printf(" ├─ Source Path: %s\n", sourcePath)
fmt.Printf(" ├─ Patches Clone Dir: %s\n", patchesCloneDir)
fmt.Printf(" ├─ Patches Repo: %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) {
// Clone the patches repository
fmt.Println("📥 Cloning patches repository...")
cmdClone := exec.Command("git", "clone", patchesRepo, patchesCloneDir)
cmdClone.Stdout = os.Stdout
cmdClone.Stderr = os.Stderr
if err := cmdClone.Run(); err != nil {
return fmt.Errorf("❌ Failed to clone patches repository: %v", err)
}
fmt.Println("✅ Patches repository cloned successfully")
} else {
if !skipUpdateRepo {
fmt.Println("🔄 Updating patches repository...")
// Fetch updates
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: %v", err)
}
// Reset to latest
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 repository: %v", err)
}
fmt.Println("✅ Patches repository updated")
} else {
fmt.Println("⏩ Skipping patches update (requested by user)")
}
}
// 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 `run.sh`
applyCmd := exec.Command("go", "run", ".", "--path", sourcePath, "--patches", fullPatchesPath)
applyCmd.Dir = patchesCloneDir
applyCmd.Stdout = os.Stdout
applyCmd.Stderr = os.Stderr
if err := applyCmd.Run(); err != nil {
return fmt.Errorf("❌ Failed to apply patches: %v", err)
}
fmt.Println("🎉 Patches applied successfully")
return nil
}