Skip to content

Instantly share code, notes, and snippets.

@steveh
Created November 14, 2010 04:15
Show Gist options
  • Save steveh/675902 to your computer and use it in GitHub Desktop.
Save steveh/675902 to your computer and use it in GitHub Desktop.
require "digest/md5"
namespace :jammit do
desc "Lock packages to md5sum"
task :lock do
srcdir = File.join(Rails.root, "public", "packages")
dstdir = File.join(Rails.root, "public", "cache")
lockfile = File.join(Rails.root, "config", "assets.yml.lock")
locks = {}
Dir.new(srcdir).each do |file|
if file =~ /\.(css|js|gz)/
hash = Digest::MD5.hexdigest(File.join(srcdir, file))
locks[file] = hash
FileUtils.mkdir_p(File.join(dstdir, hash))
File.symlink(File.join(srcdir, file), File.join(dstdir, hash, file))
end
end
File.open(lockfile, "w") do |file|
file.puts locks.to_yaml
end
end
end
module Jammit
HASHES = YAML.load_file(File.join(Rails.root, "config", "assets.yml.lock")) || {}
def self.asset_url(package, extension, suffix=nil, mtime=nil)
file = filename(package, extension, suffix)
hash = HASHES[file]
"/cache/#{hash}/#{file}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment