Class: Isort::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/isort.rb

Overview

CLI handles command-line interface for the isort gem

Class Method Summary collapse

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, options)
  would_change = processor.check
  if would_change
    puts "#{file} - imports are not sorted" unless options[:quiet]
    1
  else
    puts "#{file} - imports are sorted" if options[: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, options)
  diff_output = processor.diff
  if diff_output && !diff_output.empty?
    puts diff_output
    1
  else
    puts "#{file} - no changes" if options[: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, options)
  changed = processor.process
  if changed
    puts "Imports sorted in #{file}" unless options[:quiet]
  else
    puts "#{file} - no changes needed" if options[: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, options)
  unless Dir.exist?(dir)
    puts "Error: Directory not found: #{dir}" unless options[:quiet]
    return 1
  end

  files = Dir.glob("#{dir}/**/*.rb")
  if files.empty?
    puts "No Ruby files found in #{dir}" unless options[:quiet]
    return 0
  end

  total = 0
  changed = 0
  errors = 0
  error_messages = []

  files.each do |file|
    result = process_file(file, options.merge(quiet: true))
    total += 1
    if result == 0
      changed += 1 if options[:check] || options[:diff]
    else
      errors += 1
    end
  rescue StandardError => e
    errors += 1
    error_messages << "#{file}: #{e.message}"
  end

  unless options[:quiet]
    if options[:check]
      unsorted = total - changed
      puts "Checked #{total} files: #{changed} sorted, #{unsorted} need sorting"
    elsif options[:diff]
      puts "Checked #{total} files: #{total - changed} would change"
    else
      puts "Sorted imports in #{total} files in directory: #{dir}"
    end

    if error_messages.any?
      puts "\nErrors encountered:"
      error_messages.each { |err| puts "  - #{err}" }
    end
  end

  errors > 0 || (options[: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, options)
  unless File.exist?(file)
    puts "Error: File not found: #{file}" unless options[:quiet]
    return 1
  end

  processor = FileProcessor.new(file, options)

  if options[:check]
    return handle_check(file, processor, options)
  elsif options[:diff]
    return handle_diff(file, processor, options)
  else
    return handle_sort(file, processor, options)
  end
rescue ExistingSyntaxErrors => e
  puts "ERROR: #{e.message}" unless options[:quiet]
  return 1
rescue IntroducedSyntaxErrors => e
  puts "ERROR: #{e.message}" unless options[:quiet]
  return 1
rescue FileSkipped => e
  puts e.message if options[:verbose]
  return 0
rescue StandardError => e
  puts "Error processing #{file}: #{e.message}" unless options[:quiet]
  return 1
end

.startObject



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
  options = {
    check: false,
    diff: false,
    atomic: false,
    quiet: false,
    verbose: false
  }

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: isort [options] [file_or_directory]"
    opts.separator ""
    opts.separator "Options:"

    opts.on("-f", "--file=FILE", "File to sort") do |file|
      options[:file] = file
    end

    opts.on("-d", "--directory=DIRECTORY", "Directory to sort (recursive)") do |dir|
      options[: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
      options[:check] = true
    end

    opts.on("--diff", "Show diff of changes without modifying files.") do
      options[:diff] = true
    end

    opts.on("--atomic", "Validate Ruby syntax before and after sorting.",
            "Won't save if it would introduce syntax errors.") do
      options[:atomic] = true
    end

    opts.separator ""
    opts.separator "Output options:"

    opts.on("-q", "--quiet", "Suppress all output except errors.") do
      options[:quiet] = true
    end

    opts.on("--verbose", "Show detailed output.") do
      options[: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? && !options[:file] && !options[:directory]
    target = ARGV.first
    if File.directory?(target)
      options[:directory] = target
    elsif File.file?(target)
      options[:file] = target
    else
      puts "Error: #{target} is not a valid file or directory"
      exit 1
    end
  end

  if options[:file]
    exit_code = process_file(options[:file], options)
    exit exit_code
  elsif options[:directory]
    exit_code = process_directory(options[:directory], options)
    exit exit_code
  else
    puts parser
    exit 1
  end
end