Class: Zenflow::Shell
- Inherits:
-
Object
- Object
- Zenflow::Shell
- Defined in:
- lib/zenflow/helpers/shell.rb
Class Method Summary collapse
- .[](command) ⇒ Object
- .failed!(x) ⇒ Object
- .failed? ⇒ Boolean
- .last_exit_status ⇒ Object
-
.log_stream(stream) ⇒ Object
Stolen from ActiveSupport.
- .run(command, options = {}) ⇒ Object
- .run_with_output(command, options = {}) ⇒ Object
- .run_with_result_check(command, options = {}) ⇒ Object
- .run_without_output(command, options = {}) ⇒ Object
- .shell_escape_for_single_quoting(string) ⇒ Object
- .status ⇒ Object
Class Method Details
.[](command) ⇒ Object
18 19 20 |
# File 'lib/zenflow/helpers/shell.rb', line 18 def [](command) run(command) end |
.failed!(x) ⇒ Object
5 6 7 8 |
# File 'lib/zenflow/helpers/shell.rb', line 5 def failed!(x) @failed = true @status = x end |
.failed? ⇒ Boolean
10 11 12 |
# File 'lib/zenflow/helpers/shell.rb', line 10 def failed? !!@failed end |
.last_exit_status ⇒ Object
68 69 70 |
# File 'lib/zenflow/helpers/shell.rb', line 68 def last_exit_status $? end |
.log_stream(stream) ⇒ Object
Stolen from ActiveSupport
78 79 80 81 82 83 84 85 |
# File 'lib/zenflow/helpers/shell.rb', line 78 def log_stream(stream) old_stream = stream.dup stream.reopen(Zenflow::LOG_PATH, "a") stream.sync = true yield ensure stream.reopen(old_stream) end |
.run(command, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/zenflow/helpers/shell.rb', line 22 def run(command, ={}) if [:silent] Zenflow::LogToFile("$ #{command}\n") else Zenflow::Log("$ #{command}", arrows: false, color: :yellow) end if !failed? if [:silent] run_without_output(command, ) else run_with_output(command, ) end end end |
.run_with_output(command, options = {}) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/zenflow/helpers/shell.rb', line 37 def run_with_output(command, ={}) output = run_with_result_check(command, ) if output.strip != "" puts "#{output.strip}\n" end output end |
.run_with_result_check(command, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/zenflow/helpers/shell.rb', line 53 def run_with_result_check(command, ={}) output = `#{command}` Zenflow::LogToFile(output) if last_exit_status.to_i > 0 && ![:silent] if output.strip != "" puts "#{output.strip}\n" end Zenflow::Log("Process aborted", color: :red) Zenflow::Log("Exit status: #{last_exit_status}", color: :red, indent: true) Zenflow::Log("You may need to run any following commands manually...", color: :red) failed!($?.to_i) end output end |
.run_without_output(command, options = {}) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/zenflow/helpers/shell.rb', line 45 def run_without_output(command, ={}) output = "" log_stream(STDERR) do output = run_with_result_check(command, ) end output end |
.shell_escape_for_single_quoting(string) ⇒ Object
72 73 74 |
# File 'lib/zenflow/helpers/shell.rb', line 72 def shell_escape_for_single_quoting(string) string.gsub("'","'\\\\''") end |
.status ⇒ Object
14 15 16 |
# File 'lib/zenflow/helpers/shell.rb', line 14 def status @status || 0 end |