Skip to content

Instantly share code, notes, and snippets.

@MarkBorcherding
Created July 1, 2010 15:34
Show Gist options
  • Select an option

  • Save MarkBorcherding/460124 to your computer and use it in GitHub Desktop.

Select an option

Save MarkBorcherding/460124 to your computer and use it in GitHub Desktop.
# this is how it would be invoked as a new task in the rakefile
# you can override the hashing function or exclude/include files
revfiles :revfiles do |rev|
rev.files_to_rev << './**/*.bmp'
end
require 'rake'
require 'digest/md5'
require 'albacore/support/albacore_helper'
class RevFiles
extend AttrMethods
include YAMLConfig
attr_accessor :rev_function
attr_array :files_to_rev
def initialize
super()
@rev_function = Proc.new { |f| Digest::MD5.hexdigest(File.read(f))[0..6] }
@files_to_rev = []
@files_to_rev << './**/*.js'
@files_to_rev << './**/*.png'
@files_to_rev << './**/*.jpg'
@files_to_rev << './**/*.gif'
@files_to_rev << './**/*.css'
end
def execute
files = FileList.new(@files_to_rev)
files.each { |f|
ext = File.extname(f)
filename = File.basename(f,ext)
dir = File.dirname(f)
oldFiles = FileList.new("#{dir}/#{filename}.*#{ext}").exclude(f)
oldFiles.each{|old| File.delete(old)}
new_filename = "#{filename}.#{@rev_function.call(f)}#{ext}"
new_path = File.join(dir,new_filename)
cp(f,new_path)
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment