Class: Danger::DangerPeriphery

Inherits:
Plugin
  • Object
show all
Defined in:
lib/danger_plugin.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.



53
54
55
56
57
# File 'lib/danger_plugin.rb', line 53

def initialize(dangerfile)
  super(dangerfile)
  @postprocessor = ->(_path, _line, _column, _message) { true }
  @format = :checkstyle
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_plugin.rb', line 24

def binary_path
  @binary_path
end

#format=(value) ⇒ Symbol (writeonly)

For internal use only.

Returns:

  • (Symbol)


46
47
48
# File 'lib/danger_plugin.rb', line 46

def format=(value)
  @format = value
end

#postprocessorProc

Deprecated.

Use #scan with block instead.

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.

Returns:

  • (Proc)


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

def postprocessor
  @postprocessor
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.



135
136
137
138
139
# File 'lib/danger_plugin.rb', line 135

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

#process_warnings(&block) ⇒ Proc

Deprecated.

Use #scan with block instead.

Convenience method to set #postprocessor with block.

Returns:

  • (Proc)


100
101
102
103
# File 'lib/danger_plugin.rb', line 100

def process_warnings(&block)
  deprecate_in_favor_of_scan
  @postprocessor = block
end

#scan(options = {}, &block) ⇒ 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.

  • block (Proc)

    Block to process each warning just before showing it. The Proc receives 1 Periphery::ScanResult instance as argument. If the Proc returns falsy value, the warning corresponding to the given ScanResult will be suppressed, otherwise not.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/danger_plugin.rb', line 81

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

    result = postprocess(entry, &block)
    next unless result

    path, line, _column, message = result
    warn(message, file: path, line: line)
  end
end