asset_hat 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.gitignore +1 -0
  2. data/HISTORY +12 -0
  3. data/LICENSE +20 -0
  4. data/README.markdown +153 -0
  5. data/Rakefile +22 -0
  6. data/VERSION.yml +5 -0
  7. data/app/helpers/asset_hat_helper.rb +171 -0
  8. data/asset_hat.gemspec +98 -0
  9. data/config/assets.yml +59 -0
  10. data/lib/asset_hat.rb +85 -0
  11. data/lib/asset_hat/css.rb +76 -0
  12. data/lib/asset_hat/js.rb +86 -0
  13. data/lib/asset_hat/tasks.rb +296 -0
  14. data/lib/asset_hat/vcs.rb +65 -0
  15. data/lib/asset_hat/version.rb +11 -0
  16. data/public/javascripts/bundles/js-bundle-1.min.js +6 -0
  17. data/public/javascripts/bundles/js-bundle-2.min.js +6 -0
  18. data/public/javascripts/bundles/js-bundle-3.min.js +6 -0
  19. data/public/javascripts/js-file-1-1.js +1 -0
  20. data/public/javascripts/js-file-1-2.js +1 -0
  21. data/public/javascripts/js-file-1-3.js +1 -0
  22. data/public/javascripts/js-file-2-1.js +1 -0
  23. data/public/javascripts/js-file-2-2.js +1 -0
  24. data/public/javascripts/js-file-2-3.js +1 -0
  25. data/public/javascripts/js-file-3-1.js +1 -0
  26. data/public/javascripts/js-file-3-2.js +1 -0
  27. data/public/javascripts/js-file-3-3.js +1 -0
  28. data/public/stylesheets/bundles/css-bundle-1.min.css +3 -0
  29. data/public/stylesheets/bundles/css-bundle-2.min.css +3 -0
  30. data/public/stylesheets/bundles/css-bundle-3.min.css +3 -0
  31. data/public/stylesheets/css-file-1-1.css +1 -0
  32. data/public/stylesheets/css-file-1-2.css +1 -0
  33. data/public/stylesheets/css-file-1-3.css +1 -0
  34. data/public/stylesheets/css-file-2-1.css +1 -0
  35. data/public/stylesheets/css-file-2-2.css +1 -0
  36. data/public/stylesheets/css-file-2-3.css +1 -0
  37. data/public/stylesheets/css-file-3-1.css +1 -0
  38. data/public/stylesheets/css-file-3-2.css +1 -0
  39. data/public/stylesheets/css-file-3-3.css +1 -0
  40. data/rails/init.rb +17 -0
  41. data/tasks/asset_hat.rake +1 -0
  42. data/test/asset_hat_helper_test.rb +237 -0
  43. data/test/asset_hat_test.rb +46 -0
  44. data/test/test_helper.rb +23 -0
  45. metadata +141 -0
@@ -0,0 +1 @@
1
+ pkg/
data/HISTORY ADDED
@@ -0,0 +1,12 @@
1
+ HISTORY
2
+ =======
3
+
4
+ Version 0.1.1 (2010-01-20)
5
+ --------------------------
6
+ * Rewrote `AssetHat::VERSION` to be based on `VERSION.yml`.
7
+ * Prefixed `AssetHat::CONFIG_FILEPATH` with `RAILS_ROOT`, which fixes ability
8
+ to run an app's individual test files.
9
+
10
+ Version 0.1.0 (2010-01-19)
11
+ --------------------------
12
+ * Initial release.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ron DeVera, Mint Digital
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,153 @@
1
+ AssetHat
2
+ ========
3
+ Your assets are covered.
4
+
5
+ * Minify CSS and JS with one command. (Can be done on deploy instead of
6
+ at runtime.)
7
+ * Bundle CSS and JS to reduce HTTP requests.
8
+ * Reuse CSS and JS bundles across layouts without repetition.
9
+ * Bust image caches by changing CSS URLs whenever an image is modified.
10
+ * Force image URLs in your CSS to use CDN subdomains, not just the current
11
+ host.
12
+
13
+ After setup, you can use these in your layouts and views:
14
+
15
+ include_css :bundle => 'application'
16
+ # => <link href="/stylesheets/bundles/application.min.css"
17
+ # media="screen,projection" rel="stylesheet" type="text/css" />
18
+
19
+ include_js :bundles => ['plugins', 'common']
20
+ # => <script src="/javascripts/bundles/plugins.min.js"
21
+ # type="text/javascript"></script>
22
+ # <script src="/javascripts/bundles/common.min.js"
23
+ # type="text/javascript"></script>
24
+
25
+ And this in your deploy script:
26
+
27
+ rake asset_hat:minify
28
+
29
+ Works with Rails 2.3.4 and above.
30
+
31
+
32
+
33
+ Installation
34
+ ------------
35
+
36
+ 1. Install the gem:
37
+
38
+ gem install asset_hat
39
+
40
+ 2. Configure the gem:
41
+
42
+ * Using [Bundler](http://github.com/wycats/bundler):
43
+
44
+ 1. Add to your app's Gemfile: `gem 'asset_hat', '0.x.x'`
45
+
46
+ 2. Command-line: `gem bundle`
47
+
48
+ * Using Rails' `config.gem`, add to your app's `config/environment.rb`:
49
+
50
+ `config.gem 'asset_hat', :version => '0.x.x'`
51
+
52
+ 3. In your app, create `lib/tasks/asset_hat.rake` with the following contents:
53
+
54
+ begin
55
+ require 'asset_hat/tasks'
56
+ rescue LoadError
57
+ puts %q{Could not load AssetHat tasks: 'asset_hat' not found.}
58
+ end
59
+
60
+
61
+
62
+ Configuration
63
+ -------------
64
+
65
+ 1. Create the default config file:
66
+
67
+ rake asset_hat:config
68
+
69
+ 2. In your app, open the new config file at `config/assets.yml`, and set up
70
+ your CSS/JS bundles according to that file's example.
71
+
72
+ 3. Minify your bundles:
73
+
74
+ rake asset_hat:minify
75
+
76
+ This minifies all of the CSS/JS files listed in `config/assets.yml`,
77
+ concatenates the minified code into bundle files, and adds CDN asset hosts
78
+ and cache-busting commit IDs to image URLs in your CSS.
79
+
80
+ Bundles are created as new files in `public/stylesheets/bundles/` and
81
+ `public/javascripts/bundles/`. Your original CSS/JS files remain intact.
82
+
83
+ 4. Set your deployment script to run `rake asset_hat:minify` after deploying
84
+ your latest CSS/JS. This overwrites previously minified bundles, and
85
+ leaves your original CSS/JS files intact.
86
+
87
+ ### Advanced configuration ###
88
+
89
+ Additional settings are supported in `config/assets.yml`:
90
+
91
+ * `engine`: Indicates how CSS and JS are minified; omit this setting to use
92
+ the defaults. If the default engines cause problems by minifying too
93
+ strongly, try switching each to `weak`. The `weak` engines are much safer,
94
+ but don't save as many bytes.
95
+
96
+ * `vendors`: Currently only allows for setting the jQuery version number:
97
+
98
+ js:
99
+ vendors:
100
+ jquery:
101
+ version: 1.4
102
+
103
+ In the future, this will be used for configuring the retrieval of other
104
+ third-party code.
105
+
106
+
107
+
108
+ Usage
109
+ -----
110
+
111
+ In your layouts and views, instead of these:
112
+
113
+ <%= stylesheet_link_tag 'reset', 'application', 'clearfix',
114
+ :media => 'screen,projection',
115
+ :cache => 'bundles/application' %>
116
+ <%= javascript_include_tag 'plugin-1', 'plugin-2', 'plugin-3',
117
+ :cache => 'bundles/application' %>
118
+
119
+ **Use these:**
120
+
121
+ <%= include_css :bundle => 'application' %>
122
+ <%= include_js :bundle => 'application' %>
123
+
124
+ These turn into:
125
+
126
+ <link href="/stylesheets/bundles/application.min.css"
127
+ media="screen,projection" rel="stylesheet" type="text/css" />
128
+ <script src="/javascripts/bundles/application.min.js"
129
+ type="text/javascript"></script>
130
+
131
+ If your environment has `config.action_controller.perform_caching` set to
132
+ `true` (e.g., in production), the layout/view will include minified bundle
133
+ files. Otherwise, the separate, unminified files will be included.
134
+
135
+ ### Advanced usage ###
136
+
137
+ You can also include single files as expected:
138
+
139
+ <%= include_css 'reset', 'application' %>
140
+ <%= include_js 'plugin.min', 'application' %>
141
+
142
+ Or include multiple bundles at once:
143
+
144
+ <%= include_js :bundles => %w[plugins common] %>
145
+
146
+ When including multiple bundles at once, this yields one `<link>` or
147
+ `<script>` element per bundle.
148
+
149
+ You may want to use multiple bundles to separate plugins (rarely changed) from
150
+ application code (frequently changed). If all code is in one huge bundle, then
151
+ whenever there's a change, browsers have to re-download the whole bundle. By
152
+ using multiple bundles based on change frequency, browsers cache the rarely
153
+ changed code, and only re-download the frequently changed code.
@@ -0,0 +1,22 @@
1
+ require File.join(File.dirname(__FILE__), %w[lib asset_hat tasks])
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gemspec|
6
+ gemspec.name = %q{asset_hat}
7
+ gemspec.summary = %q{Your assets are covered.}
8
+ gemspec.description = %q{Minify, bundle, and optimize CSS/JS assets.}
9
+ gemspec.homepage = %q{http://github.com/mintdigital/asset_hat}
10
+
11
+ gemspec.authors = ['Ron DeVera', 'Mint Digital']
12
+ gemspec.email = %q{ronald.devera@gmail.com}
13
+
14
+ gemspec.add_development_dependency 'shoulda', '>= 2.10.2'
15
+ gemspec.add_development_dependency 'flexmock', '>= 0.8.6'
16
+ gemspec.add_runtime_dependency 'cssmin', '>= 1.0.2'
17
+ gemspec.add_runtime_dependency 'jsmin', '>= 1.0.1'
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts 'Jeweler is not available. Install it with: `gem install jeweler`'
22
+ end
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
5
+ :build:
@@ -0,0 +1,171 @@
1
+ module AssetHatHelper
2
+ unless defined?(RAILS_ROOT)
3
+ RAILS_ROOT = File.join(File.dirname(__FILE__), '..', '..')
4
+ end
5
+
6
+ def include_assets(type, *args)
7
+ # `include_css` and `include_js` are recommended instead.
8
+
9
+ type = type.to_sym
10
+ allowed_types = AssetHat::TYPES
11
+ unless allowed_types.include?(type)
12
+ expected_types = allowed_types.map { |x| ":#{x}" }.to_sentence(
13
+ :two_words_connector => ' or ',
14
+ :last_word_connector => ', or '
15
+ )
16
+ raise "Unknown type :#{type}; should be #{expected_types}"
17
+ return
18
+ end
19
+
20
+ options = args.extract_options!
21
+ options.symbolize_keys!
22
+ options.reverse_merge!(:media => 'screen,projection') if type == :css
23
+
24
+ filenames = []
25
+ sources = [] # The URLs that are ultimately included via HTML
26
+ source_commit_ids = {} # Last commit ID for each source
27
+
28
+ # Set to `true` to use bundles and minified code:
29
+ use_caching = AssetHat.cache?
30
+ use_caching = options[:cache] unless options[:cache].nil?
31
+ options.delete :cache # Completely avoid Rails' built-in caching
32
+
33
+ if options[:bundle].present? || options[:bundles].present?
34
+ bundles = [options.delete(:bundle), options.delete(:bundles)].
35
+ flatten.reject(&:blank?)
36
+ if use_caching
37
+ sources += bundles.map { |b| "bundles/#{b}.min.#{type}" }
38
+ else
39
+ config = AssetHat.config
40
+ filenames = bundles.map { |b| AssetHat.bundle_filenames(b, type) }.
41
+ flatten.reject(&:blank?)
42
+ end
43
+ else
44
+ filenames = args
45
+ end
46
+
47
+ filenames.each do |filename|
48
+ if filename.match(/\.#{type}$/)
49
+ sources << filename
50
+ else
51
+ min_filename_with_ext = "#{filename}.min.#{type}"
52
+ if use_caching && AssetHat.asset_exists?(min_filename_with_ext, type)
53
+ sources << min_filename_with_ext # Use minified version
54
+ else
55
+ sources << "#{filename}.#{type}" # Use original version
56
+ end
57
+ end
58
+ end
59
+
60
+ sources.uniq!
61
+
62
+ if use_caching
63
+ # Add commit IDs to bust browser caches based on when each file was
64
+ # last updated in the repository. If `use_caching` is false (e.g., in
65
+ # development environments), skip this, and instead default to Rails'
66
+ # mtime-based cache busting.
67
+ sources.map! do |src|
68
+ if src =~ /^bundles\//
69
+ # Get commit ID of bundle file with most recently committed update
70
+ bundle = src.match(/^bundles\/(.*)\.min\.#{type}$/)[1]
71
+ commit_id = AssetHat.last_bundle_commit_id(bundle, type)
72
+ else
73
+ # Get commit ID of file's most recently committed update
74
+ commit_id = AssetHat.last_commit_id(
75
+ File.join(AssetHat.assets_dir(type), src))
76
+ end
77
+ if commit_id.present? # False if file isn't committed to repo
78
+ src += "#{src =~ /\?/ ? '&' : '?'}#{commit_id}"
79
+ end
80
+ src
81
+ end
82
+ end
83
+
84
+ # Build output HTML
85
+ sources.map do |src|
86
+ case type
87
+ when :css
88
+ stylesheet_link_tag(src, options)
89
+ when :js
90
+ javascript_include_tag(src, options)
91
+ end
92
+ end.join("\n")
93
+ end
94
+
95
+ def include_css(*args)
96
+ # Usage:
97
+ #
98
+ # Include a single stylesheet:
99
+ # include_css 'diagnostics'
100
+ # => <link href="/stylesheets/diagnostics.min.css" media="screen,projection" rel="stylesheet" type="text/css" />
101
+ #
102
+ # Include a single unminified stylesheet:
103
+ # include_css 'diagnostics.css'
104
+ # => <link href="/stylesheets/diagnostics.css" media="screen,projection" rel="stylesheet" type="text/css" />
105
+ #
106
+ # Include a bundle of stylesheets (i.e., a concatenated set of
107
+ # stylesheets; configure in config/assets.yml):
108
+ # include_css :bundle => 'application'
109
+ # => <link href="/stylesheets/bundles/application.min.css" ... />
110
+ #
111
+ # Include multiple stylesheets separately (not as cool):
112
+ # include_css 'reset', 'application', 'clearfix'
113
+ # => <link href="/stylesheets/reset.min.css" ... />
114
+ # <link href="/stylesheets/application.min.css" ... />
115
+ # <link href="/stylesheets/clearfix.min.css" ... />
116
+
117
+ return if args.blank?
118
+ include_assets :css, *args
119
+ end
120
+
121
+ def include_js(*args)
122
+ # Usage:
123
+ #
124
+ # Include a single JS file:
125
+ # include_js 'application'
126
+ # => <script src="/javascripts/application.min.js" type="text/javascript"></script>
127
+ #
128
+ # Include a single JS unminified file:
129
+ # include_js 'application.js'
130
+ # => <script src="/javascripts/application.js" type="text/javascript"></script>
131
+ #
132
+ # Include jQuery:
133
+ # include_js :jquery # Development/test environment
134
+ # => <script src="/javascripts/jquery-VERSION.min.js" ...></script>
135
+ # include_js :jquery # Staging/production environment
136
+ # => <script src="http://ajax.googleapis.com/.../jquery.min.js" ...></script>
137
+ # # Set jQuery versions either in `config/assets.yml`, or by using
138
+ # # `include_js :jquery, :version => '1.4'`.
139
+ #
140
+ # Include a bundle of JS files (i.e., a concatenated set of files;
141
+ # configure in config/assets.yml):
142
+ # include_js :bundle => 'application'
143
+ # => <script src="/javascripts/bundles/application.min.js" ...></script>
144
+ #
145
+ # Include multiple bundles of JS files:
146
+ # include_js :bundles => %w[plugins common]
147
+ # => <script src="/javascripts/bundles/plugins.min.js" ...></script>
148
+ # <script src="/javascripts/bundles/common.min.js" ...></script>
149
+ #
150
+ # Include multiple JS files separately (not as cool):
151
+ # include_js 'bloombox', 'jquery.cookie', 'jquery.json.min'
152
+ # => <script src="/javascripts/bloombox.min.js" ...></script>
153
+ # <script src="/javascripts/jquery.cookie.min.js" ...></script>
154
+ # <script src="/javascripts/jquery.json.min.js" ...></script>
155
+
156
+ return if args.blank?
157
+ html = []
158
+ options = args.extract_options!
159
+
160
+ included_vendors = (args & AssetHat::JS::VENDORS)
161
+ included_vendors.each do |vendor|
162
+ args.delete vendor
163
+ source = AssetHat::JS::Vendors.source_for(vendor, options.slice(:version))
164
+ html << include_assets(:js, source, :cache => true)
165
+ end
166
+
167
+ html << include_assets(:js, *(args + [options]))
168
+ html.join("\n").strip
169
+ end
170
+
171
+ end
@@ -0,0 +1,98 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{asset_hat}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ron DeVera", "Mint Digital"]
12
+ s.date = %q{2010-01-20}
13
+ s.description = %q{Minify, bundle, and optimize CSS/JS assets.}
14
+ s.email = %q{ronald.devera@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "HISTORY",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION.yml",
26
+ "app/helpers/asset_hat_helper.rb",
27
+ "asset_hat.gemspec",
28
+ "config/assets.yml",
29
+ "lib/asset_hat.rb",
30
+ "lib/asset_hat/css.rb",
31
+ "lib/asset_hat/js.rb",
32
+ "lib/asset_hat/tasks.rb",
33
+ "lib/asset_hat/vcs.rb",
34
+ "lib/asset_hat/version.rb",
35
+ "public/javascripts/bundles/js-bundle-1.min.js",
36
+ "public/javascripts/bundles/js-bundle-2.min.js",
37
+ "public/javascripts/bundles/js-bundle-3.min.js",
38
+ "public/javascripts/js-file-1-1.js",
39
+ "public/javascripts/js-file-1-2.js",
40
+ "public/javascripts/js-file-1-3.js",
41
+ "public/javascripts/js-file-2-1.js",
42
+ "public/javascripts/js-file-2-2.js",
43
+ "public/javascripts/js-file-2-3.js",
44
+ "public/javascripts/js-file-3-1.js",
45
+ "public/javascripts/js-file-3-2.js",
46
+ "public/javascripts/js-file-3-3.js",
47
+ "public/stylesheets/bundles/css-bundle-1.min.css",
48
+ "public/stylesheets/bundles/css-bundle-2.min.css",
49
+ "public/stylesheets/bundles/css-bundle-3.min.css",
50
+ "public/stylesheets/css-file-1-1.css",
51
+ "public/stylesheets/css-file-1-2.css",
52
+ "public/stylesheets/css-file-1-3.css",
53
+ "public/stylesheets/css-file-2-1.css",
54
+ "public/stylesheets/css-file-2-2.css",
55
+ "public/stylesheets/css-file-2-3.css",
56
+ "public/stylesheets/css-file-3-1.css",
57
+ "public/stylesheets/css-file-3-2.css",
58
+ "public/stylesheets/css-file-3-3.css",
59
+ "rails/init.rb",
60
+ "tasks/asset_hat.rake",
61
+ "test/asset_hat_helper_test.rb",
62
+ "test/asset_hat_test.rb",
63
+ "test/test_helper.rb"
64
+ ]
65
+ s.homepage = %q{http://github.com/mintdigital/asset_hat}
66
+ s.rdoc_options = ["--charset=UTF-8"]
67
+ s.require_paths = ["lib"]
68
+ s.rubygems_version = %q{1.3.5}
69
+ s.summary = %q{Your assets are covered.}
70
+ s.test_files = [
71
+ "test/asset_hat_helper_test.rb",
72
+ "test/asset_hat_test.rb",
73
+ "test/test_helper.rb"
74
+ ]
75
+
76
+ if s.respond_to? :specification_version then
77
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
78
+ s.specification_version = 3
79
+
80
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
81
+ s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
82
+ s.add_development_dependency(%q<flexmock>, [">= 0.8.6"])
83
+ s.add_runtime_dependency(%q<cssmin>, [">= 1.0.2"])
84
+ s.add_runtime_dependency(%q<jsmin>, [">= 1.0.1"])
85
+ else
86
+ s.add_dependency(%q<shoulda>, [">= 2.10.2"])
87
+ s.add_dependency(%q<flexmock>, [">= 0.8.6"])
88
+ s.add_dependency(%q<cssmin>, [">= 1.0.2"])
89
+ s.add_dependency(%q<jsmin>, [">= 1.0.1"])
90
+ end
91
+ else
92
+ s.add_dependency(%q<shoulda>, [">= 2.10.2"])
93
+ s.add_dependency(%q<flexmock>, [">= 0.8.6"])
94
+ s.add_dependency(%q<cssmin>, [">= 1.0.2"])
95
+ s.add_dependency(%q<jsmin>, [">= 1.0.1"])
96
+ end
97
+ end
98
+