11module Danger
2- # This plugin looks for code style violations for
3- # added lines on the current MR / PR,
4- # and offers inline patches.
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.
56 #
6- # It uses 'clang-format' and only checks ".h", ".m" and ".mm" files
7- #
8- # @example Ensure that added lines does not violate code style
7+ # @example Ensure that changes do not violate code style in Objective-C files
98 #
109 # code_style_validation.check
1110 #
12- # @example Ensure that changes don't violate code style, ignoring Pods directory
11+ # @example Ensure that changes do not violate code style in files with given extensions
12+ #
13+ # code_style_validation.check file_extensions: ['.hpp', '.cpp']
14+ #
15+ # @example Ensure that changes do not violate code style, ignoring Pods directory
1316 #
1417 # code_style_validation.check ignore_file_patterns: [/^Pods\//]
1518 #
@@ -18,12 +21,15 @@ module Danger
1821 #
1922 class DangerCodeStyleValidation < Plugin
2023 VIOLATION_ERROR_MESSAGE = 'Code style violations detected.' . freeze
21-
24+
2225 # Validates the code style of changed & added files using clang-format.
2326 # Generates Markdown message with respective patches.
2427 #
2528 # @return [void]
2629 def check ( config = { } )
30+ defaults = { file_extensions : [ '.h' , '.m' , '.mm' ] , ignore_file_patterns : [ ] }
31+ config = defaults . merge ( config )
32+ file_extensions = [ *config [ :file_extensions ] ]
2733 ignore_file_patterns = [ *config [ :ignore_file_patterns ] ]
2834
2935 diff = ''
@@ -38,19 +44,31 @@ def check(config = {})
3844 raise 'Unknown SCM Provider'
3945 end
4046
41- changes = get_changes ( diff , ignore_file_patterns )
42- message = resolve_changes ( changes )
47+ changes = get_changes ( diff , file_extensions , ignore_file_patterns )
48+ offending_files , patches = resolve_changes ( changes )
49+
50+ message = ''
51+ unless offending_files . empty?
52+ message = 'Code style violations detected in the following files:' + "\n "
53+ offending_files . each do |file_name |
54+ message += '* `' + file_name + "`\n \n "
55+ end
56+ message += 'Execute one of the following actions and commit again:' + "\n "
57+ message += '1. Run `clang-format` on the offending files' + "\n "
58+ message += '2. Apply the suggested patches with `git apply patch`.' + "\n \n "
59+ message += patches . join ( "\n " )
60+ end
4361
4462 return if message . empty?
4563 fail VIOLATION_ERROR_MESSAGE
46- markdown '### Code Style Check (`.h`, `.m` and `.mm`) '
64+ markdown '### Code Style Check'
4765 markdown '---'
4866 markdown message
4967 end
5068
5169 private
5270
53- def get_changes ( diff_str , ignore_file_patterns )
71+ def get_changes ( diff_str , file_extensions , ignore_file_patterns )
5472 changes = { }
5573 line_cursor = 0
5674
@@ -69,7 +87,7 @@ def get_changes(diff_str, ignore_file_patterns)
6987
7088 file_name = filename_line . split ( '+++ b/' ) . last . chomp
7189
72- unless file_name . end_with? ( '.m' , '.h' , '.mm' )
90+ unless file_name . end_with? ( * file_extensions )
7391 next
7492 end
7593
@@ -121,17 +139,17 @@ def parse_diff(diff)
121139 patches
122140 end
123141
124- def generate_markdown ( title , content )
125- markup_message = '#### ' + title + "\n "
126- markup_message += "```diff \n " + content + "\n ``` \n "
127- markup_message
142+ def generate_patch ( title , content )
143+ markup_patch = '#### ' + title + "\n "
144+ markup_patch += "```diff \n " + content + "\n ``` \n "
145+ markup_patch
128146 end
129147
130148 def resolve_changes ( changes )
131149 # Parse all patches from diff string
132150
133- markup_message = ''
134-
151+ offending_files = [ ]
152+ patches = [ ]
135153 # patches.each do |patch|
136154 changes . each do |file_name , changed_lines |
137155 changed_lines_command_array = [ ]
@@ -157,14 +175,16 @@ def resolve_changes(changes)
157175 formatted_temp_file . close
158176 formatted_temp_file . unlink
159177
160- # generate Markup message of patch suggestions
161- # to prevent code-style violations
178+ # generate arrays with:
179+ # 1. Name of offending files
180+ # 2. Suggested patches, in Markdown format
162181 unless diff . empty?
163- markup_message += generate_markdown ( file_name , diff )
182+ offending_files . push ( file_name )
183+ patches . push ( generate_patch ( file_name , diff ) )
164184 end
165185 end
166186
167- markup_message
187+ return offending_files , patches
168188 end
169189 end
170190end
0 commit comments