Class: Danger::DangerSwiftlint
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::DangerSwiftlint
- Defined in:
- lib/danger_plugin.rb
Overview
Lint Swift files inside your projects. This is done using the [SwiftLint](github.com/realm/SwiftLint) tool. Results are passed out as a table in markdown.
Instance Attribute Summary collapse
-
#config_file ⇒ Object
Allows you to specify a config file location for swiftlint.
Instance Method Summary collapse
-
#lint_files(files = nil) ⇒ void
Lints Swift files.
-
#parse_results(results, heading) ⇒ String
Parses swiftlint invocation results into a string which is formatted as a markdown table.
-
#swiftlint_installed? ⇒ Bool
Determine if swiftlint is currently installed in the system paths.
Instance Attribute Details
#config_file ⇒ Object
Allows you to specify a config file location for swiftlint.
19 20 21 |
# File 'lib/danger_plugin.rb', line 19 def config_file @config_file end |
Instance Method Details
#lint_files(files = nil) ⇒ void
This method returns an undefined value.
Lints Swift files. Will fail if ‘swiftlint` cannot be installed correctly. Generates a `markdown` list of warnings for the prose in a corpus of .markdown and .md files.
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 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/danger_plugin.rb', line 29 def lint_files(files=nil) # Installs SwiftLint if needed system "brew install swiftlint" unless swiftlint_installed? # Check that this is in the user's PATH after installing unless swiftlint_installed? fail "swiftlint is not in the user's PATH, or it failed to install" return end # Either use files provided, or use the modified + added swift_files = files ? Dir.glob(files) : (modified_files + added_files) swift_files.select! do |line| line.end_with?(".swift") end swiftlint_command = "swiftlint lint --quiet --reporter json" swiftlint_command += " --config #{config_file}" if config_file require 'json' result_json = swift_files.uniq.collect { |f| JSON.parse(`#{swiftlint_command} --path #{f}`.strip).flatten }.flatten # Convert to swiftlint results warnings = result_json.flatten.select do |results| results['severity'] == 'Warning' end errors = result_json.select do |results| results['severity'] == 'Error' end = '' # We got some error reports back from swiftlint if warnings.count > 0 || errors.count > 0 = '### SwiftLint found issues\n\n' end << parse_results(warnings, 'Warnings') unless warnings.empty? << parse_results(errors, 'Errors') unless errors.empty? markdown end |
#parse_results(results, heading) ⇒ String
Parses swiftlint invocation results into a string which is formatted as a markdown table.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/danger_plugin.rb', line 75 def parse_results (results, heading) = "#### #{heading}\n\n" << 'File | Line | Reason |\n' << '| --- | ----- | ----- |\n' results.each do |r| filename = r['file'].split('/').last line = r['line'] reason = r['reason'] << "#{filename} | #{line} | #{reason} \n" end end |
#swiftlint_installed? ⇒ Bool
Determine if swiftlint is currently installed in the system paths.
95 96 97 |
# File 'lib/danger_plugin.rb', line 95 def swiftlint_installed? `which swiftlint`.strip.empty? == false end |