Class: Capistrano::GitCopy::Bundle::Utility
- Inherits:
-
Object
- Object
- Capistrano::GitCopy::Bundle::Utility
- Defined in:
- lib/capistrano/git_copy/bundle/utility.rb
Overview
Utility stuff to avoid cluttering of bundle.rake
Instance Method Summary collapse
-
#cache ⇒ Object
Cache used gems.
-
#cached_gemfile_md5 ⇒ String
MD5 sum of Gemfile.lock for local gem cache.
-
#cached_gemfile_md5_path ⇒ String
Path to cache for MD5 sum of Gemfile.lock.
-
#clear_local ⇒ Object
Clear local cached gems.
-
#clear_remote ⇒ Object
Clear remote cached gems.
-
#gemfile_md5 ⇒ String
MD5 sum of Gemfile.lock to deploy.
-
#gems_changed? ⇒ Boolean
Checks if Gemfile.lock has changed since last deploy.
-
#initialize(context) ⇒ Utility
constructor
A new instance of Utility.
-
#local_cache_path ⇒ String
Path for local bundle cache.
-
#local_gems ⇒ Array
List of filenames for locally cached gems.
-
#remote_cache_path ⇒ String
Path for remote bundle cache.
-
#upload ⇒ Object
Upload cached gems.
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
#cache ⇒ Object
Cache used gems
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_md5 ⇒ String
MD5 sum of Gemfile.lock for local gem cache
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_path ⇒ String
Path to cache for MD5 sum of Gemfile.lock
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_local ⇒ Object
Clear local cached gems
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_remote ⇒ Object
Clear remote cached gems
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_md5 ⇒ String
MD5 sum of Gemfile.lock to deploy
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
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_path ⇒ String
Path for local bundle cache
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_gems ⇒ Array
List of filenames for locally cached gems
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_path ⇒ String
Path for remote bundle cache
65 66 67 |
# File 'lib/capistrano/git_copy/bundle/utility.rb', line 65 def remote_cache_path File.join(shared_path, 'bundle', 'cache') end |
#upload ⇒ Object
Upload cached gems
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 |