fnichol/capistrano-fanfare

View on GitHub
lib/capistrano/fanfare/bundler.rb

Summary

Maintainability
A
2 hrs
Test Coverage
require 'capistrano'

module Capistrano::Fanfare::Bundler
  def self.load_into(configuration)
    configuration.load do
      set :bundle_cmd,      "bundle"
      set :bundle_shebang,  "ruby-local-exec"

      set(:default_environment) {
        { 'PATH' => "#{current_path}/bin:$PATH" }
      }

      set(:bundle_flags) do
        flags = "--deployment"
        flags << " --quiet" if ENV['QUIET']
        flags << " --binstubs"
        flags << " --shebang #{bundle_shebang}"
        flags
      end

      set(:bundle_without) do
        without = [:development, :test]
        if exists?(:os_type) && exists?(:os_types)
          without += (fetch(:os_types) - Array(fetch(:os_type)))
        end
        without
      end

      set :bundle_binstub_template do
        <<-BINSTUB.gsub(/^ {10}/, '')
          #!/usr/bin/env #{bundle_shebang}
          #
          # This file was generated by capistrano.
          #

          require 'pathname'
          ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
            Pathname.new(__FILE__).realpath)

          require 'rubygems'

          load Gem.bin_path('bundler', 'bundle')
        BINSTUB
      end

      require 'bundler/capistrano'

      # =========================================================================
      # These are the tasks that are available to help with deploying web apps.
      # You can have cap give you a summary of them with `cap -T'.
      # =========================================================================

      namespace :bundle do
        desc <<-DESC
          [internal] Creates a binstub script for the bundle command.
        DESC
        task :create_binstub_script, :roles => :app, :except => { :no_release => true } do
          run "mkdir -p #{shared_path}/bin"
          put bundle_binstub_template, "#{shared_path}/bin/bundle", { :mode => "0755" }
        end

        desc <<-DESC
          [internal] Copies bin/bundle from shared_path into current_path.
        DESC
        task :cp_bundle_binstub, :roles => :app, :except => { :no_release => true } do
          run [
            "mkdir -p #{current_path}/bin",
            "cp #{shared_path}/bin/bundle #{current_path}/bin/bundle",
            "chmod 0755 #{current_path}/bin/bundle"
          ].join(" && ")
        end
      end

      after "deploy:setup", "bundle:create_binstub_script"
      before "deploy:finalize_update", "bundle:cp_bundle_binstub"
    end
  end
end

if Capistrano::Configuration.instance
  Capistrano::Fanfare::Bundler.load_into(Capistrano::Configuration.instance)
end