Class: Danger::DangerL10nlint
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::DangerL10nlint
- Defined in:
- lib/danger_plugin.rb
Overview
This is your plugin class. Any attributes or methods you expose here will be available from within your Dangerfile.
To be published on the Danger plugins site, you will need to have the public interface documented. Danger uses [YARD](yardoc.org/) for generating documentation from your plugin source, and you can verify by running ‘danger plugins lint` or `bundle exec rake spec`.
You should replace these comments with a public description of your library.
Instance Attribute Summary collapse
-
#binary_path ⇒ String
The path to L10nLint’s execution.
-
#config_file ⇒ String
The path to L10nLint’s configuration file.
-
#errors ⇒ Array<Hash>
Errors found.
-
#inline_except_rules ⇒ Array<String>
Rules for not wanting to make inline comments.
-
#issues ⇒ Array<Hash>
All issues found.
-
#max_num_violations ⇒ Integer
Maximum number of issues to be reported.
-
#strict ⇒ Boolean
Whether we should fail on warnings.
-
#verbose ⇒ Boolean
Provides additional logging diagnostic information.
-
#warnings ⇒ Array<Hash>
Warnings found.
Instance Method Summary collapse
-
#l10nlint ⇒ L10nLint
Make L10nLint object for binary_path.
-
#lint_files(inline_mode: false, fail_on_error: false, additional_l10nlint_args: '', subtitle: '') ⇒ void
Lints Localizable.strings.
- #log(text) ⇒ Object
-
#markdown_issues(results, heading) ⇒ String
Create a markdown table from l10nlint issues.
- #other_issues_message(issues_count) ⇒ Object
-
#run_l10nlint(options, additional_l10nlint_args) ⇒ Array
Run l10nlint on all files and returns the issues.
-
#send_inline_comment(results, method) ⇒ void
Send inline comment with danger’s warn or fail method.
Instance Attribute Details
#binary_path ⇒ String
The path to L10nLint’s execution
27 28 29 |
# File 'lib/danger_plugin.rb', line 27 def binary_path @binary_path end |
#config_file ⇒ String
The path to L10nLint’s configuration file
31 32 33 |
# File 'lib/danger_plugin.rb', line 31 def config_file @config_file end |
#errors ⇒ Array<Hash>
Errors found
51 52 53 |
# File 'lib/danger_plugin.rb', line 51 def errors @errors end |
#inline_except_rules ⇒ Array<String>
Rules for not wanting to make inline comments
59 60 61 |
# File 'lib/danger_plugin.rb', line 59 def inline_except_rules @inline_except_rules end |
#issues ⇒ Array<Hash>
All issues found
55 56 57 |
# File 'lib/danger_plugin.rb', line 55 def issues @issues end |
#max_num_violations ⇒ Integer
Maximum number of issues to be reported.
35 36 37 |
# File 'lib/danger_plugin.rb', line 35 def max_num_violations @max_num_violations end |
#strict ⇒ Boolean
Whether we should fail on warnings
43 44 45 |
# File 'lib/danger_plugin.rb', line 43 def strict @strict end |
#verbose ⇒ Boolean
Provides additional logging diagnostic information.
39 40 41 |
# File 'lib/danger_plugin.rb', line 39 def verbose @verbose end |
#warnings ⇒ Array<Hash>
Warnings found
47 48 49 |
# File 'lib/danger_plugin.rb', line 47 def warnings @warnings end |
Instance Method Details
#l10nlint ⇒ L10nLint
Make L10nLint object for binary_path
142 143 144 |
# File 'lib/danger_plugin.rb', line 142 def l10nlint L10nLint.new(binary_path) end |
#lint_files(inline_mode: false, fail_on_error: false, additional_l10nlint_args: '', subtitle: '') ⇒ void
This method returns an undefined value.
Lints Localizable.strings
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/danger_plugin.rb', line 64 def lint_files(inline_mode: false, fail_on_error: false, additional_l10nlint_args: '', subtitle: '') raise 'l10nlint is not installed' unless l10nlint.installed? config_file_path = config_file if config_file_path log "Using config file: #{config_file_path}" else log 'config file was not specified' end # Prepare l10nlint options = { # Make sure we don't fail when config path has spaces config: config_file_path ? Shellwords.escape(config_file_path) : nil, reporter: 'json' } log "linting with options: #{}" issues = run_l10nlint(, additional_l10nlint_args) @issues = issues other_issues_count = 0 unless @max_num_violations.nil? || no_comment other_issues_count = issues.count - @max_num_violations if issues.count > @max_num_violations issues = issues.take(@max_num_violations) end log "Received issues from L10nLint: #{issues.count}" # Filter warnings and errors @warnings = issues.select { |issue| issue['severity'] == 'warning' } @errors = issues.select { |issue| issue['severity'] == 'error' } if inline_mode # Separate each warnings and errors by inline_except_rules if inline_except_rules markdown_warnings = warnings.select { |issue| inline_except_rules.include?(issue['ruleIdentifier']) } inline_warnings = warnings - markdown_warnings markdown_errors = @errors.select { |issue| inline_except_rules.include?(issue['ruleIdentifier']) } inline_errors = @errors - markdown_errors end # Report with inline comment send_inline_comment(inline_warnings, strict ? :fail : :warn) send_inline_comment(inline_errors, (fail_on_error || strict) ? :fail : :warn) if markdown_warnings.count > 0 || markdown_errors.count > 0 = "### L10nLint found issues\n\n".dup << markdown_issues(markdown_warnings, 'Warnings') unless markdown_warnings.empty? << markdown_issues(markdown_errors, 'Errors') unless markdown_errors.empty? markdown end warn (other_issues_count) if other_issues_count > 0 elsif warnings.count > 0 || errors.count > 0 # Report if any warning or error subtitle_in_title = subtitle.empty? ? '' : "(#{subtitle})" = "### L10nLint found issues #{subtitle_in_title}\n\n".dup << markdown_issues(warnings, 'Warnings') unless warnings.empty? << markdown_issues(errors, 'Errors') unless errors.empty? << "\n#{(other_issues_count)}" if other_issues_count > 0 markdown # Fail danger on errors should_fail_by_errors = fail_on_error && errors.count > 0 # Fail danger if any warnings or errors and we are strict should_fail_by_strict = strict && (errors.count > 0 || warnings.count > 0) if should_fail_by_errors || should_fail_by_strict fail 'Failed due to L10nLint errors' end end end |
#log(text) ⇒ Object
146 147 148 |
# File 'lib/danger_plugin.rb', line 146 def log(text) puts text if @verbose end |
#markdown_issues(results, heading) ⇒ String
Create a markdown table from l10nlint issues
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/danger_plugin.rb', line 165 def markdown_issues(results, heading) = "#### #{heading}\n\n".dup << "File | Line | Reason |\n" << "| --- | ----- | ----- |\n" results.each do |r| filename = r['location']['file'].split('/').last(2).join("/") line = r['location']['line'] reason = r['reason'] rule = r['ruleIdentifier'] # Other available properties can be found int L10nLint/…/JSONReporter.swift << "#{filename} | #{line} | #{reason} (#{rule})\n" end end |
#other_issues_message(issues_count) ⇒ Object
202 203 204 205 |
# File 'lib/danger_plugin.rb', line 202 def (issues_count) violations = issues_count == 1 ? 'violation' : 'violations' "L10nLint also found #{issues_count} more #{violations} with this PR." end |
#run_l10nlint(options, additional_l10nlint_args) ⇒ Array
Run l10nlint on all files and returns the issues
153 154 155 156 157 158 159 160 |
# File 'lib/danger_plugin.rb', line 153 def run_l10nlint(, additional_l10nlint_args) result = l10nlint.lint(, additional_l10nlint_args) if result == '' {} else JSON.parse(result) end end |
#send_inline_comment(results, method) ⇒ void
This method returns an undefined value.
Send inline comment with danger’s warn or fail method
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/danger_plugin.rb', line 186 def send_inline_comment(results, method) dir = "#{Dir.pwd}/" results.each do |r| github_filename = r['location']['file'].gsub(dir, '') = "#{r['reason']}".dup # extended content here filename = r['location']['file'].split('/').last(2).join("/") << "\n" << "`#{r['ruleIdentifier']}`" << " `#{filename}:#{r['location']['line']}`" # file:line for pasting into Xcode Quick Open send(method, , file: github_filename, line: r['location']['line']) end end |