Class: Vinter::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



3
4
5
# File 'lib/vinter/cli.rb', line 3

def initialize
  @linter = Linter.new
end

Instance Method Details

#run(args) ⇒ Object



7
8
9
10
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/vinter/cli.rb', line 7

def run(args)
  if args.empty?
    puts "Usage: vinter [file.vim|directory] [--exclude=dir1,dir2] [--stdio]"
    return 1
  end

  # Parse args: first non-option argument is the target path.

  exclude_value = nil
  target_path = nil
  stdio = false
  format_value = nil

  args.each_with_index do |a, i|
    if a.start_with?("--exclude=")
      exclude_value = a.split("=", 2)[1]
    elsif a == "--exclude"
      exclude_value = args[i + 1]
    elsif a == "--stdio" || a.start_with?("--stdio=")
      stdio = true
    elsif a.start_with?("--format=")
      format_value = a.split("=", 2)[1]
    elsif a == "--format"
      format_value = args[i + 1]
    elsif !a.start_with?('-') && target_path.nil?
      target_path = a
    end
  end

  if target_path.nil? && !stdio
    puts "Usage: vinter [file.vim|directory] [--exclude=dir1,dir2] [--stdio]"
    return 1
  end

  unless stdio
    unless File.exist?(target_path)
      puts "Error: File or directory not found: #{target_path}"
      return 1
    end
  end

  excludes = Array(exclude_value).flat_map { |v| v.to_s.split(',') }.map(&:strip).reject(&:empty?)

  # normalize format value

  format_value = format_value.to_s.downcase if format_value

  # Handle STDIN input mode

  if stdio
    content = STDIN.read

    if content.nil? || content.empty?
      puts "No input received on stdin"
      return 0
    end

    issues = @linter.lint(content)
    total_issues = issues.length

    if format_value == 'json'
      require 'json'

      files = [
        {
          path: 'stdin',
          offenses: issues.map do |issue|
            {
              severity: (issue[:type] == :error ? 'fatal' : (issue[:type] == :warning ? 'warning' : 'convention')),
              message: issue[:message],
              cop_name: issue[:rule],
              corrected: false,
              correctable: false,
              location: {
                start_line: issue[:line] || 1,
                start_column: issue[:column] || 1,
                last_line: issue[:line] || 1,
                last_column: issue[:column] || 1,
                length: 0,
                line: issue[:line] || 1,
                column: issue[:column] || 1
              }
            }
          end
        }
      ]

       = {
        'rubocop_version' => defined?(Vinter::VERSION) ? Vinter::VERSION : nil,
        'ruby_engine' => defined?(RUBY_ENGINE) ? RUBY_ENGINE : RUBY_PLATFORM,
        'ruby_version' => RUBY_VERSION,
        'ruby_patchlevel' => RUBY_PATCHLEVEL.to_s,
        'ruby_platform' => RUBY_PLATFORM
      }

      summary = {
        'offense_count' => total_issues,
        'target_file_count' => files.length,
        'inspected_file_count' => files.length
      }

      output = { metadata: , files: files, summary: summary }
      options = {
        indent: '',
        space: '',
        space_before: '',
        object_nl: '',
        array_nl: ''
      }
      puts JSON.pretty_generate(output, options)

      return total_issues > 0 ? 1 : 0
    end
  end

  vim_files = if File.directory?(target_path)
                find_vim_files(target_path, excludes)
              else
                # Check if single file is inside an excluded directory

                if excluded_file?(target_path, excludes, File.dirname(target_path))
                  []
                else
                  [target_path]
                end
              end

  if vim_files.empty?
    puts "No .vim files found in #{target_path}"
    return 0
  end

  total_issues = 0
  error_count = 0
  json_files = []

  vim_files.each do |file_path|
    content = File.read(file_path)
    issues = @linter.lint(content)
    total_issues += issues.length

    if format_value == 'json'
      json_files << {
        path: file_path,
        offenses: issues.map do |issue|
          {
            severity: (issue[:type] == :error ? 'fatal' : (issue[:type] == :warning ? 'warning' : 'convention')),
            message: issue[:message],
            cop_name: issue[:rule],
            corrected: false,
            correctable: false,
            location: {
              start_line: issue[:line] || 1,
              start_column: issue[:column] || 1,
              last_line: issue[:line] || 1,
              last_column: issue[:column] || 1,
              length: 0,
              line: issue[:line] || 1,
              column: issue[:column] || 1
            }
          }
        end
      }
    else
      if issues.empty?
        puts "No issues found in #{file_path}" if vim_files.length == 1
      else
        puts "Found #{issues.length} issues in #{file_path}:" if vim_files.length > 1

        issues.each do |issue|
          type_str = case issue[:type]
                     when :error then "ERROR"
                     when :warning then "WARNING"
                     when :rule then "RULE(#{issue[:rule]})"
                     else "UNKNOWN"
                     end

          line = issue[:line] || 1
          column = issue[:column] || 1

          puts "#{file_path}:#{line}:#{column}: #{type_str}: #{issue[:message]}"
        end

        error_count += 1 if issues.any? { |i| i[:type] == :error }
      end
    end
  end

  #if vim_files.length > 1

    #puts "\nProcessed #{vim_files.length} files, found #{total_issues} total issues"

  #end


  if format_value == 'json'
    require 'json'

     = {
      'rubocop_version' => defined?(Vinter::VERSION) ? Vinter::VERSION : nil,
      'ruby_engine' => defined?(RUBY_ENGINE) ? RUBY_ENGINE : RUBY_PLATFORM,
      'ruby_version' => RUBY_VERSION,
      'ruby_patchlevel' => RUBY_PATCHLEVEL.to_s,
      'ruby_platform' => RUBY_PLATFORM
    }

    summary = {
      'offense_count' => total_issues,
      'target_file_count' => vim_files.length,
      'inspected_file_count' => vim_files.length
    }

    output = { metadata: , files: json_files, summary: summary }
    options = {
      indent: '',
      space: '',
      space_before: '',
      object_nl: '',
      array_nl: ''
    }
    puts JSON.pretty_generate(output, options)

    return total_issues > 0 ? 1 : 0
  end

  return total_issues > 0 ? 1 : 0
end