Class: Canon::Commands::DiffCommand
- Inherits:
-
Object
- Object
- Canon::Commands::DiffCommand
- Defined in:
- lib/canon/commands/diff_command.rb
Overview
Command for semantic diffing of two files
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ DiffCommand
constructor
A new instance of DiffCommand.
-
#run(file1, file2) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
Constructor Details
#initialize(options = {}) ⇒ DiffCommand
Returns a new instance of DiffCommand.
13 14 15 |
# File 'lib/canon/commands/diff_command.rb', line 13 def initialize( = {}) @options = end |
Instance Method Details
#run(file1, file2) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/canon/commands/diff_command.rb', line 19 def run(file1, file2) # Detect formats format1 = @options[:format1] || @options[:format] || detect_format(file1) format2 = @options[:format2] || @options[:format] || detect_format(file2) # Check file sizes before reading check_file_size(file1, format1) check_file_size(file2, format2) # Read raw content for potential by-line diff content1 = File.read(file1, encoding: "utf-8") content2 = File.read(file2, encoding: "utf-8") # Parse documents doc1 = parse_document_content(content1, format1) doc2 = parse_document_content(content2, format2) # Build comparison options comp_opts = # Perform semantic comparison result = Canon::Comparison.equivalent?(doc1, doc2, comp_opts) # Determine diff mode mode = determine_mode(format1) # Prepare formatted content for by-line mode formatted1, formatted2 = prepare_formatted_content( content1, content2, format1, mode ) # Format and output results formatter = Canon::DiffFormatter.new( use_color: resolve_color_option, mode: mode, context_lines: @options.fetch(:context_lines, 3), diff_grouping_lines: @options[:diff_grouping_lines], show_diffs: @options[:show_diffs]&.to_sym || :all, show_raw_inputs: @options[:show_raw_inputs] || false, show_preprocessed_inputs: @options[:show_preprocessed_inputs] || false, show_preprocessed_expected: @options[:show_preprocessed_expected] || false, show_preprocessed_received: @options[:show_preprocessed_received] || false, show_prettyprint_inputs: @options[:show_prettyprint_inputs] || false, show_prettyprint_expected: @options[:show_prettyprint_expected] || false, show_prettyprint_received: @options[:show_prettyprint_received] || false, show_line_numbered_inputs: @options[:show_line_numbered_inputs] || false, ) # Show configuration in verbose mode using shared DebugOutput if @options[:verbose] require_relative "../diff_formatter/debug_output" config_output = Canon::DiffFormatter::DebugOutput.verbose_tables_only( result, { use_color: resolve_color_option, mode: mode, context_lines: @options.fetch(:context_lines, 3), diff_grouping_lines: @options[:diff_grouping_lines], show_diffs: @options[:show_diffs]&.to_sym || :all, verbose_diff: true, # Enable verbose table output }, ) puts config_output unless config_output.empty? end # Always show diff when files are not equivalent # result is always a ComparisonResult object when verbose: true output = formatter.format( result, format1, doc1: formatted1, doc2: formatted2, ) puts output exit result.equivalent? ? 0 : 1 rescue Errno::ENOENT => e abort "Error: #{e.}" rescue JSON::ParserError => e abort "Error parsing JSON: #{e.}" rescue Psych::SyntaxError => e abort "Error parsing YAML: #{e.}" rescue Canon::Error => e abort "Error: #{e.}" rescue StandardError => e abort "Error processing files: #{e.}" end |