lib/pdk/cli/exec_group.rb in pdk-1.5.0 vs lib/pdk/cli/exec_group.rb in pdk-1.6.0

- old
+ new

@@ -4,47 +4,61 @@ require 'pdk/util' module PDK module CLI class ExecGroup - attr_reader :commands - def initialize(message, opts = {}) @options = opts.merge(PDK::CLI::Util.spinner_opts_for_platform) if PDK::CLI::Util.interactive? - @multi_spinner = TTY::Spinner::Multi.new("[:spinner] #{message}", @options) - @multi_spinner.auto_spin + @spinner = if parallel? + TTY::Spinner::Multi.new("[:spinner] #{message}", @options) + else + TTY::Spinner.new("[:spinner] #{message}", @options) + end + @spinner.auto_spin end - @threads = [] + @threads_or_procs = [] @exit_codes = [] end - def register + def parallel? + @options[:parallel].nil? ? true : @options[:parallel] + end + + def register(&block) raise PDK::CLI::FatalError, 'No block registered' unless block_given? - @threads << Thread.new do - GettextSetup.initialize(File.absolute_path('../../../locales', File.dirname(__FILE__))) - GettextSetup.negotiate_locale!(GettextSetup.candidate_locales) - @exit_codes << yield - end + @threads_or_procs << if parallel? + Thread.new do + GettextSetup.initialize(File.absolute_path('../../../locales', File.dirname(__FILE__))) + GettextSetup.negotiate_locale!(GettextSetup.candidate_locales) + @exit_codes << yield + end + else + block + end end def add_spinner(message, opts = {}) return unless PDK::CLI::Util.interactive? - @multi_spinner.register("[:spinner] #{message}", @options.merge(opts).merge(PDK::CLI::Util.spinner_opts_for_platform)) + @spinner.register("[:spinner] #{message}", @options.merge(opts).merge(PDK::CLI::Util.spinner_opts_for_platform)) end def exit_code - @threads.each(&:join) + if parallel? + @threads_or_procs.each(&:join) + else + @exit_codes = @threads_or_procs.map(&:call) + end exit_code = @exit_codes.max - if exit_code.zero? && @multi_spinner - @multi_spinner.success - elsif @multi_spinner - @multi_spinner.error + if exit_code.zero? && @spinner + @spinner.success + elsif @spinner + @spinner.error end exit_code end end