diff --git a/main.go b/main.go
index 862ea9d..9658560 100644
--- a/main.go
+++ b/main.go
@@ -111,26 +111,29 @@ end
// Process each file
for _, file := range files {
- err := processFile(file, pattern, fullScript)
+ log.Printf("Processing file: %s", file)
+ fullPath := filepath.Join(".", file)
+
+ content, err := os.ReadFile(fullPath)
+ if err != nil {
+ Error.Printf("error reading file: %v", err)
+ continue
+ }
+
+ fileContent := string(content)
+ result, err := process(fileContent, pattern, fullScript)
if err != nil {
Error.Printf("Error processing file %s: %v", file, err)
}
+
+ err = os.WriteFile(fullPath, []byte(result), 0644)
+ if err != nil {
+ Error.Printf("error writing file: %v", err)
+ }
}
}
-func processFile(filename string, pattern *regexp.Regexp, luaScript string) error {
- log.Printf("Processing file: %s", filename)
- fullPath := filepath.Join(".", filename)
-
- content, err := os.ReadFile(fullPath)
- if err != nil {
- return fmt.Errorf("error reading file: %v", err)
- }
-
- fileContent := string(content)
- modified := false
-
- // Initialize Lua state once per file
+func process(data string, pattern *regexp.Regexp, luaScript string) (string, error) {
L := lua.NewState()
defer L.Close()
@@ -138,16 +141,16 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
L.Push(L.GetGlobal("require"))
L.Push(lua.LString("math"))
if err := L.PCall(1, 1, nil); err != nil {
- return fmt.Errorf("error loading Lua math library: %v", err)
+ return data, fmt.Errorf("error loading Lua math library: %v", err)
}
// Load the Lua script
if err := L.DoString(luaScript); err != nil {
- return fmt.Errorf("error in Lua script: %v", err)
+ return data, fmt.Errorf("error in Lua script: %v", err)
}
// Process all regex matches
- result := pattern.ReplaceAllStringFunc(fileContent, func(match string) string {
+ result := pattern.ReplaceAllStringFunc(data, func(match string) string {
captures := pattern.FindStringSubmatch(match)
if len(captures) <= 1 {
// No capture groups, return unchanged
@@ -200,7 +203,6 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
// Replace old value with new value
result = strings.Replace(result, oldVal, newVal, 1)
}
- modified = true
return result
}
@@ -208,16 +210,5 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
return match
})
- // Only write if changes were made
- if modified {
- err = os.WriteFile(fullPath, []byte(result), 0644)
- if err != nil {
- return fmt.Errorf("error writing file: %v", err)
- }
- log.Printf("File %s updated successfully", filename)
- } else {
- log.Printf("No changes made to %s", filename)
- }
-
- return nil
+ return result, nil
}
diff --git a/main_test.go b/main_test.go
new file mode 100644
index 0000000..b1b4342
--- /dev/null
+++ b/main_test.go
@@ -0,0 +1,204 @@
+package main
+
+import (
+ "regexp"
+ "testing"
+)
+
+func TestSimpleFileSimpleExpression(t *testing.T) {
+ fileContents := `
+
+ -
+ 100
+
+
+ `
+ expected := `
+
+ -
+ 150
+
+
+ `
+
+ regex := regexp.MustCompile(`(\d+)`)
+ luaExpr := `*1.5`
+
+ modifiedContent, err := process(fileContents, regex, luaExpr)
+ if err != nil {
+ t.Fatalf("Error processing file: %v", err)
+ }
+
+ if modifiedContent != expected {
+ t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
+ }
+}
+
+func TestSimpleFileVExpression(t *testing.T) {
+ fileContents := `
+
+ -
+ 100
+
+
+ `
+ expected := `
+
+ -
+ 150
+
+
+ `
+
+ regex := regexp.MustCompile(`(\d+)`)
+ luaExpr := `v1 * 1.5`
+
+ modifiedContent, err := process(fileContents, regex, luaExpr)
+ if err != nil {
+ t.Fatalf("Error processing file: %v", err)
+ }
+
+ if modifiedContent != expected {
+ t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
+ }
+}
+
+func TestSimpleFileTableVExpression(t *testing.T) {
+ fileContents := `
+
+ -
+ 100
+
+
+ `
+ expected := `
+
+ -
+ 150
+
+
+ `
+
+ regex := regexp.MustCompile(`(\d+)`)
+ luaExpr := `v[1] * 1.5`
+
+ modifiedContent, err := process(fileContents, regex, luaExpr)
+ if err != nil {
+ t.Fatalf("Error processing file: %v", err)
+ }
+
+ if modifiedContent != expected {
+ t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
+ }
+}
+
+func TestComplexFileSimpleExpression(t *testing.T) {
+ fileContents := `
+
+ -
+ 100
+
+ -
+ 200
+
+ - 300
+
+ `
+ expected := `
+
+ -
+ 150
+
+ -
+ 300
+
+ - 450
+
+ `
+
+ regex := regexp.MustCompile(`(\d+)`)
+ luaExpr := `*1.5`
+
+ modifiedContent, err := process(fileContents, regex, luaExpr)
+ if err != nil {
+ t.Fatalf("Error processing file: %v", err)
+ }
+
+ if modifiedContent != expected {
+ t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
+ }
+}
+
+func TestComplexFileVExpression(t *testing.T) {
+ fileContents := `
+
+ -
+ 100
+
+ -
+ 200
+
+ - 300
+
+ `
+ expected := `
+
+ -
+ 150
+
+ -
+ 300
+
+ - 450
+
+ `
+
+ regex := regexp.MustCompile(`(\d+)`)
+ luaExpr := `v1 * 1.5`
+
+ modifiedContent, err := process(fileContents, regex, luaExpr)
+ if err != nil {
+ t.Fatalf("Error processing file: %v", err)
+ }
+
+ if modifiedContent != expected {
+ t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
+ }
+}
+
+func TestComplexFileTableVExpression(t *testing.T) {
+ fileContents := `
+
+ -
+ 100
+
+ -
+ 200
+
+ - 300
+
+ `
+ expected := `
+
+ -
+ 150
+
+ -
+ 300
+
+ - 450
+
+ `
+
+ regex := regexp.MustCompile(`(\d+)`)
+ luaExpr := `v[1] * 1.5`
+
+ modifiedContent, err := process(fileContents, regex, luaExpr)
+ if err != nil {
+ t.Fatalf("Error processing file: %v", err)
+ }
+
+ if modifiedContent != expected {
+ t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
+ }
+}
\ No newline at end of file