diff --git a/example_cook.toml b/example_cook.toml index 79d663a..4c77823 100644 --- a/example_cook.toml +++ b/example_cook.toml @@ -102,6 +102,68 @@ regex = '(?P[A-Za-z0-9_]+)\s*=' lua = 'key = prefix .. key; return true' files = ['**/*.properties'] +# HTTP fetch example - get version from API and update config +[[commands]] +name = 'UpdateVersionFromAPI' +regex = 'version\s*=\s*"(?P[^"]+)"' +lua = ''' +local response = fetch("https://api.example.com/version", { + method = "GET", + headers = { ["Accept"] = "application/json" } +}) +if response and response.body then + local data = fromJSON(response.body) + if data.latest then + version = data.latest + return true + end +end +return false +''' +files = ['version.conf'] + +# Complex multiline block replacement with state machine +[[commands]] +name = 'ModifyConfigBlock' +regex = '''(?x) +\[server\] +\s+host\s*=\s*"(?P[^"]+)" +\s+port\s*=\s*(?P\d+) +\s+ssl\s*=\s*(?Ptrue|false)''' +lua = ''' +port = num(port) + 1000 +ssl = "true" +replacement = format('[server]\n host = "%s"\n port = %d\n ssl = %s', host, port, ssl) +return true +''' +files = ['server.conf'] + +# Regex with !any to capture entire sections +[[commands]] +name = 'WrapInComment' +regex = 'FEATURE_START\n(?P!any)\nFEATURE_END' +lua = ''' +replacement = "FEATURE_START\n# " .. feature:gsub("\n", "\n# ") .. "\nFEATURE_END" +return true +''' +files = ['features/**/*.txt'] + +# Advanced capture groups with complex logic +[[commands]] +name = 'UpdateDependencies' +regex = 'dependency\("(?P[^"]+)", "(?P[^"]+)", "(?P[^"]+)"\)' +lua = ''' +local major, minor, patch = version:match("(%d+)%.(%d+)%.(%d+)") +if major and minor and patch then + -- Bump minor version + minor = num(minor) + 1 + version = format("%s.%s.0", major, minor) + return true +end +return false +''' +files = ['build.gradle', 'build.gradle.kts'] + # JSON mode examples - modify single field [[commands]] name = 'JSONModifyField' @@ -172,6 +234,66 @@ end ''' files = ['items/**/*.json'] +# JSON mode - remove array elements conditionally +[[commands]] +name = 'JSONRemoveDisabled' +json = true +lua = ''' +if data.features then + local i = 1 + while i <= #data.features do + if data.features[i].enabled == false then + table.remove(data.features, i) + else + i = i + 1 + end + end + modified = true +end +''' +files = ['config/**/*.json'] + +# JSON mode - deep nested object manipulation +[[commands]] +name = 'JSONDeepUpdate' +json = true +lua = ''' +if data.game and data.game.balance and data.game.balance.economy then + local econ = data.game.balance.economy + econ.inflation = (econ.inflation or 1.0) * 1.05 + econ.taxRate = 0.15 + econ.lastUpdate = os.date("%Y-%m-%d") + modified = true +end +''' +files = ['settings/**/*.json'] + +# JSON mode - iterate and transform all matching objects +[[commands]] +name = 'JSONTransformItems' +json = true +lua = ''' +local function processItem(item) + if item.type == "weapon" and item.damage then + item.damage = item.damage * multiply + item.modified = true + end +end + +if data.items then + for _, item in ipairs(data.items) do + processItem(item) + end + modified = true +elseif data.inventory then + for _, item in ipairs(data.inventory) do + processItem(item) + end + modified = true +end +''' +files = ['data/**/*.json'] + # CSV processing example - read, modify, write [[commands]] name = 'CSVProcess' @@ -222,3 +344,201 @@ replacement = toCSV(rows, { hasheader = true }) return true ''' files = ['exports/**/*.csv'] + +# XML mode - multiply numeric attributes using helper functions +[[commands]] +name = 'XMLMultiplyAttributes' +regex = '(?P!any)' +lua = ''' +visitElements(data, function(elem) + if elem._tag == "Item" then + modifyNumAttr(elem, "Weight", function(val) return val * multiply end) + modifyNumAttr(elem, "Value", function(val) return val * foobar end) + end +end) +modified = true +''' +files = ['game/**/*.xml'] + +# XML mode - modify specific element attributes +[[commands]] +name = 'XMLUpdateAfflictions' +regex = '(?P!any)' +lua = ''' +local afflictions = findElements(data, "Affliction") +for _, affliction in ipairs(afflictions) do + local id = getAttr(affliction, "identifier") + if id == "burn" or id == "bleeding" then + modifyNumAttr(affliction, "strength", function(val) return val * 0.5 end) + setAttr(affliction, "description", "Weakened effect") + end +end +modified = true +''' +files = ['config/Afflictions.xml'] + +# XML mode - add new elements using helpers +[[commands]] +name = 'XMLAddItems' +regex = '(?P!any)' +lua = ''' +local items = findFirstElement(data, "Items") +if items then + local newItem = { + _tag = "Item", + _attr = { + identifier = "new_item", + Weight = "10", + Value = "500" + } + } + addChild(items, newItem) + modified = true +end +''' +files = ['items/**/*.xml'] + +# XML mode - remove elements by attribute value +[[commands]] +name = 'XMLRemoveDisabled' +regex = '(?P!any)' +lua = ''' +visitElements(data, function(elem) + if elem._tag == "Feature" and getAttr(elem, "enabled") == "false" then + -- Mark for removal (actual removal happens via parent) + elem._remove = true + end +end) + +-- Remove marked children +visitElements(data, function(elem) + if elem._children then + local i = 1 + while i <= #elem._children do + if elem._children[i]._remove then + table.remove(elem._children, i) + else + i = i + 1 + end + end + end +end) + +modified = true +''' +files = ['config/**/*.xml'] + +# XML mode - conditional attribute updates based on other attributes +[[commands]] +name = 'XMLConditionalUpdate' +regex = '(?P!any)' +lua = ''' +visitElements(data, function(elem) + if elem._tag == "Weapon" then + local tier = getAttr(elem, "tier") + if tier and num(tier) >= 3 then + -- High tier weapons get damage boost + modifyNumAttr(elem, "damage", function(val) return val * 1.5 end) + setAttr(elem, "rarity", "legendary") + end + end +end) +modified = true +''' +files = ['weapons/**/*.xml'] + +# XML mode - modify nested elements +[[commands]] +name = 'XMLNestedModify' +regex = '(?P!any)' +lua = ''' +local config = findFirstElement(data, "Configuration") +if config then + local settings = findFirstElement(config, "Settings") + if settings then + setAttr(settings, "timeout", "120") + setAttr(settings, "maxRetries", "5") + + -- Add or update nested element + local logging = findFirstElement(settings, "Logging") + if not logging then + logging = { + _tag = "Logging", + _attr = { level = "DEBUG", enabled = "true" } + } + addChild(settings, logging) + else + setAttr(logging, "level", "INFO") + end + end +end +modified = true +''' +files = ['config/**/*.xml'] + +# XML mode - batch attribute operations +[[commands]] +name = 'XMLBatchAttributeUpdate' +regex = '(?P!any)' +lua = ''' +-- Update all Price attributes across entire document +visitElements(data, function(elem) + if hasAttr(elem, "Price") then + modifyNumAttr(elem, "Price", function(val) return val * 1.1 end) + end + if hasAttr(elem, "Cost") then + modifyNumAttr(elem, "Cost", function(val) return val * 0.9 end) + end +end) +modified = true +''' +files = ['economy/**/*.xml'] + +# XML mode - clone and modify elements +[[commands]] +name = 'XMLCloneItems' +regex = '(?P!any)' +lua = ''' +local items = findElements(data, "Item") +local newItems = {} + +for _, item in ipairs(items) do + local id = getAttr(item, "identifier") + if id and id:match("^weapon_") then + -- Clone weapon as upgraded version + local upgraded = { + _tag = "Item", + _attr = { + identifier = id .. "_mk2", + Weight = getAttr(item, "Weight"), + Value = tostring(num(getAttr(item, "Value")) * 2) + } + } + table.insert(newItems, upgraded) + end +end + +-- Add all new items +for _, newItem in ipairs(newItems) do + addChild(data, newItem) +end + +if #newItems > 0 then + modified = true +end +''' +files = ['items/**/*.xml'] + +# XML mode - remove all children with specific tag +[[commands]] +name = 'XMLRemoveObsolete' +regex = '(?P!any)' +lua = ''' +visitElements(data, function(elem) + -- Remove all "Deprecated" children + removeChildren(elem, "Deprecated") + removeChildren(elem, "Legacy") +end) +modified = true +''' +files = ['config/**/*.xml'] \ No newline at end of file