Module: Bj::Util::ModuleMethods

Defined in:
lib/bj/util.rb

Instance Method Summary collapse

Instance Method Details

#alive(pid) ⇒ Object Also known as: alive?



32
33
34
35
36
37
38
39
# File 'lib/bj/util.rb', line 32

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

#find_script(basename) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bj/util.rb', line 61

def find_script basename
  path = ENV["PATH"] || ENV["path"]
  raise "no env PATH" unless path
  path = path.split File::PATH_SEPARATOR
  path.push File.join(Bj.rails_root, "script")
  path.each do |directory|
    script = File.join directory, basename
    return File.expand_path(script) if(test(?s, script) and test(?r, script))
  end
  raise "no #{ basename } found in #{ path.inspect }"
end

#ping(pid, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bj/util.rb', line 42

def ping pid, options = {}
  return false unless pid
  pid = Integer pid
  signal = options[:signal] || 'SIGHUP'
  begin
    Process::kill signal, pid
    pid
  rescue Exception 
    false
  end
end

#spawn(cmd, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/bj/util.rb', line 22

def spawn cmd, options = {}
  options.to_options!
  logger = options[: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

#valid_rails_root(root = ".", expected = %w[ config script app ]) ⇒ Object Also known as: valid_rails_root?



73
74
75
76
# File 'lib/bj/util.rb', line 73

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_rubyObject



54
55
56
57
58
59
# File 'lib/bj/util.rb', line 54

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