Sha256: cb2c5bf626cc7c55e4a1abb8040bd3bd675c33e7e449f523a469aebf92c3c8b8

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

module Aai
  module CoreExtensions
    module Time
      def date_and_time fmt="%F %T.%L"
        Object::Time.now.strftime fmt
      end

      def time_it title="", logger=nil, run: true
        if run
          t = Object::Time.now

          yield

          time = Object::Time.now - t

          if title == ""
            msg = "Finished in #{time} seconds"
          else
            msg = "#{title} finished in #{time} seconds"
          end

          if logger
            logger.info msg
          else
            $stderr.puts msg
          end
        end
      end
    end

    module Process
      include CoreExtensions::Time

      def run_it *a, &b
        exit_status, stdout, stderr = systemu *a, &b

        puts stdout unless stdout.empty?
        $stderr.puts stderr unless stderr.empty?

        exit_status.exitstatus
      end

      def run_it! *a, &b
        exit_status = self.run_it *a, &b

        AbortIf.abort_unless exit_status.zero?,
                             "Non-zero exit status " +
                             "(#{exit_status}) " +
                             "when running '#{a.inspect}', " +
                             "'#{b.inspect}'"

        exit_status
      end

      # Examples
      #
      # Process.extend CoreExtensions::Process
      # Time.extend CoreExtensions::Time
      #
      # Process.run_and_time_it! "Saying hello",
      #                          %Q{echo "hello world"}
      #
      # Process.run_and_time_it! "This will raise SystemExit",
      #                          "ls arstoeiarntoairnt" do
      #   puts "i like pie"
      # end
      def run_and_time_it! title="",
                           cmd="",
                           logger=AbortIf::logger,
                           &b

        AbortIf.logger.debug { "Running: #{cmd}" }

        time_it title, logger do
          run_it! cmd, &b
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aai-0.5.3 lib/aai/core_extensions.rb
aai-0.5.2 lib/aai/core_extensions.rb
aai-0.5.1 lib/aai/core_extensions.rb