Top Level Namespace

Defined Under Namespace

Modules: Capup, SSHKit

Instance Method Summary collapse

Instance Method Details

#smart_template(from, to = nil) ⇒ Object

will first try and copy the file: config/deploy/#full_app_name/#from.erb to: shared/config/to if the original source path doesn exist then it will search in: config/deploy/shared/#from.erb this allows files which are common to all enviros to come from a single source while allowing specific ones to be over-ridden if the target file name is the same as the source then the second parameter can be left out



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/capup/template.rb', line 13

def smart_template(from, to=nil)
  to ||= from
  full_to_path = "#{shared_path}/config/#{to.sub(/\.sample$/, '')}"
  if from_erb_path = template_file(from)
    from_erb = StringIO.new(ERB.new(File.read(from_erb_path)).result(binding))
    upload! from_erb, full_to_path
    info "copying: #{from_erb_path} to: #{full_to_path}"
  else
    error "error #{from} not found"
  end
end

#sub_strings(input_string) ⇒ Object

we often want to refer to variables which are defined in subsequent stage files. This let’s us use the {var} to represent fetch(:var) in strings which are only evaluated at runtime.



6
7
8
9
10
11
12
# File 'lib/capup/sub_strings.rb', line 6

def sub_strings(input_string)
  output_string = input_string
  input_string.scan(/{{(\w*)}}/).each do |var|
    output_string.gsub!("{{#{var[0]}}}", fetch(var[0].to_sym))
  end
  output_string
end

#template_file(name) ⇒ Object

find a template file in order.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/capup/template.rb', line 26

def template_file(name)
  app_root = Dir.pwd #may use Rake.application.original_dir
  unless File.exist?(File.join(app_root, 'Capfile'))
    raise "Assume run in app root(there exists a Capfile), but now in #{app_root}!" 
  end
  items = []
  items << "#{app_root}/config/#{name}"
  items << "#{app_root}/config/deploy/#{fetch(:stage)}/#{name}.erb"
  items << "#{app_root}/config/deploy/shared/#{name}.erb"
  tmpl_path = File.join(File.dirname(__FILE__), "templates")
  items << "#{tmpl_path}/#{name}.erb"
  items.each do |f|
    return f if File.exist?(f)
  end
  nil
end