11module 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.
68 #
79 # @example Ensure that changes do not violate code style in Objective-C files
810 #
@@ -12,6 +14,11 @@ module Danger
1214 #
1315 # code_style_validation.check file_extensions: ['.hpp', '.cpp']
1416 #
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+ #
1522 # @example Ensure that changes do not violate code style, ignoring Pods directory
1623 #
1724 # code_style_validation.check ignore_file_patterns: [/^Pods\//]
@@ -22,12 +29,12 @@ module Danger
2229 class DangerCodeStyleValidation < Plugin
2330 VIOLATION_ERROR_MESSAGE = 'Code style violations detected.' . freeze
2431
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 .
2633 # Generates Markdown message with respective patches.
2734 #
2835 # @return [void]
2936 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 : [ ] }
3138 config = defaults . merge ( config )
3239 validator = *config [ :validator ]
3340 file_extensions = [ *config [ :file_extensions ] ]
@@ -55,7 +62,7 @@ def check(config = {})
5562 message += '* `' + file_name + "`\n \n "
5663 end
5764 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 "
5966 message += '2. Apply the suggested patches with `git apply patch`.' + "\n \n "
6067 message += patches . join ( "\n " )
6168 end
@@ -151,18 +158,24 @@ def resolve_changes(validator, changes)
151158
152159 offending_files = [ ]
153160 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
155168 changes . each do |file_name , changed_lines |
156169 changed_lines_command_array = [ ]
157170
158171 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 ] )
160173 end
161174
162175 changed_lines_command = changed_lines_command_array . join ( ' ' )
163176 format_command_array = [ validator , changed_lines_command , file_name ]
164177
165- # clang-format command for formatting JUST changed lines
178+ # validator command for formatting JUST changed lines
166179 formatted = `#{ format_command_array . join ( ' ' ) } `
167180
168181 formatted_temp_file = Tempfile . new ( 'temp-formatted' )
0 commit comments