class Danger::DangerIosLogs

This is a danger plugin to detect any `NSLog`/`print` entries left in the code.

@example Ensure, by warning, there are no `NSLog`/`print` entries left in the modified code

ios_logs.check

@example Ensure, by fail, there are no `NSLog`/`print` entries left in the modified code

ios_logs.check :fail

@example Ensure, there are no `print` left in the modified code. Ignore `NSLog`

ios_logs.nslog = false
ios_logs.check

@see Bartosz Janda/danger-ios_logs @tags ios, logs, print, nslog, swift

Constants

NSLOG_MESSAGE
NSLOG_REGEXP

Attributes

nslog[RW]

Notify usage of `NSLog`. `true` by default

@return [Bool]

nslogs[RW]

List of `NSLog` in changeset

@return [Array<FileLog>] List of `NSLog`

print[RW]

Notify usage of `print`. `true` by default

@return [Bool]

prints[RW]

List of `print` in changeset

@return [Array<FileLog>] List of `print`

Public Class Methods

new(dangerfile) click to toggle source

Initialize plugin @param dangerfile [Dangerfile] Dangerfile used to initialize plugin

@return [DangerIosLogs] Initialized DangerIosLogs plugin

Calls superclass method
# File lib/ios_logs/plugin.rb, line 60
def initialize(dangerfile)
  super
  @print = true
  @nslog = true
  @nslogs = []
  @prints = []
end

Public Instance Methods

check(method = :warn) click to toggle source

Checks if in the changed code `NSLog` or `print` are used. @param method = :warn [Symbol] Used method to indicate log method usage.

By default `:warn`. Possible values: `:message`, `:warn`, `:fail`

@return [Void]

# File lib/ios_logs/plugin.rb, line 82
def check(method = :warn)
  raise 'Unsupported method' unless %i[message warn fail].include?(method)

  @nslogs = []
  @prints = []

  check_files files_of_interest

  print_logs method
end
logs() click to toggle source

Combined list of both NSLog and print

@return [Array<FileLog>] List of `NSLog` and `print`

# File lib/ios_logs/plugin.rb, line 72
def logs
  prints + nslogs
end

Private Instance Methods

changed_lines(file) click to toggle source

Returns changed lines in modified file. @param file [String] Path to modified files.

@return [Array<GitDiffParser::Line>] Modified lines

# File lib/ios_logs/plugin.rb, line 128
def changed_lines(file)
  diff = git.diff_for_file(file)
  GitDiffParser::Patch.new(diff.patch).changed_lines
end
check_file(file) click to toggle source

Checks if in the file `NSLog` or `print` are used. @param file [String] Modified file to check.

@return [Void]

# File lib/ios_logs/plugin.rb, line 117
def check_file(file)
  changed_lines(file).each do |line|
    check_line(file, line)
  end
end
check_files(files) click to toggle source

Checks if in the files `NSLog` or `print` are used. @param files [Array<String>] List of interested / modified files to check.

@return [Void]

# File lib/ios_logs/plugin.rb, line 100
def check_files(files)
  files.each { |file| check_file(file) if file.is_a? String }
end
check_line(file, line) click to toggle source

Check line if contains any `NSLog` or `print` @param file [String] Path to file @param line [GitDiffParser::Line] Line number

@return [Void]

# File lib/ios_logs/plugin.rb, line 139
def check_line(file, line)
  prints << Danger::FileLog.new(file, line.number) if @print && !line.content.match(PRINT_REGEXP).nil?
  nslogs << Danger::FileLog.new(file, line.number) if @nslog && !line.content.match(NSLOG_REGEXP).nil?
end
files_of_interest() click to toggle source

List of interested files.

@return [Array<String>] List of interested / modified files to check.

# File lib/ios_logs/plugin.rb, line 108
def files_of_interest
  git.modified_files + git.added_files
end
print_log_for(logs, message, method) click to toggle source

Print logs for given logs set and message @param logs [Array<FileLog>] List of `NSLog` or `print` @param message [String] Message to print @param method [Symbol] Method to indicate usage

@return [Void]

print_logs(method) click to toggle source

Print logs @param method [Symbol] Method to indicate usage

@return [Void]