Module: Rubysh

Defined in:
lib/rubysh.rb,
lib/rubysh/fd.rb,
lib/rubysh/pipe.rb,
lib/rubysh/util.rb,
lib/rubysh/error.rb,
lib/rubysh/runner.rb,
lib/rubysh/command.rb,
lib/rubysh/version.rb,
lib/rubysh/pipeline.rb,
lib/rubysh/redirect.rb,
lib/rubysh/subprocess.rb,
lib/rubysh/base_command.rb,
lib/rubysh/base_directive.rb,
lib/rubysh/triple_less_than.rb

Defined Under Namespace

Modules: Error, Plugin, Util Classes: BaseCommand, BaseDirective, Command, FD, Pipe, Pipeline, Redirect, Runner, Subprocess, TripleLessThan

Constant Summary collapse

VERSION =
'0.3.0'

Class Method Summary collapse

Class Method Details

.<(target = (t=true; nil), opts = (o=true; nil)) ⇒ Object



134
135
136
137
138
139
# File 'lib/rubysh.rb', line 134

def self.<(target=(t=true; nil), opts=(o=true; nil))
  target, opts = handle_redirect_args(target, t, opts, o)
  target ||= :stdin

  Redirect.new(0, '<', target, opts)
end

.<<(fd = (f=true; nil), opts = (o=true; nil)) ⇒ Object

Hack to implement <<<



142
143
144
145
146
147
# File 'lib/rubysh.rb', line 142

def self.<<(fd=(f=true; nil), opts=(o=true; nil))
  fd, opts = handle_redirect_args(fd, f, opts, o)
  fd ||= FD.new(0)

  TripleLessThan::Shell.new(fd, opts)
end

.>(target = (t=true; nil), opts = (o=true; nil)) ⇒ Object



120
121
122
123
124
125
# File 'lib/rubysh.rb', line 120

def self.>(target=(t=true; nil), opts=(o=true; nil))
  target, opts = handle_redirect_args(target, t, opts, o)
  target ||= :stdout

  Redirect.new(1, '>', target, opts)
end

.>>(target = (t=true; nil), opts = (o=true; nil)) ⇒ Object



127
128
129
130
131
132
# File 'lib/rubysh.rb', line 127

def self.>>(target=(t=true; nil), opts=(o=true; nil))
  target, opts = handle_redirect_args(target, t, opts, o)
  target ||= :stdout

  Redirect.new(1, '>>', target, opts)
end

.assert(fact, msg, hard = false) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/rubysh.rb', line 159

def self.assert(fact, msg, hard=false)
  return if fact

  msg = msg ? "Assertion Failure: #{msg}" : "Assertion Failure"
  formatted = "#{msg}\n  #{caller.join("\n  ")}"
  log.error(formatted)
  raise msg if hard
end

.check_call(*args, &blk) ⇒ Object



91
92
93
94
# File 'lib/rubysh.rb', line 91

def self.check_call(*args, &blk)
  command = Rubysh::Command.new(args, &blk)
  command.check_call
end

.Command(*args, &blk) ⇒ Object



96
97
98
# File 'lib/rubysh.rb', line 96

def self.Command(*args, &blk)
  Command.new(*args, &blk)
end

.FD(*args) ⇒ Object



104
105
106
# File 'lib/rubysh.rb', line 104

def self.FD(*args)
  FD.new(*args)
end

.handle_redirect_args(target, target_omitted, opts, opts_omitted) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rubysh.rb', line 168

def self.handle_redirect_args(target, target_omitted, opts, opts_omitted)
  if opts_omitted && target.kind_of?(Hash)
    # Shift over if user provided target as a hash but omitted opts.
    opts = target
    opts_omitted = target_omitted

    target = nil
    target_omitted = true
  end

  # User provided a false-y value for target. This probably
  # indicates a bug in the user's code, where a variable is
  # accidentally nil.
  if !target_omitted && !target
    raise Rubysh::Error::BaseError.new("You provided #{target.inspect} as your redirect target. This probably indicates a bug in your code. Either omit the target argument or provide a non-false-y value for it.")
  end

  return target, opts
end

.logObject

Internal utility methods



150
151
152
153
154
155
156
157
# File 'lib/rubysh.rb', line 150

def self.log
  unless @log
    @log = Logger.new(STDERR)
    @log.level = Logger::WARN
  end

  @log
end

.Pipeline(*args) ⇒ Object



100
101
102
# File 'lib/rubysh.rb', line 100

def self.Pipeline(*args)
  Pipeline.new(*args)
end

.run(*args, &blk) ⇒ Object

Convenience methods



81
82
83
84
# File 'lib/rubysh.rb', line 81

def self.run(*args, &blk)
  command = Rubysh::Command.new(args, &blk)
  command.run
end

.run_async(*args, &blk) ⇒ Object



86
87
88
89
# File 'lib/rubysh.rb', line 86

def self.run_async(*args, &blk)
  command = Rubysh::Command.new(args, &blk)
  command.run_async
end

.stderrObject



116
117
118
# File 'lib/rubysh.rb', line 116

def self.stderr
  FD.new(2)
end

.stdinObject



108
109
110
# File 'lib/rubysh.rb', line 108

def self.stdin
  FD.new(0)
end

.stdoutObject



112
113
114
# File 'lib/rubysh.rb', line 112

def self.stdout
  FD.new(1)
end