danger-periphery 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15302ed92faf0a3230ddcb167fe3db0ae2e8c1a51d6551d85a4c2cc37c760dd4
4
- data.tar.gz: 18880214efa7cb5a29ee9b6872688a6eeb19d805e07cad6c445afeeb24f4cad2
3
+ metadata.gz: 9c9c3f228ac9ddf39d67684faca46dfaf969f9576a566a045b2ef0d50a714473
4
+ data.tar.gz: 7c60528ef9459c67a57c74566f6c76beb8871e04557afde65c8d8a05ffc81db1
5
5
  SHA512:
6
- metadata.gz: 5e33a6b9fa62d733004fb63337fdff4973dd3b38d40a096db0b84a1249bc7789a3612a99a88d31020e22dd6494c3b7d3c953fe721be16347e825a58e8900a177
7
- data.tar.gz: 47a37136e81dc9c4b6fc6b3331e476810e4520503c7fd062714363675926c51525502dc58ef2d610b890ee0d73d731d8ac87910bd8e6ab2f051f47c980b4ac88
6
+ metadata.gz: 5f51ab7485b787644820d4df9a87a4b9f2d52be587792c03271a9f2defe8710697f1bc10578fa6ecf49bd2bc596807e79606c6ec888851bf6a8833c9a18a24a5
7
+ data.tar.gz: 92db186ee32f74f37d38b99787338bfae9101b5b4971f41a051a3bf0e19f2b888db542a735ecb6c9fefe87d7bd88f40d33df1703b9b8782b08c8460bda9f103a
data/lib/danger_plugin.rb CHANGED
@@ -23,6 +23,8 @@ module Danger
23
23
  # @return [String]
24
24
  attr_accessor :binary_path
25
25
 
26
+ # @deprecated Use {#scan} with block instead.
27
+ #
26
28
  # Proc object to process each warnings just before showing them.
27
29
  # The Proc must receive 4 arguments: path, line, column, message
28
30
  # and return one of:
@@ -36,17 +38,22 @@ module Danger
36
38
  #
37
39
  # By default the Proc returns true.
38
40
  # @return [Proc]
39
- attr_accessor :postprocessor
41
+ attr_reader :postprocessor
42
+
43
+ # For internal use only.
44
+ #
45
+ # @return [Symbol]
46
+ attr_writer :format
40
47
 
41
48
  OPTION_OVERRIDES = {
42
49
  disable_update_check: true,
43
- format: 'checkstyle',
44
50
  quiet: true
45
51
  }.freeze
46
52
 
47
53
  def initialize(dangerfile)
48
54
  super(dangerfile)
49
55
  @postprocessor = ->(_path, _line, _column, _message) { true }
56
+ @format = :checkstyle
50
57
  end
51
58
 
52
59
  # Scans Swift files.
@@ -54,7 +61,7 @@ module Danger
54
61
  #
55
62
  # @example Ignore all warnings from files matching regular expression
56
63
  # periphery.scan do |violation|
57
- # ! violation.path.match(/.*\/generated\.swift/)
64
+ # !violation.path.match(/.*\/generated\.swift/)
58
65
  # end
59
66
  #
60
67
  # @param [Hash] options Options passed to Periphery with the following translation rules.
@@ -71,10 +78,10 @@ module Danger
71
78
  # suppressed, otherwise not.
72
79
  #
73
80
  # @return [void]
74
- def scan(**options, &block)
75
- output = Periphery::Runner.new(binary_path).scan(options.merge(OPTION_OVERRIDES))
81
+ def scan(options = {}, &block)
82
+ output = Periphery::Runner.new(binary_path).scan(options.merge(OPTION_OVERRIDES).merge(format: @format))
76
83
  files = files_in_diff
77
- Periphery::CheckstyleParser.new.parse(output).each do |entry|
84
+ parser.parse(output).each do |entry|
78
85
  next unless files.include?(entry.path)
79
86
 
80
87
  result = postprocess(entry, &block)
@@ -85,18 +92,52 @@ module Danger
85
92
  end
86
93
  end
87
94
 
95
+ # @deprecated Use {#scan} with block instead.
96
+ #
88
97
  # Convenience method to set {#postprocessor} with block.
89
98
  #
90
99
  # @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
100
  def process_warnings(&block)
101
+ deprecate_in_favor_of_scan
97
102
  @postprocessor = block
98
103
  end
99
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
+
100
141
  private
101
142
 
102
143
  def files_in_diff
@@ -132,5 +173,29 @@ module Danger
132
173
  raise 'Proc passed to postprocessor must return one of nil, true, false and Array that includes 4 elements.'
133
174
  end
134
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
135
200
  end
136
201
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'net/http'
5
+ require 'open-uri'
6
+ require 'rubygems/version'
7
+ require 'zip'
8
+
9
+ module Periphery
10
+ # Downloads Periphery binary executable and install it to the specified path.
11
+ class Installer
12
+ def initialize(version_spec)
13
+ @version_spec = version_spec
14
+ end
15
+
16
+ def install(dest_path, force: false)
17
+ URI.parse(download_url).open do |src|
18
+ entry = Zip::File.open_buffer(src).get_entry('periphery')
19
+ entry.restore_permissions = true
20
+ FileUtils.rm_f(dest_path) if force
21
+ entry.extract(dest_path)
22
+ end
23
+ end
24
+
25
+ def version
26
+ @version ||= @version_spec == :latest ? fetch_latest_version : @version_spec
27
+ end
28
+
29
+ private
30
+
31
+ def download_url
32
+ if Gem::Version.new(version) >= Gem::Version.new('2.14.0')
33
+ "https://github.com/peripheryapp/periphery/releases/download/#{version}/periphery-#{version}.zip"
34
+ else
35
+ "https://github.com/peripheryapp/periphery/releases/download/#{version}/periphery-v#{version}.zip"
36
+ end
37
+ end
38
+
39
+ def fetch_latest_version
40
+ Net::HTTP.start('github.com', port: 443, use_ssl: true) do |http|
41
+ response = http.head('/peripheryapp/periphery/releases/latest')
42
+ URI.parse(response['location']).path.split('/').last
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'periphery/scan_result'
5
+
6
+ module Periphery
7
+ # Parses JSON formatted output produced by Periphery with +--format=json+ option.
8
+ class JsonParser
9
+ def parse(string)
10
+ JSON.parse(string).map do |entry|
11
+ path, line, column = parse_location(entry['location'])
12
+ message = compose_message(*entry.slice('name', 'kind', 'hints', 'modules').values)
13
+ ScanResult.new(path, line, column, message)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def relative_path(path, base = Pathname.getwd)
20
+ Pathname.new(path).relative_path_from(base).to_s
21
+ end
22
+
23
+ # Parses a string like '/path/to/file.swift:19:10'
24
+ def parse_location(location)
25
+ location.scan(/^(.+):(\d+):(\d+)$/) do |path, line, column|
26
+ return [relative_path(path), line.to_i, column.to_i]
27
+ end
28
+ raise ArgumentError, "#{location} is not in a valid format"
29
+ end
30
+
31
+ def compose_message(name, kind, hints, modules = [])
32
+ return 'unused' unless name
33
+
34
+ # Assumes hints contains only one item.
35
+ # https://github.com/peripheryapp/periphery/blob/2.9.0/Sources/Frontend/Formatters/JsonFormatter.swift#L27
36
+ # https://github.com/peripheryapp/periphery/blob/2.9.0/Sources/Frontend/Formatters/JsonFormatter.swift#L42
37
+ hint = hints[0]
38
+ +"#{display_name(kind).capitalize} '#{name}' #{describe_hint(hint, modules)}"
39
+ end
40
+
41
+ def display_name(kind)
42
+ case kind
43
+ when 'enumelement' then 'enum case'
44
+ when 'function.constructor' then 'initializer'
45
+ when 'var.parameter' then 'parameter'
46
+ when 'generic_type_param' then 'generic type parameter'
47
+ when nil then ''
48
+ else kind.start_with?('var') ? 'property' : kind.split('.', 2)[0]
49
+ end
50
+ end
51
+
52
+ def describe_hint(hint, modules)
53
+ case hint
54
+ when 'unused' then 'is unused'
55
+ when 'assignOnlyProperty' then 'is assigned, but never used'
56
+ when 'redundantProtocol' then "is redundant as it's never used as an existential type"
57
+ when 'redundantConformance' then 'conformance is redundant'
58
+ when 'redundantPublicAccessibility' then "is declared public, but not used outside of #{modules.join(', ')}"
59
+ else ''
60
+ end
61
+ end
62
+ end
63
+ end
data/lib/periphery.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'periphery/installer'
3
4
  require 'periphery/runner'
4
5
  require 'periphery/checkstyle_parser'
6
+ require 'periphery/json_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.1'
4
+ VERSION = '0.2.3'
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.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryosuke Ito
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-29 00:00:00.000000000 Z
11
+ date: 2023-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
27
41
  description: A Danger plugin to detect unused codes.
28
42
  email:
29
43
  - rito.0305@gmail.com
@@ -34,6 +48,8 @@ files:
34
48
  - lib/danger_plugin.rb
35
49
  - lib/periphery.rb
36
50
  - lib/periphery/checkstyle_parser.rb
51
+ - lib/periphery/installer.rb
52
+ - lib/periphery/json_parser.rb
37
53
  - lib/periphery/runner.rb
38
54
  - lib/periphery/scan_result.rb
39
55
  - lib/version.rb
@@ -42,7 +58,7 @@ licenses:
42
58
  - MIT
43
59
  metadata:
44
60
  rubygems_mfa_required: 'true'
45
- post_install_message:
61
+ post_install_message:
46
62
  rdoc_options: []
47
63
  require_paths:
48
64
  - lib
@@ -50,15 +66,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
66
  requirements:
51
67
  - - ">="
52
68
  - !ruby/object:Gem::Version
53
- version: 2.6.0
69
+ version: 2.7.0
54
70
  required_rubygems_version: !ruby/object:Gem::Requirement
55
71
  requirements:
56
72
  - - ">="
57
73
  - !ruby/object:Gem::Version
58
74
  version: '0'
59
75
  requirements: []
60
- rubygems_version: 3.0.3.1
61
- signing_key:
76
+ rubygems_version: 3.4.6
77
+ signing_key:
62
78
  specification_version: 4
63
79
  summary: A Danger plugin to detect unused codes.
64
80
  test_files: []