danger-periphery 0.2.4 → 0.3.0

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: 994474d57939cc311e07ca3d4297c06f6b4fa7ed18ef0a8f03ba2a2ab708c1d7
4
- data.tar.gz: dce2b80c6b5cef2819fb9cc2e08f3242e38399af31080ead7dfdb7028c4a642e
3
+ metadata.gz: 3523d08dc99ca2f4cc98a33d1797c122a048fd0d1cacb025f6544efa49a4fec8
4
+ data.tar.gz: 0a715b3067bc9c9d156eca9106f8625a9acc2485bf124874ced29282188ee98c
5
5
  SHA512:
6
- metadata.gz: a0179df852baf6a50fdb31e9a1a524056777ab455131f642419b080f1a0ed5440af523b7645b2e0914081bbceded557064347051cd1f93bc4ddf22fdc23e20f8
7
- data.tar.gz: 11f68161a78dd66d1fc72075f3b2d2260efb00fa0f7041eac614784db65176b299f97651031985a37190edabf47b8d1aa5e88abfaa8de4b3a4b38f7983d6f9dd
6
+ metadata.gz: 700c5fcaa9ace60357c580a56f3d99ad795864dce645f137e80dfd26d4f8e3589bd4aa2b9a2f61bf4d29cdc716da3d58b4702605cc72d0182f1421387d98932b
7
+ data.tar.gz: cfb46c6fdddd95e41dd26de1381c2aa2dd397a4d9a243b17ec98f168e6389a93959b9bee4139e9fea005ecc33189af9fdd3788bf2c8d479b2ea2cd01b5986eae
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'periphery'
4
+
5
+ module Danger
6
+ # Analyze Swift files and detect unused codes in your project.
7
+ # This is done using {https://github.com/peripheryapp/periphery Periphery}.
8
+ #
9
+ # @example Specifying options to Periphery.
10
+ #
11
+ # periphery.scan(
12
+ # project: "Foo.xcodeproj"
13
+ # schemes: ["foo", "bar"],
14
+ # targets: "foo",
15
+ # clean_build: true
16
+ # )
17
+ #
18
+ # @see file:README.md
19
+ # @tags swift
20
+ class DangerPeriphery < Plugin
21
+ # Path to Periphery executable.
22
+ # By default the value is nil and the executable is searched from $PATH.
23
+ # @return [String]
24
+ attr_accessor :binary_path
25
+
26
+ # A flag to force Periphery report problems about all files.
27
+ # @return [Boolean] +true+ if it reports problems in all files.
28
+ # Otherwise +false+, it reports about only changed files in this pull request.
29
+ # By default +false+ is set.
30
+ attr_accessor :scan_all_files
31
+
32
+ # For internal use only.
33
+ #
34
+ # @return [Symbol]
35
+ attr_writer :format
36
+
37
+ OPTION_OVERRIDES = {
38
+ disable_update_check: true,
39
+ quiet: true
40
+ }.freeze
41
+
42
+ def initialize(dangerfile)
43
+ super(dangerfile)
44
+ @format = :checkstyle
45
+ end
46
+
47
+ # Scans Swift files.
48
+ # Raises an error when Periphery executable is not found.
49
+ #
50
+ # @example Ignore all warnings from files matching regular expression
51
+ # periphery.scan do |violation|
52
+ # !violation.path.match(/.*\/generated\.swift/)
53
+ # end
54
+ #
55
+ # @param [Hash] options Options passed to Periphery with the following translation rules.
56
+ # 1. Replace all underscores with hyphens in each key.
57
+ # 2. Prepend double hyphens to each key.
58
+ # 3. If value is an array, transform it to comma-separated string.
59
+ # 4. If value is true, drop value and treat it as option without argument.
60
+ # 5. Override some options listed in {OPTION_OVERRIDES}.
61
+ # Run +$ periphery help scan+ for available options.
62
+ #
63
+ # @yield [entry] Block to process each warning just before showing it.
64
+ # @yieldparam [Periphery::ScanResult] entry
65
+ # @yieldreturn [Boolean, Periphery::ScanResult]
66
+ # If the Proc returns falsy value, the warning corresponding to the given ScanResult will be
67
+ # suppressed, otherwise not.
68
+ #
69
+ # @return [void]
70
+ def scan(options = {})
71
+ output = Periphery::Runner.new(binary_path).scan(options.merge(OPTION_OVERRIDES).merge(format: @format))
72
+ files = files_in_diff unless @scan_all_files
73
+ parser.parse(output).each do |entry|
74
+ next unless @scan_all_files || files.include?(entry.path)
75
+
76
+ next if block_given? && !yield(entry)
77
+
78
+ warn(entry.message, file: entry.path, line: entry.line)
79
+ end
80
+ end
81
+
82
+ # Download and install Periphery executable binary.
83
+ #
84
+ # @param [String, Symbol] version The version of Periphery you want to install.
85
+ # `:latest` is treated as special keyword that specifies the latest version.
86
+ # @param [String] path The path to install Periphery including the filename itself.
87
+ # @param [Boolean] force If `true`, an existing file will be overwritten. Otherwise an error occurs.
88
+ # @return [void]
89
+ def install(version: :latest, path: 'periphery', force: false)
90
+ installer = Periphery::Installer.new(version)
91
+ installer.install(path, force: force)
92
+ self.binary_path = File.absolute_path(path)
93
+ end
94
+
95
+ private
96
+
97
+ def files_in_diff
98
+ # Taken from https://github.com/ashfurrow/danger-ruby-swiftlint/blob/5184909aab00f12954088684bbf2ce5627e08ed6/lib/danger_plugin.rb#L214-L216
99
+ renamed_files_hash = git.renamed_files.to_h { |rename| [rename[:before], rename[:after]] }
100
+ post_rename_modified_files = git.modified_files.map do |modified_file|
101
+ renamed_files_hash[modified_file] || modified_file
102
+ end
103
+ (post_rename_modified_files - git.deleted_files) + git.added_files
104
+ end
105
+
106
+ def parser
107
+ case @format
108
+ when :checkstyle
109
+ Periphery::CheckstyleParser.new
110
+ when :json
111
+ Periphery::JsonParser.new
112
+ else
113
+ raise "#{@format} is unsupported"
114
+ end
115
+ end
116
+ end
117
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DangerPeriphery
4
- VERSION = '0.2.4'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/danger_plugin.rb CHANGED
@@ -1,201 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'periphery'
4
-
5
- module Danger
6
- # Analyze Swift files and detect unused codes in your project.
7
- # This is done using {https://github.com/peripheryapp/periphery Periphery}.
8
- #
9
- # @example Specifying options to Periphery.
10
- #
11
- # periphery.scan(
12
- # project: "Foo.xcodeproj"
13
- # schemes: ["foo", "bar"],
14
- # targets: "foo",
15
- # clean_build: true
16
- # )
17
- #
18
- # @see file:README.md
19
- # @tags swift
20
- class DangerPeriphery < Plugin
21
- # Path to Periphery executable.
22
- # By default the value is nil and the executable is searched from $PATH.
23
- # @return [String]
24
- attr_accessor :binary_path
25
-
26
- # @deprecated Use {#scan} with block instead.
27
- #
28
- # Proc object to process each warnings just before showing them.
29
- # The Proc must receive 4 arguments: path, line, column, message
30
- # and return one of:
31
- # - an array that contains 4 elements [path, line, column, message]
32
- # - true
33
- # - false
34
- # - nil
35
- # If Proc returns an array, the warning will be raised based on returned elements.
36
- # If Proc returns true, the warning will not be modified.
37
- # If Proc returns false or nil, the warning will be ignored.
38
- #
39
- # By default the Proc returns true.
40
- # @return [Proc]
41
- attr_reader :postprocessor
42
-
43
- # For internal use only.
44
- #
45
- # @return [Symbol]
46
- attr_writer :format
47
-
48
- OPTION_OVERRIDES = {
49
- disable_update_check: true,
50
- quiet: true
51
- }.freeze
52
-
53
- def initialize(dangerfile)
54
- super(dangerfile)
55
- @postprocessor = ->(_path, _line, _column, _message) { true }
56
- @format = :checkstyle
57
- end
58
-
59
- # Scans Swift files.
60
- # Raises an error when Periphery executable is not found.
61
- #
62
- # @example Ignore all warnings from files matching regular expression
63
- # periphery.scan do |violation|
64
- # !violation.path.match(/.*\/generated\.swift/)
65
- # end
66
- #
67
- # @param [Hash] options Options passed to Periphery with the following translation rules.
68
- # 1. Replace all underscores with hyphens in each key.
69
- # 2. Prepend double hyphens to each key.
70
- # 3. If value is an array, transform it to comma-separated string.
71
- # 4. If value is true, drop value and treat it as option without argument.
72
- # 5. Override some options listed in {OPTION_OVERRIDES}.
73
- # Run +$ periphery help scan+ for available options.
74
- #
75
- # @param [Proc] block Block to process each warning just before showing it.
76
- # The Proc receives 1 {Periphery::ScanResult} instance as argument.
77
- # If the Proc returns falsy value, the warning corresponding to the given ScanResult will be
78
- # suppressed, otherwise not.
79
- #
80
- # @return [void]
81
- def scan(options = {}, &block)
82
- output = Periphery::Runner.new(binary_path).scan(options.merge(OPTION_OVERRIDES).merge(format: @format))
83
- files = files_in_diff
84
- parser.parse(output).each do |entry|
85
- next unless files.include?(entry.path)
86
-
87
- result = postprocess(entry, &block)
88
- next unless result
89
-
90
- path, line, _column, message = result
91
- warn(message, file: path, line: line)
92
- end
93
- end
94
-
95
- # @deprecated Use {#scan} with block instead.
96
- #
97
- # Convenience method to set {#postprocessor} with block.
98
- #
99
- # @return [Proc]
100
- def process_warnings(&block)
101
- deprecate_in_favor_of_scan
102
- @postprocessor = block
103
- end
104
-
105
- # @deprecated Use {#scan} with block instead.
106
- #
107
- # A block to manipulate each warning before it is displayed.
108
- #
109
- # @param [Proc] postprocessor Block to process each warning just before showing it.
110
- # The Proc is called like `postprocessor(path, line, column, message)`
111
- # where `path` is a String that indicates the file path the warning points out,
112
- # `line` and `column` are Integers that indicates the location in the file,
113
- # `message` is a String message body of the warning.
114
- # The Proc returns either of the following:
115
- # 1. an Array contains `path`, `line`, `column`, `message` in this order.
116
- # 2. true
117
- # 3. false or nil
118
- # If it returns falsy value, the warning will be suppressed.
119
- # If it returns `true`, the warning will be displayed as-is.
120
- # Otherwise it returns an Array, the warning is newly created by the returned array
121
- # and displayed.
122
- # @return [void]
123
- def postprocessor=(postprocessor)
124
- deprecate_in_favor_of_scan
125
- @postprocessor = postprocessor
126
- end
127
-
128
- # Download and install Periphery executable binary.
129
- #
130
- # @param [String, Symbol] version The version of Periphery you want to install.
131
- # `:latest` is treated as special keyword that specifies the latest version.
132
- # @param [String] path The path to install Periphery including the filename itself.
133
- # @param [Boolean] force If `true`, an existing file will be overwritten. Otherwise an error occurs.
134
- # @return [void]
135
- def install(version: :latest, path: 'periphery', force: false)
136
- installer = Periphery::Installer.new(version)
137
- installer.install(path, force: force)
138
- self.binary_path = File.absolute_path(path)
139
- end
140
-
141
- private
142
-
143
- def files_in_diff
144
- # Taken from https://github.com/ashfurrow/danger-ruby-swiftlint/blob/5184909aab00f12954088684bbf2ce5627e08ed6/lib/danger_plugin.rb#L214-L216
145
- renamed_files_hash = git.renamed_files.to_h { |rename| [rename[:before], rename[:after]] }
146
- post_rename_modified_files = git.modified_files.map do |modified_file|
147
- renamed_files_hash[modified_file] || modified_file
148
- end
149
- (post_rename_modified_files - git.deleted_files) + git.added_files
150
- end
151
-
152
- def postprocess(entry, &block)
153
- if block
154
- postprocess_with_block(entry, &block)
155
- else
156
- postprocess_with_postprocessor(entry)
157
- end
158
- end
159
-
160
- def postprocess_with_block(entry, &block)
161
- [entry.path, entry.line, entry.column, entry.message] if block.call(entry)
162
- end
163
-
164
- def postprocess_with_postprocessor(entry)
165
- result = @postprocessor.call(entry.path, entry.line, entry.column, entry.message)
166
- if !result
167
- nil
168
- elsif result.is_a?(TrueClass)
169
- [entry.path, entry.line, entry.column, entry.message]
170
- elsif result.is_a?(Array) && result.size == 4
171
- result
172
- else
173
- raise 'Proc passed to postprocessor must return one of nil, true, false and Array that includes 4 elements.'
174
- end
175
- end
176
-
177
- def deprecate_in_favor_of_scan
178
- caller_method_name = caller(1, 1)[0].sub(/.*`(.*)'.*/, '\1')
179
- caller_location = caller_locations(2, 1)[0]
180
- message = [
181
- "`#{self.class}##{caller_method_name}` is deprecated; use `#{self.class}#scan` with block instead. ",
182
- 'It will be removed from future releases.'
183
- ].join
184
- location_message = "#{self.class}##{caller_method_name} called from #{caller_location}"
185
- Kernel.warn("NOTE: #{message}\n#{location_message}")
186
- issue_reference = 'See manicmaniac/danger-periphery#37 for detail.'
187
- warn("#{message}\n#{issue_reference}", file: caller_location.path, line: caller_location.lineno)
188
- end
189
-
190
- def parser
191
- case @format
192
- when :checkstyle
193
- Periphery::CheckstyleParser.new
194
- when :json
195
- Periphery::JsonParser.new
196
- else
197
- raise "#{@format} is unsupported"
198
- end
199
- end
200
- end
201
- end
3
+ require 'danger/danger_periphery'
4
+ require 'danger_periphery/version'
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.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryosuke Ito
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-22 00:00:00.000000000 Z
11
+ date: 2024-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api
@@ -45,6 +45,8 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - lib/danger/danger_periphery.rb
49
+ - lib/danger_periphery/version.rb
48
50
  - lib/danger_plugin.rb
49
51
  - lib/periphery.rb
50
52
  - lib/periphery/checkstyle_parser.rb
@@ -52,7 +54,6 @@ files:
52
54
  - lib/periphery/json_parser.rb
53
55
  - lib/periphery/runner.rb
54
56
  - lib/periphery/scan_result.rb
55
- - lib/version.rb
56
57
  homepage: https://github.com/manicmaniac/danger-periphery
57
58
  licenses:
58
59
  - MIT