Class: RubyCrystalCodemod::Command
- Inherits:
-
Object
- Object
- RubyCrystalCodemod::Command
- Defined in:
- lib/ruby_crystal_codemod/command.rb
Constant Summary collapse
- CODE_OK =
0
- CODE_ERROR =
1
- CODE_CHANGE =
3
Class Method Summary collapse
Instance Method Summary collapse
- #exit_code(status_code) ⇒ Object
- #format(code, filename, dir) ⇒ Object
- #format_args(args) ⇒ Object
- #format_file(filename) ⇒ Object
- #format_stdin ⇒ Object
-
#initialize(want_check, exit_code, filename_for_dot_ruby_crystal_codemod, loglevel) ⇒ Command
constructor
A new instance of Command.
- #run(argv) ⇒ Object
Constructor Details
#initialize(want_check, exit_code, filename_for_dot_ruby_crystal_codemod, loglevel) ⇒ Command
Returns a new instance of Command.
16 17 18 19 20 21 22 23 24 |
# File 'lib/ruby_crystal_codemod/command.rb', line 16 def initialize(want_check, exit_code, filename_for_dot_ruby_crystal_codemod, loglevel) @want_check = want_check @exit_code = exit_code @filename_for_dot_ruby_crystal_codemod = filename_for_dot_ruby_crystal_codemod @dot_file = RubyCrystalCodemod::DotFile.new @squiggly_warning_files = [] @logger = RubyCrystalCodemod::Logger.new(loglevel) @post_processor = PostProcessCrystal.new end |
Class Method Details
.parse_options(argv) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/ruby_crystal_codemod/command.rb', line 170 def self.(argv) exit_code, want_check = true, false filename_for_dot_ruby_crystal_codemod = nil loglevel = :log OptionParser.new do |opts| opts.version = RubyCrystalCodemod::VERSION opts. = "Usage: ruby_crystal_codemod files or dirs [options]" opts.on("-c", "--check", "Only check formating changes") do want_check = true end opts.on("--filename=value", "Filename to use to lookup .ruby_crystal_codemod (useful for STDIN formatting)") do |value| filename_for_dot_ruby_crystal_codemod = value end opts.on("-x", "--simple-exit", "Return 1 in the case of failure, else 0") do exit_code = false end opts.on(RubyCrystalCodemod::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_sym end opts.on("-h", "--help", "Show this help") do puts opts exit end end.parse!(argv) [want_check, exit_code, filename_for_dot_ruby_crystal_codemod, loglevel] end |
.run(argv) ⇒ Object
11 12 13 14 |
# File 'lib/ruby_crystal_codemod/command.rb', line 11 def self.run(argv) want_check, exit_code, filename_for_dot_ruby_crystal_codemod, loglevel = (argv) new(want_check, exit_code, filename_for_dot_ruby_crystal_codemod, loglevel).run(argv) end |
Instance Method Details
#exit_code(status_code) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ruby_crystal_codemod/command.rb', line 26 def exit_code(status_code) if @exit_code status_code else case status_code when CODE_OK, CODE_CHANGE 0 else 1 end end end |
#format(code, filename, dir) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/ruby_crystal_codemod/command.rb', line 157 def format(code, filename, dir) @squiggly_warning = false formatter = RubyCrystalCodemod::Formatter.new(code, filename, dir) = @dot_file.get_config_in(dir) unless .nil? formatter.init_settings() end formatter.format result = formatter.result result end |
#format_args(args) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ruby_crystal_codemod/command.rb', line 65 def format_args(args) file_finder = RubyCrystalCodemod::FileFinder.new(args) files = file_finder.to_a changed = false syntax_error = false files_exist = false files.each do |(exists, file)| if exists files_exist = true else logger.warn("Error: file or directory not found: #{file}") next end result = format_file(file) changed |= result == CODE_CHANGE syntax_error |= result == CODE_ERROR end return CODE_ERROR unless files_exist case when syntax_error then CODE_ERROR when changed then CODE_CHANGE else CODE_OK end end |
#format_file(filename) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ruby_crystal_codemod/command.rb', line 95 def format_file(filename) logger.debug("Formatting: #{filename}") code = File.read(filename) begin result = format(code, filename, @filename_for_dot_ruby_crystal_codemod || File.dirname(filename)) rescue RubyCrystalCodemod::SyntaxError # We ignore syntax errors as these might be template files # with .rb extension logger.warn("Error: #{filename} has syntax errors") return CODE_ERROR end # if code.force_encoding(result.encoding) != result if @want_check logger.warn("Formatting #{filename} produced changes") else crystal_filename = filename.sub(/\.rb$/, ".cr") File.write(crystal_filename, result) logger.log("Format: #{filename} => #{crystal_filename}") end # Run the post-processing step to remove Ruby code and uncomment Crystal. @post_processor.filename = crystal_filename @post_processor.post_process_crystal File.write(crystal_filename, @post_processor.contents) # UPDATE: It's difficult to package/compile the Crystal binary in a Ruby gem. # But it was really easy to port it to Ruby! # post_process_cmd = File.expand_path(File.join(__dir__, "../../util/post_process")) # unless File.exist?(post_process_cmd) # raise "Please run ./bin/compile_post_process to compile the post-processing command " \ # "at: #{post_process_cmd}" # end # stdout, stderr, status = Open3.capture3(post_process_cmd, crystal_filename) # unless status.success? # warn "'./util/post_process' failed with status: #{status.exitstatus}\n\n" \ # "stdout: #{stdout}\n\n" \ # "stderr: #{stderr}" # end # Format the Crystal file with the Crystal code formatter stdout, stderr, status = Open3.capture3("crystal", "tool", "format", crystal_filename) unless status.success? warn "'crystal tool format' failed with status: #{status.exitstatus}\n\n" \ "stdout: #{stdout}\n\n" \ "stderr: #{stderr}" puts "(This probably means that you will have to fix some errors manually.)" end return CODE_CHANGE # end rescue RubyCrystalCodemod::SyntaxError logger.error("Error: the given text in #{filename} is not a valid ruby program (it has syntax errors)") 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/DocSpring/ruby_crystal_codemod/issues with code that triggers it\n") raise e end |
#format_stdin ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruby_crystal_codemod/command.rb', line 48 def format_stdin code = STDIN.read result = format(code, nil, @filename_for_dot_ruby_crystal_codemod || Dir.getwd) print(result) if !@want_check code == result ? CODE_OK : CODE_CHANGE rescue RubyCrystalCodemod::SyntaxError logger.error("Error: the given text is not a valid ruby program (it has syntax errors)") CODE_ERROR rescue => e logger.error("You've found a bug!") logger.error("Please report it to https://github.com/DocSpring/ruby_crystal_codemod/issues with code that triggers it\n") raise e end |
#run(argv) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/ruby_crystal_codemod/command.rb', line 39 def run(argv) status_code = if argv.empty? format_stdin else format_args argv end exit exit_code(status_code) end |