-
Notifications
You must be signed in to change notification settings - Fork 8
Add more file extensions #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
30f4c41
5ca556e
6930556
af2b349
e221856
3ae72a4
b9537b1
1571793
eb1ca60
dfaae35
7eb737a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,18 @@ | ||
| module Danger | ||
| # This plugin looks for code style violations for | ||
| # added lines on the current MR / PR, | ||
| # and offers inline patches. | ||
| # This plugin uses 'clang-format' to look for code style violations in added | ||
| # lines on the current MR / PR, and offers inline patches. | ||
| # By default only Objective-C files, with extensions ".h", ".m", ".mm" and | ||
| # ".C", are checked. | ||
| # | ||
| # @example Ensure that changes do not violate code style in Objective-C files | ||
| # | ||
| # It uses 'clang-format' and only checks ".h", ".m" and ".mm" files | ||
| # code_style_validation.check | ||
| # | ||
| # @example Ensure that added lines does not violate code style | ||
| # @example Ensure that changes do not violate code style in files with given extensions | ||
| # | ||
| # code_style_validation.check | ||
| # code_style_validation.check file_extensions: ['.hpp', '.cpp'] | ||
| # | ||
| # @example Ensure that changes don't violate code style, ignoring Pods directory | ||
| # @example Ensure that changes do not violate code style, ignoring Pods directory | ||
| # | ||
| # code_style_validation.check ignore_file_patterns: [/^Pods\//] | ||
| # | ||
|
|
@@ -24,6 +27,9 @@ class DangerCodeStyleValidation < Plugin | |
| # | ||
| # @return [void] | ||
| def check(config = {}) | ||
| defaults = {file_extensions: ['.h', '.m', '.mm', '.C'], ignore_file_patterns: []} | ||
| config = defaults.merge(config) | ||
| file_extensions = [*config[:file_extensions]] | ||
| ignore_file_patterns = [*config[:ignore_file_patterns]] | ||
|
|
||
| diff = '' | ||
|
|
@@ -38,19 +44,31 @@ def check(config = {}) | |
| raise 'Unknown SCM Provider' | ||
| end | ||
|
|
||
| changes = get_changes(diff, ignore_file_patterns) | ||
| message = resolve_changes(changes) | ||
| changes = get_changes(diff, file_extensions, ignore_file_patterns) | ||
| offending_files, patches = resolve_changes(changes) | ||
|
|
||
| message = '' | ||
| unless offending_files.empty? | ||
| message = 'Code style violations detected in the following files:' + "\n" | ||
|
||
| offending_files.each do |file_name| | ||
|
||
| message += '* `' + file_name + "`\n\n" | ||
| end | ||
| message += 'Execute one of the following actions and commit again:' + "\n" | ||
| message += '1. Run `clang-format` on the offending files' + "\n" | ||
| message += '2. Apply the suggested patches with `git apply patch`.' + "\n\n" | ||
| message += patches.join(' ') | ||
| end | ||
|
|
||
| return if message.empty? | ||
| fail VIOLATION_ERROR_MESSAGE | ||
| markdown '### Code Style Check (`.h`, `.m` and `.mm`)' | ||
| markdown '### Code Style Check' | ||
| markdown '---' | ||
| markdown message | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def get_changes(diff_str, ignore_file_patterns) | ||
| def get_changes(diff_str, file_extensions, ignore_file_patterns) | ||
| changes = {} | ||
| line_cursor = 0 | ||
|
|
||
|
|
@@ -69,7 +87,7 @@ def get_changes(diff_str, ignore_file_patterns) | |
|
|
||
| file_name = filename_line.split('+++ b/').last.chomp | ||
|
|
||
| unless file_name.end_with?('.m', '.h', '.mm') | ||
| unless file_name.end_with?(*file_extensions) | ||
| next | ||
| end | ||
|
|
||
|
|
@@ -121,17 +139,17 @@ def parse_diff(diff) | |
| patches | ||
| end | ||
|
|
||
| def generate_markdown(title, content) | ||
| markup_message = '#### ' + title + "\n" | ||
| markup_message += "```diff \n" + content + "\n``` \n" | ||
| markup_message | ||
| def generate_patch(title, content) | ||
| markup_patch = '#### ' + title + "\n" | ||
| markup_patch += "```diff \n" + content + "\n``` \n" | ||
| markup_patch | ||
| end | ||
|
|
||
| def resolve_changes(changes) | ||
| # Parse all patches from diff string | ||
|
|
||
| markup_message = '' | ||
|
|
||
| offending_files = [] | ||
| patches = [] | ||
| # patches.each do |patch| | ||
| changes.each do |file_name, changed_lines| | ||
| changed_lines_command_array = [] | ||
|
|
@@ -157,14 +175,16 @@ def resolve_changes(changes) | |
| formatted_temp_file.close | ||
| formatted_temp_file.unlink | ||
|
|
||
| # generate Markup message of patch suggestions | ||
| # to prevent code-style violations | ||
| # generate arrays with: | ||
| # 1. Name of offending files | ||
| # 2. Suggested patches, in Markdown format | ||
| unless diff.empty? | ||
| markup_message += generate_markdown(file_name, diff) | ||
| offending_files.push(file_name) | ||
| patches.push(generate_patch(file_name, diff)) | ||
| end | ||
| end | ||
|
|
||
| markup_message | ||
| return offending_files, patches | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does
.Chave to be capitalized?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Wikipedia Objective-C files usually have those extensions. I only kept Objective-C source file extensions as defaults for the
file_extensionsinput parameter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find, I've never seen that extension in the wild, though. Also, it's effectively equal to
.cextension on case-insensitive filesystems (i.e. on most Macs).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, of course! I'll remove it and leave
.m,.mmand.has defaults.