asset_hash 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "3.0.5"
4
+ gem "capybara", ">= 0.4.0"
5
+ gem "sqlite3"
6
+
7
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
8
+ # gem 'ruby-debug'
9
+ # gem 'ruby-debug19'
@@ -0,0 +1,22 @@
1
+ Copyright 2011 Donald Piret
2
+
3
+ This code was inspired by the https://github.com/moocode/asset_id project by Richard Taylor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ h1. AssetHash
2
+
3
+ h2. About
4
+
5
+ AssetHash is a gem that allows you to easily generate unique paths for your static assets in order to avoid caching issues.
6
+ Its main purpose is to allow you to use Amazon Cloudfront with a custom origin policy to serve your static assets easily.
7
+
8
+ It uses an MD5 hash of your static asset to modify the filename, so only changed files will result in a different filename.
9
+
10
+ It also comes with a rake task to automatically create hashed versions of your asset filenames, useful for deployments.
11
+
12
+ h2. Usage
13
+
14
+ Add the gem to your Gemfile
15
+
16
+ <code>gem "asset_hash"</code>
17
+
18
+ Set up a CloudFront distribution with a custom origin that points to your website's URL.
19
+
20
+ Check the following website as well as the Amazon CloudFront documentation for help on setting up a custom origin CloudFront distribution:
21
+ http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?DistributionConfigDatatype.html#CustomOriginChildElements
22
+
23
+ Configure your environment file to use this CloudFront distribution as your asset host and the hashed filenames in the helpers.
24
+
25
+ <pre><code>config.action_controller.asset_host = Proc.new do |source, request|
26
+ request.ssl? ? "https://distributionID.cloudfront.net" : "http://distributionID.cloudfront.net"
27
+ end
28
+ config.action_controller.asset_path = Proc.new do |source|
29
+ AssetHash.fingerprint(source)
30
+ end
31
+ </code></pre>
32
+
33
+ Set up your deployment file (Capistrano, Chef callback or other) to run the included rake task on deploy
34
+
35
+ <code>rake asset:hash:generate</code>
36
+
37
+ This will copy all your assets from a path like <code>stylesheets/application.css</code> to something like <code>stylesheets/applications-234fe3b632356brdedf3115ee743250.css</code> and create a gzipped version as well at <code>stylesheets/applications-234fe3b632356brdedf3115ee743250.css.gz</code>
38
+ This ensures that when CloudFront caches your static assets, it will always serve the up-to-date version.
39
+
40
+ The code by default plays nice with Jammit by including the assets directory to the default images, javascripts and stylesheets directories, and you can customize which directories are scanned for assets using <code>AssetHash::Base.asset_paths = ['custom', 'directories']</code>
41
+
42
+ h2. License
43
+
44
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/rdoctask'
11
+
12
+ require 'rake/testtask'
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.libs << 'test'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = false
19
+ end
20
+
21
+ task :default => :test
22
+
23
+ Rake::RDocTask.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'AssetHash'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README.rdoc')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
@@ -0,0 +1,87 @@
1
+ require 'digest/md5'
2
+ require 'time'
3
+ require 'mime/types'
4
+ require 'asset_hash/railtie' if defined?(Rails)
5
+
6
+ module AssetHash
7
+ def self.process!
8
+ AssetHash::Base.process!
9
+ end
10
+
11
+ def self.fingerprint(path)
12
+ AssetHash::Base.fingerprint(path)
13
+ end
14
+
15
+ class Base
16
+ DEFAULT_ASSET_PATHS = ['favicon.ico', 'images', 'javascripts', 'stylesheets', 'assets']
17
+ @@asset_paths = DEFAULT_ASSET_PATHS
18
+
19
+ DEFAULT_GZIP_TYPES = ['text/css', 'application/javascript']
20
+ @@gzip_types = DEFAULT_GZIP_TYPES
21
+
22
+ def self.gzip_types=(types)
23
+ @@gzip_types = types
24
+ end
25
+
26
+ def self.gzip_types
27
+ @@gzip_types
28
+ end
29
+
30
+ def self.path_prefix
31
+ File.join Rails.root, 'public'
32
+ end
33
+
34
+ def self.asset_paths=(paths)
35
+ @@asset_paths = paths
36
+ end
37
+
38
+ def self.asset_paths
39
+ @@asset_paths
40
+ end
41
+
42
+ def self.absolute_path(path)
43
+ File.join path_prefix, path
44
+ end
45
+
46
+ def self.assets
47
+ asset_paths.inject([]) {|assets, path|
48
+ path = absolute_path(path)
49
+ assets << path if File.exists? path and !File.directory? path
50
+ assets += Dir.glob(path+'/**/*').inject([]) {|m, file|
51
+ m << file unless File.directory? file; m
52
+ }
53
+ }
54
+ end
55
+
56
+ def self.fingerprint(path)
57
+ path = File.join path_prefix, path unless path =~ /#{path_prefix}/
58
+ d = Digest::MD5.file(path).hexdigest
59
+ path = path.gsub(path_prefix, '')
60
+ extension = (path =~ /\.gz$/ ? File.extname(File.basename(path, ".gz")) + ".gz" : File.extname(path))
61
+ File.join File.dirname(path), "#{File.basename(path, extension)}-#{d}#{extension}"
62
+ end
63
+
64
+ def self.original_path(path)
65
+ path = File.join path_prefix, path unless path =~ /#{path_prefix}/
66
+ path = path.gsub(path_prefix, '')
67
+ extension = (path =~ /\.gz$/ ? File.extname(File.basename(path, ".gz")) + ".gz" : File.extname(path))
68
+ File.join File.dirname(path), "#{File.basename(path, extension)}#{extension}"
69
+ end
70
+
71
+ def self.process!
72
+ assets.each do |asset|
73
+ mime_type = MIME::Types.of(asset).first.to_s
74
+ if gzip_types.include?(mime_type) && !File.exists?(asset + ".gz")
75
+ # You can GZIP the type of this file and no gzipped version was found already
76
+ # Gzip the original asset then move it to the right filename
77
+ gz_path = asset + ".gz"
78
+ `gzip -c "#{asset}" > "#{gz_path}"`
79
+ FileUtils.mv(gz_path, File.join(path_prefix, fingerprint(asset) + ".gz"))
80
+ end
81
+ FileUtils.cp(asset, File.join(path_prefix, fingerprint(asset)))
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,9 @@
1
+ require 'asset_hash'
2
+ require 'rails'
3
+ module AssetHash
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load "tasks/asset_hash.rake"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'asset_hash'
2
+
3
+ namespace :asset do
4
+ namespace :hash do
5
+ desc 'Generate the assets with the hashed filenames'
6
+ task :generate do
7
+ AssetHash.process!
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asset_hash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0.1"
6
+ platform: ruby
7
+ authors: []
8
+
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-29 00:00:00 +08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: mime-types
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "1.16"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: This gem allows you to copy your static assets to a versions including a unique hash in the filename. By using this and modifying your Rails asset path you can easily serve static content on Cloudfront using the custom origin policy.
28
+ email:
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - lib/asset_hash/railtie.rb
37
+ - lib/asset_hash.rb
38
+ - lib/tasks/asset_hash.rake
39
+ - MIT-LICENSE
40
+ - Rakefile
41
+ - Gemfile
42
+ - README.textile
43
+ has_rdoc: true
44
+ homepage:
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.5.2
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: Asset hasher for Cloudfront custom origin domain asset hosting.
71
+ test_files: []
72
+