Class: Yorishiro::Tools::EditFile
Instance Method Summary
collapse
#configure, #definition, #read_only?
Instance Method Details
#description ⇒ Object
10
11
12
13
|
# File 'lib/yorishiro/tools/edit_file.rb', line 10
def description
"Replace an exact string in a file. old_string must match the file content exactly " \
"(including whitespace and indentation) and must be unique unless replace_all is true."
end
|
#execute(**params) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/yorishiro/tools/edit_file.rb', line 28
def execute(**params)
path = params[:path] || params["path"]
old_string = params[:old_string] || params["old_string"]
new_string = params[:new_string] || params["new_string"]
replace_all = params[:replace_all] || params["replace_all"] || false
raise "File not found: #{path}" unless File.exist?(path)
raise "Not a file: #{path}" unless File.file?(path)
content = File.read(path)
new_content, count = apply_edit(content, old_string, new_string, replace_all, path)
File.write(path, new_content)
"Successfully replaced #{count} occurrence(s) in #{path}"
end
|
#name ⇒ Object
6
7
8
|
# File 'lib/yorishiro/tools/edit_file.rb', line 6
def name
"edit_file"
end
|
#parameters ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/yorishiro/tools/edit_file.rb', line 15
def parameters
{
type: "object",
properties: {
path: { type: "string", description: "The file path to edit" },
old_string: { type: "string", description: "The exact text to replace" },
new_string: { type: "string", description: "The text to replace it with" },
replace_all: { type: "boolean", description: "Replace all occurrences (default: false)" }
},
required: %w[path old_string new_string]
}
end
|
#permission_check(_arguments) ⇒ Object
44
45
46
|
# File 'lib/yorishiro/tools/edit_file.rb', line 44
def permission_check(_arguments)
:ask
end
|
#preview(arguments) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/yorishiro/tools/edit_file.rb', line 48
def preview(arguments)
path = arguments[:path] || arguments["path"]
old_string = arguments[:old_string] || arguments["old_string"]
new_string = arguments[:new_string] || arguments["new_string"]
replace_all = arguments[:replace_all] || arguments["replace_all"] || false
return nil unless path && old_string && new_string
return nil unless File.file?(path)
content = File.read(path)
new_content, = apply_edit(content, old_string, new_string, replace_all, path)
Diff.unified(content, new_content, path: path)
rescue StandardError
nil
end
|