Module: AgileUtils::Helper

Defined in:
lib/agile_utils/helper.rb

Class Method Summary collapse

Class Method Details

.capture(stream) ⇒ Object

Good for capturing output from the :stdout e.g. sample usage output = capture(:stdout) { puts “Hello” }



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/agile_utils/helper.rb', line 45

def capture(stream)
  begin
    stream = stream.to_s
    eval "$#{stream} = StringIO.new"
    yield
    result = eval("$#{stream}").string
  ensure
    eval("$#{stream} = #{stream.upcase}")
  end
  result
end

.linux?Boolean



25
26
27
# File 'lib/agile_utils/helper.rb', line 25

def linux?
  RbConfig::CONFIG["host_os"] =~ /linux/
end

.make_list(options) ⇒ Array<String>

Convert the hash options to list for use with Thor



61
62
63
64
65
66
67
# File 'lib/agile_utils/helper.rb', line 61

def make_list(options)
  list = []
  to_switches(options).split(" ").each do |a|
    list << a.gsub('"', "")
  end
  list
end

.osx?Boolean



21
22
23
# File 'lib/agile_utils/helper.rb', line 21

def osx?
  RbConfig::CONFIG["host_os"] =~ /darwin/
end

.shell(commands = []) ⇒ String

Wrapper function to call the ‘popen3’ and return the result

Raises:

  • (RuntimeError)

    an exception to raise when the command result in error



11
12
13
14
15
16
17
18
19
# File 'lib/agile_utils/helper.rb', line 11

def shell(commands = [])
  begin
    command = commands.join(" ")
    stdin, _stderr, _status = Open3.capture3(command)
  rescue => e
    raise "Problem processing #{command}, #{e.message}"
  end
  stdin
end

.timeObject

For tuning the operation



35
36
37
38
39
40
# File 'lib/agile_utils/helper.rb', line 35

def time
  beg_time = Time.now
  yield
  end_time = Time.now
  end_time - beg_time
end

.unameObject

Wrap the call to ‘uname` command



30
31
32
# File 'lib/agile_utils/helper.rb', line 30

def uname
  shell(%w[uname])
end

.which(command) ⇒ String, NilClass

Cross-platform way of finding an executable in the $PATH.

Example:

which('ruby')           #=> /usr/bin/ruby
which('/usr/bin/ruby')  #=> nil
which('bad-executable') #=> nil


78
79
80
81
82
83
84
85
86
87
# File 'lib/agile_utils/helper.rb', line 78

def which(command)
  exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{command}#{ext}")
      return exe if File.executable? exe
    end
  end
  nil
end