Class: PMDFormatter

Inherits:
XCPretty::Simple
  • Object
show all
Defined in:
lib/pmd_formatter.rb

Constant Summary collapse

FILE_PATH =
'build/reports/errors.pmd'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(use_unicode, colorize) ⇒ PMDFormatter

Returns a new instance of PMDFormatter.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pmd_formatter.rb', line 7

def initialize(use_unicode, colorize)
  super
  @warnings = []
  @ld_warnings = []
  @compile_warnings = []
  @errors = []
  @compile_errors = []
  @file_missing_errors = []
  @undefined_symbols_errors = []
  @duplicate_symbols_errors = []
  @failures = {}
end

Instance Method Details

#finishObject



94
95
96
97
# File 'lib/pmd_formatter.rb', line 94

def finish
  write_to_file
  super
end

#format_compile_error(file, file_path, reason, line, cursor) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pmd_formatter.rb', line 50

def format_compile_error(file, file_path, reason, line, cursor)
  @compile_errors << {
    file_name: file,
    file_path: file_path,
    reason: reason,
    line: line,
    cursor: cursor
  }
  write_to_file_if_needed
  super
end

#format_compile_warning(file_name, file_path, reason, line, cursor) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pmd_formatter.rb', line 32

def format_compile_warning(file_name, file_path, reason, line, cursor)
  @compile_warnings << {
    file_name: file_name,
    file_path: file_path,
    reason: reason,
    line: line,
    cursor: cursor
  }
  write_to_file_if_needed
  super
end

#format_duplicate_symbols(message, file_paths) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/pmd_formatter.rb', line 81

def format_duplicate_symbols(message, file_paths)
  @duplicate_symbols_errors = {
    message: message,
    file_paths: file_paths
  }
  write_to_file_if_needed
  super
end

#format_error(message) ⇒ Object



44
45
46
47
48
# File 'lib/pmd_formatter.rb', line 44

def format_error(message)
  @errors << message
  write_to_file_if_needed
  super
end

#format_file_missing_error(reason, file_path) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/pmd_formatter.rb', line 62

def format_file_missing_error(reason, file_path)
  @file_missing_errors << {
    file_path: file_path,
    reason: reason
  }
  write_to_file_if_needed
  super
end

#format_ld_warning(message) ⇒ Object



20
21
22
23
24
# File 'lib/pmd_formatter.rb', line 20

def format_ld_warning(message)
  @ld_warnings << message
  write_to_file_if_needed
  super
end

#format_test_summary(message, failures_per_suite) ⇒ Object



90
91
92
# File 'lib/pmd_formatter.rb', line 90

def format_test_summary(message, failures_per_suite)
  super
end

#format_undefined_symbols(message, symbol, reference) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/pmd_formatter.rb', line 71

def format_undefined_symbols(message, symbol, reference)
  @undefined_symbols_errors = {
    message: message,
    symbol: symbol,
    reference: reference
  }
  write_to_file_if_needed
  super
end

#format_warning(message) ⇒ Object



26
27
28
29
30
# File 'lib/pmd_formatter.rb', line 26

def format_warning(message)
  @warnings << message
  write_to_file_if_needed
  super
end

#pmd_outputObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pmd_formatter.rb', line 99

def pmd_output
  {
    warnings: @warnings,
    ld_warnings: @ld_warnings,
    compile_warnings: @compile_warnings,
    errors: @errors,
    compile_errors: @compile_errors,
    file_missing_errors: @file_missing_errors,
    undefined_symbols_errors: @undefined_symbols_errors,
    duplicate_symbols_errors: @duplicate_symbols_errors
  }
end

#write_to_fileObject



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
# File 'lib/pmd_formatter.rb', line 116

def write_to_file
  file_name = ENV['XCPRETTY_PMD_FILE_OUTPUT'] || FILE_PATH
  dirname = File.dirname(file_name)
  FileUtils.mkdir_p dirname

  doc = XML::Document.new
  rootnode = XML::Node.new('pmd')
  rootnode['version'] = 'xcpretty-pmd-formatter'
  doc.root = rootnode

  pmd_output.each do |key1, array|
    unless array.count
      next
    end
    file_node = XML::Node.new('file')
    violation = XML::Node.new('violation')
    array.each do |x|
      if x.kind_of?(Hash)
        x.each do |key, value|
          if key.to_s == 'file_path'
            file_node = XML::Node.new('file')
            file_node['name'] = value.split(':')[0]
            violation = XML::Node.new('violation')
            violation['begincolumn'] = value.split(':')[2]
            violation['endcolumn'] = '0'
            violation['beginline'] = value.split(':')[1]
            violation['endline'] = '0'
          end
          if key1.to_s == 'compile_errors'
            violation['priority'] = '1'
          else
            violation['priority'] = '2'
          end
          violation['rule'] = 'clang static analyzer'
          violation['ruleset'] = 'clang static analyzer'
          if key.to_s == 'reason'
            violation.content = value
          end
          has_everything = !violation.parent && violation['begincolumn']
          has_everything &&= violation['endcolumn']
          has_everything &&= violation['beginline'] && violation['endline'] && violation.content
          if has_everything
            file_node << violation
          end
          if file_node['name']
            doc.root << file_node
          end
        end
      elsif x.kind_of?(String)
        file_node = XML::Node.new('file')
        file_node['name'] = ''
        violation = XML::Node.new('violation')
        violation['begincolumn'] = '0'
        violation['endcolumn'] = '0'
        violation['beginline'] = '0'
        violation['endline'] = '0'
        violation['priority'] = '2'
        violation['rule'] = 'clang static analyzer'
        violation['ruleset'] = 'clang static analyzer'
        violation.content = x
        file_node << violation
        doc.root << file_node
      end
    end
  end
  doc_as_str = doc.to_s
  # puts doc_as_str
  File.write(file_name, doc_as_str)
end

#write_to_file_if_neededObject



112
113
114
# File 'lib/pmd_formatter.rb', line 112

def write_to_file_if_needed
  write_to_file unless XCPretty::Formatter.method_defined? :finish
end