Method: Sunzi::Command#compile

Defined in:
lib/sunzi/command.rb

#compile(role = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sunzi/command.rb', line 60

def compile(role = nil)
  abort_with 'You must be in a sunzi folder' unless File.exist?('sunzi.yml')
  abort_with "#{role} doesn't exist!" if role && !File.exist?("roles/#{role}.sh")
  abort_with 'As of v2, "attributes" are now "vars" in sunzi.yml and shell scripts.' if config.attributes

  # Retrieve remote recipes via HTTP
  (config.recipes || []).each do |key, value|
    dest = "compiled/recipes/#{key}.sh"
    next if config.preferences.cache_remote_recipes && File.exist?(dest)
    get value, dest
  end

  @vars = config.vars # Used within ERB templates

  # Copy local files to compiled folder
  files = glob('{recipes,roles,files}/**/*').select { |file| File.file?(file) }

  files.each do |file|
    render file, "compiled/#{file}"
  end

  # Copy files specified in sunzi.yml
  (config.files || []).each do |file|
    render file, "compiled/files/#{File.basename(file)}"
  end

  # Build install.sh
  render 'install.sh', 'compiled/install.sh'

  # Append role at the bottom of install.sh
  if role
    append_to_file 'compiled/install.sh', "\n" + File.read("compiled/roles/#{role}.sh")
  end
end