Module: AgileUtils::Helper
- Defined in:
- lib/agile_utils/helper.rb
Class Method Summary collapse
-
.capture(stream) ⇒ Object
Good for capturing output from the :stdout e.g.
- .linux? ⇒ Boolean
-
.make_list(options) ⇒ Array<String>
Convert the hash options to list for use with Thor.
- .osx? ⇒ Boolean
-
.shell(commands = []) ⇒ String
Wrapper function to call the ‘popen3’ and return the result.
-
.time ⇒ Object
For tuning the operation.
-
.uname ⇒ Object
Wrap the call to ‘uname` command.
-
.which(command) ⇒ String, NilClass
Cross-platform way of finding an executable in the $PATH.
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() list = [] to_switches().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
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 |
.time ⇒ Object
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 |
.uname ⇒ Object
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 |