class Danger::DangerSwiftformat
A danger plugin to check Swift formatting using SwiftFormat
.
@example Check that the added and modified files are properly formatted:
swiftformat.check_format
@see garriguv/danger-swiftformat @tags swiftformat
Attributes
additional_args[RW]
Additional swiftformat command line arguments
@return [String]
additional_message[RW]
Additional message to be appended the report
@return [String]
binary_path[RW]
The path to SwiftFormat's executable
@return [String]
exclude[RW]
An array of file and directory paths to exclude
@return [Array<String>]
swiftversion[RW]
The project Swift version
@return [String]
Public Instance Methods
check_format(fail_on_error: false)
click to toggle source
Runs swiftformat
@param [Boolean] fail_on_error
@return [void]
# File lib/swiftformat/plugin.rb, line 43 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? # Process the errors message = "### SwiftFormat found issues:\n\n" message << "| File | Rules |\n" message << "| ---- | ----- |\n" results[:errors].each do |error| message << "| #{error[:file].gsub("#{Dir.pwd}/", '')} | #{error[:rules].join(', ')} |\n" end unless additional_message.nil? message << "\n" << additional_message end markdown message if fail_on_error fail "SwiftFormat found issues" end end
find_swift_files()
click to toggle source
Find the files on which SwiftFormat
should be run
@return [Array<String]
# File lib/swiftformat/plugin.rb, line 81 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
swiftformat()
click to toggle source
Constructs the SwiftFormat
class
@return [SwiftFormat]
# File lib/swiftformat/plugin.rb, line 101 def swiftformat SwiftFormat.new(binary_path) end