class Danger::DangerKtlint
Attributes
filtering[RW]
TODO: Lint all files if `filtering: false`
Public Instance Methods
limit()
click to toggle source
# File lib/ktlint/plugin.rb, line 12 def limit @limit ||= nil end
limit=(limit)
click to toggle source
# File lib/ktlint/plugin.rb, line 16 def limit=(limit) if limit != nil && limit.integer? @limit = limit else raise UnexpectedLimitTypeError end end
lint(inline_mode: false)
click to toggle source
Run ktlint task using command line interface Will fail if `ktlint` is not installed Skip lint task if files changed are empty @return [void] def lint(inline_mode: false)
# File lib/ktlint/plugin.rb, line 29 def lint(inline_mode: false) unless ktlint_exists? fail("Couldn't find ktlint command. Install first.") return end targets = target_files(git.added_files + git.modified_files) return if targets.empty? results = JSON.parse(`ktlint #{targets.join(' ')} --reporter=json --relative`) return if results.empty? if inline_mode send_inline_comments(results) else send_markdown_comment(results) end end
send_inline_comments(results)
click to toggle source
# File lib/ktlint/plugin.rb, line 83 def send_inline_comments(results) catch(:loop_break) do count = 0 results.each do |result| result['errors'].each do |error| file = result['file'] message = error['message'] line = error['line'] fail(message, file: result['file'], line: line) unless limit.nil? count += 1 if count >= limit throw(:loop_break) end end end end end end
send_markdown_comment(results)
click to toggle source
Comment to a PR by ktlint result json
// Sample ktlint result [
{ "file": "app/src/main/java/com/mataku/Model.kt", "errors": [ { "line": 46, "column": 1, "message": "Unexpected blank line(s) before \"}\"", "rule": "no-blank-line-before-rbrace" } ] }
]
# File lib/ktlint/plugin.rb, line 64 def send_markdown_comment(results) catch(:loop_break) do count = 0 results.each do |result| result['errors'].each do |error| file = "#{result['file']}#L#{error['line']}" message = "#{github.html_link(file)}: #{error['message']}" fail(message) unless limit.nil? count += 1 if count >= limit throw(:loop_break) end end end end end end
target_files(changed_files)
click to toggle source
# File lib/ktlint/plugin.rb, line 103 def target_files(changed_files) changed_files.select do |file| file.end_with?('.kt') end end
Private Instance Methods
ktlint_exists?()
click to toggle source
# File lib/ktlint/plugin.rb, line 111 def ktlint_exists? system 'which ktlint > /dev/null 2>&1' end