2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2018-06-12 18:17:37 +02:00
|
|
|
|
# Write the given objects to the standard output – except if Rake is configured
|
|
|
|
|
# to be quiet.
|
|
|
|
|
#
|
|
|
|
|
# This is useful when running tests (when Rake is configured to be quiet),
|
|
|
|
|
# to avoid spamming the output with extra informations.
|
|
|
|
|
def rake_puts(*args)
|
|
|
|
|
if Rake.verbose
|
|
|
|
|
puts(*args)
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-12-06 17:02:08 +01:00
|
|
|
|
|
|
|
|
|
def rake_print(*args)
|
|
|
|
|
if Rake.verbose
|
|
|
|
|
print(*args)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-08-24 14:55:03 +02:00
|
|
|
|
# Display progress of a long-running Rake task.
|
|
|
|
|
#
|
|
|
|
|
# Usage:
|
|
|
|
|
#
|
|
|
|
|
# ```
|
|
|
|
|
# progress = ProgressReport.new(100)
|
|
|
|
|
# (0..100).times do
|
|
|
|
|
# progress.inc
|
|
|
|
|
# end
|
|
|
|
|
# progress.finish
|
|
|
|
|
# ````
|
2018-12-06 17:02:08 +01:00
|
|
|
|
class ProgressReport
|
|
|
|
|
def initialize(total)
|
|
|
|
|
@start = Time.zone.now
|
|
|
|
|
rake_puts
|
|
|
|
|
set_progress(total: total, count: 0)
|
|
|
|
|
end
|
|
|
|
|
|
2022-03-09 18:08:03 +01:00
|
|
|
|
def inc(amount = 1)
|
|
|
|
|
set_progress(count: @count + amount)
|
2018-12-06 17:02:08 +01:00
|
|
|
|
if @per_10_000 % 10 == 0
|
|
|
|
|
print_progress
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-16 14:42:16 +01:00
|
|
|
|
def set(count)
|
|
|
|
|
set_progress(count: count)
|
|
|
|
|
|
|
|
|
|
if @per_10_000 % 10 == 0
|
|
|
|
|
print_progress
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-06 17:02:08 +01:00
|
|
|
|
def finish
|
|
|
|
|
if @count > 0 && @per_10_000 != 10_000
|
|
|
|
|
set_progress(total: @count)
|
|
|
|
|
print_progress
|
|
|
|
|
end
|
|
|
|
|
rake_puts
|
|
|
|
|
end
|
|
|
|
|
|
2019-05-29 11:48:41 +02:00
|
|
|
|
private
|
|
|
|
|
|
2018-12-06 17:02:08 +01:00
|
|
|
|
def set_progress(total: nil, count: nil)
|
|
|
|
|
if total.present?
|
|
|
|
|
@total = total
|
|
|
|
|
end
|
|
|
|
|
if count.present?
|
|
|
|
|
@count = count
|
|
|
|
|
@total = [@count, @total].max
|
|
|
|
|
end
|
2018-12-07 16:45:14 +01:00
|
|
|
|
if @total&.nonzero?
|
|
|
|
|
@per_10_000 = 10_000 * @count / @total
|
|
|
|
|
end
|
2018-12-06 17:02:08 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def print_progress
|
|
|
|
|
elapsed = Time.zone.now - @start
|
2018-12-18 21:55:45 +01:00
|
|
|
|
percent = format('%5.1f%%', @per_10_000 / 100.0)
|
2018-12-06 17:02:08 +01:00
|
|
|
|
total = @total.to_s
|
|
|
|
|
count = @count.to_s.rjust(total.length)
|
2018-12-07 16:45:14 +01:00
|
|
|
|
rake_print("\r#{percent} (#{count}/#{total}) [#{format_duration(elapsed)}/#{format_duration(elapsed * 10_000.0 / @per_10_000)}]")
|
2018-12-06 17:02:08 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def format_duration(seconds)
|
2018-12-07 16:45:14 +01:00
|
|
|
|
if seconds.finite?
|
2019-05-29 11:48:21 +02:00
|
|
|
|
Time.zone.at(seconds).utc.strftime('%H:%M:%S')
|
2018-12-07 16:45:14 +01:00
|
|
|
|
else
|
|
|
|
|
'--:--:--'
|
|
|
|
|
end
|
2018-12-06 17:02:08 +01:00
|
|
|
|
end
|
|
|
|
|
end
|