Class: Brute::Tools::FSPatch

Inherits:
Brute::Tool show all
Defined in:
lib/brute/tools/fs_patch.rb

Instance Method Summary collapse

Methods inherited from Brute::Tool

#call, description, #description, param, param_definitions, params, #params, #params_schema

Instance Method Details

#execute(file_path:, old_string:, new_string:, replace_all: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brute/tools/fs_patch.rb', line 20

def execute(file_path:, old_string:, new_string:, replace_all: false)
  path = File.expand_path(file_path)
  Brute::Tools::FS::FileMutationQueue.serialize(path) do
    unless File.exist?(path)
      raise "File not found: #{path}"
    end

    original = File.read(path)
    unless original.include?(old_string)
      raise "old_string not found in #{path}"
    end

    Brute::Tools::FS::SnapshotStore.save(path)

    if replace_all
      updated = original.gsub(old_string, new_string)
    else
      updated = original.sub(old_string, new_string)
    end

    File.write(path, updated)
    diff = Brute::Diff.unified(original, updated)
    if replace_all
      count = original.scan(old_string).size
    else
      count = 1
    end
    { success: true, file_path: path, replacements: count, diff: diff }
  end
end

#nameObject



18
# File 'lib/brute/tools/fs_patch.rb', line 18

def name; "patch"; end