Class: Danger::DangerSwiftformat
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::DangerSwiftformat
- Defined in:
- lib/swiftformat/plugin.rb
Overview
A danger plugin to check Swift formatting using SwiftFormat.
Instance Attribute Summary collapse
-
#additional_args ⇒ String
Additional swiftformat command line arguments.
-
#additional_message ⇒ String
Additional message to be appended the report.
-
#binary_path ⇒ String
The path to SwiftFormat’s executable.
-
#exclude ⇒ Array<String>
An array of file and directory paths to exclude.
-
#inline_mode ⇒ Boolean
Show issues inline in the diff instead of in a comment.
-
#swiftversion ⇒ String
The project Swift version.
Instance Method Summary collapse
-
#check_format(fail_on_error: false) ⇒ void
Runs swiftformat.
-
#find_swift_files ⇒ Array<String]
Find the files on which SwiftFormat should be run.
-
#send_inline_comment(results, method) ⇒ void
Send inline comment with danger’s warn or fail method.
-
#swiftformat ⇒ SwiftFormat
Constructs the SwiftFormat class.
Instance Attribute Details
#additional_args ⇒ String
Additional swiftformat command line arguments
20 21 22 |
# File 'lib/swiftformat/plugin.rb', line 20 def additional_args @additional_args end |
#additional_message ⇒ String
Additional message to be appended the report
25 26 27 |
# File 'lib/swiftformat/plugin.rb', line 25 def @additional_message end |
#binary_path ⇒ String
The path to SwiftFormat’s executable
15 16 17 |
# File 'lib/swiftformat/plugin.rb', line 15 def binary_path @binary_path end |
#exclude ⇒ Array<String>
An array of file and directory paths to exclude
30 31 32 |
# File 'lib/swiftformat/plugin.rb', line 30 def exclude @exclude end |
#inline_mode ⇒ Boolean
Show issues inline in the diff instead of in a comment.
40 41 42 |
# File 'lib/swiftformat/plugin.rb', line 40 def inline_mode @inline_mode end |
#swiftversion ⇒ String
The project Swift version
35 36 37 |
# File 'lib/swiftformat/plugin.rb', line 35 def swiftversion @swiftversion end |
Instance Method Details
#check_format(fail_on_error: false) ⇒ void
This method returns an undefined value.
Runs swiftformat
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/swiftformat/plugin.rb', line 48 def check_format(fail_on_error: false) # Check if SwiftFormat is installed raise "Could not find SwiftFormat executable" unless swiftformat.installed? # Find Swift files swift_files = find_swift_files # Stop processing if there are no swift files return if swift_files.empty? # Run swiftformat results = swiftformat.check_format(swift_files, additional_args, swiftversion) # Stop processing if the errors array is empty return if results[:errors].empty? if inline_mode send_inline_comment(results, fail_on_error ? :fail : :warn) else # Process the errors = "### SwiftFormat found issues:\n\n" << "| File | Rules |\n" << "| ---- | ----- |\n" results[:errors].each do |error| << "| #{error[:file].gsub("#{Dir.pwd}/", '')} | #{error[:rules].join(', ')} |\n" end unless .nil? << "\n" << end markdown end if fail_on_error fail "SwiftFormat found issues" end end |
#find_swift_files ⇒ Array<String]
Find the files on which SwiftFormat should be run
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/swiftformat/plugin.rb', line 90 def find_swift_files renamed_files_hash = git.renamed_files.map { |rename| [rename[:before], rename[:after]] }.to_h post_rename_modified_files = git.modified_files .map { |modified_file| renamed_files_hash[modified_file] || modified_file } files = (post_rename_modified_files - git.deleted_files) + git.added_files @exclude = %w() if @exclude.nil? files .select { |file| file.end_with?(".swift") } .reject { |file| @exclude.any? { |glob| File.fnmatch(glob, file) } } .uniq .sort end |
#send_inline_comment(results, method) ⇒ void
This method returns an undefined value.
Send inline comment with danger’s warn or fail method
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/swiftformat/plugin.rb', line 110 def send_inline_comment(results, method) results[:errors].each do |error| file = error[:file] file_components = file.split(":") line = file_components[1] filename = file_components.first.split("/").last file_path = file_components.first = error[:rules].join(", ").to_s.dup << " `#{filename}:#{line}`" # file:line for pasting into Xcode Quick Open send(method, , file: file_path, line: line) end end |
#swiftformat ⇒ SwiftFormat
Constructs the SwiftFormat class
128 129 130 |
# File 'lib/swiftformat/plugin.rb', line 128 def swiftformat SwiftFormat.new(binary_path) end |