class Object

Public Class Methods

method_missing(m, *args, &block) click to toggle source

Note that I defy convention and don't define `respond_to_missing?`. This is because doing so screws with irb.

Calls superclass method
# File lib/rash.rb, line 207
def self.method_missing(m, *args, &block) 
  exe = which(m.to_s)
  if exe || ($env.alias?(m) && !$env.aliasing_disabled)
    $env.dispatch(m, *args)
  else
    super
  end
end

Public Instance Methods

as_background(&block) click to toggle source
# File lib/rash/jobcontrol.rb, line 17
def as_background(&block)
  $env.async(&block)
end
capture(*cmd, &block) click to toggle source

This explicitly doesn't support pipelining, as the output is ripped out of sequence.

# File lib/rash/capturing.rb, line 37
def capture(*cmd, &block)
  if block_given?
    $env.capture_block(&block)
  elsif cmd.size > 0 && (which(cmd[0]) || ($env.alias?(m) && !$env.aliasing_disabled))
    $env.capture_command(cmd[0].to_s, *cmd[1..])
  else
    raise ArgumentError.new("nothing to capture from")
  end
end
cd(dir = nil) click to toggle source

note for later documentation: any aliases of cd must be functions, not environmental aliases. Limitation of implementation.

# File lib/rash.rb, line 135
def cd(dir = nil)
  d = dir
  case d
  when File, Dir
    d = d.path if File.directory?(d.path)
  end
  $env.chdir(d)
end
in_pipeline(async: true, &block) click to toggle source
# File lib/rash/pipeline.rb, line 192
def in_pipeline(async: true, &block)
  if async
    $env.make_pipeline(&block)
  else
    $env.make_sync_pipeline(&block)
  end
end
popd() click to toggle source
# File lib/rash.rb, line 152
def popd
  $env.pop_dir
end
pushd(dir = nil) click to toggle source
# File lib/rash.rb, line 144
def pushd(dir = nil)
  case dir
  when File, Dir
    dir = dir.path if File.directory(dir.path)
  end
  $env.push_dir(dir)
end
run(file, *args) click to toggle source
# File lib/rash.rb, line 156
def run(file, *args)
  filename = file.to_s
  exe = (filename.start_with?("/") ? filename : File.expand_path(filename.strip))
  unless File.executable?(exe)
    raise SystemCallError.new("No such executable file - #{exe}", Errno::ENOENT::Errno)
  end
  $env.dispatch(exe, *args, literal: true)
end
sourcesh(file) click to toggle source

Defines `bash` psuedo-compatibility. Filesystem effects happen like normal and environmental variable changes are copied

This is an artifact of an old design and is deprecated until further notice.

# File lib/rash.rb, line 171
def sourcesh(file) 
  bash_env = lambda do |cmd = nil|
    tmpenv = `#{cmd + ';' if cmd} printenv`
    tmpenv.split("\n").grep(/[a-zA-Z0-9_]+=.*/).map {|l| l.split("=")}
  end
  bash_source = lambda do |f|
    Hash[bash_env.call("source #{File.realpath f}") - bash_env.()]
  end

  bash_source.call(file).each {|k,v| ENV[k] = v if k != "SHLVL" && k != "_"}
end
which(command) click to toggle source
# File lib/rash.rb, line 184
def which(command)
  cmd = File.expand_path(command.to_s)
  return cmd if File.executable?(cmd) && !File.directory?(cmd)
  
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |pt|
    path = File.expand_path(pt)
    exts.each do |ext|
      exe = File.join(path, "#{command}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end
with_all_out_as(file) { |$stdout| ... } click to toggle source
# File lib/rash/redirection.rb, line 142
def with_all_out_as(file)
  $env.stdout = file
  $env.stderr = $stdout
  if block_given?
    begin
      yield $stdout
    ensure
      $env.reset_stdout
      $env.reset_stderr
    end
  end
end
with_stderr_as(file = STDERR) { |$stderr| ... } click to toggle source
# File lib/rash/redirection.rb, line 98
def with_stderr_as(file = STDERR)
  $env.stderr = file
  if block_given?
    begin
      yield $stderr
    ensure
      $env.reset_stderr
    end
  end 
end
with_stderr_as_stdout() { |$stderr| ... } click to toggle source
# File lib/rash/redirection.rb, line 131
def with_stderr_as_stdout
  $env.stderr = $stdout
  if block_given?
    begin
      yield $stderr
    ensure
      $env.reset_stderr
    end
  end
end
with_stdin_as(file = STDIN) { |$stdin| ... } click to toggle source
# File lib/rash/redirection.rb, line 109
def with_stdin_as(file = STDIN)
  $env.stdin = file
  if block_given?
    begin
      yield $stdin
    ensure
      $env.reset_stdin
    end
  end
end
with_stdout_as(file = STDOUT) { |$stdout| ... } click to toggle source

If you want to append, you need to get the file object yourself. Check if not flushing immediately is a concern. If so, set $stdout.sync for files

# File lib/rash/redirection.rb, line 87
def with_stdout_as(file = STDOUT)
  $env.stdout = file
  if block_given?
    begin
      yield $stdout
    ensure
      $env.reset_stdout
    end
  end
end
with_stdout_as_stderr() { |$stdout| ... } click to toggle source
# File lib/rash/redirection.rb, line 120
def with_stdout_as_stderr
  $env.stdout = $stderr
  if block_given?
    begin
      yield $stdout
    ensure
      $env.reset_stdout
    end
  end
end