added windows compatibility
This commit is contained in:
parent
ea4f2edcfd
commit
a6d2d75c45
3 changed files with 93 additions and 78 deletions
5
main.go
5
main.go
|
@ -87,8 +87,9 @@ func applyPatchWrapper(patchPath, rootPath string, successfulPatches, failedPatc
|
||||||
fmt.Printf("Applying patch: %s\n", patchPath)
|
fmt.Printf("Applying patch: %s\n", patchPath)
|
||||||
err := applyPatch(patchPath, rootPath)
|
err := applyPatch(patchPath, rootPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to apply patch %s: %v\n", patchPath, err)
|
errorMsg := fmt.Sprintf("Failed to apply patch '%s': %v", patchPath, err)
|
||||||
*failedPatches = append(*failedPatches, patchPath)
|
fmt.Println(errorMsg)
|
||||||
|
*failedPatches = append(*failedPatches, errorMsg) // Log detailed error message
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Successfully applied patch: %s\n", patchPath)
|
fmt.Printf("Successfully applied patch: %s\n", patchPath)
|
||||||
*successfulPatches = append(*successfulPatches, patchPath)
|
*successfulPatches = append(*successfulPatches, patchPath)
|
||||||
|
|
35
pref.go
35
pref.go
|
@ -16,6 +16,7 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
|
||||||
}
|
}
|
||||||
defer targetFile.Close()
|
defer targetFile.Close()
|
||||||
|
|
||||||
|
// Create a temporary file
|
||||||
tempFilePath := targetFilePath + ".tmp"
|
tempFilePath := targetFilePath + ".tmp"
|
||||||
tempFile, err := os.Create(tempFilePath)
|
tempFile, err := os.Create(tempFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -23,30 +24,32 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
|
||||||
}
|
}
|
||||||
defer tempFile.Close()
|
defer tempFile.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(targetFile)
|
// Parse modifications into a map for easy lookup
|
||||||
modMap := map[string]string{}
|
modMap := make(map[string]string)
|
||||||
|
|
||||||
// Prepare the modification map
|
|
||||||
for _, mod := range modifications {
|
for _, mod := range modifications {
|
||||||
if strings.HasPrefix(mod, "+pref") {
|
if strings.HasPrefix(mod, "+pref") {
|
||||||
// Strip the `+` and extract the key within parentheses
|
// Strip the `+` and extract the key within parentheses
|
||||||
start := strings.Index(mod, "(")
|
start := strings.Index(mod, "(")
|
||||||
end := strings.LastIndex(mod, ")")
|
end := strings.LastIndex(mod, ")")
|
||||||
if start > 0 && end > start {
|
if start > 0 && end > start {
|
||||||
key := strings.TrimSpace(mod[start+1 : end])
|
key := strings.TrimSpace(mod[start+1 : end]) // Extract the preference key
|
||||||
modMap[key] = strings.TrimPrefix(mod, "+") // Remove the `+` prefix
|
modMap[key] = mod // Full modification line
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
modifiedLines := map[string]bool{}
|
scanner := bufio.NewScanner(targetFile)
|
||||||
|
modifiedLines := map[string]bool{} // Tracks applied modifications
|
||||||
|
|
||||||
|
// Process each line of the target file
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
lineModified := false
|
||||||
|
|
||||||
// Check if the line matches any modification key
|
// Check if the line matches any modification key
|
||||||
lineModified := false
|
|
||||||
for key, replacement := range modMap {
|
for key, replacement := range modMap {
|
||||||
if strings.Contains(line, key) && !modifiedLines[key] {
|
if strings.Contains(line, key) && !modifiedLines[key] {
|
||||||
|
// Write the replacement line
|
||||||
fmt.Printf("Replacing line: %s\n", line)
|
fmt.Printf("Replacing line: %s\n", line)
|
||||||
if _, err := tempFile.WriteString(replacement + "\n"); err != nil {
|
if _, err := tempFile.WriteString(replacement + "\n"); err != nil {
|
||||||
return fmt.Errorf("failed to write to temp file: %v", err)
|
return fmt.Errorf("failed to write to temp file: %v", err)
|
||||||
|
@ -69,21 +72,25 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
|
||||||
return fmt.Errorf("failed to read target file: %v", err)
|
return fmt.Errorf("failed to read target file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write remaining modifications (new preferences)
|
// Append remaining modifications that were not in the file
|
||||||
for key, replacement := range modMap {
|
for key, replacement := range modMap {
|
||||||
if !modifiedLines[key] {
|
if !modifiedLines[key] {
|
||||||
// Remove the `+` prefix before writing
|
fmt.Printf("Adding new preference: %s\n", replacement)
|
||||||
cleanReplacement := strings.TrimPrefix(replacement, "+")
|
if _, err := tempFile.WriteString(replacement + "\n"); err != nil {
|
||||||
if _, err := tempFile.WriteString(cleanReplacement + "\n"); err != nil {
|
return fmt.Errorf("failed to append new preference to temp file: %v", err)
|
||||||
return fmt.Errorf("failed to write new preference to temp file: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the original file with the temporary file
|
// Close the files to ensure proper cleanup
|
||||||
|
tempFile.Close()
|
||||||
|
targetFile.Close()
|
||||||
|
|
||||||
|
// Replace the original file with the modified temp file
|
||||||
if err := os.Rename(tempFilePath, targetFilePath); err != nil {
|
if err := os.Rename(tempFilePath, targetFilePath); err != nil {
|
||||||
return fmt.Errorf("failed to replace target file: %v", err)
|
return fmt.Errorf("failed to replace target file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("Preferences successfully modified.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
125
standard.go
125
standard.go
|
@ -7,89 +7,96 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// applyStandardModifications applies standard modifications
|
// applyStandardModifications handles `standard` type patches
|
||||||
func applyStandardModifications(targetFilePath string, modifications []string) error {
|
func applyStandardModifications(targetFilePath string, modifications []string) error {
|
||||||
// Open the target file
|
fmt.Printf("Applying standard modifications to file: %s\n", targetFilePath)
|
||||||
targetFile, err := os.Open(targetFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to open target file: %v", err)
|
|
||||||
}
|
|
||||||
defer targetFile.Close()
|
|
||||||
|
|
||||||
// Prepare a temporary file for output
|
// Open the target file for reading and writing
|
||||||
tempFilePath := targetFilePath + ".tmp"
|
file, err := os.OpenFile(targetFilePath, os.O_RDWR, 0644)
|
||||||
tempFile, err := os.Create(tempFilePath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create temp file: %v", err)
|
return fmt.Errorf("failed to open target file '%s': %v", targetFilePath, err)
|
||||||
}
|
}
|
||||||
defer tempFile.Close()
|
defer file.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(targetFile)
|
scanner := bufio.NewScanner(file)
|
||||||
var targetLines []string
|
targetLines := []string{}
|
||||||
|
|
||||||
|
// Read all lines from the target file
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
targetLines = append(targetLines, scanner.Text())
|
targetLines = append(targetLines, scanner.Text())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
return fmt.Errorf("failed to read target file: %v", err)
|
return fmt.Errorf("failed to read target file '%s': %v", targetFilePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the modifications
|
modifiedLines := map[string]bool{}
|
||||||
updatedLines, err := applyModifications(targetLines, modifications)
|
modMap := parseModifications(modifications)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the updated lines to the temporary file
|
var updatedContent strings.Builder
|
||||||
for _, line := range updatedLines {
|
|
||||||
_, err := fmt.Fprintln(tempFile, line)
|
// Apply modifications to the target lines
|
||||||
if err != nil {
|
for _, line := range targetLines {
|
||||||
return fmt.Errorf("failed to write to temp file: %v", err)
|
lineModified := false
|
||||||
|
|
||||||
|
for key, mod := range modMap {
|
||||||
|
if strings.Contains(line, key) && !modifiedLines[key] {
|
||||||
|
fmt.Printf("Replacing line in file '%s': %s -> %s\n", targetFilePath, line, mod)
|
||||||
|
updatedContent.WriteString(mod + "\n")
|
||||||
|
modifiedLines[key] = true
|
||||||
|
lineModified = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the original file with the updated file
|
// Write the original line if no modification matches
|
||||||
err = os.Rename(tempFilePath, targetFilePath)
|
if !lineModified {
|
||||||
if err != nil {
|
updatedContent.WriteString(line + "\n")
|
||||||
return fmt.Errorf("failed to replace target file: %v", err)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Append any remaining modifications that were not applied
|
||||||
|
for key, mod := range modMap {
|
||||||
|
if !modifiedLines[key] {
|
||||||
|
fmt.Printf("Adding new line to file '%s': %s\n", targetFilePath, mod)
|
||||||
|
updatedContent.WriteString(mod + "\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Truncate and rewrite the file with updated content
|
||||||
|
if err := file.Truncate(0); err != nil {
|
||||||
|
return fmt.Errorf("failed to truncate file '%s': %v", targetFilePath, err)
|
||||||
|
}
|
||||||
|
if _, err := file.Seek(0, 0); err != nil {
|
||||||
|
return fmt.Errorf("failed to reset file pointer for '%s': %v", targetFilePath, err)
|
||||||
|
}
|
||||||
|
if _, err := file.WriteString(updatedContent.String()); err != nil {
|
||||||
|
return fmt.Errorf("failed to write updated content to file '%s': %v", targetFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Standard modifications successfully applied to file: %s\n", targetFilePath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyModifications applies the changes from the patch file to the target lines
|
// parseModifications converts a list of modification strings into a map for processing
|
||||||
func applyModifications(targetLines, modifications []string) ([]string, error) {
|
func parseModifications(modifications []string) map[string]string {
|
||||||
var result []string
|
modMap := map[string]string{}
|
||||||
i := 0
|
|
||||||
|
|
||||||
for _, mod := range modifications {
|
for _, mod := range modifications {
|
||||||
switch {
|
if strings.HasPrefix(mod, "+") || strings.HasPrefix(mod, "-") {
|
||||||
case strings.HasPrefix(mod, "-"): // Delete or replace
|
trimmed := strings.TrimSpace(strings.TrimPrefix(mod, "+"))
|
||||||
mod = strings.TrimPrefix(mod, "-")
|
trimmed = strings.TrimPrefix(trimmed, "-")
|
||||||
for i < len(targetLines) {
|
key := extractKey(trimmed)
|
||||||
if strings.TrimSpace(targetLines[i]) == strings.TrimSpace(mod) {
|
modMap[key] = trimmed
|
||||||
i++ // Skip this line (delete)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
result = append(result, targetLines[i])
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
case strings.HasPrefix(mod, "+"): // Add or replace
|
|
||||||
mod = strings.TrimPrefix(mod, "+")
|
|
||||||
result = append(result, mod)
|
|
||||||
default: // Keep existing lines
|
|
||||||
if i < len(targetLines) {
|
|
||||||
result = append(result, targetLines[i])
|
|
||||||
i++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return modMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append remaining lines from the original file
|
// extractKey extracts the key from a modification string
|
||||||
for i < len(targetLines) {
|
func extractKey(line string) string {
|
||||||
result = append(result, targetLines[i])
|
start := strings.Index(line, "(")
|
||||||
i++
|
end := strings.LastIndex(line, ")")
|
||||||
|
if start > 0 && end > start {
|
||||||
|
return strings.TrimSpace(line[start+1 : end])
|
||||||
}
|
}
|
||||||
|
return line // Fallback: return the full line
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue