Module: Roast::Tools::WriteFile
Class Method Summary collapse
-
.included(base) ⇒ Object
Add this method to be included in other classes.
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
Add this method to be included in other classes
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/roast/tools/write_file.rb', line 13 def included(base) base.class_eval do function( :write_file, "Write content to a file. Creates the file if it does not exist, or overwrites it if it does.", path: { type: "string", description: "The path to the file to write, relative to the current working directory: #{Dir.pwd}", }, content: { type: "string", description: "The content to write to the file" }, ) do |params| Roast::Tools::WriteFile.call(params[:path], params[:content]).tap do |_result| Roast::Helpers::Logger.info(params[:content]) end end end end |
Instance Method Details
#call(path, content) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/roast/tools/write_file.rb', line 32 def call(path, content) if path.start_with?("test/") Roast::Helpers::Logger.info("📝 Writing to file: #{path}\n") # Ensure the directory exists dir = File.dirname(path) FileUtils.mkdir_p(dir) unless File.directory?(dir) # Write the content to the file # Check if path is absolute or relative absolute_path = path.start_with?("/") ? path : File.join(Dir.pwd, path) File.write(absolute_path, content) "Successfully wrote #{content.lines.count} lines to #{path}" else Roast::Helpers::Logger.error("😳 Path must start with 'test/' to use the write_file tool\n") "Error: Path must start with 'test/' to use the write_file tool, try again." end rescue StandardError => e "Error writing file: #{e.}".tap do || Roast::Helpers::Logger.error( + "\n") Roast::Helpers::Logger.debug(e.backtrace.join("\n") + "\n") if ENV["DEBUG"] end end |