Class: ShellB::Shell
- Inherits:
-
Object
show all
- Defined in:
- lib/shellb/shell.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opts = {}) ⇒ Shell
Returns a new instance of Shell.
17
18
19
20
|
# File 'lib/shellb/shell.rb', line 17
def initialize(opts = {})
@commands = []
@opts = opts
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
87
88
89
90
|
# 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
|
Instance Attribute Details
#opts ⇒ Object
Returns the value of attribute opts.
16
17
18
|
# File 'lib/shellb/shell.rb', line 16
def opts
@opts
end
|
Class Method Details
.alias_command(name, *args) ⇒ Object
11
12
13
|
# File 'lib/shellb/shell.rb', line 11
def alias_command(name, *args)
Commander.alias_command(name, *args)
end
|
.def_system_command(name, path = nil) ⇒ Object
Instance Method Details
#add_command(command) ⇒ Object
39
40
41
42
|
# File 'lib/shellb/shell.rb', line 39
def add_command(command)
@commands << command
command
end
|
#attempt(opts = {}, &block) ⇒ Object
35
36
37
|
# File 'lib/shellb/shell.rb', line 35
def attempt(opts = {}, &block)
run(opts.merge(ignore_errors: true), &block)
end
|
#check_point(opts = {}) ⇒ Object
Also known as:
execute
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# 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
|
#commander ⇒ Object
107
108
109
|
# File 'lib/shellb/shell.rb', line 107
def commander
@commander ||= Commander.new(self)
end
|
#decorate_command(command, opts) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
|
# 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) ⇒ Object
44
45
46
|
# File 'lib/shellb/shell.rb', line 44
def drop_command(command)
@commands -= [command]
end
|
#make_preamble(opts) ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# 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
|
#pretty_print(pp) ⇒ Object
111
112
113
114
115
116
117
|
# 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) ⇒ Boolean
103
104
105
|
# File 'lib/shellb/shell.rb', line 103
def respond_to?(meth)
super || commander.respond_to?(meth)
end
|
#run(opts = {}, &block) ⇒ Object
27
28
29
|
# File 'lib/shellb/shell.rb', line 27
def run(opts = {}, &block)
transact(opts.merge(execute: true), &block)
end
|
#run!(opts = {}, &block) ⇒ Object
31
32
33
|
# File 'lib/shellb/shell.rb', line 31
def run!(opts = {}, &block)
run(opts.merge(exit_on_errors: true), &block)
end
|
#to_sh(opts = {}) ⇒ Object
66
67
68
69
70
71
72
|
# 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) ⇒ Object
22
23
24
25
|
# File 'lib/shellb/shell.rb', line 22
def transact(opts = {}, &block)
instance_eval(&block) if block
check_point(opts) if opts[:execute]
end
|