Class: Aiko::Tools::EditFile
- Inherits:
-
Base
- Object
- Base
- Aiko::Tools::EditFile
show all
- Defined in:
- lib/aiko/tools/edit_file.rb
Constant Summary
Constants inherited
from Base
Base::OUTPUT_LIMIT
Instance Method Summary
collapse
Methods inherited from Base
#initialize
Instance Method Details
#approval_message(arguments) ⇒ Object
57
58
59
|
# File 'lib/aiko/tools/edit_file.rb', line 57
def approval_message(arguments)
"edit_file: #{arguments["path"]} を編集します\n--- 置換前 ---\n#{arguments["old_string"]}\n--- 置換後 ---\n#{arguments["new_string"]}"
end
|
#call(arguments) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/aiko/tools/edit_file.rb', line 36
def call(arguments)
path = resolve_path(arguments.fetch("path"))
return "Error: no such file: #{arguments["path"]}" unless File.file?(path)
old_string = arguments.fetch("old_string")
new_string = arguments.fetch("new_string")
content = File.read(path)
count = count_occurrences(content, old_string)
return "Error: old_string not found" if count.zero?
return "Error: old_string is not unique (#{count} matches)" if count > 1
index = content.index(old_string)
File.write(path, content[0, index] + new_string + content[(index + old_string.length)..])
"Edited #{arguments["path"]}"
end
|
#description ⇒ Object
10
11
12
13
|
# File 'lib/aiko/tools/edit_file.rb', line 10
def description
"Replace a unique occurrence of old_string with new_string in a file. " \
"old_string must match exactly one location in the file; read the file first."
end
|
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/aiko/tools/edit_file.rb', line 15
def input_schema
{
"type" => "object",
"properties" => {
"path" => {
"type" => "string",
"description" => "File path relative to the working directory"
},
"old_string" => {
"type" => "string",
"description" => "Exact text to replace (must be unique in the file)"
},
"new_string" => {
"type" => "string",
"description" => "Replacement text"
}
},
"required" => %w[path old_string new_string]
}
end
|
#name ⇒ Object
6
7
8
|
# File 'lib/aiko/tools/edit_file.rb', line 6
def name
"edit_file"
end
|
#requires_approval?(_arguments) ⇒ Boolean
53
54
55
|
# File 'lib/aiko/tools/edit_file.rb', line 53
def requires_approval?(_arguments)
true
end
|