Skip to content

Commit

Permalink
Merge pull request #2 from noboru-i/add-text-interface
Browse files Browse the repository at this point in the history
add text interface
  • Loading branch information
noboru-i authored Sep 7, 2017
2 parents e9d9576 + 4a500c2 commit 1035cce
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
danger-checkstyle_format (0.0.4)
danger-checkstyle_format (0.1.0)
danger-plugin-api (~> 1.0)
ox (~> 2.0)

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ checkstyle_format.base_path = Dir.pwd
checkstyle_format.report 'app/build/reports/checkstyle/checkstyle.xml'</pre>
</blockquote>

<blockquote>Parse the XML text, and let the plugin do your reporting
<pre>
checkstyle_format.base_path = Dir.pwd
checkstyle_format.report_by_text '<?xml ...'</pre>
</blockquote>

## Development

1. Clone this repo
Expand Down
2 changes: 1 addition & 1 deletion lib/checkstyle_format/gem_version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CheckstyleFormat
VERSION = "0.0.4".freeze
VERSION = "0.1.0".freeze
end
37 changes: 28 additions & 9 deletions lib/checkstyle_format/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module Danger
# checkstyle_format.base_path = Dir.pwd
# checkstyle_format.report 'app/build/reports/checkstyle/checkstyle.xml'
#
# @example Parse the XML text, and let the plugin do your reporting
#
# checkstyle_format.base_path = Dir.pwd
# checkstyle_format.report_by_text '<?xml ...'
#
# @see noboru-i/danger-checkstyle_format
# @tags lint, reporting
#
Expand All @@ -18,26 +23,32 @@ class DangerCheckstyleFormat < Plugin
attr_accessor :base_path

# Report checkstyle warnings
# @return [void]
#
# @return [void]
def report(file, inline_mode = true)
raise "Please specify file name." if file.empty?
raise "No checkstyle file was found at #{file}" unless File.exist? file
errors = parse(file)
errors = parse(File.read(file))

if inline_mode
send_inline_comment(errors)
else
raise "not implemented." # TODO: not implemented.
end
send_comment(errors, inline_mode)
end

# Report checkstyle warnings by XML text
#
# @return [void]
def report_by_text(text, inline_mode = true)
raise "Please specify xml text." if text.empty?
errors = parse(text)

send_comment(errors, inline_mode)
end

private

def parse(file)
def parse(text)
require "ox"

doc = Ox.parse(File.read(file))
doc = Ox.parse(text)
present_elements = doc.nodes.first.nodes.reject do |test|
test.nodes.empty?
end
Expand All @@ -52,6 +63,14 @@ def parse(file)
elements
end

def send_comment(errors, inline_mode)
if inline_mode
send_inline_comment(errors)
else
raise "not implemented." # TODO: not implemented.
end
end

def send_inline_comment(errors)
errors.each do |error|
warn(error.message, file: error.file_name, line: error.line)
Expand Down
2 changes: 1 addition & 1 deletion spec/checkstyle_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Danger
describe ".parse" do
subject(:errors) do
@checkstyle_format.base_path = "/path/to"
@checkstyle_format.send(:parse, "spec/fixtures/checkstyle.xml")
@checkstyle_format.send(:parse, File.read("spec/fixtures/checkstyle.xml"))
end
it "have 4 items" do
expect(errors.size).to be 4
Expand Down

0 comments on commit 1035cce

Please sign in to comment.