Class: EacRubyUtils::Envs::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/envs/command.rb,
lib/eac_ruby_utils/envs/command/concat.rb,
lib/eac_ruby_utils/envs/command/envvars.rb,
lib/eac_ruby_utils/envs/command/extra_options.rb

Direct Known Subclasses

Ruby::Command

Defined Under Namespace

Modules: Concat, Envvars, ExtraOptions Classes: ExecuteResult

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Command.



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

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



30
31
32
# File 'lib/eac_ruby_utils/envs/command.rb', line 30

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

#argsObject



26
27
28
# File 'lib/eac_ruby_utils/envs/command.rb', line 26

def args
  @command
end

#command(options = {}) ⇒ Object



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

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_concat(append_envvars(c)))
    ),
    options
  )
end

#execute(options = {}) ⇒ Object



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

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

#execute!(options = {}) ⇒ Object



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

def execute!(options = {})
  options[:exit_outputs] = status_results.merge(options[:exit_outputs].presence || {})
  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



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

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

#spawn(options = {}) ⇒ Object



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

def spawn(options = {})
  c = command(options)
  debug_print("SPAWN: #{c}")
  ::EacRubyUtils::Envs::Spawn.new(c)
end

#system(options = {}) ⇒ Object



83
84
85
86
87
# File 'lib/eac_ruby_utils/envs/command.rb', line 83

def system(options = {})
  c = command(options)
  debug_print(c)
  Kernel.system(c)
end

#system!(options = {}) ⇒ Object



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

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

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

#to_sObject



38
39
40
# File 'lib/eac_ruby_utils/envs/command.rb', line 38

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