Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
a2201053c5 | |||
04cedf5ece | |||
ebb07854cc | |||
8a86ae2f40 | |||
e8f16dda2b | |||
513773f641 |
38
main.go
38
main.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -20,14 +21,14 @@ type GlobalStats struct {
|
|||||||
TotalModifications int
|
TotalModifications int
|
||||||
ProcessedFiles int
|
ProcessedFiles int
|
||||||
FailedFiles int
|
FailedFiles int
|
||||||
ModificationsPerCommand map[string]int
|
ModificationsPerCommand sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
repo *git.Repository
|
repo *git.Repository
|
||||||
worktree *git.Worktree
|
worktree *git.Worktree
|
||||||
stats GlobalStats = GlobalStats{
|
stats GlobalStats = GlobalStats{
|
||||||
ModificationsPerCommand: make(map[string]int),
|
ModificationsPerCommand: sync.Map{},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -52,6 +53,7 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, " You can use any valid Lua code, including if statements, loops, etc.\n")
|
fmt.Fprintf(os.Stderr, " You can use any valid Lua code, including if statements, loops, etc.\n")
|
||||||
fmt.Fprintf(os.Stderr, " Glob patterns are supported for file selection (*.xml, data/**.xml, etc.)\n")
|
fmt.Fprintf(os.Stderr, " Glob patterns are supported for file selection (*.xml, data/**.xml, etc.)\n")
|
||||||
}
|
}
|
||||||
|
// TODO: Fix bed shitting when doing *.yml in barotrauma directory
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
|
||||||
@@ -68,6 +70,12 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *utils.Filter != "" {
|
||||||
|
logger.Info("Filtering commands by name: %s", *utils.Filter)
|
||||||
|
commands = utils.FilterCommands(commands, *utils.Filter)
|
||||||
|
logger.Info("Filtered %d commands", len(commands))
|
||||||
|
}
|
||||||
|
|
||||||
// Then aggregate all the globs and deduplicate them
|
// Then aggregate all the globs and deduplicate them
|
||||||
globs := utils.AggregateGlobs(commands)
|
globs := utils.AggregateGlobs(commands)
|
||||||
logger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands))
|
logger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands))
|
||||||
@@ -91,7 +99,6 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Utilize parallel workers for this
|
|
||||||
// Then for each file run all commands associated with the file
|
// Then for each file run all commands associated with the file
|
||||||
workers := make(chan struct{}, *utils.ParallelFiles)
|
workers := make(chan struct{}, *utils.ParallelFiles)
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
@@ -131,7 +138,11 @@ func main() {
|
|||||||
// It is not guranteed that all the commands will be executed...
|
// It is not guranteed that all the commands will be executed...
|
||||||
// TODO: Make this better
|
// TODO: Make this better
|
||||||
// We'd have to pass the map to executemodifications or something...
|
// We'd have to pass the map to executemodifications or something...
|
||||||
stats.ModificationsPerCommand[command.Name] += len(commands)
|
count, ok := stats.ModificationsPerCommand.Load(command.Name)
|
||||||
|
if !ok {
|
||||||
|
count = 0
|
||||||
|
}
|
||||||
|
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(commands))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(modifications) == 0 {
|
if len(modifications) == 0 {
|
||||||
@@ -208,14 +219,23 @@ func main() {
|
|||||||
// Print summary
|
// Print summary
|
||||||
if stats.TotalModifications == 0 {
|
if stats.TotalModifications == 0 {
|
||||||
logger.Warning("No modifications were made in any files")
|
logger.Warning("No modifications were made in any files")
|
||||||
fmt.Fprintf(os.Stderr, "No modifications were made in any files\n")
|
|
||||||
} else {
|
} else {
|
||||||
logger.Info("Operation complete! Modified %d values in %d/%d files",
|
logger.Info("Operation complete! Modified %d values in %d/%d files",
|
||||||
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles)
|
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles)
|
||||||
fmt.Printf("Operation complete! Modified %d values in %d/%d files\n",
|
sortedCommands := []string{}
|
||||||
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles)
|
stats.ModificationsPerCommand.Range(func(key, value interface{}) bool {
|
||||||
for command, count := range stats.ModificationsPerCommand {
|
sortedCommands = append(sortedCommands, key.(string))
|
||||||
logger.Info("Command %q made %d modifications", command, count)
|
return true
|
||||||
|
})
|
||||||
|
sort.Strings(sortedCommands)
|
||||||
|
|
||||||
|
for _, command := range sortedCommands {
|
||||||
|
count, _ := stats.ModificationsPerCommand.Load(command)
|
||||||
|
if count.(int) > 0 {
|
||||||
|
logger.Info("\tCommand %q made %d modifications", command, count)
|
||||||
|
} else {
|
||||||
|
logger.Warning("\tCommand %q made no modifications", command)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -250,7 +250,7 @@ func deduplicateGroups(captureGroups []*CaptureGroup) []*CaptureGroup {
|
|||||||
}
|
}
|
||||||
if overlaps {
|
if overlaps {
|
||||||
// We CAN just continue despite this fuckup
|
// We CAN just continue despite this fuckup
|
||||||
logger.Error("Overlapping capture group: %s", group.Name)
|
logger.Warning("Overlapping capture group: %s", group.Name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
logger.Debug("No overlap detected for capture group: %s. Adding to deduplicated groups.", group.Name)
|
logger.Debug("No overlap detected for capture group: %s. Adding to deduplicated groups.", group.Name)
|
||||||
@@ -268,8 +268,6 @@ func resolveRegexPlaceholders(pattern string) string {
|
|||||||
// Handle special pattern modifications
|
// Handle special pattern modifications
|
||||||
if !strings.HasPrefix(pattern, "(?s)") {
|
if !strings.HasPrefix(pattern, "(?s)") {
|
||||||
pattern = "(?s)" + pattern
|
pattern = "(?s)" + pattern
|
||||||
// Use fmt.Printf for test compatibility
|
|
||||||
fmt.Printf("Pattern modified to include (?s): %s\n", pattern)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namedGroupNum := regexp.MustCompile(`(?:(\?<[^>]+>)(!num))`)
|
namedGroupNum := regexp.MustCompile(`(?:(\?<[^>]+>)(!num))`)
|
||||||
|
@@ -979,29 +979,12 @@ func TestPatternWithoutPrefixGetsModified(t *testing.T) {
|
|||||||
// Setup
|
// Setup
|
||||||
pattern := "some.*pattern"
|
pattern := "some.*pattern"
|
||||||
|
|
||||||
// Redirect stdout to capture fmt.Printf output
|
|
||||||
oldStdout := os.Stdout
|
|
||||||
r, w, _ := os.Pipe()
|
|
||||||
os.Stdout = w
|
|
||||||
|
|
||||||
// Execute function
|
// Execute function
|
||||||
result := resolveRegexPlaceholders(pattern)
|
result := resolveRegexPlaceholders(pattern)
|
||||||
|
|
||||||
// Restore stdout
|
|
||||||
w.Close()
|
|
||||||
os.Stdout = oldStdout
|
|
||||||
|
|
||||||
// Read captured output
|
|
||||||
var buf bytes.Buffer
|
|
||||||
io.Copy(&buf, r)
|
|
||||||
output := buf.String()
|
|
||||||
|
|
||||||
// Verify results
|
// Verify results
|
||||||
expectedPattern := "(?s)some.*pattern"
|
expectedPattern := "(?s)some.*pattern"
|
||||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||||
|
|
||||||
expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern)
|
|
||||||
assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty string input returns "(?s)"
|
// Empty string input returns "(?s)"
|
||||||
@@ -1009,29 +992,12 @@ func TestEmptyStringReturnsWithPrefix(t *testing.T) {
|
|||||||
// Setup
|
// Setup
|
||||||
pattern := ""
|
pattern := ""
|
||||||
|
|
||||||
// Redirect stdout to capture fmt.Printf output
|
|
||||||
oldStdout := os.Stdout
|
|
||||||
r, w, _ := os.Pipe()
|
|
||||||
os.Stdout = w
|
|
||||||
|
|
||||||
// Execute function
|
// Execute function
|
||||||
result := resolveRegexPlaceholders(pattern)
|
result := resolveRegexPlaceholders(pattern)
|
||||||
|
|
||||||
// Restore stdout
|
|
||||||
w.Close()
|
|
||||||
os.Stdout = oldStdout
|
|
||||||
|
|
||||||
// Read captured output
|
|
||||||
var buf bytes.Buffer
|
|
||||||
io.Copy(&buf, r)
|
|
||||||
output := buf.String()
|
|
||||||
|
|
||||||
// Verify results
|
// Verify results
|
||||||
expectedPattern := "(?s)"
|
expectedPattern := "(?s)"
|
||||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||||
|
|
||||||
expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern)
|
|
||||||
assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Named group with "!num" pattern gets replaced with proper regex for numbers
|
// Named group with "!num" pattern gets replaced with proper regex for numbers
|
||||||
|
@@ -12,4 +12,5 @@ var (
|
|||||||
LogLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
|
LogLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
|
||||||
Cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed")
|
Cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed")
|
||||||
ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
|
ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
|
||||||
|
Filter = flag.String("filter", "", "Filter commands before running them")
|
||||||
)
|
)
|
||||||
|
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"modify/logger"
|
"modify/logger"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/bmatcuk/doublestar/v4"
|
"github.com/bmatcuk/doublestar/v4"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -36,14 +37,29 @@ func (c *ModifyCommand) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ehh.. Not much better... Guess this wasn't the big deal
|
||||||
|
var matchesMemoTable map[string]bool = make(map[string]bool)
|
||||||
|
func Matches(path string, glob string) (bool, error) {
|
||||||
|
key := fmt.Sprintf("%s:%s", path, glob)
|
||||||
|
if matches, ok := matchesMemoTable[key]; ok {
|
||||||
|
logger.Debug("Found match for file %q and glob %q in memo table", path, glob)
|
||||||
|
return matches, nil
|
||||||
|
}
|
||||||
|
matches, err := doublestar.Match(glob, path)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to match glob %s with file %s: %w", glob, path, err)
|
||||||
|
}
|
||||||
|
matchesMemoTable[key] = matches
|
||||||
|
return matches, 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)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
for _, glob := range command.Files {
|
for _, glob := range command.Files {
|
||||||
// TODO: Maybe memoize this function call
|
matches, err := Matches(file, glob)
|
||||||
matches, err := doublestar.Match(glob, file)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
||||||
continue
|
continue
|
||||||
@@ -206,3 +222,16 @@ func CountGlobsBeforeDedup(commands []ModifyCommand) int {
|
|||||||
}
|
}
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FilterCommands(commands []ModifyCommand, filter string) []ModifyCommand {
|
||||||
|
filteredCommands := []ModifyCommand{}
|
||||||
|
filters := strings.Split(filter, ",")
|
||||||
|
for _, cmd := range commands {
|
||||||
|
for _, filter := range filters {
|
||||||
|
if strings.Contains(cmd.Name, filter) {
|
||||||
|
filteredCommands = append(filteredCommands, cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredCommands
|
||||||
|
}
|
Reference in New Issue
Block a user