# frozen_string_literal: truerequire"optparse"classRufo::CommandCODE_OK=0CODE_ERROR=1CODE_CHANGE=3defself.run(argv)want_check,exit_code,filename_for_dot_rufo,loglevel=parse_options(argv)new(want_check,exit_code,filename_for_dot_rufo,loglevel).run(argv)enddefinitialize(want_check,exit_code,filename_for_dot_rufo,loglevel)@want_check=want_check@exit_code=exit_code@filename_for_dot_rufo=filename_for_dot_rufo@dot_file=Rufo::DotFile.new@logger=Rufo::Logger.new(loglevel)enddefexit_code(status_code)if@exit_codestatus_codeelsecasestatus_codewhenCODE_OK,CODE_CHANGE0else1endendenddefrun(argv)status_code=ifargv.empty?format_stdinelseformat_argsargvendexitexit_code(status_code)enddefformat_stdincode=STDIN.readresult=format(code,@filename_for_dot_rufo||Dir.getwd)print(result)if!@want_checkcode==result?CODE_OK:CODE_CHANGErescueRufo::SyntaxError=>elogger.error("STDIN is invalid code. Error on line:#{e.lineno}#{e.message}")CODE_ERRORrescue=>exlogger.error("You've found a bug!")logger.error("Please report it to https://github.com/ruby-formatter/rufo/issues with code that triggers it\n")raiseexenddefformat_args(args)top_level_dot_file=@filename_for_dot_rufo||Dir.getwdoptions=@dot_file.get_config_in(top_level_dot_file)||{}file_finder=Rufo::FileFinder.new(args,includes: options[:includes],excludes: options[:excludes],)files=file_finder.to_achanged=falsesyntax_error=falsefiles_exist=falsefiles.eachdo|(exists,file)|ifexistsfiles_exist=trueelselogger.warn("Error: file or directory not found: #{file}")nextendresult=format_file(file)changed|=result==CODE_CHANGEsyntax_error|=result==CODE_ERRORendreturnCODE_ERRORunlessfiles_existcasewhensyntax_errorthenCODE_ERRORwhenchangedthenCODE_CHANGEelseCODE_OKendenddefformat_file(filename)logger.debug("Formatting: #{filename}")code=File.read(filename)beginlocation=@filename_for_dot_rufo||File.dirname(filename)erb=filename.end_with?(".erb")result=format(code,location,erb: erb)rescueRufo::SyntaxError=>e# We ignore syntax errors as these might be template files# with .rb extensionlogger.warn("#{filename}:#{e.lineno}#{e.message}")returnCODE_ERRORendifcode.force_encoding(result.encoding)!=resultif@want_checklogger.warn("Formatting #{filename} produced changes")elseFile.write(filename,result)logger.log("Format: #{filename}")endreturnCODE_CHANGEendrescueRufo::SyntaxError=>elogger.error("#{filename}:#{e.lineno}#{e.message}")CODE_ERRORrescue=>exlogger.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")raiseexenddefformat(code,dir,erb: false)options=@dot_file.get_config_in(dir)||{}iferbformatter=Rufo::ErbFormatter.new(code,**options)elseformatter=Rufo::Formatter.new(code,**options)endformatter.formatresult=formatter.resultresultenddefself.parse_options(argv)exit_code,want_check=true,falsefilename_for_dot_rufo=nilloglevel=:logOptionParser.newdo|opts|opts.version=Rufo::VERSIONopts.banner="Usage: rufo files or dirs [options]"opts.on("-c","--check","Only check formating changes")dowant_check=trueendopts.on("--filename=value","Filename to use to lookup .rufo (useful for STDIN formatting)")do|value|filename_for_dot_rufo=valueendopts.on("-x","--simple-exit","Return 1 in the case of failure, else 0")doexit_code=falseendopts.on(Rufo::Logger::LEVELS,"--loglevel[=LEVEL]","Change the level of logging for the CLI. Options are: error, warn, log (default), debug, silent")do|value|loglevel=value.to_symendopts.on("-h","--help","Show this help")doputsoptsexitendend.parse!(argv)[want_check,exit_code,filename_for_dot_rufo,loglevel]endprivateattr_reader:loggerend