Module: Bj::Util::ModuleMethods

Defined in:
lib/bj/util.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) alive(pid) Also known as: alive?



65
66
67
68
69
70
71
72
# File 'lib/bj/util.rb', line 65

def alive pid
  return false unless pid 
  pid = Integer pid.to_s 
  Process::kill 0, pid
  true
rescue Errno::ESRCH, Errno::EPERM
  false
end

- (Object) const_or_env(const, &block)



18
19
20
# File 'lib/bj/util.rb', line 18

def const_or_env const, &block
  constant_get(const){ ENV[const] || block.call }
end

- (Object) constant_get(const, &block)



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

- (Object) emsg(e)



124
125
126
127
128
129
# File 'lib/bj/util.rb', line 124

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

- (Object) find_script(basename)



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bj/util.rb', line 106

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

- (Object) ipc_signals_supported Also known as: ipc_signals_supported?



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bj/util.rb', line 92

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

- (Object) spawn(cmd, options = {})



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bj/util.rb', line 22

def spawn cmd, options = {}
  options.to_options!
  logger = options.has_key?(:logger) ? options[:logger] : Bj.logger
  verbose = options.has_key? :verbose
  logger.info{ "cmd <#{ cmd }>" } if logger
  status = systemu cmd, 1=>(stdout=""), 2=>(stderr="")
  logger.info{ "status <#{ status.exitstatus }>" } if logger
  if verbose
    if logger
      if stdout.empty?
        logger.info{ "stdout <>" }
      else
        logger.info{ "stdout <" }
        logger << stdout.strip.gsub(%r/^/, '  ')
        logger << "\n>\n"
      end
      if stderr.empty?
        logger.info{ "stderr <>" }
      else
        logger.info{ "stderr <" }
        logger << stderr.strip.gsub(%r/^/, '  ')
        logger << "\n>\n"
      end
    else
      STDOUT.puts stdout
      STDERR.puts stderr
    end
  end
  status.exitstatus.zero? or raise "#{ cmd.inspect } failed with #{ $?.inspect }"
  [ stdout, stderr ]
end

- (Object) start(*a)



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

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

- (Object) valid_rails_root(root = ".",, expected = %w[ config) Also known as: valid_rails_root?



118
119
120
121
# File 'lib/bj/util.rb', line 118

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

- (Object) which_rake



82
83
84
85
86
87
88
89
90
# File 'lib/bj/util.rb', line 82

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

- (Object) which_ruby



75
76
77
78
79
80
# File 'lib/bj/util.rb', line 75

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