class Object

Public Instance Methods

action() click to toggle source
# File lib/nite/owl/niteowl.rb, line 30
def action
  Nite::Owl::NiteOwl.instance.current_action()
end
cancel() click to toggle source
# File lib/nite/owl/niteowl.rb, line 38
def cancel
  action.cancel
end
cd(dir) click to toggle source
# File lib/nite/owl/niteowl.rb, line 12
def cd(dir)
  Dir.chdir quote(dir)
  puts "cd #{dir}"
end
delay(time) click to toggle source
# File lib/nite/owl/niteowl.rb, line 34
def delay(time)
  action.delay(time)
end
kill(pids,signal=15) click to toggle source

kill process

# File lib/nite/owl/niteowl.rb, line 157
def kill(pids,signal=15)
  if pids.is_a?(Array)
    pids.each do |pid|
      Process.kill(signal,pid)
    end
  else
    Process.kill(signal,pids)
  end
end
process(name,full=true) click to toggle source

get process pid(s)

# File lib/nite/owl/niteowl.rb, line 134
def process(name,full=true)
  if full
    full = '-f '
  else
    full = ''
  end
  out,_ = Open3.capture2("/usr/bin/pgrep #{full}#{name}")
  if out == ''
    nil
  else
    pids = []
    out.lines.each do |pid|
      pids << pid.to_i
    end
    if pids.size == 1
      pids[0]
    else
      pids
    end
  end
end
pwd() click to toggle source
# File lib/nite/owl/niteowl.rb, line 17
def pwd
  d = Dir.pwd
  puts "pwd: #{d}"
  d
end
quote(path) click to toggle source

Nite Owl DSL

# File lib/nite/owl/niteowl.rb, line 4
def quote(path)
  if path.include? " "
    "\"#{path}\""
  else
    path
  end
end
shell(command,options={:verbose => false,:silent => false,:stdin => nil}) click to toggle source

Run command in shell and redirect it's output to stdout and stderr

# File lib/nite/owl/niteowl.rb, line 43
def shell(command,options={:verbose => false,:silent => false,:stdin => nil})
  stdout = ""
  stderr = ""
  verbose = options[:verbose]
  silent = options[:silent]
  stdin = options[:stdin]
  if verbose
    puts "Executing: #{command}"
  end
  Open3.popen3(command) do |i,o,e,t|
    stdin_i = 0
    stdin_open = stdin != nil
    stdout_buffer = ""
    stderr_buffer = ""
    stdout_open = true
    stderr_open = true
    stdout_ch = nil
    stderr_ch = nil
    while stdout_open or stderr_open or stdin_open
      if stdin_open
        begin
          p stdin
          i.write(stdin)
          i.close_write
          stdin_open = false
          #c = i.write_nonblock(stdin[stdin_i])
          #if c > 0
          #  stdin_i += 1
          #  if stdin_i == stdin.size
          #    i.close_write
          #    stdin_open = false
          #  end
          #end
        rescue IO::WaitWritable
          IO.select([i])
          puts "retry writeable"
          retry
        rescue EOFError
          stdin_open = false
        end
      end
      if stdout_open
        begin
          stdout_ch = o.read_nonblock(1)
        rescue IO::WaitReadable
          IO.select([o])
          retry
        rescue EOFError
          stdout_open = false
        end
      end
      if stderr_open
        begin
          stderr_ch = e.read_nonblock(1)
        rescue IO::WaitReadable
          IO.select([e])
          retry
        rescue EOFError
          stderr_open = false
        end
      end
      if stdout_ch == "\n" then
        stdout += stdout_buffer+"\n"
        unless silent
          puts stdout_buffer
        end
        stdout_buffer = ""
      elsif stdout_ch != nil
        stdout_buffer << stdout_ch
      end
      stdout_ch = nil
      if stderr_ch == "\n" then
        stderr += stderr_buffer+"\n"
        unless silent
          STDERR.puts stderr_buffer
        end
        stderr_buffer = ""
      elsif stderr_ch != nil
        stderr_buffer << stderr_ch
      end
      stderr_ch = nil
    end
  end
  if stderr != ""
    return stdout, stderr
  else
    return stdout
  end
end
whenever(files) click to toggle source
# File lib/nite/owl/niteowl.rb, line 23
def whenever(files)
  if files.is_a?(String) or files.is_a?(Regexp)
    files = [files]
  end
  Nite::Owl::NiteOwl.instance.whenever(files)
end