Class: Semdiff::CLI

Inherits:
Object
  • Object
show all
Includes:
IOUtils
Defined in:
lib/semdiff/cli/cli.rb

Constant Summary

Constants included from IOUtils

IOUtils::DIR_PREFIX, IOUtils::DIR_SUFFIX, IOUtils::FILE_PREFIX, IOUtils::FILE_SUFFIX, IOUtils::RUBY_VERSION_F

Instance Method Summary collapse

Methods included from IOUtils

#temp_dir, #temp_file, #with_diff_files, #with_full_prism, #with_full_prism_desugared, #with_prism, #with_prism_and_yard_types, #with_prism_desugared, #with_prism_parse_lex, #with_rubocop_ast, #with_rubocop_ast_and_yard_types, #with_rubocop_ast_bypass, #with_temp_dir, #with_temp_files, #with_types_desugared, #with_yard_types, #with_yardoc, #with_yardoc_and_prism, #yardoc_cmd

Constructor Details

#initializeCLI

Returns a new instance of CLI.



7
8
9
# File 'lib/semdiff/cli/cli.rb', line 7

def initialize
  @options = {}
end

Instance Method Details

#parse(args) ⇒ Object



11
12
13
14
15
16
17
18
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
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
# File 'lib/semdiff/cli/cli.rb', line 11

def parse(args)
  require 'optionparser'

  parser = OptionParser.new do |opts|
    opts.banner = 'Usage: semdiff [options] BEFORE.rb AFTER.rb'
    opts.separator ''
    opts.separator 'Specific options:'

    opts.on('-h', '--help', 'Prints this help') do
      puts opts
      exit 0
    end

    opts.on('-o TARGET', '--output-directory', 'Directory to output normalized unparsed files (default temporary)', String) do |dir|
      @options[:output_directory] = dir
    end

    opts.on('--ignore-comments', "Don't process and preserve comments in the unparsed files") do |p|
      @options[:ignore_comments] = p
    end

    opts.on('--check-only', "Report whether there are any changes, but don't calculate them (much faster).") do |c|
      @options[:check_only] = c
    end

    opts.on('--diff-original', 'Show a diff of the original files above the normalized files') do |d|
      @options[:diff_original] = d
    end

    opts.on('--yard [TARGET]', 'Use YARD (optional existing directory, default reparses input files)') do |target|
      @options[:annotation_type] = :yard
      @options[:annotation_target] = target
      @options[:override] = true if target.nil?
    end

    opts.on('--yard-files FILE1,FILE2,...', 'Reparse specific YARD files (choose --yard or this, not both)', Array) do |files|
      @options[:annotation_type] = :yard
      @options[:annotation_target] = files
    end

    opts.on('--rbs [TARGET]', 'Use RBS signatures (default ./sig/)') do |target|
      @options[:annotation_type] = :rbs
      @options[:annotation_target] = target || 'sig'
    end

    opts.on('--gumtree', 'Use gumtree webdiff after default difftastic') do |g|
      @options[:gumtree] = g
    end

    opts.on('--skip-difftastic', 'Skip difftastic text diff') do |d|
      @options[:skip_difftastic] = d
    end
  end

  begin
    files = parser.parse!(args)
    raise ArgumentError, "Expected 2 files but received #{files.size}" if files.size != 2

    types = case @options[:annotation_type]
            when :yard
              ::Typeguard::TypeModel::Builder.yard
              @options[:annotation_target] ||= files
              ::Typeguard::TypeModel::Builder::IMPLEMENTATION.new(
                @options[:annotation_target],
                @options[:annotation_target].is_a?(Array)
              ).build
            when :rbs
              ::Typeguard::TypeModel::Builder.rbs
              ::Typeguard::TypeModel::Builder::IMPLEMENTATION.new(
                @options[:annotation_target],
                false
              ).build
            end

    processed_asts = files.map do |file|
      contents = File.read(file)
      original_result = Prism.parse(contents)
      ast = original_result.value
      ast = ast.accept(Prism::DesugarCompiler.new)
      ast = ast.accept(AliasingCompiler.new)
      ast = ast.accept(StructuresCompiler.new)
      node_types = TypeVisitor.new(types).visit(ast) unless types.nil?
      ast = ast.accept(ConstantsCompiler.new)
      ast = ast.accept(AlgebraCompiler.new(node_types)) unless types.nil?
      ast = ast.accept(IdentityCompiler.new(node_types)) unless types.nil?
      ast = ast.accept(ConstantsCompiler.new)

      # NOTE: Uses the translation parser builder to
      # translate the existing AST to whitequark/parser,
      # so that we can rewrite with unparser and feed the
      # files to difftastic/GumTree.
      translator = Prism::Translation::Parser.new(
        parser: Struct.new(:prism_ast, :original_result) do
          def parse(source, **options)
            Struct.new(:value, :comments, :magic_comments, :data_loc, :errors, :warnings, :source).new(
              prism_ast,
              original_result.comments,
              original_result.magic_comments,
              original_result.data_loc,
              original_result.errors,
              original_result.warnings,
              original_result.source
            )
          end
        end.new(ast, original_result)
      )
      source_buffer = Parser::Source::Buffer.new(file)
      source_buffer.source = contents
      translator.send(@options[:ignore_comments] ? 'parse' : 'parse_with_comments', source_buffer)
    end

    unparsed = processed_asts.map do |r|
      r.is_a?(Array) ? Unparser.unparse(r.first, comments: r.last) : Unparser.unparse(r)
    end
    file_names = files.map { |f| File.basename(f) }
    if !@options[:skip_difftastic] && @options[:diff_original]
      system "        difft \#{files.first} \#{files.last} \\\n        \#{'--check-only --exit-code' if @options[:check_only]} \\\n        \#{'--ignore-comments' if @options[:ignore_comments]}\n      CMD\n    end\n    return if @options[:skip_difftastic] && !@options[:gumtree]\n\n    with_diff_files(unparsed, file_names, output_directory: @options[:output_directory]) do |b, a|\n      unless @options[:skip_difftastic]\n        system <<~CMD\n          difft \#{b.path} \#{a.path} \\\n          \#{'--check-only --exit-code' if @options[:check_only]} \\\n          \#{'--ignore-comments' if @options[:ignore_comments]}\n        CMD\n      end\n      if @options[:gumtree]\n        system <<~CMD\n          gumtree webdiff \#{b.path} \#{a.path} \\\n          -g ruby-treesitter-ng\n        CMD\n      end\n    end\n  rescue OptionParser::InvalidOption => e\n    puts e.message\n    puts parser\n    puts \"Invalid argument (use semdiff --help): \#{e}\"\n    exit 1\n  end\n\n  @options\nend\n"