Class: Afterlife::Exec

Inherits:
Object
  • Object
show all
Defined in:
lib/afterlife/exec.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Exec

Returns a new instance of Exec.



23
24
25
# File 'lib/afterlife/exec.rb', line 23

def initialize(arg)
  @commands = Array(arg)
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



21
22
23
# File 'lib/afterlife/exec.rb', line 21

def commands
  @commands
end

Class Method Details

.parse(arg, env) ⇒ Object



15
16
17
18
19
# File 'lib/afterlife/exec.rb', line 15

def self.parse(arg, env)
  Array(arg.flatten.compact).flatten.map do |command|
    format(command, env.transform_keys(&:to_sym))
  end
end

.result(arg) ⇒ Object



5
6
7
8
9
# File 'lib/afterlife/exec.rb', line 5

def self.result(arg)
  fail Error, 'Exec.result only accepts strings' unless arg.is_a?(String)

  new(arg).run(result: true).first
end

.run(arg) ⇒ Object



11
12
13
# File 'lib/afterlife/exec.rb', line 11

def self.run(arg)
  new(arg).run
end

Instance Method Details

#env_hashObject



47
48
49
# File 'lib/afterlife/exec.rb', line 47

def env_hash
  repo.env.env
end

#parsed_commandsObject



39
40
41
# File 'lib/afterlife/exec.rb', line 39

def parsed_commands
  Exec.parse(commands, env_hash)
end

#repoObject



43
44
45
# File 'lib/afterlife/exec.rb', line 43

def repo
  Afterlife.current_repo
end

#run(result: false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/afterlife/exec.rb', line 27

def run(result: false)
  parsed_commands.map do |command|
    Afterlife.cli.log_info(command) if Afterlife.cli.options['verbose']
    next if Afterlife.cli.options['dry-run']
    next squish(`#{command}`) if result

    system(env_hash, squish(command), exception: true)
  end.compact
rescue RuntimeError => e
  raise Error, e
end