Sha256: cfdd6f1f5003d4eb5b17ed4869d3a73006ff7671bac108ab059ba2de53954ef2

Contents?: true

Size: 1.87 KB

Versions: 12

Compression:

Stored size: 1.87 KB

Contents

require 'tty-spinner'
require 'tty-which'

require 'pdk/util'

module PDK
  module CLI
    class ExecGroup
      def initialize(message, opts = {})
        @options = opts.merge(PDK::CLI::Util.spinner_opts_for_platform)

        if PDK::CLI::Util.interactive?
          @spinner = if parallel?
                       TTY::Spinner::Multi.new("[:spinner] #{message}", @options)
                     else
                       TTY::Spinner.new("[:spinner] #{message}", @options)
                     end
          @spinner.auto_spin
        end

        @threads_or_procs = []
        @exit_codes = []
      end

      def parallel?
        @options[:parallel].nil? ? true : @options[:parallel]
      end

      def register(&block)
        raise PDK::CLI::FatalError, 'No block registered' unless block_given?

        @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?
        @spinner.register("[:spinner] #{message}", @options.merge(opts).merge(PDK::CLI::Util.spinner_opts_for_platform))
      end

      def exit_code
        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? && @spinner
          @spinner.success
        elsif @spinner
          @spinner.error
        end

        exit_code
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
pdk-1.11.1 lib/pdk/cli/exec_group.rb
pdk-1.11.0 lib/pdk/cli/exec_group.rb
pdk-1.10.0 lib/pdk/cli/exec_group.rb
pdk-akerl-1.9.1.1 lib/pdk/cli/exec_group.rb
pdk-1.9.1 lib/pdk/cli/exec_group.rb
pdk-1.9.0 lib/pdk/cli/exec_group.rb
pdk-akerl-1.8.0.1 lib/pdk/cli/exec_group.rb
pdk-1.8.0 lib/pdk/cli/exec_group.rb
pdk-1.7.1 lib/pdk/cli/exec_group.rb
pdk-1.7.0 lib/pdk/cli/exec_group.rb
pdk-1.6.1 lib/pdk/cli/exec_group.rb
pdk-1.6.0 lib/pdk/cli/exec_group.rb