|
20 | 20 | module RubocopInteractive |
21 | 21 | class Error < StandardError; end |
22 | 22 |
|
23 | | - def self.start_with_stdin!(confirm_patch: false, template: 'default', summary_on_exit: false, record_keypresses: false) |
24 | | - ui = UI.new(confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses) |
25 | | - ui.show_loading(source: :stdin) |
26 | | - |
27 | | - json = $stdin.read |
28 | | - |
29 | | - start!(json, ui: ui, confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses) |
30 | | - end |
31 | | - |
32 | | - def self.start_with_files!(files, confirm_patch: false, template: 'default', summary_on_exit: false, record_keypresses: false) |
33 | | - ui = UI.new(confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses) |
34 | | - ui.show_loading(source: :files, files: files) |
35 | | - |
36 | | - json = run_rubocop_on_files(files) |
37 | | - |
38 | | - start!(json, ui: ui, confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses) |
39 | | - end |
40 | | - |
41 | | - def self.start!(json, ui: nil, confirm_patch: false, template: 'default', summary_on_exit: false, record_keypresses: false) |
| 23 | + def self.start!(input, ui: nil, confirm_patch: false, template: 'default', summary_on_exit: false, record_keypresses: false) |
| 24 | + # Create UI if not provided |
42 | 25 | ui ||= UI.new(confirm_patch: confirm_patch, template: template, summary_on_exit: summary_on_exit, record_keypresses: record_keypresses) |
43 | 26 |
|
44 | | - session = Session.new(json, ui: ui) |
| 27 | + # Convert input to JSON string based on type |
| 28 | + json_string = case input |
| 29 | + when Array |
| 30 | + # Array of file paths |
| 31 | + ui.show_loading(source: :files, files: input) |
| 32 | + run_rubocop_on_files(input) |
| 33 | + when Hash |
| 34 | + # Already parsed JSON - convert back to string |
| 35 | + JSON.generate(input) |
| 36 | + when String |
| 37 | + # Raw JSON string |
| 38 | + input |
| 39 | + else |
| 40 | + # IO-like object (IO, StringIO, File, etc) - must have #read method |
| 41 | + if input.respond_to?(:read) |
| 42 | + ui.show_loading(source: :stdin) |
| 43 | + input.read |
| 44 | + else |
| 45 | + raise ArgumentError, "input must be IO-like (with #read), Array (files), Hash (parsed JSON), or String (JSON)" |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + session = Session.new(json_string, ui: ui) |
45 | 50 | stats = session.run |
46 | 51 |
|
47 | 52 | # Write keypress recording if enabled |
|
0 commit comments