Module: CapFig

Defined in:
lib/capfig.rb

Instance Method Summary collapse

Instance Method Details

#configuration_for(name, settings = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/capfig.rb', line 4

def configuration_for(name, settings = {})
  settings[:path], settings[:filename] = File.split(settings[:filepath]) if settings[:filepath]
  settings[:path] ||= 'config'
  settings[:chmod] ||= 775

  after 'deploy:update_code', "#{name}:symlink"
  before 'deploy:setup', name.to_sym
  namespace(name.to_sym) do    
    desc "Configuration for #{name}"
    task(:default) do
      set(:shared_dir) { File.join(fetch(:shared_path), settings[:path]) }
      set(:shared_file) { File.join(fetch(:shared_dir), settings[:filename]) } if settings[:filename]
      run "mkdir -p #{shared_dir}"
      run "chmod #{settings[:chmod]} #{shared_dir}" if settings[:chmod]
      if settings[:filename]
        settings[:text] = yield if block_given?
        puts settings[:text], shared_file
        run "chmod #{settings[:chmod]} #{shared_file}"
      elsif block_given?
        yield
      end
    end
    sym_path = File.join(File.split("#{settings[:path]}/#{settings[:filename]}"))
    symlink_task name, sym_path, :chmod => settings[:chmod]
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/capfig.rb', line 31

def symlink_task(nspace, path, settings = {})
  settings[:chmod] ||= 775
  desc "Create the symlink for #{File.basename(path)}"
  namespace(nspace.to_sym) do
    task(:symlink) do
      from = File.join(fetch(:shared_path), path)
      to = File.join(fetch(:release_path), path)
      run "mkdir -p #{File.dirname(to)}"
      run "chmod #{settings[:chmod]} #{File.dirname(to)}" if settings[:chmod]
      run "ln -nfs #{from} #{to}"
    end
  end
end