Class: EacRubyUtils::Envs::Command

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

Defined Under Namespace

Modules: ExtraOptions Classes: ExecuteResult

Instance Method Summary collapse

Methods included from ExtraOptions

#chdir, #envvar, #pipe

Methods included from Console::Speaker

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

Constructor Details

#initialize(env, command, extra_options = {}) ⇒ Command

Returns a new instance of Command.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/eac_ruby_utils/envs/command.rb', line 16

def initialize(env, command, extra_options = {})
  @env = env
  @extra_options = extra_options.with_indifferent_access
  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
end

Instance Method Details

#append(args) ⇒ Object



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

def append(args)
  duplicate_by_command(@command + args)
end

#argsObject



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

def args
  @command
end

#command(options = {}) ⇒ Object



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

def command(options = {})
  c = @command
  c = c.map { |x| escape(x) }.join(' ') if c.is_a?(Enumerable)
  append_command_options(
    @env.command_line(
      append_chdir(append_pipe(append_envvars(c)))
    ),
    options
  )
end

#execute(options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/eac_ruby_utils/envs/command.rb', line 62

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



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

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

#prepend(args) ⇒ Object



36
37
38
# File 'lib/eac_ruby_utils/envs/command.rb', line 36

def prepend(args)
  duplicate_by_command(args + @command)
end

#system(options = {}) ⇒ Object



78
79
80
81
82
# File 'lib/eac_ruby_utils/envs/command.rb', line 78

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

#system!(options = {}) ⇒ Object



72
73
74
75
76
# File 'lib/eac_ruby_utils/envs/command.rb', line 72

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

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

#to_sObject



40
41
42
# File 'lib/eac_ruby_utils/envs/command.rb', line 40

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