Class: Danger::DangerPmd

Inherits:
Plugin
  • Object
show all
Defined in:
lib/pmd/plugin.rb

Overview

Checks on your Gradle project’s Java source files. This is done using [PMD](pmd.github.io) Results are passed out as tables in markdown.

Examples:

Running PMD with its basic configuration


pmd.report

Running PMD with a specific Gradle task or report file (glob accepted)


pmd.gradle_task = 'module:pmd' # default: 'pmd'
pmd.report_file = 'module/build/reports/pmd/pmd.xml' # default: 'app/build/reports/pmd/pmd.xml'
pmd.report

Running PMD with a specific root path


pmd.root_path = '/Users/developer/project' # default: result of `git rev-parse --show-toplevel`
pmd.report

Running PMD with an array of report files (glob accepted)


pmd.report_files = ['modules/**/build/reports/pmd/pmd.xml', 'app/build/reports/pmd/pmd.xml']
pmd.report

Running PMD without running a Gradle task


pmd.skip_gradle_task = true # default: false
pmd.report

Running PMD without inline comment


pmd.report(inline_mode: false) # default: true

See Also:

  • mathroule/danger-pmd

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gradle_taskString

A getter for ‘gradle_task`, returning ’pmd’ if value is nil.

Returns:

  • (String)


52
53
54
# File 'lib/pmd/plugin.rb', line 52

def gradle_task
  @gradle_task ||= 'pmd'
end

#report_fileString

A getter for ‘report_file`, returning ’app/build/reports/pmd/pmd.xml’ if value is nil.

Returns:

  • (String)


94
95
96
# File 'lib/pmd/plugin.rb', line 94

def report_file
  @report_file ||= 'app/build/reports/pmd/pmd.xml'
end

#report_filesArray[String]

A getter for ‘report_files`, returning [’app/build/reports/pmd/pmd.xml’] if value is nil.

Returns:

  • (Array[String])


108
109
110
# File 'lib/pmd/plugin.rb', line 108

def report_files
  @report_files ||= [report_file]
end

#root_pathString

A getter for ‘root_path`, returning result of `git rev-parse –show-toplevel` if value is nil.

Returns:

  • (String)


80
81
82
# File 'lib/pmd/plugin.rb', line 80

def root_path
  @root_path ||= `git rev-parse --show-toplevel`.chomp
end

#skip_gradle_taskBoolean

A getter for ‘skip_gradle_task`, returning false if value is nil.

Returns:

  • (Boolean)


66
67
68
# File 'lib/pmd/plugin.rb', line 66

def skip_gradle_task
  @skip_gradle_task ||= false
end

Instance Method Details

#report(inline_mode: true) ⇒ Array[PmdFile]

Calls PMD task of your Gradle project. It fails if ‘gradlew` cannot be found inside current directory. It fails if `report_file` cannot be found inside current directory. It fails if `report_files` is empty.

Parameters:

  • inline_mode (Boolean) (defaults to: true)

    Report as inline comment, defaults to [true].

Returns:



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pmd/plugin.rb', line 120

def report(inline_mode: true)
  unless skip_gradle_task
    raise('Could not find `gradlew` inside current directory') unless gradlew_exists?

    exec_gradle_task
  end

  report_files_expanded = Dir.glob(report_files).sort
  raise("Could not find matching PMD report files for #{report_files} inside current directory") if report_files_expanded.empty?

  do_comment(report_files_expanded, inline_mode)
end