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
- PRINT_MESSAGE
- PRINT_REGEXP
Attributes
Notify usage of `NSLog`. `true` by default
@return [Bool]
List of `NSLog` in changeset
@return [Array<FileLog>] List of `NSLog`
Notify usage of `print`. `true` by default
@return [Bool]
List of `print` in changeset
@return [Array<FileLog>] List of `print`
Public Class Methods
Initialize plugin @param dangerfile [Dangerfile] Dangerfile used to initialize plugin
@return [DangerIosLogs] Initialized DangerIosLogs
plugin
# File lib/ios_logs/plugin.rb, line 60 def initialize(dangerfile) super @print = true @nslog = true @nslogs = [] @prints = [] end
Public Instance Methods
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
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
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
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
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 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
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 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]
# File lib/ios_logs/plugin.rb, line 161 def print_log_for(logs, message, method) logs.each do |log| public_send( method, message, sticky: false, file: log.file, line: log.line ) end end
Print logs @param method [Symbol] Method to indicate usage
@return [Void]
# File lib/ios_logs/plugin.rb, line 149 def print_logs(method) print_log_for(@prints, PRINT_MESSAGE, method) print_log_for(@nslogs, NSLOG_MESSAGE, method) end