Class: TravisBundleCache::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/travis_bundle_cache/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



6
7
8
9
10
11
12
13
14
# File 'lib/travis_bundle_cache/cache.rb', line 6

def initialize
  @architecture        = `uname -m`.strip
  @bundle_archive      = ENV['BUNDLE_ARCHIVE'] || ENV['TRAVIS_REPO_SLUG'].gsub(/\//, '-')
  @file_name           = "#{@bundle_archive}-#{@architecture}-#{RUBY_VERSION}p#{RUBY_PATCHLEVEL}.tgz"
  @file_path           = File.expand_path("~/#{@file_name}")
  @lock_file           = File.join(File.expand_path(ENV["TRAVIS_BUILD_DIR"]), "Gemfile.lock")
  @digest_filename     = "#{@file_name}.sha2"
  @old_digest_filename = File.expand_path("~/remote_#{@digest_filename}")
end

Instance Method Details

#archive_and_upload_bundleObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/travis_bundle_cache/cache.rb', line 29

def archive_and_upload_bundle
  if @old_digest == ""
    puts "=> There was no existing digest, uploading a new version of the archive"
  else
    puts "=> There were changes, uploading a new version of the archive"
    puts "  => Old checksum: #{@old_digest}"
    puts "  => New checksum: #{@bundle_digest}"

    puts "=> Cleaning old gem versions from the bundle"
    run_command "bundle clean"
  end

  puts "=> Preparing bundle archive"
  run_command %(cd ~ && tar -cjf "#{@file_name}" .bundle), exit_on_error: true

  puts "=> Uploading the bundle"
  storage[@file_name].write(Pathname.new(@file_path), :reduced_redundancy => true)

  puts "=> Uploading the digest file"
  storage[@digest_filename].write(@bundle_digest, :content_type => "text/plain", :reduced_redundancy => true)

  puts "All done now."
end

#cache_bundleObject



22
23
24
25
26
27
# File 'lib/travis_bundle_cache/cache.rb', line 22

def cache_bundle
  @bundle_digest = Digest::SHA2.file(@lock_file).hexdigest
  @old_digest    = File.exists?(@old_digest_filename) ? File.read(@old_digest_filename) : ""

  archive_and_upload_bundle
end

#installObject



16
17
18
19
20
# File 'lib/travis_bundle_cache/cache.rb', line 16

def install
  run_command %(cd ~ && wget -O "remote_#{@file_name}" "#{storage[@file_name].url_for(:read)}" && tar -xf "remote_#{@file_name}")
  run_command %(cd ~ && wget -O "remote_#{@file_name}.sha2" "#{storage[@digest_filename].url_for(:read)}")
  run_command %(bundle install --without #{ENV['BUNDLE_WITHOUT'] || "development production"} --path=~/.bundle), retry: true
end