Class: Tidewave::Tools::EditProjectFile

Inherits:
Base
  • Object
show all
Defined in:
lib/tidewave/tools/edit_project_file.rb

Instance Method Summary collapse

Methods inherited from Base

file_system_tool, file_system_tool?

Instance Method Details

#call(path:, old_string:, new_string:) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tidewave/tools/edit_project_file.rb', line 31

def call(path:, old_string:, new_string:)
  # Check if the file exists within the project root and has been read
  Tidewave::FileTracker.validate_path_is_editable!(path)

  old_content = Tidewave::FileTracker.read_file(path)

  # Ensure old_string is unique within the file
  scan_result = old_content.scan(old_string)
  raise ArgumentError, "old_string is not found" if scan_result.empty?
  raise ArgumentError, "old_string is not unique" if scan_result.size > 1

  new_content = old_content.sub(old_string, new_string)

  Tidewave::FileTracker.write_file(path, new_content)
end