class Capistrano::SCM::Archive

SCM archive

Public Instance Methods

define_tasks() click to toggle source
# File lib/capistrano-scm-local_copy.rb, line 16
def define_tasks
  namespace :archive do
    archive_plugin = self
    task :create_release do
      archive_name = fetch(:archive_name)
      on release_roles :all do
        execute :mkdir, '-p', release_path
        temp_file = capture(:mktemp)
        upload!(archive_name, temp_file)
        execute :mv, '-fv', temp_file, archive_name
        execute :unzip, '-q -d', release_path, archive_name
        execute :rm, '-fv', archive_name, temp_file
      end
    end

    desc 'Create archive'
    task :create_archive do
      archive_name = fetch(:archive_name)
      puts "Create archive #{archive_name}"
      include_dir = fetch(:include_dir)
      excludes = fetch(:excludes) + [archive_name]
      run_locally do
        archive_objects = FileList[include_dir]
        excludes.each do |file|
          archive_objects = archive_objects.exclude(file)
        end

        Zip::File.open(archive_name, Zip::File::CREATE) do |zip_file|
          archive_objects.each do |object|
            if File.directory?(object)
              Dir.glob("#{object}/**/*.*").each do |child_file|
                zip_file.add(child_file, File.join('./', child_file))
              end
            else
              zip_file.add(object, File.join('./', object))
            end
          end
        end
      end
    end

    task :set_current_revision do
      set :current_revision, archive_plugin.md5(fetch(:archive_name))
    end

    Rake::Task['deploy:log_revision'].clear_actions
    desc 'log revision'
    task :log_revision do
      on release_roles :all do
        execute :echo,
                '"from local files,',
                "(archive md5: #{fetch(:current_revision)})",
                "deployed as release #{fetch(:release_timestamp)}",
                "by #{local_user}\"",
                '>>',
                revision_log

      end
    end

    task :clean do
      archive_name = fetch(:archive_name)
      puts "Clean: remove #{archive_name}"
      FileUtils.rm(archive_name, force: true)
    end

    task :clean_all do
      puts 'Clean deploy_*.zip archives'
      Dir.glob('./deploy_*.zip').each { |file| FileUtils.rm(file, force: true) }
    end

    before :create_archive, :clean_all
    before :clean, :set_current_revision
    after :create_release, :clean
    before :create_release, :create_archive

  end
end
md5(file) click to toggle source
# File lib/capistrano-scm-local_copy.rb, line 95
def md5(file)
  Digest::MD5.hexdigest File.open(file, 'r').read
end
random_string(length = 40) click to toggle source
# File lib/capistrano-scm-local_copy.rb, line 99
def random_string(length = 40)
  (0...length).map { rand(97..122).chr }.join
end
register_hooks() click to toggle source
# File lib/capistrano-scm-local_copy.rb, line 103
def register_hooks
  after 'deploy:new_release_path', 'archive:create_release'
  before 'deploy:set_current_revision', 'archive:set_current_revision'
  before 'deploy:log_revision', 'archive:log_revision'
end
set_defaults() click to toggle source
# File lib/capistrano-scm-local_copy.rb, line 9
def set_defaults
  set_if_empty :archive_name, 'deploy_' + random_string(10) + '.zip'
  set_if_empty :include_dir, '*'
  set_if_empty :tar_roles, :all
  set_if_empty :excludes, ''
end