Refactor entirety of replace command to main for now
This commit is contained in:
50
main.go
50
main.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -135,27 +136,30 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
|
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
|
||||||
|
fileDataStr := string(fileData)
|
||||||
|
|
||||||
|
// Aggregate all the modifications and execute them
|
||||||
modifications := []ReplaceCommand{}
|
modifications := []ReplaceCommand{}
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
logger.Info("Processing file %q with command %q", file, command.Pattern)
|
logger.Info("Processing file %q with command %q", file, command.Pattern)
|
||||||
// TODO: Run processor and return modifications
|
// TODO: Run processor and return modifications
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, modification := range modifications {
|
// Sort commands in reverse order for safe replacements
|
||||||
logger.Info("Modifying file %q from %d to %d with %q", file, modification.From, modification.To, modification.With)
|
sort.Slice(modifications, func(i, j int) bool {
|
||||||
// TODO: Execute modifications
|
return modifications[i].From > modifications[j].From
|
||||||
}
|
})
|
||||||
|
logger.Trace("Applying %d replacement commands in reverse order", len(modifications))
|
||||||
|
|
||||||
err = os.WriteFile(file, fileData, 0644)
|
fileDataStr = ExecuteModifications(modifications, fileDataStr)
|
||||||
|
|
||||||
|
err = os.WriteFile(file, []byte(fileDataStr), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to write file %q: %v", file, err)
|
logger.Error("Failed to write file %q: %v", file, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aggregate all the modifications and execute them
|
|
||||||
// Taking care of the whole duplicates and shit
|
|
||||||
// This will also relieve processor of some of the file loading
|
// This will also relieve processor of some of the file loading
|
||||||
// But we will also have to rework the tests.......
|
// But we will also have to rework the tests.......
|
||||||
// Also give each command its own logger, maybe prefix it with something... Maybe give commands a name?
|
// Also give each command its own logger, maybe prefix it with something... Maybe give commands a name?
|
||||||
@@ -253,6 +257,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExecuteModifications(modifications []ReplaceCommand, fileData string) string {
|
||||||
|
var err error
|
||||||
|
for _, modification := range modifications {
|
||||||
|
fileData, err = modification.Execute(fileData)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to execute modification: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fileData
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ReplaceCommand) Execute(fileDataStr string) (string, error) {
|
||||||
|
err := m.Validate(len(fileDataStr))
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to validate modification: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Trace("Replace pos %d-%d with %q", m.From, m.To, m.With)
|
||||||
|
return fileDataStr[:m.From] + m.With + fileDataStr[m.To:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ReplaceCommand) Validate(maxsize int) error {
|
||||||
|
if m.To < m.From {
|
||||||
|
return fmt.Errorf("command to is less than from: %v", m)
|
||||||
|
}
|
||||||
|
if m.From > maxsize || m.To > maxsize {
|
||||||
|
return fmt.Errorf("command from or to is greater than replacement length: %v", m)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
|
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
|
||||||
associationCount := 0
|
associationCount := 0
|
||||||
fileCommands := make(map[string][]ModifyCommand)
|
fileCommands := make(map[string][]ModifyCommand)
|
||||||
|
@@ -3,7 +3,6 @@ package processor
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -240,59 +239,40 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if replacement == "" {
|
// if replacement == "" {
|
||||||
commands := make([]ReplaceCommand, 0, len(captureGroups))
|
// commands := make([]ReplaceCommand, 0, len(captureGroups))
|
||||||
// Apply the modifications to the original match
|
// // Apply the modifications to the original match
|
||||||
replacement = match
|
// replacement = match
|
||||||
|
//
|
||||||
// Count groups that were actually modified
|
// // Count groups that were actually modified
|
||||||
modifiedGroups := 0
|
// modifiedGroups := 0
|
||||||
for _, capture := range captureGroups {
|
// for _, capture := range captureGroups {
|
||||||
if capture.Value != capture.Updated {
|
// if capture.Value != capture.Updated {
|
||||||
modifiedGroups++
|
// modifiedGroups++
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
logger.Debug("%d of %d capture groups were modified", modifiedGroups, len(captureGroups))
|
// logger.Debug("%d of %d capture groups were modified", modifiedGroups, len(captureGroups))
|
||||||
|
//
|
||||||
for _, capture := range captureGroups {
|
// for _, capture := range captureGroups {
|
||||||
if capture.Value == capture.Updated {
|
// if capture.Value == capture.Updated {
|
||||||
logger.Trace("Capture group unchanged: %s", capture.Value)
|
// logger.Trace("Capture group unchanged: %s", capture.Value)
|
||||||
continue
|
// continue
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Log what changed with context
|
// // Log what changed with context
|
||||||
logger.Debug("Modifying group %s: %q → %q",
|
// logger.Debug("Modifying group %s: %q → %q",
|
||||||
capture.Name, capture.Value, capture.Updated)
|
// capture.Name, capture.Value, capture.Updated)
|
||||||
|
//
|
||||||
// Indices of the group are relative to content
|
// // Indices of the group are relative to content
|
||||||
// To relate them to match we have to subtract the match start index
|
// // To relate them to match we have to subtract the match start index
|
||||||
// replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
|
// // replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
|
||||||
commands = append(commands, ReplaceCommand{
|
// commands = append(commands, ReplaceCommand{
|
||||||
From: capture.Range[0] - matchIndices[0],
|
// From: capture.Range[0] - matchIndices[0],
|
||||||
To: capture.Range[1] - matchIndices[0],
|
// To: capture.Range[1] - matchIndices[0],
|
||||||
With: capture.Updated,
|
// With: capture.Updated,
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
// Sort commands in reverse order for safe replacements
|
|
||||||
sort.Slice(commands, func(i, j int) bool {
|
|
||||||
return commands[i].From > commands[j].From
|
|
||||||
})
|
|
||||||
logger.Trace("Applying %d replacement commands in reverse order", len(commands))
|
|
||||||
|
|
||||||
for _, command := range commands {
|
|
||||||
logger.Trace("Replace pos %d-%d with %q", command.From, command.To, command.With)
|
|
||||||
if command.To < command.From {
|
|
||||||
logger.Error("Command to is less than from: %v", command)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if command.From > len(replacement) || command.To > len(replacement) {
|
|
||||||
logger.Error("Command from or to is greater than replacement length: %v", command)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
replacement = replacement[:command.From] + command.With + replacement[command.To:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preview the replacement for logging
|
// Preview the replacement for logging
|
||||||
replacementPreview := replacement
|
replacementPreview := replacement
|
||||||
|
Reference in New Issue
Block a user