danger-periphery 0.0.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8478fb86f4d5952f88ee7772f352049288bbc5c21f2f5d452ab7b37389b561ec
4
- data.tar.gz: 63c976e46da5ad41bd5797adb3f1ddc83edd5c09f8d607c043757924b3b32cad
3
+ metadata.gz: 15302ed92faf0a3230ddcb167fe3db0ae2e8c1a51d6551d85a4c2cc37c760dd4
4
+ data.tar.gz: 18880214efa7cb5a29ee9b6872688a6eeb19d805e07cad6c445afeeb24f4cad2
5
5
  SHA512:
6
- metadata.gz: aeae221bc8e42918a500eb3050b3bd44451a4fa4af5e2e42a77e252a64cfc17ca7213b455af830d26c1c001a0250a2af4e532303b1bce8c5e45dd2b02c817dfb
7
- data.tar.gz: d07fd15ea7105b6fbb875d628771a70c557bbd4066246712e26e5f1599d80d6d42e078d61b22e3c0c30c677105c0da317596a2d3a60595b3f16355193a8bed48
6
+ metadata.gz: 5e33a6b9fa62d733004fb63337fdff4973dd3b38d40a096db0b84a1249bc7789a3612a99a88d31020e22dd6494c3b7d3c953fe721be16347e825a58e8900a177
7
+ data.tar.gz: 47a37136e81dc9c4b6fc6b3331e476810e4520503c7fd062714363675926c51525502dc58ef2d610b890ee0d73d731d8ac87910bd8e6ab2f051f47c980b4ac88
data/lib/danger_plugin.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "periphery/runner"
4
- require "periphery/scan_log_parser"
3
+ require 'periphery'
5
4
 
6
5
  module Danger
7
6
  # Analyze Swift files and detect unused codes in your project.
8
- # This is done using [Periphery](https://github.com/peripheryapp/periphery).
7
+ # This is done using {https://github.com/peripheryapp/periphery Periphery}.
9
8
  #
10
9
  # @example Specifying options to Periphery.
11
10
  #
@@ -16,7 +15,7 @@ module Danger
16
15
  # clean_build: true
17
16
  # )
18
17
  #
19
- # @see manicmaniac/danger-periphery
18
+ # @see file:README.md
20
19
  # @tags swift
21
20
  class DangerPeriphery < Plugin
22
21
  # Path to Periphery executable.
@@ -24,32 +23,114 @@ module Danger
24
23
  # @return [String]
25
24
  attr_accessor :binary_path
26
25
 
26
+ # Proc object to process each warnings just before showing them.
27
+ # The Proc must receive 4 arguments: path, line, column, message
28
+ # and return one of:
29
+ # - an array that contains 4 elements [path, line, column, message]
30
+ # - true
31
+ # - false
32
+ # - nil
33
+ # If Proc returns an array, the warning will be raised based on returned elements.
34
+ # If Proc returns true, the warning will not be modified.
35
+ # If Proc returns false or nil, the warning will be ignored.
36
+ #
37
+ # By default the Proc returns true.
38
+ # @return [Proc]
39
+ attr_accessor :postprocessor
40
+
41
+ OPTION_OVERRIDES = {
42
+ disable_update_check: true,
43
+ format: 'checkstyle',
44
+ quiet: true
45
+ }.freeze
46
+
47
+ def initialize(dangerfile)
48
+ super(dangerfile)
49
+ @postprocessor = ->(_path, _line, _column, _message) { true }
50
+ end
51
+
27
52
  # Scans Swift files.
28
53
  # Raises an error when Periphery executable is not found.
29
54
  #
55
+ # @example Ignore all warnings from files matching regular expression
56
+ # periphery.scan do |violation|
57
+ # ! violation.path.match(/.*\/generated\.swift/)
58
+ # end
59
+ #
30
60
  # @param [Hash] options Options passed to Periphery with the following translation rules.
31
61
  # 1. Replace all underscores with hyphens in each key.
32
62
  # 2. Prepend double hyphens to each key.
33
63
  # 3. If value is an array, transform it to comma-separated string.
34
64
  # 4. If value is true, drop value and treat it as option without argument.
35
- # 5. Override some options like --disable-update-check, --format, --quiet and so.
65
+ # 5. Override some options listed in {OPTION_OVERRIDES}.
66
+ # Run +$ periphery help scan+ for available options.
67
+ #
68
+ # @param [Proc] block Block to process each warning just before showing it.
69
+ # The Proc receives 1 {Periphery::ScanResult} instance as argument.
70
+ # If the Proc returns falsy value, the warning corresponding to the given ScanResult will be
71
+ # suppressed, otherwise not.
72
+ #
36
73
  # @return [void]
37
- def scan(**options)
38
- output = Periphery::Runner.new(binary_path).scan(options.merge(disable_update_check: true, format: "xcode", quiet: true))
39
- entries = Periphery::ScanLogParser.new.parse(output)
74
+ def scan(**options, &block)
75
+ output = Periphery::Runner.new(binary_path).scan(options.merge(OPTION_OVERRIDES))
40
76
  files = files_in_diff
41
- entries.
42
- select { |entry| files.include?(entry.path) }.
43
- each { |entry| warn(entry.message, file: entry.path, line: entry.line) }
77
+ Periphery::CheckstyleParser.new.parse(output).each do |entry|
78
+ next unless files.include?(entry.path)
79
+
80
+ result = postprocess(entry, &block)
81
+ next unless result
82
+
83
+ path, line, _column, message = result
84
+ warn(message, file: path, line: line)
85
+ end
86
+ end
87
+
88
+ # Convenience method to set {#postprocessor} with block.
89
+ #
90
+ # @return [Proc]
91
+ #
92
+ # @example Ignore all warnings from files matching regular expression
93
+ # periphery.process_warnings do |path, line, column, message|
94
+ # ! path.match(/.*\/generated\.swift/)
95
+ # end
96
+ def process_warnings(&block)
97
+ @postprocessor = block
44
98
  end
45
99
 
46
100
  private
47
101
 
48
102
  def files_in_diff
49
103
  # Taken from https://github.com/ashfurrow/danger-ruby-swiftlint/blob/5184909aab00f12954088684bbf2ce5627e08ed6/lib/danger_plugin.rb#L214-L216
50
- renamed_files_hash = git.renamed_files.map { |rename| [rename[:before], rename[:after]] }.to_h
51
- post_rename_modified_files = git.modified_files.map { |modified_file| renamed_files_hash[modified_file] || modified_file }
104
+ renamed_files_hash = git.renamed_files.to_h { |rename| [rename[:before], rename[:after]] }
105
+ post_rename_modified_files = git.modified_files.map do |modified_file|
106
+ renamed_files_hash[modified_file] || modified_file
107
+ end
52
108
  (post_rename_modified_files - git.deleted_files) + git.added_files
53
109
  end
110
+
111
+ def postprocess(entry, &block)
112
+ if block
113
+ postprocess_with_block(entry, &block)
114
+ else
115
+ postprocess_with_postprocessor(entry)
116
+ end
117
+ end
118
+
119
+ def postprocess_with_block(entry, &block)
120
+ [entry.path, entry.line, entry.column, entry.message] if block.call(entry)
121
+ end
122
+
123
+ def postprocess_with_postprocessor(entry)
124
+ result = @postprocessor.call(entry.path, entry.line, entry.column, entry.message)
125
+ if !result
126
+ nil
127
+ elsif result.is_a?(TrueClass)
128
+ [entry.path, entry.line, entry.column, entry.message]
129
+ elsif result.is_a?(Array) && result.size == 4
130
+ result
131
+ else
132
+ raise 'Proc passed to postprocessor must return one of nil, true, false and Array that includes 4 elements.'
133
+ end
134
+ end
54
135
  end
55
136
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require 'periphery/scan_result'
5
+ require 'rexml/parsers/streamparser'
6
+ require 'rexml/streamlistener'
7
+
8
+ module Periphery
9
+ # Parses {https://checkstyle.sourceforge.io/ Checkstyle} format XML produced by Periphery with
10
+ # +--format=checkstyle+ option.
11
+ class CheckstyleParser
12
+ class Listener # :nodoc:
13
+ include REXML::StreamListener
14
+
15
+ attr_reader :results
16
+
17
+ def initialize
18
+ @current_file = nil
19
+ @results = []
20
+ end
21
+
22
+ def tag_start(name, attrs)
23
+ case name
24
+ when 'file'
25
+ @current_file = relative_path(attrs['name'])
26
+ when 'error'
27
+ if @current_file
28
+ @results << ScanResult.new(@current_file, attrs['line'].to_i, attrs['column'].to_i, attrs['message'])
29
+ end
30
+ end
31
+ end
32
+
33
+ def tag_end(name)
34
+ @current_file = nil if name == 'file'
35
+ end
36
+
37
+ private
38
+
39
+ def relative_path(path, base = Pathname.getwd)
40
+ Pathname.new(path).relative_path_from(base).to_s
41
+ end
42
+ end
43
+
44
+ def parse(string)
45
+ listener = Listener.new
46
+ REXML::Parsers::StreamParser.new(string, listener).parse
47
+ listener.results
48
+ end
49
+ end
50
+ end
@@ -1,17 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "open3"
3
+ require 'open3'
4
4
 
5
5
  module Periphery
6
- class Runner
6
+ class Runner # :nodoc:
7
7
  attr_reader :binary_path
8
8
 
9
9
  def initialize(binary_path)
10
- @binary_path = binary_path || "periphery"
10
+ @binary_path = binary_path || 'periphery'
11
11
  end
12
12
 
13
13
  def scan(options)
14
- arguments = [binary_path, "scan"] + scan_arguments(options)
14
+ arguments = [binary_path, 'scan'] + scan_arguments(options)
15
15
  stdout, stderr, status = Open3.capture3(*arguments)
16
16
  raise "error: #{arguments} exited with status code #{status.exitstatus}. #{stderr}" unless status.success?
17
17
 
@@ -19,13 +19,14 @@ module Periphery
19
19
  end
20
20
 
21
21
  def scan_arguments(options)
22
- options.
23
- select { |_key, value| value }.
24
- map { |key, value| value.kind_of?(TrueClass) ? [key, nil] : [key, value] }.
25
- map { |key, value| value.kind_of?(Array) ? [key, value.join(",")] : [key, value] }.
26
- map { |key, value| ["--#{key.to_s.tr('_', '-')}", value] }.
27
- flatten.
28
- compact
22
+ options.each_with_object([]) do |(key, value), new_options|
23
+ next unless value
24
+
25
+ value = nil if value.is_a?(TrueClass)
26
+ value = value.join(',') if value.is_a?(Array)
27
+ new_options << "--#{key.to_s.tr('_', '-')}"
28
+ new_options << value&.to_s if value
29
+ end
29
30
  end
30
31
  end
31
32
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Periphery
4
+ ScanResult = Struct.new(:path, :line, :column, :message)
5
+ end
data/lib/periphery.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'periphery/runner'
4
+ require 'periphery/checkstyle_parser'
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DangerPeriphery
4
- VERSION = "0.0.1"
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger-periphery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryosuke Ito
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-24 00:00:00.000000000 Z
11
+ date: 2022-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api
@@ -27,39 +27,21 @@ dependencies:
27
27
  description: A Danger plugin to detect unused codes.
28
28
  email:
29
29
  - rito.0305@gmail.com
30
- executables:
31
- - download_periphery
30
+ executables: []
32
31
  extensions: []
33
32
  extra_rdoc_files: []
34
33
  files:
35
- - ".github/workflows/lint.yml"
36
- - ".github/workflows/test.yml"
37
- - ".gitignore"
38
- - ".rspec"
39
- - ".rubocop.yml"
40
- - Dangerfile
41
- - Gemfile
42
- - Guardfile
43
- - LICENSE.txt
44
- - README.md
45
- - Rakefile
46
- - bin/download_periphery
47
- - danger-periphery.gemspec
48
34
  - lib/danger_plugin.rb
35
+ - lib/periphery.rb
36
+ - lib/periphery/checkstyle_parser.rb
49
37
  - lib/periphery/runner.rb
50
- - lib/periphery/scan_log_parser.rb
38
+ - lib/periphery/scan_result.rb
51
39
  - lib/version.rb
52
- - spec/danger_plugin_spec.rb
53
- - spec/periphery/runner_spec.rb
54
- - spec/periphery/scan_log_parser_spec.rb
55
- - spec/spec_helper.rb
56
- - spec/support/fixtures/github_pr.json
57
- - spec/support/fixtures/test.xcodeproj/project.pbxproj
58
- - spec/support/fixtures/test/main.swift
59
40
  homepage: https://github.com/manicmaniac/danger-periphery
60
41
  licenses:
61
42
  - MIT
62
- metadata: {}
43
+ metadata:
44
+ rubygems_mfa_required: 'true'
63
45
  post_install_message:
64
46
  rdoc_options: []
65
47
  require_paths:
@@ -68,22 +50,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
50
  requirements:
69
51
  - - ">="
70
52
  - !ruby/object:Gem::Version
71
- version: 2.3.0
53
+ version: 2.6.0
72
54
  required_rubygems_version: !ruby/object:Gem::Requirement
73
55
  requirements:
74
56
  - - ">="
75
57
  - !ruby/object:Gem::Version
76
58
  version: '0'
77
59
  requirements: []
78
- rubygems_version: 3.0.3
60
+ rubygems_version: 3.0.3.1
79
61
  signing_key:
80
62
  specification_version: 4
81
63
  summary: A Danger plugin to detect unused codes.
82
- test_files:
83
- - spec/danger_plugin_spec.rb
84
- - spec/periphery/runner_spec.rb
85
- - spec/periphery/scan_log_parser_spec.rb
86
- - spec/spec_helper.rb
87
- - spec/support/fixtures/github_pr.json
88
- - spec/support/fixtures/test.xcodeproj/project.pbxproj
89
- - spec/support/fixtures/test/main.swift
64
+ test_files: []
@@ -1,12 +0,0 @@
1
- name: Lint
2
- on: [pull_request]
3
- jobs:
4
- lint:
5
- runs-on: macOS-11
6
- env:
7
- DANGER_GITHUB_API_TOKEN: ${{ secrets.DANGER_GITHUB_API_TOKEN }}
8
- steps:
9
- - uses: actions/checkout@v2
10
- - run: bin/download_periphery
11
- - run: bundle install
12
- - run: bundle exec danger
@@ -1,10 +0,0 @@
1
- name: Test
2
- on: [push]
3
- jobs:
4
- unit-test:
5
- runs-on: macOS-11
6
- steps:
7
- - uses: actions/checkout@v2
8
- - run: bin/download_periphery
9
- - run: bundle install
10
- - run: bundle exec rake
data/.gitignore DELETED
@@ -1,10 +0,0 @@
1
- .DS_Store
2
- .idea/
3
- .yardoc
4
- /tmp
5
- Gemfile.lock
6
- bin/lib_InternalSwiftSyntaxParser.dylib
7
- bin/periphery
8
- pkg
9
- project.xcworkspace
10
- xcuserdata
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --color
2
- --require spec_helper
3
- --format documentation
data/.rubocop.yml DELETED
@@ -1,281 +0,0 @@
1
- # Defaults can be found here: https://github.com/bbatsov/rubocop/blob/master/config/default.yml
2
-
3
- # If you don't like these settings, just delete this file :)
4
-
5
- AllCops:
6
- TargetRubyVersion: 2.6
7
- SuggestExtensions: false
8
-
9
- Style/StringLiterals:
10
- EnforcedStyle: double_quotes
11
- Enabled: true
12
-
13
- # kind_of? is a good way to check a type
14
- Style/ClassCheck:
15
- EnforcedStyle: kind_of?
16
-
17
- # specs sometimes have useless assignments, which is fine
18
- Lint/UselessAssignment:
19
- Exclude:
20
- - '**/spec/**/*'
21
-
22
- # We could potentially enable the 2 below:
23
- Layout/FirstHashElementIndentation:
24
- Enabled: false
25
-
26
- Layout/HashAlignment:
27
- Enabled: false
28
-
29
- # HoundCI doesn't like this rule
30
- Layout/DotPosition:
31
- Enabled: false
32
-
33
- # We allow !! as it's an easy way to convert ot boolean
34
- Style/DoubleNegation:
35
- Enabled: false
36
-
37
- # Cop supports --auto-correct.
38
- Lint/UnusedBlockArgument:
39
- Enabled: false
40
-
41
- # We want to allow class Fastlane::Class
42
- Style/ClassAndModuleChildren:
43
- Enabled: false
44
-
45
- Metrics/AbcSize:
46
- Max: 60
47
-
48
- # The %w might be confusing for new users
49
- Style/WordArray:
50
- MinSize: 19
51
-
52
- # raise and fail are both okay
53
- Style/SignalException:
54
- Enabled: false
55
-
56
- # Better too much 'return' than one missing
57
- Style/RedundantReturn:
58
- Enabled: false
59
-
60
- # Having if in the same line might not always be good
61
- Style/IfUnlessModifier:
62
- Enabled: false
63
-
64
- # and and or is okay
65
- Style/AndOr:
66
- Enabled: false
67
-
68
- # Configuration parameters: CountComments.
69
- Metrics/ClassLength:
70
- Max: 350
71
-
72
- Metrics/CyclomaticComplexity:
73
- Max: 17
74
-
75
- # Configuration parameters: AllowURI, URISchemes.
76
- Layout/LineLength:
77
- Max: 370
78
-
79
- # Configuration parameters: CountKeywordArgs.
80
- Metrics/ParameterLists:
81
- Max: 10
82
-
83
- Metrics/PerceivedComplexity:
84
- Max: 18
85
-
86
- # Sometimes it's easier to read without guards
87
- Style/GuardClause:
88
- Enabled: false
89
-
90
- # something = if something_else
91
- # that's confusing
92
- Style/ConditionalAssignment:
93
- Enabled: false
94
-
95
- # Better to have too much self than missing a self
96
- Style/RedundantSelf:
97
- Enabled: false
98
-
99
- Metrics/MethodLength:
100
- Max: 60
101
-
102
- # We're not there yet
103
- Style/Documentation:
104
- Enabled: false
105
-
106
- # Adds complexity
107
- Style/IfInsideElse:
108
- Enabled: false
109
-
110
- # danger specific
111
-
112
- Style/BlockComments:
113
- Enabled: false
114
-
115
- Layout/MultilineMethodCallIndentation:
116
- EnforcedStyle: indented
117
-
118
- # FIXME: 25
119
- Metrics/BlockLength:
120
- Max: 345
121
- Exclude:
122
- - "**/*_spec.rb"
123
-
124
- Style/MixinGrouping:
125
- Enabled: false
126
-
127
- Naming/FileName:
128
- Enabled: false
129
-
130
- Layout/HeredocIndentation:
131
- Enabled: false
132
-
133
- Style/SpecialGlobalVars:
134
- Enabled: false
135
-
136
- Style/PercentLiteralDelimiters:
137
- PreferredDelimiters:
138
- "%": ()
139
- "%i": ()
140
- "%q": ()
141
- "%Q": ()
142
- "%r": "{}"
143
- "%s": ()
144
- "%w": ()
145
- "%W": ()
146
- "%x": ()
147
-
148
- Security/YAMLLoad:
149
- Enabled: false
150
-
151
- Gemspec/DateAssignment:
152
- Enabled: true
153
-
154
- Layout/LineEndStringConcatenationIndentation:
155
- Enabled: true
156
-
157
- Layout/SpaceBeforeBrackets:
158
- Enabled: true
159
-
160
- Lint/AmbiguousAssignment:
161
- Enabled: true
162
-
163
- Lint/AmbiguousOperatorPrecedence:
164
- Enabled: true
165
-
166
- Lint/AmbiguousRange:
167
- Enabled: true
168
-
169
- Lint/DeprecatedConstants:
170
- Enabled: true
171
-
172
- Lint/DuplicateBranch:
173
- Enabled: true
174
-
175
- Lint/DuplicateRegexpCharacterClassElement:
176
- Enabled: true
177
-
178
- Lint/EmptyBlock:
179
- Enabled: true
180
-
181
- Lint/EmptyClass:
182
- Enabled: true
183
-
184
- Lint/EmptyInPattern:
185
- Enabled: true
186
-
187
- Lint/IncompatibleIoSelectWithFiberScheduler:
188
- Enabled: true
189
-
190
- Lint/LambdaWithoutLiteralBlock:
191
- Enabled: true
192
-
193
- Lint/NoReturnInBeginEndBlocks:
194
- Enabled: true
195
-
196
- Lint/NumberedParameterAssignment:
197
- Enabled: true
198
-
199
- Lint/OrAssignmentToConstant:
200
- Enabled: true
201
-
202
- Lint/RedundantDirGlobSort:
203
- Enabled: true
204
-
205
- Lint/RequireRelativeSelfPath:
206
- Enabled: true
207
-
208
- Lint/SymbolConversion:
209
- Enabled: true
210
-
211
- Lint/ToEnumArguments:
212
- Enabled: true
213
-
214
- Lint/TripleQuotes:
215
- Enabled: true
216
-
217
- Lint/UnexpectedBlockArity:
218
- Enabled: true
219
-
220
- Lint/UnmodifiedReduceAccumulator:
221
- Enabled: true
222
-
223
- Security/IoMethods:
224
- Enabled: true
225
-
226
- Style/ArgumentsForwarding:
227
- Enabled: true
228
-
229
- Style/CollectionCompact:
230
- Enabled: true
231
-
232
- Style/DocumentDynamicEvalDefinition:
233
- Enabled: true
234
-
235
- Style/EndlessMethod:
236
- Enabled: true
237
-
238
- Style/HashConversion:
239
- Enabled: true
240
-
241
- Style/HashExcept:
242
- Enabled: true
243
-
244
- Style/IfWithBooleanLiteralBranches:
245
- Enabled: true
246
-
247
- Style/InPatternThen:
248
- Enabled: true
249
-
250
- Style/MultilineInPatternThen:
251
- Enabled: true
252
-
253
- Style/NegatedIfElseCondition:
254
- Enabled: true
255
-
256
- Style/NilLambda:
257
- Enabled: true
258
-
259
- Style/NumberedParameters:
260
- Enabled: true
261
-
262
- Style/NumberedParametersLimit:
263
- Enabled: true
264
-
265
- Style/QuotedSymbols:
266
- Enabled: true
267
-
268
- Style/RedundantArgument:
269
- Enabled: true
270
-
271
- Style/RedundantSelfAssignmentBranch:
272
- Enabled: true
273
-
274
- Style/SelectByRegexp:
275
- Enabled: true
276
-
277
- Style/StringChars:
278
- Enabled: true
279
-
280
- Style/SwapValues:
281
- Enabled: true
data/Dangerfile DELETED
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- rubocop.lint inline_comment: true
4
-
5
- periphery.binary_path = "bin/periphery"
6
- periphery.scan(
7
- project: "spec/support/fixtures/test.xcodeproj",
8
- schemes: "test",
9
- targets: "test"
10
- )