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

Returns a new instance of 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.

Returns:

  • (String)


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.

Returns:

  • (Symbol)


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.

Returns:

  • (Boolean)

    true if it reports problems in all files. Otherwise false, it reports about only changed files in this pull request. By default false is set.



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.

Returns:

  • (Boolean)

    true if this plugin reports all violations as errors. Otherwise false, it reports violations as warnings. By default false is set.



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.

Parameters:

  • version (String, Symbol) (defaults to: :latest)

    The version of Periphery you want to install. ‘:latest` is treated as special keyword that specifies the latest version.

  • path (String) (defaults to: 'periphery')

    The path to install Periphery including the filename itself.

  • force (Boolean) (defaults to: false)

    If ‘true`, an existing file will be overwritten. Otherwise an error occurs.



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

Parameters:

  • options (Hash) (defaults to: {})

    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 listed in OPTION_OVERRIDES.

    Run $ periphery help scan for available options.

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