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” }
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/agile_utils/helper.rb', line 43 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
23 24 25 |
# File 'lib/agile_utils/helper.rb', line 23 def linux? RbConfig::CONFIG["host_os"] =~ /linux/ end |
.make_list(options) ⇒ Array<String>
Convert the hash options to list for use with Thor
59 60 61 62 63 64 65 |
# File 'lib/agile_utils/helper.rb', line 59 def make_list() list = [] to_switches().split(" ").each do |a| list << a.gsub('"', "") end list end |
.osx? ⇒ Boolean
19 20 21 |
# File 'lib/agile_utils/helper.rb', line 19 def osx? RbConfig::CONFIG["host_os"] =~ /darwin/ end |
.shell(commands = []) ⇒ String
Wrapper function to call the ‘popen3’ and return the result
9 10 11 12 13 14 15 16 17 |
# File 'lib/agile_utils/helper.rb', line 9 def shell(commands = []) begin command = commands.join(" ") stdin, _stderr, _status = Open3.capture3(command) rescue => e raise "Problem processing #{command}, #{e.}" end stdin end |
.time ⇒ Object
For tuning the operation
33 34 35 36 37 38 |
# File 'lib/agile_utils/helper.rb', line 33 def time beg_time = Time.now yield end_time = Time.now end_time - beg_time end |
.uname ⇒ Object
Wrap the call to ‘uname` command
28 29 30 |
# File 'lib/agile_utils/helper.rb', line 28 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
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/agile_utils/helper.rb', line 76 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 |