discourse/migrations/lib/common/extended_progress_bar.rb
Gerhard Schlager 89f26da39d
MT: Switch to nested module style across migrations/ (#38564)
Ruby's compact module syntax (`module
Migrations::Database::Schema::DSL`) breaks lexical constant lookup —
`Module.nesting` only includes the innermost constant, so every
cross-module reference must be fully qualified. In practice this means
writing `Migrations::Database::Schema::Helpers` even when you're already
inside `Migrations::Database::Schema`.

Nested module definitions restore the full nesting chain, which brings
several practical benefits:

- **Less verbose code**: references like `Schema::Helpers`,
`Database::IntermediateDB`, or `Converters::Base::ProgressStep` work
without repeating the full path from root
- **Easier to write new code**: contributors don't need to remember
which prefixes are required — if you're inside the namespace, short
names just work
- **Fewer aliasing workarounds**: removes the need for constants like
`MappingType = Migrations::Importer::MappingType` that existed solely to
shorten references
- **Standard Ruby style**: consistent with how most Ruby projects and
gems structure their namespaces

The diff is large but mechanical — no logic changes, just module
wrapping and shortening references that the nesting now resolves.
Generated code (intermediate_db models/enums) keeps fully qualified
references like `Migrations::Database.format_*` since it must work
regardless of the configured output namespace.

- Convert 138 lib files from compact to nested module definitions
- Remove now-redundant fully qualified prefixes and aliases
- Update model and enum writers to generate nested modules with correct
indentation
- Regenerate all intermediate_db models and enums
2026-03-19 18:15:19 +01:00

120 lines
2.8 KiB
Ruby

# frozen_string_literal: true
require "colored2"
require "ruby-progressbar"
module Migrations
class ExtendedProgressBar
def initialize(max_progress: nil)
@max_progress = max_progress
@skip_count = 0
@warning_count = 0
@error_count = 0
@extra_information = +""
@base_format = nil
@progressbar = nil
end
def run
raise "ProgressBar already started" if @progressbar
format = calculate_format
setup_progressbar
yield self
finalize_progressbar(format)
nil
end
def update(increment_by:, skip_count: 0, warning_count: 0, error_count: 0)
updated = false
if skip_count > 0
@skip_count += skip_count
updated = true
end
if warning_count > 0
@warning_count += warning_count
updated = true
end
if error_count > 0
@error_count += error_count
updated = true
end
update_format if updated
if increment_by == 1
@progressbar.increment
else
@progressbar.progress += increment_by
end
end
private
def calculate_format
if @max_progress
format = I18n.t("progressbar.processed.progress_with_max", current: "%c", max: "%C")
@base_format = " %a | %E | #{format}"
else
format = I18n.t("progressbar.processed.progress", current: "%c")
@base_format = " %a | #{format}"
end
format
end
def setup_progressbar
@progressbar =
ProgressBar.create(
total: @max_progress,
autofinish: false,
projector: {
type: "smoothing",
strength: 0.5,
},
format: @base_format,
throttle_rate: 0.5,
)
end
def update_format
@extra_information.clear
messages = []
messages << I18n.t("progressbar.skips", count: @skip_count).cyan if @skip_count > 0
messages << I18n.t("progressbar.warnings", count: @warning_count).yellow if @warning_count > 0
messages << I18n.t("progressbar.errors", count: @error_count).red if @error_count > 0
@extra_information << " | #{messages.join(" | ")}" unless messages.empty?
@progressbar.format = "#{@base_format}#{@extra_information}"
end
def finalize_progressbar(format)
print "\033[K" # delete the output of progressbar, because it doesn't overwrite longer lines
@progressbar.format = " %a | #{format}#{@extra_information}"
@progressbar.finish
end
end
end
class ProgressBar
module Components
class Time
def estimated_with_label(out_of_bounds_time_format = nil)
I18n.t("progressbar.estimated", duration: estimated(out_of_bounds_time_format))
end
def elapsed_with_label
I18n.t("progressbar.elapsed", duration: elapsed)
end
end
end
end