Class: Capistrano::GitCopy::Bundle::Utility

Inherits:
Utility
  • Object
show all
Defined in:
lib/capistrano/git_copy/bundle/utility.rb

Overview

Utility stuff to avoid cluttering of bundle.cap

Instance Method Summary collapse

Instance Method Details

#cacheObject

Cache used gems

Returns:

  • void



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 11

def cache
  local_vendor_path = File.join(repo_path, 'vendor')

  execute(:mkdir, '-p', local_cache_path)  unless test!("[ -d #{local_cache_path} ]")
  execute(:mkdir, '-p', local_vendor_path) unless test!("[ -d #{local_vendor_path} ]")

  execute(:ln, '-s', local_cache_path, File.join(local_vendor_path, 'cache'))

  if gems_changed?
    Bundler.with_clean_env do
      execute("bundle package --gemfile #{File.join(repo_path, 'Gemfile')} --all --all-platforms")
    end

    File.open(cached_gemfile_md5_path, 'w') { |f| f.write(gemfile_md5) } unless gemfile_md5.nil?
  end
end

#cached_gemfile_md5String

MD5 sum of Gemfile.lock for local gem cache

Returns:

  • (String)


85
86
87
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 85

def cached_gemfile_md5
  @_cached_gemfile_md5 ||= File.read(cached_gemfile_md5_path) rescue nil
end

#cached_gemfile_md5_pathString

Path to cache for MD5 sum of Gemfile.lock

Returns:

  • (String)


92
93
94
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 92

def cached_gemfile_md5_path
  File.join(tmp_path, 'git_copy_bundle_gemfile_lock.md5')
end

#clear_localObject

Clear local cached gems

Returns:

  • void



49
50
51
52
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 49

def clear_local
  execute(:rm, '-rf', File.join(local_cache_path, '*')) if test!("[ -d #{local_cache_path} ]")
  File.unlink(cached_gemfile_md5_path)
end

#clear_remoteObject

Clear remote cached gems

Returns:

  • void



57
58
59
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 57

def clear_remote
  execute(:rm, '-rf', File.join(remote_cache_path, '*')) if test!("[ -d #{remote_cache_path} ]")
end

#gemfile_md5String

MD5 sum of Gemfile.lock to deploy

Returns:

  • (String)


78
79
80
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 78

def gemfile_md5
  @_gemfile_md5 ||= Digest::MD5.file(File.join(repo_path, 'Gemfile.lock')).hexdigest rescue nil
end

#gems_changed?Boolean

Checks if Gemfile.lock has changed since last deploy

Returns:

  • (Boolean)


99
100
101
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 99

def gems_changed?
  gemfile_md5.nil? || cached_gemfile_md5.nil? || gemfile_md5 != cached_gemfile_md5
end

#local_cache_pathString

Path for local bundle cache

Returns:

  • (String)


71
72
73
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 71

def local_cache_path
  File.join(tmp_path, 'bundle_cache')
end

#local_gemsArray

List of filenames for locally cached gems

Returns:

  • (Array)


106
107
108
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 106

def local_gems
  %x(ls #{local_cache_path}).split(/\s+/)
end

#remote_cache_pathString

Path for remote bundle cache

Returns:

  • (String)


64
65
66
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 64

def remote_cache_path
  File.join(shared_path, 'bundle', 'cache')
end

#uploadObject

Upload cached gems

Returns:

  • void



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 31

def upload
  vendor_path = File.join(release_path, 'vendor')

  execute(:mkdir, '-p', remote_cache_path) unless test!("[ -d #{remote_cache_path} ]")
  execute(:mkdir, '-p', vendor_path)       unless test!("[ -d #{vendor_path} ]")

  execute(:ln, '-s', remote_cache_path, File.join(vendor_path, 'cache'))

  remote_gems = capture(:ls, remote_cache_path).split(/\s+/)

  (local_gems - remote_gems).each do |file|
    upload!(File.join(local_cache_path, file), File.join(remote_cache_path, file), recursive: true)
  end
end