Module: Bj::Util::ModuleMethods

Defined in:
lib/bj/util.rb

Instance Method Summary collapse

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



102
103
104
105
106
107
# File 'lib/bj/util.rb', line 102

def emsg e
  m = e.message rescue ""
  c = e.class rescue Exception
  b = e.backtrace.join("\n") rescue ""
  "#{ m }(#{ c })\n#{ b }"
end

#find_script(basename) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bj/util.rb', line 84

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.expand_path(script) if(test(?s, script) and test(?r, script))
  end
  raise "no #{ basename } found in #{ path.inspect }"
end

#ipc_signals_supportedObject Also known as: ipc_signals_supported?



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bj/util.rb', line 70

def ipc_signals_supported
  @ipc_signals_supported ||=
    IO.popen 'ruby', 'r+' do |ruby|
      pid = ruby.pid
      begin
        Process.kill 'TERM', pid
        true
      rescue Exception
        false
      end
    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.has_key?(: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

#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?



96
97
98
99
# File 'lib/bj/util.rb', line 96

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_rakeObject



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

def which_rake 
  tmp = Tempfile.new Process.pid
  tmp.write "task(:foobar){ puts 42 }"
  tmp.close
  bat = spawn("rake.bat -f #{ tmp.path.inspect } foobar", :logger => false) rescue false
  bat ? "rake.bat" : "rake"
ensure
  tmp.close! rescue nil
end

#which_rubyObject



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