Class: Danger::DangerPeriphery

Inherits:
Plugin
  • Object
show all
Defined in:
lib/danger/danger_periphery.rb

Overview

Analyze Swift files and detect unused codes in your project. This is done using Periphery.

Examples:

Specifying options to Periphery.


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

See Also:

Constant Summary collapse

OPTION_OVERRIDES =
{
  disable_update_check: true,
  quiet: true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dangerfile) ⇒ DangerPeriphery



48
49
50
51
52
# File 'lib/danger/danger_periphery.rb', line 48

def initialize(dangerfile)
  super
  @format = :checkstyle
  @warning_as_error = false
end

Instance Attribute Details

#binary_pathString

Path to Periphery executable. By default the value is nil and the executable is searched from $PATH.



24
25
26
# File 'lib/danger/danger_periphery.rb', line 24

def binary_path
  @binary_path
end

#format=(value) ⇒ Symbol (writeonly)

For internal use only.



41
42
43
# File 'lib/danger/danger_periphery.rb', line 41

def format=(value)
  @format = value
end

#scan_all_filesBoolean

A flag to force Periphery report problems about all files.



30
31
32
# File 'lib/danger/danger_periphery.rb', line 30

def scan_all_files
  @scan_all_files
end

#warning_as_errorBoolean

A flag to treat warnings as errors.



36
37
38
# File 'lib/danger/danger_periphery.rb', line 36

def warning_as_error
  @warning_as_error
end

Instance Method Details

#install(version: :latest, path: 'periphery', force: false) ⇒ void

This method returns an undefined value.

Download and install Periphery executable binary.



96
97
98
99
100
# File 'lib/danger/danger_periphery.rb', line 96

def install(version: :latest, path: 'periphery', force: false)
  installer = Periphery::Installer.new(version)
  installer.install(path, force: force)
  self.binary_path = File.absolute_path(path)
end

#scan(options = {}) {|entry| ... } ⇒ void

This method returns an undefined value.

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

Examples:

Ignore all warnings from files matching regular expression

periphery.scan do |violation|
  !violation.path.match(/.*\/generated\.swift/)
end

Yields:

  • (entry)

    Block to process each warning just before showing it.

Yield Parameters:

Yield Returns:

  • (Boolean, Periphery::ScanResult)

    If the Proc returns falsy value, the warning corresponding to the given ScanResult will be suppressed, otherwise not.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/danger/danger_periphery.rb', line 77

def scan(options = {})
  output = Periphery::Runner.new(binary_path).scan(options.merge(OPTION_OVERRIDES).merge(format: @format))
  files = files_in_diff unless @scan_all_files
  parser.parse(output).each do |entry|
    next unless @scan_all_files || files.include?(entry.path)

    next if block_given? && !yield(entry)

    report(entry.message, file: entry.path, line: entry.line)
  end
end