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.
-
#binary_path ⇒ String
The path to SwiftFormat’s executable.
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.
-
#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 |
#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 |
Instance Method Details
#check_format(fail_on_error: false) ⇒ void
This method returns an undefined value.
Runs swiftformat
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/swiftformat/plugin.rb', line 28 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) # Stop processing if the errors array is empty return if results[:errors].empty? # 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 markdown if fail_on_error fail "SwiftFormat found issues" end end |
#find_swift_files ⇒ Array<String]
Find the files on which SwiftFormat should be run
61 62 63 64 65 66 67 68 |
# File 'lib/swiftformat/plugin.rb', line 61 def find_swift_files files = (git.modified_files - git.deleted_files) + git.added_files files .select { |file| file.end_with?(".swift") } .uniq .sort end |
#swiftformat ⇒ SwiftFormat
Constructs the SwiftFormat class
73 74 75 |
# File 'lib/swiftformat/plugin.rb', line 73 def swiftformat SwiftFormat.new(binary_path) end |