Skip to content

Commit

Permalink
extract cache into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
hone committed Jun 17, 2013
1 parent 4542be5 commit 5e3c944
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
50 changes: 4 additions & 46 deletions lib/language_pack/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "yaml"
require "digest/sha1"
require "language_pack/shell_helpers"
require "language_pack/cache"

Encoding.default_external = Encoding::UTF_8 if defined?(Encoding)

Expand All @@ -12,15 +13,15 @@ class LanguagePack::Base

VENDOR_URL = "https://s3.amazonaws.com/heroku-buildpack-ruby"

attr_reader :build_path, :cache_path
attr_reader :build_path, :cache

# changes directory to the build_path
# @param [String] the path of the build dir
# @param [String] the path of the cache dir
def initialize(build_path, cache_path=nil)
@build_path = build_path
@cache_path = cache_path
@id = Digest::SHA1.hexdigest("#{Time.now.to_f}-#{rand(1000000)}")[0..10]
@cache = LanguagePack::Cache.new(cache_path) if cache_path
@id = Digest::SHA1.hexdigest("#{Time.now.to_f}-#{rand(1000000)}")[0..10]

Dir.chdir build_path
end
Expand Down Expand Up @@ -128,48 +129,5 @@ def build_log_message(args)
end
end.join(" ")
end

# create a Pathname of the cache dir
# @return [Pathname] the cache dir
def cache_base
Pathname.new(cache_path)
end

# removes the the specified
# @param [String] relative path from the cache_base
def cache_clear(path)
target = (cache_base + path)
target.exist? && target.rmtree
end

# write cache contents
# @param [String] path of contents to store. it will be stored using this a relative path from the cache_base.
# @param [Boolean] defaults to true. if set to true, the cache store directory will be cleared before writing to it.
def cache_store(path, clear_first=true)
cache_clear(path) if clear_first
cache_copy path, (cache_base + path)
end

# load cache contents
# @param [String] relative path of the cache contents
def cache_load(path)
cache_copy (cache_base + path), path
end

# copy cache contents
# @param [String] source directory
# @param [String] destination directory
def cache_copy(from, to)
return false unless File.exist?(from)
FileUtils.mkdir_p File.dirname(to)
system("cp -a #{from}/. #{to}")
end

# check if the cache content exists
# @param [String] relative path of the cache contents
# @param [Boolean] true if the path exists in the cache and false if otherwise
def cache_exists?(path)
File.exists?(cache_base + path)
end
end

45 changes: 45 additions & 0 deletions lib/language_pack/cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "pathname"
require "language_pack"

class LanguagePack::Cache
def initialize(cache_path)
@cache_base = Pathname.new(cache_path)
end

# removes the the specified
# @param [String] relative path from the cache_base
def clear(path)
target = (@cache_base + path)
target.exist? && target.rmtree
end

# write cache contents
# @param [String] path of contents to store. it will be stored using this a relative path from the cache_base.
# @param [Boolean] defaults to true. if set to true, the cache store directory will be cleared before writing to it.
def store(path, clear_first=true)
clear(path) if clear_first
copy path, (@cache_base + path)
end

# load cache contents
# @param [String] relative path of the cache contents
def load(path)
copy (@cache_base + path), path
end

# copy cache contents
# @param [String] source directory
# @param [String] destination directory
def copy(from, to)
return false unless File.exist?(from)
FileUtils.mkdir_p File.dirname(to)
system("cp -a #{from}/. #{to}")
end

# check if the cache content exists
# @param [String] relative path of the cache contents
# @param [Boolean] true if the path exists in the cache and false if otherwise
def exists?(path)
File.exists?(@cache_base + path)
end
end
16 changes: 8 additions & 8 deletions lib/language_pack/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def build_bundler
else
# using --deployment is preferred if we can
bundle_command += " --deployment"
cache_load ".bundle"
cache.load ".bundle"
end

version = run_stdout("bundle version").strip
Expand Down Expand Up @@ -429,8 +429,8 @@ def build_bundler
log "bundle", :status => "success"
puts "Cleaning up the bundler cache."
pipe "bundle clean 2> /dev/null"
cache_store ".bundle"
cache_store "vendor/bundle"
cache.store ".bundle"
cache.store "vendor/bundle"

# Keep gem cache out of the slug
FileUtils.rm_rf("#{slug_vendor_base}/cache")
Expand Down Expand Up @@ -612,7 +612,7 @@ def bundler_cache
end

def load_bundler_cache
cache_load "vendor"
cache.load "vendor"

full_ruby_version = run_stdout(%q(ruby -v)).chomp
rubygems_version = run_stdout(%q(gem -v)).chomp
Expand All @@ -628,14 +628,14 @@ def load_bundler_cache
# fix bug from v37 deploy
if File.exists?("vendor/ruby_version")
puts "Broken cache detected. Purging build cache."
cache_clear("vendor")
cache.clear("vendor")
FileUtils.rm_rf("vendor/ruby_version")
purge_bundler_cache
# fix bug introduced in v38
elsif !File.exists?(buildpack_version_cache) && File.exists?(ruby_version_cache)
puts "Broken cache detected. Purging build cache."
purge_bundler_cache
elsif cache_exists?(bundler_cache) && File.exists?(ruby_version_cache) && full_ruby_version != File.read(ruby_version_cache).chomp
elsif cache.exists?(bundler_cache) && File.exists?(ruby_version_cache) && full_ruby_version != File.read(ruby_version_cache).chomp
puts "Ruby version change detected. Clearing bundler cache."
puts "Old: #{File.read(ruby_version_cache).chomp}"
puts "New: #{full_ruby_version}"
Expand Down Expand Up @@ -669,12 +669,12 @@ def load_bundler_cache
File.open(rubygems_version_cache, 'w') do |file|
file.puts rubygems_version
end
cache_store heroku_metadata
cache.store heroku_metadata
end

def purge_bundler_cache
FileUtils.rm_rf(bundler_cache)
cache_clear bundler_cache
cache.clear bundler_cache
# need to reinstall language pack gems
install_language_pack_gems
end
Expand Down

0 comments on commit 5e3c944

Please sign in to comment.