Skip to content
代码片段 群组 项目
未验证 提交 5f503fd2 编辑于 作者: Kerri Miller's avatar Kerri Miller 提交者: GitLab
浏览文件

Suppress script output based on ENV

上级 b1f5e041
No related branches found
No related tags found
无相关合并请求
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
# #
require 'parallel' require 'parallel'
require 'rainbow'
UNUSED_METHODS = 51
print_output = %w[true 1].include? ENV["REPORT_ALL_UNUSED_METHODS"]
start = Process.clock_gettime(Process::CLOCK_MONOTONIC) start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
...@@ -30,7 +35,7 @@ ...@@ -30,7 +35,7 @@
end end
end end
puts "Scanning #{source_files.size} files for #{helpers.size} helpers..." puts "Scanning #{source_files.size} files for #{helpers.size} helpers..." if print_output
# Combine all the source code into one big string, because regex are fast. # Combine all the source code into one big string, because regex are fast.
# #
...@@ -38,18 +43,37 @@ ...@@ -38,18 +43,37 @@
# Iterate over all the helpers and reject any that appear anywhere in the complete source. # Iterate over all the helpers and reject any that appear anywhere in the complete source.
# #
unused = Parallel.flat_map(helpers, progress: 'Checking helpers') do |helper| unused = Parallel.flat_map(helpers, progress: ('Checking helpers' if print_output)) do |helper|
/(?<!def )#{Regexp.quote(helper[:method].sub(/^self\./, ''))}\W/.match?(source_code) ? [] : helper /(?<!def )#{Regexp.quote(helper[:method].sub(/^self\./, ''))}\W/.match?(source_code) ? [] : helper
end end
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) if print_output
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
if unused
puts "\nFound #{unused.size} unused helpers:\n\n"
unused.each { |helper| puts " - [ ] `#{helper[:file]}`: `#{helper[:method]}`" }
puts "\n"
else
puts Rainbow('No unused helpers were found.').green.bright
end
if unused puts "Finished in #{finish - start} seconds."
puts "\nFound #{unused.size} unused helpers:\n\n" exit 0
unused.each { |helper| puts " - [ ] `#{helper[:file]}`: `#{helper[:method]}`" }
puts "\n"
else
puts 'No unused helpers were found.'
end end
puts "Finished in #{finish - start} seconds." if unused.size > UNUSED_METHODS
added = unused.size - UNUSED_METHODS
puts Rainbow("ERROR: #{added} unused methods were added. Please remove them.").red.bright
exit 1
elsif unused.size < UNUSED_METHODS
warning = <<~UPDATE_UNUSED
WARNING: It appears you have removed unused methods. Thank you!
Please update scripts/lint/unused_helper_methods.rb to reflect the new number:
UNUSED_METHODS = #{unused.size}
UPDATE_UNUSED
puts Rainbow(warning).yellow.bright
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册