class Rufo::Command
def format_file(filename)
def format_file(filename) logger.debug("Formatting: #{filename}") code = File.read(filename, encoding: "UTF-8") begin location = @filename_for_dot_rufo || File.dirname(filename) erb = filename.end_with?(".erb") result = format(code, location, erb: erb) rescue Rufo::SyntaxError => e # We ignore syntax errors as these might be template files # with .rb extension logger.warn("#{filename}:#{e.lineno} #{e.message}") return CODE_ERROR end if code.force_encoding(result.encoding) != result if @want_check logger.warn("Formatting #{filename} produced changes") else File.write(filename, result) logger.log("Format: #{filename}") end return CODE_CHANGE end rescue Rufo::SyntaxError => e logger.error("#{filename}:#{e.lineno} #{e.message}") CODE_ERROR rescue Rufo::UnknownSyntaxError logger.error("#{filename} is invalid code. Try running the code for a better error.") CODE_ERROR rescue => e logger.error("You've found a bug!") logger.error("It happened while trying to format the file #{filename}") logger.error("Please report it to https://github.com/ruby-formatter/rufo/issues with code that triggers it\n") raise e end