Module: Bj::Util::ModuleMethods
- Defined in:
- lib/bj/util.rb
Instance Method Summary collapse
- #alive(pid) ⇒ Object (also: #alive?)
- #const_or_env(const, &block) ⇒ Object
- #constant_get(const, &block) ⇒ Object
- #emsg(e) ⇒ Object
- #find_script(basename) ⇒ Object
- #spawn(cmd, options = {}) ⇒ Object
- #start(*a) ⇒ Object
- #valid_rails_root(root = ".", expected = %w[ config script app ]) ⇒ Object (also: #valid_rails_root?)
- #which_ruby ⇒ Object
Instance Method Details
#alive(pid) ⇒ Object Also known as: alive?
43 44 45 46 47 48 49 50 |
# File 'lib/bj/util.rb', line 43 def alive pid return false unless pid pid = Integer pid.to_s Process::kill 0, pid true rescue Errno::ESRCH, Errno::EPERM false end |
#const_or_env(const, &block) ⇒ Object
18 19 20 |
# File 'lib/bj/util.rb', line 18 def const_or_env const, &block constant_get(const){ ENV[const] || block.call } end |
#constant_get(const, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/bj/util.rb', line 4 def constant_get const, &block begin ancestors = const.split(%r/::/) parent = Object while((child = ancestors.shift)) klass = parent.const_get child parent = klass end klass rescue block ? block.call : raise end end |
#emsg(e) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/bj/util.rb', line 78 def emsg e m = e. rescue "" c = e.class rescue Exception b = e.backtrace.join("\n") rescue "" "#{ m }(#{ c })\n#{ b }" end |
#find_script(basename) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bj/util.rb', line 60 def find_script basename path = ENV["PATH"] || ENV["path"] || Bj.default_path raise "no env PATH" unless path path = path.split File::PATH_SEPARATOR path.unshift File.join(Bj.rails_root, "script") path.each do |directory| script = File.join directory, basename return File.(script) if(test(?s, script) and test(?r, script)) end raise "no #{ basename } found in #{ path.inspect }" end |
#spawn(cmd, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/bj/util.rb', line 22 def spawn cmd, = {} . logger = [:logger] || Bj.logger logger.info{ "cmd <#{ cmd }>" } if logger status = systemu cmd, 1=>(stdout=""), 2=>(stderr="") logger.info{ "status <#{ status.exitstatus }>" } if logger status.exitstatus.zero? or raise "#{ cmd.inspect } failed with #{ $?.inspect }" [ stdout, stderr ] end |
#start(*a) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bj/util.rb', line 32 def start *a q = Queue.new thread = Thread.new do Thread.current.abort_on_exception = true systemu(*a){|pid| q << pid} end pid = q.pop thread.singleton_class{ define_method(:pid){ pid } } thread end |
#valid_rails_root(root = ".", expected = %w[ config script app ]) ⇒ Object Also known as: valid_rails_root?
72 73 74 75 |
# File 'lib/bj/util.rb', line 72 def valid_rails_root root = ".", expected = %w[ config script app ] directories = expected.flatten.compact.map{|dir| dir.to_s} directories.all?{|dir| test(?d, File.join(root, dir))} end |
#which_ruby ⇒ Object
53 54 55 56 57 58 |
# File 'lib/bj/util.rb', line 53 def which_ruby c = ::Config::CONFIG ruby = File::join(c['bindir'], c['ruby_install_name']) << c['EXEEXT'] raise "ruby @ #{ ruby } not executable!?" unless test(?e, ruby) ruby end |