Skip to content

Instantly share code, notes, and snippets.

@coneybeare
Created March 5, 2013 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coneybeare/5092430 to your computer and use it in GitHub Desktop.
Save coneybeare/5092430 to your computer and use it in GitHub Desktop.
s3_access = ""
s3_secret = ""
s3_bucket = ""
cdn_path = "http://cloudfront.your.server.com"
desc "Swaps the local style.css file for a cdn hosted one"
task :css_to_cdn do
puts "## Checking AWS Credentials..."
unless s3_access && s3_secret
puts "\n## ERROR: Please setup up both s3_access and s3_secret"
next false
end
ENV['AWS_ACCESS_KEY_ID'] = s3_access
ENV['AWS_SECRET_ACCESS_KEY'] = s3_secret
puts "\n## Deploying website onto Amazon S3"
s3 = AWS::S3.new
bucket = s3.buckets[s3_bucket]
file_path = "public/stylesheets/screen.css"
bucket_path = file_path.sub('public/', '')
puts "## Uploading \"#{file_path}\" to \"#{cdn_path}/#{bucket_path}\""
fd = File.open(file_path)
obj = bucket.objects[bucket_path]
write = !obj.exists? || obj.last_modified < fd.mtime
puts " -> #{obj.public_url}" && obj.write(fd, :acl => :public_read) if write
puts "## Swapping all relative references of 'href=\"/stylesheets/screen.css\"' for one hosted at \"#{cdn_path}\""
files = %x[find #{public_dir} -type f -iname "*.html"].split
files.map do |file_path|
puts " -> #{file_path}"
text = File.read(file_path)
replace = text.gsub("href=\"/stylesheets/screen.css\"", "href=\"#{cdn_path}/stylesheets/screen.css\"")
File.open(file_path, "w") {|file| file.puts replace}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment