Module: Roast::Tools::UpdateFiles

Extended by:
UpdateFiles
Included in:
UpdateFiles
Defined in:
lib/roast/tools/update_files.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Add this method to be included in other classes



10
11
12
13
14
15
16
17
18
19
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
# File 'lib/roast/tools/update_files.rb', line 10

def included(base)
  base.class_eval do
    function(
      :update_files,
      "Apply a unified diff/patch to files in the workspace. Changes are applied atomically if possible.",
      diff: {
        type: "string",
        description: "The unified diff/patch content to apply",
      },
      base_path: {
        type: "string",
        description: "Base path for relative file paths in the diff (default: current working directory)",
        required: false,
      },
      restrict_path: {
        type: "string",
        description: "Optional path restriction to limit where files can be modified",
        required: false,
      },
      create_files: {
        type: "boolean",
        description: "Whether to create new files if they don't exist (default: true)",
        required: false,
      },
    ) do |params|
      base_path = params[:base_path] || Dir.pwd
      create_files = params.fetch(:create_files, true)
      restrict_path = params[:restrict_path]

      Roast::Tools::UpdateFiles.call(
        params[:diff],
        base_path,
        restrict_path,
        create_files,
      )
    end
  end
end

Instance Method Details

#call(diff, base_path = Dir.pwd, restrict_path = nil, create_files = true) ⇒ String

Apply a unified diff to files

Parameters:

  • diff (String)

    unified diff content

  • base_path (String) (defaults to: Dir.pwd)

    base path for relative paths in the diff

  • restrict_path (String, nil) (defaults to: nil)

    optional path restriction

  • create_files (Boolean) (defaults to: true)

    whether to create new files if they don’t exist

Returns:

  • (String)

    result message



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/roast/tools/update_files.rb', line 56

def call(diff, base_path = Dir.pwd, restrict_path = nil, create_files = true)
  Roast::Helpers::Logger.info("🔄 Applying patch to files\n")

  # Parse the unified diff to identify files and changes
  file_changes = parse_unified_diff(diff)

  if file_changes.empty?
    return "Error: No valid file changes found in the provided diff"
  end

  # Validate changes
  validation_result = validate_changes(file_changes, base_path, restrict_path, create_files)
  return validation_result if validation_result.is_a?(String) && validation_result.start_with?("Error:")

  # Apply changes atomically
  apply_changes(file_changes, base_path, create_files)
rescue StandardError => e
  "Error applying patch: #{e.message}".tap do |error_message|
    Roast::Helpers::Logger.error(error_message + "\n")
    Roast::Helpers::Logger.debug(e.backtrace.join("\n") + "\n") if ENV["DEBUG"]
  end
end