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.2.4'

Class Method Summary collapse

Class Method Details

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



145
146
147
148
149
150
# File 'lib/rubysh.rb', line 145

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



153
154
155
156
157
158
# File 'lib/rubysh.rb', line 153

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



131
132
133
134
135
136
# File 'lib/rubysh.rb', line 131

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



138
139
140
141
142
143
# File 'lib/rubysh.rb', line 138

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



170
171
172
173
174
175
176
177
# File 'lib/rubysh.rb', line 170

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



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

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

.Command(*args) ⇒ Object



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

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

.FD(*args) ⇒ Object



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

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

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rubysh.rb', line 179

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



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

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

  @log
end

.Pipeline(*args) ⇒ Object



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

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

.run(*args, &blk) ⇒ Object

Convenience methods



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

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

.run_async(*args, &blk) ⇒ Object



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

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

.stderrObject



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

def self.stderr
  FD.new(2)
end

.stdinObject



119
120
121
# File 'lib/rubysh.rb', line 119

def self.stdin
  FD.new(0)
end

.stdoutObject



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

def self.stdout
  FD.new(1)
end