Class: Zenflow::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/zenflow/helpers/shell.rb

Class Method Summary collapse

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

Returns:

  • (Boolean)


10
11
12
# File 'lib/zenflow/helpers/shell.rb', line 10

def failed?
  !!@failed
end

.last_exit_statusObject



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, options={})
  if options[:silent]
    Zenflow::LogToFile("$ #{command}\n")
  else
    Zenflow::Log("$ #{command}", arrows: false, color: :yellow)
  end
  if !failed?
    if options[:silent]
      run_without_output(command, options)
    else
      run_with_output(command, options)
    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, options={})
  output = run_with_result_check(command, options)
  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, options={})
  output = `#{command}`
  Zenflow::LogToFile(output)
  if last_exit_status.to_i > 0 && !options[: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, options={})
  output = ""
  log_stream(STDERR) do
    output = run_with_result_check(command, options)
  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

.statusObject



14
15
16
# File 'lib/zenflow/helpers/shell.rb', line 14

def status
  @status || 0
end