class ShellB::Shell
Attributes
opts[R]
Public Class Methods
alias_command(name, *args)
click to toggle source
# File lib/shellb/shell.rb, line 11 def alias_command(name, *args) Commander.alias_command(name, *args) end
def_system_command(name, path = nil)
click to toggle source
# File lib/shellb/shell.rb, line 7 def def_system_command(name, path = nil) Commander.def_system_command(name, path) end
new(opts = {})
click to toggle source
# File lib/shellb/shell.rb, line 17 def initialize(opts = {}) @commands = [] @opts = opts end
Public Instance Methods
add_command(command)
click to toggle source
# File lib/shellb/shell.rb, line 39 def add_command(command) @commands << command command end
attempt(opts = {}, &block)
click to toggle source
# File lib/shellb/shell.rb, line 35 def attempt(opts = {}, &block) run(opts.merge(ignore_errors: true), &block) end
check_point(opts = {})
click to toggle source
# File lib/shellb/shell.rb, line 48 def check_point(opts = {}) script = Tempfile.new("script.sh") script.write(to_sh(opts)) script.close output = `bash #{script.path}` unless $?.exitstatus.zero? ee = ShellB::ExecutionError.new(output) ee.script = File.read(script) raise ee end rescue ShellB::ExecutionError => ee raise(ee) unless opts[:ignore_errors] ensure @commands = [] script.close! end
Also aliased as: execute
commander()
click to toggle source
# File lib/shellb/shell.rb, line 107 def commander @commander ||= Commander.new(self) end
decorate_command(command, opts)
click to toggle source
# File lib/shellb/shell.rb, line 74 def decorate_command(command, opts) cmd_str = command.to_sh append = if opts[:exit_on_errors] "exit $?" elsif opts[:ignore_errors] "true" else nil end cmd_str = wrap_it(cmd_str, "(") unless command.name == "cd" [cmd_str, append].compact.join(" || ") end
drop_command(command)
click to toggle source
# File lib/shellb/shell.rb, line 44 def drop_command(command) @commands -= [command] end
make_preamble(opts)
click to toggle source
# File lib/shellb/shell.rb, line 92 def make_preamble(opts) preamble = [] if opts[:exit_on_errors] preamble << %w[set -x] preamble << %w[set -e] preamble << [] end preamble = preamble.map { |pream| Shellwords.join(pream) } preamble.join("\n") end
method_missing(meth, *args)
click to toggle source
Calls superclass method
# File lib/shellb/shell.rb, line 87 def method_missing(meth, *args) return super unless commander.respond_to?(meth) add_command(commander.public_send(meth, *args)) end
pretty_print(pp)
click to toggle source
# File lib/shellb/shell.rb, line 111 def pretty_print(pp) pp.object_group(self) do pp.breakable pp.text "@commands=" pp.pp @commands end end
respond_to?(meth)
click to toggle source
Calls superclass method
# File lib/shellb/shell.rb, line 103 def respond_to?(meth) super || commander.respond_to?(meth) end
run(opts = {}, &block)
click to toggle source
# File lib/shellb/shell.rb, line 27 def run(opts = {}, &block) transact(opts.merge(execute: true), &block) end
run!(opts = {}, &block)
click to toggle source
# File lib/shellb/shell.rb, line 31 def run!(opts = {}, &block) run(opts.merge(exit_on_errors: true), &block) end
to_sh(opts = {})
click to toggle source
# File lib/shellb/shell.rb, line 66 def to_sh(opts = {}) str = make_preamble(opts) str += @commands.map do |command| decorate_command(command, opts) end.join("\n\n") str end
transact(opts = {}, &block)
click to toggle source
# File lib/shellb/shell.rb, line 22 def transact(opts = {}, &block) instance_eval(&block) if block check_point(opts) if opts[:execute] end
Private Instance Methods
wrap_it(str, wrap_char)
click to toggle source
# File lib/shellb/shell.rb, line 121 def wrap_it(str, wrap_char) return str if wrap_char.nil? wrap_char = "(" if wrap_char == true end_char = { "(" => ")", "{" => "}", "[" => "]" }[wrap_char] || raise("No matching character for #{wrap_char}") "#{wrap_char} #{str} #{end_char}" end