Sha256: 5c4ad1bbcfa76e7529029f5b8baa9538e32942153431936375bc983a6048ac49

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'fileutils'
require 'shellwords'

module SCM
  module Util
    protected

    #
    # Runs a program.
    #
    # @param [String, Symbol] program
    #   The name or path of the program.
    #
    # @param [Array] arguments
    #   Optional arguments for the program.
    #
    # @return [Boolean]
    #   Specifies whether the program exited successfully.
    #
    def run(program,*arguments)
      arguments = arguments.map { |arg| arg.to_s }

      # filter out empty Strings
      arguments.reject! { |arg| arg.empty? }

      system(program.to_s,*arguments)
    end

    #
    # Runs a command as a separate process.
    #
    # @param [String] command
    #   The command to run.
    #
    # @param [Array] arguments
    #   Additional arguments for the command.
    #
    # @yield [line]
    #   The given block will be passed each line read-in.
    #
    # @yieldparam [String] line
    #   A line read from the program.
    #
    # @return [IO]
    #   The stdout of the command being ran.
    #
    def popen(command,*arguments)
      unless arguments.empty?
        command = command.dup

        arguments.each do |arg|
          command << ' ' << Shellwords.shellescape(arg.to_s)
        end
      end

      io = IO.popen(command)

      if block_given?
        io.each_line do |line|
          line.chomp!
          yield line
        end
      end

      return io
    end

    #
    # Read lines until a separator line is encountered.
    #
    # @param [IO] io
    #   The IO stream to read from.
    #
    # @param [String] separator
    #   The separator line to stop at.
    #
    # @return [Array<String>]
    #   The read lines.
    #
    def readlines_until(io,separator='')
      lines = []

      until io.eof?
        line = io.readline
        line.chomp!

        break if line == separator

        lines << line
      end

      return lines
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scm-0.1.0.pre2 lib/scm/util.rb