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

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

Overview

Utility stuff to avoid cluttering of bundle.rake

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Utility

Returns a new instance of Utility.



9
10
11
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 9

def initialize(context)
  @context = context
end

Instance Method Details

#cacheObject

Cache used gems

Returns:

  • void



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

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

  if gems_changed?
    Bundler.with_clean_env do
      execute("bundle package --gemfile #{File.join(Dir.pwd, '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)


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

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)


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

def cached_gemfile_md5_path
  File.join(Dir.pwd, '.capistrano-git-copy-bundle-gemfile-lock.md5')
end

#clear_localObject

Clear local cached gems

Returns:

  • void



49
50
51
52
53
# 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



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

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)


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

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

#gems_changed?Boolean

Checks if Gemfile.lock has changed since last deploy

Returns:

  • (Boolean)


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

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)


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

def local_cache_path
  File.join(Dir.pwd, 'vendor', 'cache')
end

#local_gemsArray

List of filenames for locally cached gems

Returns:

  • (Array)


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

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

#remote_cache_pathString

Path for remote bundle cache

Returns:

  • (String)


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

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