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



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
# 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
  
  fileNode1 = XML::Node.new('file')
  fileNode1['name'] = 'AppDelegate.m'
  violation1 = XML::Node.new('violation')
  violation1['begincolumn'] = '5'
  violation1['endcolumn'] = '0'
  violation1['beginline'] = '28'
  violation1['endline'] = '0'
  violation1['priority'] = '1'
  violation1['rule'] = 'clang static analyzer'
  violation1['ruleset'] = 'clang static analyzer'
  violation1.content = 'code will never be executed [-Wunreachable-code]'
  fileNode1 << violation1
  doc.root << fileNode1
  
  fileNode2 = XML::Node.new('file')
  fileNode2['name'] = 'BGE/AppDelegate.m'
  violation2 = XML::Node.new('violation')
  violation2['begincolumn'] = '23'
  violation2['endcolumn'] = '0'
  violation2['beginline'] = '20'
  violation2['endline'] = '0'
  violation2['priority'] = '1'
  violation2['rule'] = 'clang static analyzer'
  violation2['ruleset'] = 'clang static analyzer'
  violation2.content = "Potential leak of an object stored into 'key'"
  fileNode2 << violation2
  doc.root << fileNode2
  
  docAsStr = doc.to_s
  # result = docAsStr.gsub(/xml/, 'pmd')
  # doc.save(file_name, :indent => true, :encoding => XML::Encoding::UTF_8)
  
  puts(docAsStr)
  File.write(file_name, docAsStr)
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