class Danger::DangerPeriphery

Analyze Swift files and detect unused codes in your project. This is done using [Periphery](github.com/peripheryapp/periphery).

@example Specifying options to Periphery.

periphery.scan(
  project: "Foo.xcodeproj"
  schemes: ["foo", "bar"],
  targets: "foo",
  clean_build: true
)

@see manicmaniac/danger-periphery @tags swift

Constants

OPTION_OVERRIDES

Attributes

binary_path[RW]

Path to Periphery executable. By default the value is nil and the executable is searched from $PATH. @return [String]

postprocessor[RW]

Proc object to process each warnings just before showing them. The Proc must receive 4 arguments: path, line, column, message and return one of:

- an array that contains 4 elements [path, line, column, message]
- true
- false
- nil

If Proc returns an array, the warning will be raised based on returned elements. If Proc returns true, the warning will not be modified. If Proc returns false or nil, the warning will be ignored.

By default the Proc returns true. @return [Proc]

Public Class Methods

new(dangerfile) click to toggle source
Calls superclass method
# File lib/danger_plugin.rb, line 47
def initialize(dangerfile)
  super(dangerfile)
  @postprocessor = ->(path, line, column, message) { true }
end

Public Instance Methods

process_warnings(&block) click to toggle source

Convenience method to set ‘postprocessor` with block.

@return [Proc]

@example Ignore all warnings from files matching regular expression

periphery.process_warnings do |path, line, column, message|
  !path.match(/.*\/generated\.swift/)
end
# File lib/danger_plugin.rb, line 82
def process_warnings(&block)
  @postprocessor = block
end
scan(**options) click to toggle source

Scans Swift files. Raises an error when Periphery executable is not found.

@param [Hash] options Options passed to Periphery with the following translation rules.

1. Replace all underscores with hyphens in each key.
2. Prepend double hyphens to each key.
3. If value is an array, transform it to comma-separated string.
4. If value is true, drop value and treat it as option without argument.
5. Override some options like --disable-update-check, --format, --quiet and so.

@return [void]

# File lib/danger_plugin.rb, line 62
def scan(**options)
  output = Periphery::Runner.new(binary_path).scan(options.merge(OPTION_OVERRIDES))
  files = files_in_diff
  Periphery::CheckstyleParser.new.parse(output).
    lazy.
    select { |entry| files.include?(entry.path) }.
    map { |entry| postprocess(entry) }.
    force.
    compact.
    each { |path, line, column, message| warn(message, file: path, line: line) }
end

Private Instance Methods

files_in_diff() click to toggle source
# File lib/danger_plugin.rb, line 88
def files_in_diff
  # Taken from https://github.com/ashfurrow/danger-ruby-swiftlint/blob/5184909aab00f12954088684bbf2ce5627e08ed6/lib/danger_plugin.rb#L214-L216
  renamed_files_hash = git.renamed_files.map { |rename| [rename[:before], rename[:after]] }.to_h
  post_rename_modified_files = git.modified_files.map { |modified_file| renamed_files_hash[modified_file] || modified_file }
  (post_rename_modified_files - git.deleted_files) + git.added_files
end
postprocess(entry) click to toggle source
# File lib/danger_plugin.rb, line 95
def postprocess(entry)
  result = @postprocessor.call(entry.path, entry.line, entry.column, entry.message)
  if !result
    nil
  elsif result.kind_of?(TrueClass)
    [entry.path, entry.line, entry.column, entry.message]
  elsif result.kind_of?(Array) && result.size == 4
    result
  else
    raise "Proc passed to postprocessor must return one of nil, true, false and Array that includes 4 elements."
  end
end