Class: Airb::Tools::FS::EditFile

Inherits:
VSM::ToolCapsule
  • Object
show all
Defined in:
lib/airb/tools/fs/edit_file.rb

Instance Method Summary collapse

Instance Method Details

#execution_modeObject

change to :thread if you do heavy CPU work



19
# File 'lib/airb/tools/fs/edit_file.rb', line 19

def execution_mode = :fiber # change to :thread if you do heavy CPU work

#run(args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/airb/tools/fs/edit_file.rb', line 21

def run(args)
  path = governance.send(:safe_path, args.fetch("path"))
  old  = args.fetch("old_str")
  newv = args.fetch("new_str")

  if !File.exist?(path) && old.to_s.empty?
    FileUtils.mkdir_p(File.dirname(path))
    File.write(path, newv)
    return "OK"
  end

  content  = File.read(path, mode: "r:UTF-8")
  replaced = content.gsub(old, newv)
  raise "old_str not found" if replaced == content && !old.empty?
  File.write(path, replaced)
  "OK"
end