lib/pmd/plugin.rb in danger-pmd-0.1.1 vs lib/pmd/plugin.rb in danger-pmd-0.2.0

- old
+ new

@@ -13,10 +13,15 @@ # # pmd.gradle_task = 'app:pmd' #defalut: pmd # pmd.report_file = "module/build/reports/pmd/pmd.xml" #defalut: app/build/reports/pmd/pmd.xml # pmd.report # + # @example Running PMD with a specific root path + # + # pmd.root_path = '/Users/developer/project + # pmd.report + # # @example 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 # @@ -53,10 +58,22 @@ # @return [Boolean] def skip_gradle_task @skip_gradle_task ||= false end + # An absolute path to a root. + # To comment errors to VCS, this needs to know relative path of files from the root. + # Defaults to result of "git rev-parse --show-toplevel". + # @return [String] the root path of git repository by default. + attr_writer :root_path + + # A getter for `root_path`, returning result of "git rev-parse --show-toplevel" if value is nil. + # @return [String] + def root_path + @root_path ||= `git rev-parse --show-toplevel`.chomp + end + # Location of report file. # If your pmd task outputs to a different location, you can specify it here. # Defaults to "app/build/reports/pmd/pmd.xml". # @return [String] attr_writer :report_file @@ -82,21 +99,21 @@ # 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. # @return [Array[PmdFile]] - def report(inline_mode = true) + def report(inline_comment = true) unless skip_gradle_task return fail("Could not find `gradlew` inside current directory") unless gradlew_exists? exec_gradle_task end report_files_expanded = Dir.glob(report_files).sort return fail("Could not find matching PMD report files for #{report_files} inside current directory") if report_files_expanded.empty? - report_and_send_inline_comment(report_files_expanded, inline_mode) + do_comment(report_files_expanded, inline_comment) end private # Check gradlew file exists in current directory. @@ -126,11 +143,11 @@ # A getter for PMD issues, returning PMD issues. # @return [Array[PmdFile]] def pmd_issues(report_file) pmd_report(report_file).xpath("//file").map do |pmd_file| - PmdFile.new(pmd_file) + PmdFile.new(root_path, pmd_file) end end # A getter for current updated files. # @return [Array[String]] @@ -138,22 +155,24 @@ @target_files ||= (git.modified_files - git.deleted_files) + git.added_files end # Generate report and send inline comment with Danger's warn or fail method. # @return [Array[PmdFile]] - def report_and_send_inline_comment(report_files, inline_mode = true) + def do_comment(report_files, inline_comment = true) pmd_issues = [] report_files.each do |report_file| pmd_issues(report_file).each do |pmd_file| - next unless target_files.include? pmd_file.absolute_path + next unless target_files.include? pmd_file.relative_path pmd_issues.push(pmd_file) - next if inline_mode - pmd_file.violations.each do |pmd_violation| - send(pmd_violation.type, pmd_violation.description, file: pmd_file.absolute_path, line: pmd_violation.line) + if inline_comment + send(pmd_violation.type, pmd_violation.description, file: pmd_file.relative_path, line: pmd_violation.line) + else + send(pmd_violation.type, "#{pmd_file.relative_path} : #{pmd_violation.description} at #{pmd_violation.line}") + end end end end pmd_issues