Class: EacRubyUtils::Envs::Command

Inherits:
Object
  • Object
show all
Includes:
Console::Speaker
Defined in:
lib/eac_ruby_utils/envs/command.rb

Defined Under Namespace

Classes: ExecuteResult

Instance Method Summary collapse

Methods included from Console::Speaker

#fatal_error, #info, #infov, #out, #puts, #success, #title, #warn

Constructor Details

#initialize(env, command) ⇒ Command

Returns a new instance of Command.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/eac_ruby_utils/envs/command.rb', line 8

def initialize(env, command)
  @env = env
  if command.count == 1 && command.first.is_a?(Array)
    @command = command.first
  elsif command.is_a?(Array)
    @command = command
  else
    raise "Invalid argument command: #{command}|#{command.class}"
  end
  @envvars = {}
end

Instance Method Details

#append(args) ⇒ Object



20
21
22
# File 'lib/eac_ruby_utils/envs/command.rb', line 20

def append(args)
  self.class.new(@env, @command + args)
end

#command(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/eac_ruby_utils/envs/command.rb', line 37

def command(options = {})
  c = @command
  c = c.map { |x| escape(x) }.join(' ') if c.is_a?(Enumerable)
  e = @envvars.map { |k, v| "#{Shellwords.escape(k)}=#{Shellwords.escape(v)}" }.join(' ')
  c = "#{e} #{c}" if e.present?
  c = "#{c} | #{@pipe.command}" if @pipe.present?
  c = @env.command_line(c)
  append_command_options(c, options)
end

#envvar(name, value) ⇒ Object



32
33
34
35
# File 'lib/eac_ruby_utils/envs/command.rb', line 32

def envvar(name, value)
  @envvars[name] = value
  self
end

#execute(options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/eac_ruby_utils/envs/command.rb', line 54

def execute(options = {})
  c = command(options)
  puts "BEFORE: #{c}".light_red if debug?
  t1 = Time.now
  r = ::EacRubyUtils::Envs::Process.new(c).to_h
  i = Time.now - t1
  puts "AFTER [#{i}]: #{c}".light_red if debug?
  r
end

#execute!(options = {}) ⇒ Object



47
48
49
50
51
52
# File 'lib/eac_ruby_utils/envs/command.rb', line 47

def execute!(options = {})
  er = ExecuteResult.new(execute(options), options)
  return er.result if er.success?

  raise "execute! command failed: #{self}\n#{er.r.pretty_inspect}"
end

#pipe(other_command) ⇒ Object



64
65
66
67
# File 'lib/eac_ruby_utils/envs/command.rb', line 64

def pipe(other_command)
  @pipe = other_command
  self
end

#prepend(args) ⇒ Object



24
25
26
# File 'lib/eac_ruby_utils/envs/command.rb', line 24

def prepend(args)
  self.class.new(@env, args + @command)
end

#system(options = {}) ⇒ Object



75
76
77
78
79
# File 'lib/eac_ruby_utils/envs/command.rb', line 75

def system(options = {})
  c = command(options)
  puts c.light_red if debug?
  Kernel.system(c)
end

#system!(options = {}) ⇒ Object



69
70
71
72
73
# File 'lib/eac_ruby_utils/envs/command.rb', line 69

def system!(options = {})
  return if system(options)

  raise "system! command failed: #{self}"
end

#to_sObject



28
29
30
# File 'lib/eac_ruby_utils/envs/command.rb', line 28

def to_s
  "#{@command} [ENV: #{@env}]"
end