module Capistrano::Distribution::Distributor

This module is a factory for creating distributor objects that are able to fetch distributable artifacts from various sources and deploy them to a target host.

Public Class Methods

create(context, definition) click to toggle source

Create a concrete distributor object based on definition.

When definition is a String, it is assumed to be a URL that is used to guess about the type of distributor to create. For instance, URLs that end with `.git` will elicit the Git::Pull distributor. A URL without a scheme and ending with .tgz will elicit the Tar distributor.

When definition is an Array and the first element is a String, the distributor class is guessed using that String as in the case when definition is simply a String itself. The remaining members of the array are passed to the selected class' initializer as arguments.

When definition is an Array and the first element is a Symbol, the Symbol is assumed to map to the class of the distributor to instantiate. The Symbol :curl_tar maps to the Curl::Tar distributor for example. The remaining members of the array are passed to the selected class' initializer as arguments.

When definition is anything else, it is returned directly under the assumption that the configuration explicitly created a distributor.

@param context [{#test, execute}] a Capistrano context used to run

commands.

@param definition [String, Array, other] a distributor definition.

@return a distributor

# File lib/capistrano/distribution/distributor.rb, line 40
def self.create(context, definition)
  case definition
  when String
    type = type_from_url(definition)
    constantize(type).new(context, definition)
  when Array
    if Symbol === definition.first
      type, *args = definition
    else
      type = type_from_url(definition.first)
      args = definition
    end
    constantize(type).new(context, *args)
  else
    definition
  end
end

Private Class Methods

constantize(type) click to toggle source

Finds the class to use for a distributor given a Symbol.

@param type [Symbol] maps to a distributor class.

@return a distributor class.

# File lib/capistrano/distribution/distributor.rb, line 95
def self.constantize(type)
  require "capistrano/distribution/distributor/#{type}"
  const_get(type.to_s.split('_').map(&:capitalize).join)
end
type_from_url(url) click to toggle source

Return a distributor type based on url.

@param url [URI] a URL denoting the location of the artifact to be

deployed.

@return [Symbol] a distributor type

# File lib/capistrano/distribution/distributor.rb, line 67
def self.type_from_url(url)
  url = URI.parse(url.to_s)

  type =
    if url.scheme == 'git' || url.path =~ %r{\.git$}
      :git_pull
    elsif url.scheme.nil? || url.scheme == 'file'
      if url.path =~ %r{\.(tar(\.(gz|bzip2))?|tgz|tbz2)$}i
        :tar
      elsif url.path =~ %r{\.zip$}i
        :zip
      end
    elsif url.path =~ %r{\.(tar(\.(gz|bzip2))?|tgz|tbz2)$}i
      :curl_tar
    elsif url.path =~ %r{\.zip$}i
      :curl_zip
    end

  raise "Unable to guess distributor type for URL: #{url}" if type.nil?
  type
end