danger-periphery 0.2.0 → 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: 5b949d4e8279afb04d63616f89e15f3ef0efaf700b83d027273f1973d085fa23
4
- data.tar.gz: cd8e2564827d41bdee17e7b8fead197f3dc1481f129ee9dcbc22779ea1783489
3
+ metadata.gz: 15302ed92faf0a3230ddcb167fe3db0ae2e8c1a51d6551d85a4c2cc37c760dd4
4
+ data.tar.gz: 18880214efa7cb5a29ee9b6872688a6eeb19d805e07cad6c445afeeb24f4cad2
5
5
  SHA512:
6
- metadata.gz: 3d0e28054ab01f50603b92cf32b6005d1befa875545afaa9d8fbd80bdf048d097776bcfcdfe01cc746a0cc24d9455284a400be7b9d11142249db9b2731d1b578
7
- data.tar.gz: 97d0f89856d52fe03fbda8f3b8539279343e2b28340042ee5db2aa2984222cf6e3ac5eacde6ad17eebc7b393f9d6163ce16f296e8a044fe984442b31566ae6ee
6
+ metadata.gz: 5e33a6b9fa62d733004fb63337fdff4973dd3b38d40a096db0b84a1249bc7789a3612a99a88d31020e22dd6494c3b7d3c953fe721be16347e825a58e8900a177
7
+ data.tar.gz: 47a37136e81dc9c4b6fc6b3331e476810e4520503c7fd062714363675926c51525502dc58ef2d610b890ee0d73d731d8ac87910bd8e6ab2f051f47c980b4ac88
data/lib/danger_plugin.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "periphery"
3
+ require 'periphery'
4
4
 
5
5
  module Danger
6
6
  # Analyze Swift files and detect unused codes in your project.
7
- # This is done using [Periphery](https://github.com/peripheryapp/periphery).
7
+ # This is done using {https://github.com/peripheryapp/periphery Periphery}.
8
8
  #
9
9
  # @example Specifying options to Periphery.
10
10
  #
@@ -15,7 +15,7 @@ module Danger
15
15
  # clean_build: true
16
16
  # )
17
17
  #
18
- # @see manicmaniac/danger-periphery
18
+ # @see file:README.md
19
19
  # @tags swift
20
20
  class DangerPeriphery < Plugin
21
21
  # Path to Periphery executable.
@@ -40,44 +40,58 @@ module Danger
40
40
 
41
41
  OPTION_OVERRIDES = {
42
42
  disable_update_check: true,
43
- format: "checkstyle",
43
+ format: 'checkstyle',
44
44
  quiet: true
45
45
  }.freeze
46
46
 
47
47
  def initialize(dangerfile)
48
48
  super(dangerfile)
49
- @postprocessor = ->(path, line, column, message) { true }
49
+ @postprocessor = ->(_path, _line, _column, _message) { true }
50
50
  end
51
51
 
52
52
  # Scans Swift files.
53
53
  # Raises an error when Periphery executable is not found.
54
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
+ #
55
60
  # @param [Hash] options Options passed to Periphery with the following translation rules.
56
61
  # 1. Replace all underscores with hyphens in each key.
57
62
  # 2. Prepend double hyphens to each key.
58
63
  # 3. If value is an array, transform it to comma-separated string.
59
64
  # 4. If value is true, drop value and treat it as option without argument.
60
- # 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
+ #
61
73
  # @return [void]
62
74
  def scan(**options, &block)
63
75
  output = Periphery::Runner.new(binary_path).scan(options.merge(OPTION_OVERRIDES))
64
76
  files = files_in_diff
65
- Periphery::CheckstyleParser.new.parse(output).
66
- lazy.
67
- select { |entry| files.include?(entry.path) }.
68
- map { |entry| postprocess(entry, &block) }.
69
- force.
70
- compact.
71
- each { |path, line, column, message| warn(message, file: path, line: 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
72
86
  end
73
87
 
74
- # Convenience method to set `postprocessor` with block.
88
+ # Convenience method to set {#postprocessor} with block.
75
89
  #
76
90
  # @return [Proc]
77
91
  #
78
92
  # @example Ignore all warnings from files matching regular expression
79
93
  # periphery.process_warnings do |path, line, column, message|
80
- # !path.match(/.*\/generated\.swift/)
94
+ # ! path.match(/.*\/generated\.swift/)
81
95
  # end
82
96
  def process_warnings(&block)
83
97
  @postprocessor = block
@@ -88,26 +102,34 @@ module Danger
88
102
  def files_in_diff
89
103
  # Taken from https://github.com/ashfurrow/danger-ruby-swiftlint/blob/5184909aab00f12954088684bbf2ce5627e08ed6/lib/danger_plugin.rb#L214-L216
90
104
  renamed_files_hash = git.renamed_files.to_h { |rename| [rename[:before], rename[:after]] }
91
- post_rename_modified_files = git.modified_files.map { |modified_file| renamed_files_hash[modified_file] || modified_file }
105
+ post_rename_modified_files = git.modified_files.map do |modified_file|
106
+ renamed_files_hash[modified_file] || modified_file
107
+ end
92
108
  (post_rename_modified_files - git.deleted_files) + git.added_files
93
109
  end
94
110
 
95
111
  def postprocess(entry, &block)
96
112
  if block
97
- if block.call(entry)
98
- [entry.path, entry.line, entry.column, entry.message]
99
- end
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
100
131
  else
101
- result = @postprocessor.call(entry.path, entry.line, entry.column, entry.message)
102
- if !result
103
- nil
104
- elsif result.kind_of?(TrueClass)
105
- [entry.path, entry.line, entry.column, entry.message]
106
- elsif result.kind_of?(Array) && result.size == 4
107
- result
108
- else
109
- raise "Proc passed to postprocessor must return one of nil, true, false and Array that includes 4 elements."
110
- end
132
+ raise 'Proc passed to postprocessor must return one of nil, true, false and Array that includes 4 elements.'
111
133
  end
112
134
  end
113
135
  end
@@ -1,14 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "pathname"
4
- require "periphery/scan_result"
5
- require "rexml/parsers/baseparser"
6
- require "rexml/parsers/streamparser"
7
- require "rexml/streamlistener"
3
+ require 'pathname'
4
+ require 'periphery/scan_result'
5
+ require 'rexml/parsers/streamparser'
6
+ require 'rexml/streamlistener'
8
7
 
9
8
  module Periphery
9
+ # Parses {https://checkstyle.sourceforge.io/ Checkstyle} format XML produced by Periphery with
10
+ # +--format=checkstyle+ option.
10
11
  class CheckstyleParser
11
- class Listener
12
+ class Listener # :nodoc:
12
13
  include REXML::StreamListener
13
14
 
14
15
  attr_reader :results
@@ -20,22 +21,17 @@ module Periphery
20
21
 
21
22
  def tag_start(name, attrs)
22
23
  case name
23
- when "file"
24
- @current_file = relative_path(attrs["name"])
25
- when "error"
24
+ when 'file'
25
+ @current_file = relative_path(attrs['name'])
26
+ when 'error'
26
27
  if @current_file
27
- @results << ScanResult.new(
28
- @current_file,
29
- attrs["line"].to_i,
30
- attrs["column"].to_i,
31
- attrs["message"]
32
- )
28
+ @results << ScanResult.new(@current_file, attrs['line'].to_i, attrs['column'].to_i, attrs['message'])
33
29
  end
34
30
  end
35
31
  end
36
32
 
37
33
  def tag_end(name)
38
- @current_file = nil if name == "file"
34
+ @current_file = nil if name == 'file'
39
35
  end
40
36
 
41
37
  private
@@ -1,35 +1,32 @@
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
- if status.success?
17
- stdout
18
- else
19
- raise "error: #{arguments} exited with status code #{status.exitstatus}. #{stderr}" unless status.success?
20
- end
16
+ raise "error: #{arguments} exited with status code #{status.exitstatus}. #{stderr}" unless status.success?
17
+
18
+ stdout
21
19
  end
22
20
 
23
21
  def scan_arguments(options)
24
- options.
25
- lazy.
26
- select { |_key, value| value }.
27
- map { |key, value| value.kind_of?(TrueClass) ? [key, nil] : [key, value] }.
28
- map { |key, value| value.kind_of?(Array) ? [key, value.join(",")] : [key, value] }.
29
- map { |key, value| ["--#{key.to_s.tr('_', '-')}", value&.to_s] }.
30
- force.
31
- flatten.
32
- 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
33
30
  end
34
31
  end
35
32
  end
data/lib/periphery.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "periphery/runner"
4
- require "periphery/checkstyle_parser"
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.2.0"
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.2.0
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: 2022-07-22 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,16 @@ 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
- - Gemfile.lock
43
- - Guardfile
44
- - LICENSE.txt
45
- - README.md
46
- - Rakefile
47
- - bin/download_periphery
48
- - danger-periphery.gemspec
49
34
  - lib/danger_plugin.rb
50
35
  - lib/periphery.rb
51
36
  - lib/periphery/checkstyle_parser.rb
52
37
  - lib/periphery/runner.rb
53
38
  - lib/periphery/scan_result.rb
54
39
  - lib/version.rb
55
- - spec/danger_plugin_spec.rb
56
- - spec/periphery/checkstyle_parser_spec.rb
57
- - spec/periphery/runner_spec.rb
58
- - spec/spec_helper.rb
59
- - spec/support/fixtures/github_pr.json
60
- - spec/support/fixtures/mock-periphery
61
- - spec/support/fixtures/test.xcodeproj/project.pbxproj
62
- - spec/support/fixtures/test/main.swift
63
40
  homepage: https://github.com/manicmaniac/danger-periphery
64
41
  licenses:
65
42
  - MIT
@@ -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,9 +0,0 @@
1
- .DS_Store
2
- .idea/
3
- .yardoc
4
- /tmp
5
- bin/lib_InternalSwiftSyntaxParser.dylib
6
- bin/periphery
7
- pkg
8
- project.xcworkspace
9
- xcuserdata
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --color
2
- --require spec_helper
3
- --format documentation