Class: Foreplay::Engine::Step

Inherits:
Object
  • Object
show all
Includes:
Foreplay
Defined in:
lib/foreplay/engine/step.rb

Constant Summary

Constants included from Foreplay

DEFAULT_PORT, PORT_GAP, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foreplay

#log, #terminate

Constructor Details

#initialize(h, s, i) ⇒ Step

Returns a new instance of Step.



7
8
9
10
11
12
# File 'lib/foreplay/engine/step.rb', line 7

def initialize(h, s, i)
  @host = h
  @step = s
  @instructions = i
  @redirect = false
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/foreplay/engine/step.rb', line 5

def host
  @host
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



5
6
7
# File 'lib/foreplay/engine/step.rb', line 5

def instructions
  @instructions
end

#stepObject (readonly)

Returns the value of attribute step.



5
6
7
# File 'lib/foreplay/engine/step.rb', line 5

def step
  @step
end

Instance Method Details

#afterObject



100
101
102
# File 'lib/foreplay/engine/step.rb', line 100

def after
  @after ||= step['after'] || ''
end

#announceObject



116
117
118
119
# File 'lib/foreplay/engine/step.rb', line 116

def announce
  log (step['commentary'] || command).yellow.to_s, host: host, silent: silent?
  log command.cyan, host: host, silent: silent? if instructions['verbose'] && step['commentary'] && command
end

#beforeObject



92
93
94
# File 'lib/foreplay/engine/step.rb', line 92

def before
  @before ||= step['before'] || ''
end

#build_commandsObject



29
30
31
32
# File 'lib/foreplay/engine/step.rb', line 29

def build_commands
  step['silent'] = !instructions.key?('verbose')
  instructions[key].is_a?(Hash) ? build_commands_from_hash : build_commands_from_string
end

#build_commands_from_hashObject



34
35
36
# File 'lib/foreplay/engine/step.rb', line 34

def build_commands_from_hash
  delimiter == ': ' ? build_commands_from_hash_to_yaml : build_commands_from_hash_to_env
end

#build_commands_from_hash_to_envObject



52
53
54
55
56
# File 'lib/foreplay/engine/step.rb', line 52

def build_commands_from_hash_to_env
  instructions[key].each do |k, v|
    @commands << "echo \"#{before}#{k}#{delimiter}#{v}#{after}\" #{redirect} #{filename}"
  end
end

#build_commands_from_hash_to_yamlObject



38
39
40
41
42
# File 'lib/foreplay/engine/step.rb', line 38

def build_commands_from_hash_to_yaml
  instructions_yaml.each_line do |l|
    @commands << "echo \"#{l.remove_trailing_newline.escape_double_quotes}\" #{redirect} #{filename}"
  end
end

#build_commands_from_stringObject



58
59
60
# File 'lib/foreplay/engine/step.rb', line 58

def build_commands_from_string
  @commands << "echo \"#{before}#{delimiter}#{instructions[key]}#{after}\" #{redirect} #{filename}"
end

#commandObject



68
69
70
# File 'lib/foreplay/engine/step.rb', line 68

def command
  @command ||= step['command']
end

#commandsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/foreplay/engine/step.rb', line 14

def commands
  return @commands if @commands
  @commands = []

  # Each step can be (1) a command or (2) a series of values to add to a file
  if step.key?('key')
    build_commands if instructions.key?(step['key'])
  else
    # ...or just execute the command specified
    @commands = [command]
  end

  @commands
end

#delimiterObject



96
97
98
# File 'lib/foreplay/engine/step.rb', line 96

def delimiter
  @delimiter ||= step['delimiter'] || ''
end

#filenameObject



72
73
74
# File 'lib/foreplay/engine/step.rb', line 72

def filename
  @filename ||= "#{path}#{prefix}#{key}#{suffix}"
end

#headerObject



104
105
106
# File 'lib/foreplay/engine/step.rb', line 104

def header
  @header ||= step['header']
end

#header?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/foreplay/engine/step.rb', line 108

def header?
  header.present?
end

#instructions_hashObject



44
45
46
# File 'lib/foreplay/engine/step.rb', line 44

def instructions_hash
  header? ? { header => instructions[key] } : instructions[key]
end

#instructions_yamlObject



48
49
50
# File 'lib/foreplay/engine/step.rb', line 48

def instructions_yaml
  YAML.dump instructions_hash
end

#keyObject



76
77
78
# File 'lib/foreplay/engine/step.rb', line 76

def key
  @key ||= step['key']
end

#pathObject



88
89
90
# File 'lib/foreplay/engine/step.rb', line 88

def path
  @path ||= step['path'] || ''
end

#prefixObject



80
81
82
# File 'lib/foreplay/engine/step.rb', line 80

def prefix
  @prefix ||= step['prefix'] || ''
end

#redirectObject



62
63
64
65
66
# File 'lib/foreplay/engine/step.rb', line 62

def redirect
  arrow = @redirect ? '>>' : '>'
  @redirect = true
  arrow
end

#silent?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/foreplay/engine/step.rb', line 112

def silent?
  @silent ||= step['silent']
end

#suffixObject



84
85
86
# File 'lib/foreplay/engine/step.rb', line 84

def suffix
  @suffix ||= step['suffix'] || ''
end