Fix up the workings
This commit is contained in:
92
main.go
92
main.go
@@ -53,22 +53,48 @@ func main() {
|
||||
luaExpr := args[1]
|
||||
files := args[2:]
|
||||
|
||||
// Generate the Lua script
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
log.Printf("Final expression: %s", luaExpr)
|
||||
|
||||
// Make sure the regex can match across multiple lines by adding (?s) flag
|
||||
if !strings.HasPrefix(regexPattern, "(?s)") {
|
||||
regexPattern = "(?s)" + regexPattern
|
||||
}
|
||||
log.Printf("Regex pattern: %s", regexPattern)
|
||||
log.Printf("Lua script: %s", luaScript)
|
||||
log.Printf("Processing files: %v", files)
|
||||
|
||||
// Compile the pattern for file processing
|
||||
pattern, err := regexp.Compile(regexPattern)
|
||||
if err != nil {
|
||||
Error.Printf("Invalid regex pattern: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Process each file
|
||||
for _, file := range files {
|
||||
err := processFile(file, pattern, luaScript)
|
||||
if err != nil {
|
||||
Error.Printf("Error processing file %s: %v", file, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// buildLuaScript creates a complete Lua script from the expression
|
||||
func buildLuaScript(luaExpr string) string {
|
||||
// Check if the expression needs v1 to be prepended
|
||||
if strings.HasPrefix(luaExpr, "*") || strings.HasPrefix(luaExpr, "/") ||
|
||||
strings.HasPrefix(luaExpr, "+") || strings.HasPrefix(luaExpr, "-") ||
|
||||
strings.HasPrefix(luaExpr, "^") || strings.HasPrefix(luaExpr, "%") {
|
||||
luaExpr = "v[1]" + luaExpr
|
||||
log.Printf("Expression modified to: %s", luaExpr)
|
||||
}
|
||||
|
||||
// Replace shorthand v1, v2, etc. with v[1], v[2]
|
||||
shorthandRegex := regexp.MustCompile(`\bv(\d+)\b`)
|
||||
luaExpr = shorthandRegex.ReplaceAllString(luaExpr, "v[$1]")
|
||||
log.Printf("Final expression: %s", luaExpr)
|
||||
|
||||
// Check if the expression is a simple expression or explicitly manipulates v table
|
||||
var fullScript string
|
||||
// Add custom script header
|
||||
// Add custom script header with helper functions
|
||||
scriptHeader := `
|
||||
-- Custom Lua helpers for math operations
|
||||
function min(a, b) return math.min(a, b) end
|
||||
@@ -78,6 +104,8 @@ function floor(x) return math.floor(x) end
|
||||
function ceil(x) return math.ceil(x) end
|
||||
`
|
||||
|
||||
// Check if the expression already has assignments or a return statement
|
||||
var fullScript string
|
||||
if strings.Contains(luaExpr, "v[1] =") || strings.Contains(luaExpr, "v[2] =") ||
|
||||
strings.Contains(luaExpr, "return") {
|
||||
// Already has assignments, use as is
|
||||
@@ -98,39 +126,31 @@ end
|
||||
`, scriptHeader, luaExpr)
|
||||
}
|
||||
|
||||
log.Printf("Regex pattern: %s", regexPattern)
|
||||
log.Printf("Lua script: %s", fullScript)
|
||||
log.Printf("Processing files: %v", files)
|
||||
return fullScript
|
||||
}
|
||||
|
||||
// Compile the pattern for file processing
|
||||
pattern, err := regexp.Compile(regexPattern)
|
||||
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 {
|
||||
Error.Printf("Invalid regex pattern: %v", err)
|
||||
return
|
||||
return fmt.Errorf("error reading file: %v", err)
|
||||
}
|
||||
|
||||
// Process each file
|
||||
for _, file := range files {
|
||||
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)
|
||||
}
|
||||
fileContent := string(content)
|
||||
result, err := process(fileContent, pattern, luaScript)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
return nil
|
||||
}
|
||||
|
||||
func process(data string, pattern *regexp.Regexp, luaScript string) (string, error) {
|
||||
@@ -149,6 +169,7 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
return data, fmt.Errorf("error in Lua script: %v", err)
|
||||
}
|
||||
|
||||
modified := false
|
||||
// Process all regex matches
|
||||
result := pattern.ReplaceAllStringFunc(data, func(match string) string {
|
||||
captures := pattern.FindStringSubmatch(match)
|
||||
@@ -203,6 +224,7 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
// Replace old value with new value
|
||||
result = strings.Replace(result, oldVal, newVal, 1)
|
||||
}
|
||||
modified = true
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -210,5 +232,9 @@ func process(data string, pattern *regexp.Regexp, luaScript string) (string, err
|
||||
return match
|
||||
})
|
||||
|
||||
if !modified {
|
||||
log.Printf("No changes made to the content")
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
203
main_test.go
203
main_test.go
@@ -2,10 +2,18 @@ package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSimpleFileSimpleExpression(t *testing.T) {
|
||||
// Helper function to normalize whitespace for comparison
|
||||
func normalizeWhitespace(s string) string {
|
||||
// Replace all whitespace with a single space
|
||||
re := regexp.MustCompile(`\s+`)
|
||||
return re.ReplaceAllString(strings.TrimSpace(s), " ")
|
||||
}
|
||||
|
||||
func TestSimpleValueMultiplication(t *testing.T) {
|
||||
fileContents := `
|
||||
<config>
|
||||
<item>
|
||||
@@ -21,20 +29,25 @@ func TestSimpleFileSimpleExpression(t *testing.T) {
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||
// Create a regex pattern with the (?s) flag for multiline matching
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
luaExpr := `*1.5`
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
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)
|
||||
// Compare normalized content
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimpleFileVExpression(t *testing.T) {
|
||||
func TestShorthandNotation(t *testing.T) {
|
||||
fileContents := `
|
||||
<config>
|
||||
<item>
|
||||
@@ -50,20 +63,23 @@ func TestSimpleFileVExpression(t *testing.T) {
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
luaExpr := `v1 * 1.5`
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
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)
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimpleFileTableVExpression(t *testing.T) {
|
||||
func TestArrayNotation(t *testing.T) {
|
||||
fileContents := `
|
||||
<config>
|
||||
<item>
|
||||
@@ -79,20 +95,23 @@ func TestSimpleFileTableVExpression(t *testing.T) {
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
luaExpr := `v[1] * 1.5`
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
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)
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComplexFileSimpleExpression(t *testing.T) {
|
||||
func TestMultipleMatches(t *testing.T) {
|
||||
fileContents := `
|
||||
<config>
|
||||
<item>
|
||||
@@ -116,89 +135,169 @@ func TestComplexFileSimpleExpression(t *testing.T) {
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
luaExpr := `*1.5`
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
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)
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComplexFileVExpression(t *testing.T) {
|
||||
func TestMultipleCaptureGroups(t *testing.T) {
|
||||
fileContents := `
|
||||
<config>
|
||||
<item>
|
||||
<value>100</value>
|
||||
<value>10</value>
|
||||
<multiplier>5</multiplier>
|
||||
</item>
|
||||
<item>
|
||||
<value>200</value>
|
||||
</item>
|
||||
<item> <value>300</value> </item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>150</value>
|
||||
<value>50</value>
|
||||
<multiplier>5</multiplier>
|
||||
</item>
|
||||
<item>
|
||||
<value>300</value>
|
||||
</item>
|
||||
<item> <value>450</value> </item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||
luaExpr := `v1 * 1.5`
|
||||
// Use (?s) flag to match across multiple lines
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>`)
|
||||
luaExpr := `v1 * v2`
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||
// Verify the regex matches before processing
|
||||
matches := regex.FindStringSubmatch(fileContents)
|
||||
if len(matches) <= 1 {
|
||||
t.Fatalf("Regex didn't match any capture groups in test input: %v", fileContents)
|
||||
}
|
||||
t.Logf("Matches: %v", matches)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
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)
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComplexFileTableVExpression(t *testing.T) {
|
||||
func TestModifyingMultipleValues(t *testing.T) {
|
||||
fileContents := `
|
||||
<config>
|
||||
<item>
|
||||
<value>100</value>
|
||||
<value>50</value>
|
||||
<multiplier>3</multiplier>
|
||||
<divider>2</divider>
|
||||
</item>
|
||||
<item>
|
||||
<value>200</value>
|
||||
</item>
|
||||
<item> <value>300</value> </item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>150</value>
|
||||
<value>75</value>
|
||||
<multiplier>5</multiplier>
|
||||
<divider>1</divider>
|
||||
</item>
|
||||
<item>
|
||||
<value>300</value>
|
||||
</item>
|
||||
<item> <value>450</value> </item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||
luaExpr := `v[1] * 1.5`
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>`)
|
||||
luaExpr := `v[1] = v[1] * v[2] / v[3]; v[2] = min(v[2] * 2, 5); v[3] = max(1, v[3] / 2)`
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||
// Verify the regex matches before processing
|
||||
matches := regex.FindStringSubmatch(fileContents)
|
||||
if len(matches) <= 1 {
|
||||
t.Fatalf("Regex didn't match any capture groups in test input: %v", fileContents)
|
||||
}
|
||||
t.Logf("Matches: %v", matches)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
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)
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalValues(t *testing.T) {
|
||||
fileContents := `
|
||||
<config>
|
||||
<item>
|
||||
<value>10.5</value>
|
||||
<multiplier>2.5</multiplier>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>26.25</value>
|
||||
<multiplier>2.5</multiplier>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`(?s)<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>`)
|
||||
luaExpr := `v1 * v2`
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing file: %v", err)
|
||||
}
|
||||
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLuaMathFunctions(t *testing.T) {
|
||||
fileContents := `
|
||||
<config>
|
||||
<item>
|
||||
<value>16</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
expected := `
|
||||
<config>
|
||||
<item>
|
||||
<value>4</value>
|
||||
</item>
|
||||
</config>
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
luaExpr := `math.sqrt(v1)`
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
if err != nil {
|
||||
t.Fatalf("Error processing file: %v", err)
|
||||
}
|
||||
|
||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||
normalizedExpected := normalizeWhitespace(expected)
|
||||
if normalizedModified != normalizedExpected {
|
||||
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user