Module: Capistrano::Generals::Helpers

Defined in:
lib/capistrano/generals/helpers.rb

Defined Under Namespace

Classes: ERBNamespace

Instance Method Summary collapse

Instance Method Details

#bundle_puma(*args) ⇒ Object



13
14
15
# File 'lib/capistrano/generals/helpers.rb', line 13

def bundle_puma(*args)
  SSHKit::Command.new(:bundle, :exec, :puma, args).to_command
end

#bundle_sidekiq(*args) ⇒ Object



9
10
11
# File 'lib/capistrano/generals/helpers.rb', line 9

def bundle_sidekiq(*args)
  SSHKit::Command.new(:bundle, :exec, :sidekiq, args).to_command
end

#bundle_unicorn(*args) ⇒ Object



5
6
7
# File 'lib/capistrano/generals/helpers.rb', line 5

def bundle_unicorn(*args)
  SSHKit::Command.new(:bundle, :exec, :unicorn, args).to_command
end

#file_exists?(file_name) ⇒ Boolean

Returns if the file exists The default File.exists? does not work on a remote

Returns:

  • (Boolean)


42
43
44
# File 'lib/capistrano/generals/helpers.rb', line 42

def file_exists?(file_name)
  test("[ -f #{file_name} ]")
end

#get_config_file(file_name, stage) ⇒ Object

Try to find stage specific file, otherwice select default If the default does not exists, abort



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/capistrano/generals/helpers.rb', line 23

def get_config_file(file_name, stage)
  # Default local_file_name is stage specific.
  local_file_name = get_config_file_name(file_name, stage)

  # Check if file exists, else select default file
  # If default file does not exists abort.
  unless file_exists? local_file_name
    unless file_exists? file_name
      abort red "Cannot find file #{local_file_name} or #{file_name} on machine"
    end
    # The default file does exists, selecting that one
    warn "Cannot find stage specific config file #{local_file_name} on machine, taking default #{file_name}"
    local_file_name = file_name
  end
  local_file_name
end

#get_config_file_name(config, stage) ⇒ Object

Get the stage specific name of the file



47
48
49
50
51
52
53
54
55
# File 'lib/capistrano/generals/helpers.rb', line 47

def get_config_file_name(config, stage)
  path = File.dirname(config)
  extension = File.extname(config)
  filename = File.basename(config, extension)
  extension.sub!(/^\./, '')
  local_file = [filename, stage].join('.')
  local_file = [local_file, extension].join('.') unless extension.empty?
  local_path = File.join(path, local_file)
end

#red(text) ⇒ Object



17
18
19
# File 'lib/capistrano/generals/helpers.rb', line 17

def red text
  "\033[31m#{text}\033[0m"
end

#sudo_upload!(from, to) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/capistrano/generals/helpers.rb', line 57

def sudo_upload!(from, to)
  filename = File.basename(to)
  to_dir = File.dirname(to)
  execute :mkdir, '-pv', to_dir
  tmp_file = "#{fetch(:tmp_dir)}/#{filename}"
  upload! from, tmp_file
  sudo :mv, tmp_file, to_dir
end

#template(template_name, locals = {}) ⇒ Object

renders the ERB template specified by template_name to a StringIO buffer



79
80
81
# File 'lib/capistrano/generals/helpers.rb', line 79

def template(template_name, locals = {})
  StringIO.new(template_to_s(template_name, locals))
end

#template_to_s(template_name, locals = {}) ⇒ Object

renders the ERB template specified by template_name to string. Use the locals variable to pass locals to the ERB template



68
69
70
71
72
73
74
75
76
# File 'lib/capistrano/generals/helpers.rb', line 68

def template_to_s(template_name, locals = {})
  config_file = "#{fetch(:templates_path)}/#{template_name}.erb"
  # if no customized file, proceed with default
  unless File.exists?(config_file)
    config_file = File.join(File.dirname(__FILE__), "../../generators/capistrano/generals/templates/#{template_name}.erb")
  end

  ERB.new(File.read(config_file)).result(ERBNamespace.new(locals).get_binding)
end