class ConfigFor::Capistrano::UploadFileTask

Attributes

generator[RW]
path[R]
tempfile[R]

Public Class Methods

new(path, options = {}, &block) click to toggle source

Rake Task generator for uploading files through Capistrano

@example generating task for config/unicorn.rb

ConfigFor::Capistrano::UploadFileTask.new('config/unicorn.rb', roles: :web) do |file|
  file.write('some template')
end

Rake::Task['config/unicorn.rb'].invoke # uploads that file if it does not exist

@param [Pathname, String] path the path of the file to be uploaded @param [Hash] options the options @option options [Array<Symbol>,Symbol] :roles (:all) the roles of servers to apply to @option options [true,false] :override (false) upload file on every run @yieldparam [Tempfile] file yields the tempfile so you generate the file to be uploaded

# File lib/config_for/capistrano.rb, line 30
def initialize(path, options = {}, &block)
  @path = path
  @roles = options.fetch(:roles, :all)
  @override = options.fetch(:override, false)
  @tempfile = ::Tempfile.new(File.basename(@path))
  @generator = block || ->(_file){ puts 'Did not passed file generator' }

  define
end

Private Instance Methods

define() click to toggle source
# File lib/config_for/capistrano.rb, line 44
def define
  desc "Upload file to #{path}"
  remote_file(path => @tempfile.path, roles: @roles, override: @override)
  desc "Generate file #{@path} to temporary location"

  generate_file(@tempfile.path, &method(:generate))
end
generate(*) click to toggle source
# File lib/config_for/capistrano.rb, line 52
def generate(*)
  @generator.call(@tempfile)
  @tempfile.close(false)
end
generate_file(task, &block) click to toggle source

So it is always generated, because it is always needed

# File lib/config_for/capistrano.rb, line 58
def generate_file(task, &block)
  GenerateFileTask.define_task(task, &block)
end
remote_file(task) click to toggle source

TODO: can be removed when github.com/capistrano/capistrano/pull/1144 is merged and released

# File lib/config_for/capistrano.rb, line 68
def remote_file(task)
  target_roles = task.delete(:roles)
  override = task.delete(:override)

  UploadTask.define_task(task) do |t|
    prerequisite_file = t.prerequisites.first
    file = shared_path.join(t.name).to_s.shellescape

    on roles(target_roles) do
      if override || !test("[ -f #{file} ]")
        info "Uploading #{prerequisite_file} to #{file}"
        upload! File.open(prerequisite_file), file
      end
    end

  end
end