1
1
module Danger
2
- # This plugin uses 'clang-format' to look for code style violations in added
3
- # lines on the current MR / PR, and offers inline patches.
4
- # By default only Objective-C files, with extensions ".h", ".m", and ".mm"
5
- # are checked.
2
+ # This plugin uses code style checker (validator in the following) to look
3
+ # for code style violations in added lines on the current MR / PR, and offers
4
+ # inline patches.
5
+ # The default validator is 'clang-format'. Only Objective-C files, with
6
+ # extensions ".h", ".m", and ".mm" are checked.
7
+ # It is possible to use other validators for other languages, e.g. 'yapf' for Python.
6
8
#
7
9
# @example Ensure that changes do not violate code style in Objective-C files
8
10
#
@@ -12,6 +14,11 @@ module Danger
12
14
#
13
15
# code_style_validation.check file_extensions: ['.hpp', '.cpp']
14
16
#
17
+ # @example Ensure that changes do not violate code style in Python files with YAPF
18
+ #
19
+ # code_style_validation.check validator: 'yapf',
20
+ # file_extensions: ['.py']
21
+ #
15
22
# @example Ensure that changes do not violate code style, ignoring Pods directory
16
23
#
17
24
# code_style_validation.check ignore_file_patterns: [/^Pods\//]
@@ -22,12 +29,12 @@ module Danger
22
29
class DangerCodeStyleValidation < Plugin
23
30
VIOLATION_ERROR_MESSAGE = 'Code style violations detected.' . freeze
24
31
25
- # Validates the code style of changed & added files using clang-format .
32
+ # Validates the code style of changed & added files using a validator program .
26
33
# Generates Markdown message with respective patches.
27
34
#
28
35
# @return [void]
29
36
def check ( config = { } )
30
- defaults = { validator : [ 'clang-format' ] , file_extensions : [ '.h' , '.m' , '.mm' ] , ignore_file_patterns : [ ] }
37
+ defaults = { validator : 'clang-format' , file_extensions : [ '.h' , '.m' , '.mm' ] , ignore_file_patterns : [ ] }
31
38
config = defaults . merge ( config )
32
39
validator = *config [ :validator ]
33
40
file_extensions = [ *config [ :file_extensions ] ]
@@ -55,7 +62,7 @@ def check(config = {})
55
62
message += '* `' + file_name + "`\n \n "
56
63
end
57
64
message += 'Execute one of the following actions and commit again:' + "\n "
58
- message += '1. Run `clang-format ` on the offending files' + "\n "
65
+ message += '1. Run `%s ` on the offending files' % validator + "\n "
59
66
message += '2. Apply the suggested patches with `git apply patch`.' + "\n \n "
60
67
message += patches . join ( "\n " )
61
68
end
@@ -151,18 +158,24 @@ def resolve_changes(validator, changes)
151
158
152
159
offending_files = [ ]
153
160
patches = [ ]
154
- # patches.each do |patch|
161
+ if validator . include? "clang-format"
162
+ # clang-format
163
+ changed_lines_option = "-lines=%s:%s"
164
+ else
165
+ # YAPF
166
+ changed_lines_option = "--lines=%s-%s"
167
+ end
155
168
changes . each do |file_name , changed_lines |
156
169
changed_lines_command_array = [ ]
157
170
158
171
changed_lines . each do |line_number |
159
- changed_lines_command_array . push ( '-lines=' + line_number . to_s + ':' + line_number . to_s )
172
+ changed_lines_command_array . push ( changed_lines_option % [ line_number . to_s , line_number . to_s ] )
160
173
end
161
174
162
175
changed_lines_command = changed_lines_command_array . join ( ' ' )
163
176
format_command_array = [ validator , changed_lines_command , file_name ]
164
177
165
- # clang-format command for formatting JUST changed lines
178
+ # validator command for formatting JUST changed lines
166
179
formatted = `#{ format_command_array . join ( ' ' ) } `
167
180
168
181
formatted_temp_file = Tempfile . new ( 'temp-formatted' )
0 commit comments