Class: Isort::CLI
- Inherits:
-
Object
- Object
- Isort::CLI
- Defined in:
- lib/isort.rb
Overview
CLI handles command-line interface for the isort gem
Class Method Summary collapse
- .handle_check(file, processor, options) ⇒ Object
- .handle_diff(file, processor, options) ⇒ Object
- .handle_sort(file, processor, options) ⇒ Object
- .process_directory(dir, options) ⇒ Object
- .process_file(file, options) ⇒ Object
- .start ⇒ Object
Class Method Details
.handle_check(file, processor, options) ⇒ Object
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/isort.rb', line 206 def self.handle_check(file, processor, ) would_change = processor.check if would_change puts "#{file} - imports are not sorted" unless [:quiet] 1 else puts "#{file} - imports are sorted" if [:verbose] 0 end end |
.handle_diff(file, processor, options) ⇒ Object
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/isort.rb', line 217 def self.handle_diff(file, processor, ) diff_output = processor.diff if diff_output && !diff_output.empty? puts diff_output 1 else puts "#{file} - no changes" if [:verbose] 0 end end |
.handle_sort(file, processor, options) ⇒ Object
228 229 230 231 232 233 234 235 236 |
# File 'lib/isort.rb', line 228 def self.handle_sort(file, processor, ) changed = processor.process if changed puts "Imports sorted in #{file}" unless [:quiet] else puts "#{file} - no changes needed" if [:verbose] end 0 end |
.process_directory(dir, options) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/isort.rb', line 238 def self.process_directory(dir, ) unless Dir.exist?(dir) puts "Error: Directory not found: #{dir}" unless [:quiet] return 1 end files = Dir.glob("#{dir}/**/*.rb") if files.empty? puts "No Ruby files found in #{dir}" unless [:quiet] return 0 end total = 0 changed = 0 errors = 0 = [] files.each do |file| result = process_file(file, .merge(quiet: true)) total += 1 if result == 0 changed += 1 if [:check] || [:diff] else errors += 1 end rescue StandardError => e errors += 1 << "#{file}: #{e.}" end unless [:quiet] if [:check] unsorted = total - changed puts "Checked #{total} files: #{changed} sorted, #{unsorted} need sorting" elsif [:diff] puts "Checked #{total} files: #{total - changed} would change" else puts "Sorted imports in #{total} files in directory: #{dir}" end if .any? puts "\nErrors encountered:" .each { |err| puts " - #{err}" } end end errors > 0 || ([:check] && changed < total) ? 1 : 0 end |
.process_file(file, options) ⇒ Object
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 203 204 |
# File 'lib/isort.rb', line 177 def self.process_file(file, ) unless File.exist?(file) puts "Error: File not found: #{file}" unless [:quiet] return 1 end processor = FileProcessor.new(file, ) if [:check] return handle_check(file, processor, ) elsif [:diff] return handle_diff(file, processor, ) else return handle_sort(file, processor, ) end rescue ExistingSyntaxErrors => e puts "ERROR: #{e.}" unless [:quiet] return 1 rescue IntroducedSyntaxErrors => e puts "ERROR: #{e.}" unless [:quiet] return 1 rescue FileSkipped => e puts e. if [:verbose] return 0 rescue StandardError => e puts "Error processing #{file}: #{e.}" unless [:quiet] return 1 end |
.start ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/isort.rb', line 84 def self.start = { check: false, diff: false, atomic: false, quiet: false, verbose: false } parser = OptionParser.new do |opts| opts. = "Usage: isort [options] [file_or_directory]" opts.separator "" opts.separator "Options:" opts.on("-f", "--file=FILE", "File to sort") do |file| [:file] = file end opts.on("-d", "--directory=DIRECTORY", "Directory to sort (recursive)") do |dir| [:directory] = dir end opts.separator "" opts.separator "Safety options:" opts.on("-c", "--check", "--check-only", "Check if files need sorting without modifying them.", "Returns exit code 0 if sorted, 1 if changes needed.") do [:check] = true end opts.on("--diff", "Show diff of changes without modifying files.") do [:diff] = true end opts.on("--atomic", "Validate Ruby syntax before and after sorting.", "Won't save if it would introduce syntax errors.") do [:atomic] = true end opts.separator "" opts.separator "Output options:" opts.on("-q", "--quiet", "Suppress all output except errors.") do [:quiet] = true end opts.on("--verbose", "Show detailed output.") do [:verbose] = true end opts.separator "" opts.separator "Information:" opts.on("-h", "--help", "Show this help message.") do puts opts exit end opts.on("-v", "--version", "Show version.") do puts "isort #{Isort::VERSION}" exit end end # Parse arguments parser.parse! # Handle positional arguments if ARGV.any? && ![:file] && ![:directory] target = ARGV.first if File.directory?(target) [:directory] = target elsif File.file?(target) [:file] = target else puts "Error: #{target} is not a valid file or directory" exit 1 end end if [:file] exit_code = process_file([:file], ) exit exit_code elsif [:directory] exit_code = process_directory([:directory], ) exit exit_code else puts parser exit 1 end end |