Sha256: 5d63f290d423847da8d7d9188287c9c19dc5e33bf2d5dffd4b78ad6216d1fab5

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'English'

class ImageOptim
  # Helper for running commands
  module Cmd
    class << self
      # Run using `system`
      # Return success status
      # Will raise SignalException if process was interrupted
      def run(*args)
        if Process.respond_to?(:spawn)
          if args.last.is_a?(Hash)
            args.last[Gem.win_platform? ? :new_pgroup : :pgroup] = true
          end

          begin
            pid = Process.spawn(*args)
          ensure
            yield pid if block_given?
          end

          begin
            Process.waitpid(pid)
          rescue Errno::ECHILD
            return
          end

          check_status!
          $CHILD_STATUS.success?
        else
          success = system(*args)

          check_status!

          success
        end
      end

      # Run using backtick
      # Return captured output
      # Will raise SignalException if process was interrupted
      def capture(cmd)
        output = `#{cmd}`

        check_status!

        output
      end

    private

      def check_status!
        status = $CHILD_STATUS

        return unless status.signaled?

        # jruby incorrectly returns true for `signaled?` if process exits with
        # non zero status. For following code
        #
        #     `sh -c 'exit 66'`
        #     p [$?.signaled?, $?.exitstatus, $?.termsig]
        #
        # jruby outputs `[true, 66, 66]` instead of expected `[false, 66, nil]`
        return if defined?(JRUBY_VERSION) && status.exitstatus == status.termsig

        fail SignalException, status.termsig
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
discourse_image_optim-0.26.2 lib/image_optim/cmd.rb