Implement "Isolate" commands

Commands that run alone one by one on reading and writing the file
This should be used on commands that will modify a large part of the
file (or generally large parts)
Since that can fuck up the indices of other commands when ran together
This commit is contained in:
2025-03-28 16:56:39 +01:00
parent b77224176b
commit 2c487bc443
2 changed files with 68 additions and 11 deletions

43
main.go
View File

@@ -110,7 +110,7 @@ func main() {
// This aggregation is great but what if one modification replaces the whole entire file?
// Shit......
// TODO: Add "Isolate" field to modifications which makes them run alone
for file, commands := range associations {
for file, association := range associations {
workers <- struct{}{}
wg.Add(1)
logger.SafeGoWithArgs(func(args ...interface{}) {
@@ -120,6 +120,39 @@ func main() {
// Track per-file processing time
fileStartTime := time.Now()
for _, isolateCommand := range association.IsolateCommands {
fileData, err := os.ReadFile(file)
if err != nil {
logger.Error("Failed to read file %q: %v", file, err)
return
}
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
fileDataStr := string(fileData)
logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex)
modifications, err := processor.ProcessRegex(fileDataStr, isolateCommand, file)
if err != nil {
logger.Error("Failed to process file %q with isolate command %q: %v", file, isolateCommand.Regex, err)
return
}
if len(modifications) == 0 {
logger.Info("No modifications found for file %q", file)
return
}
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
fileMutex.Lock()
stats.ProcessedFiles++
stats.TotalModifications += count
fileMutex.Unlock()
logger.Info("Executed %d isolate modifications for file %q", count, file)
err = os.WriteFile(file, []byte(fileDataStr), 0644)
}
fileData, err := os.ReadFile(file)
if err != nil {
logger.Error("Failed to read file %q: %v", file, err)
@@ -130,14 +163,14 @@ func main() {
// Aggregate all the modifications and execute them
modifications := []utils.ReplaceCommand{}
for _, command := range commands {
for _, command := range association.Commands {
logger.Info("Processing file %q with command %q", file, command.Regex)
commands, err := processor.ProcessRegex(fileDataStr, command, file)
newModifications, err := processor.ProcessRegex(fileDataStr, command, file)
if err != nil {
logger.Error("Failed to process file %q with command %q: %v", file, command.Regex, err)
return
}
modifications = append(modifications, commands...)
modifications = append(modifications, newModifications...)
// It is not guranteed that all the commands will be executed...
// TODO: Make this better
// We'd have to pass the map to executemodifications or something...
@@ -145,7 +178,7 @@ func main() {
if !ok {
count = 0
}
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(commands))
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
}
if len(modifications) == 0 {