smart_asset 0.3.2 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +124 -1
- data/lib/smart_asset/adapters/rails3.rb +13 -2
- data/lib/smart_asset/helper.rb +4 -2
- data/lib/smart_asset/version.rb +1 -1
- data/lib/smart_asset.rb +6 -2
- metadata +3 -3
data/README.md
CHANGED
@@ -3,9 +3,132 @@ SmartAsset
|
|
3
3
|
|
4
4
|
Smart asset packaging for Rails and Sinatra.
|
5
5
|
|
6
|
-
|
6
|
+
Features
|
7
|
+
--------
|
8
|
+
|
9
|
+
Similar to <code>AssetPackager</code>, but with the following changes:
|
10
|
+
|
11
|
+
* Git modified date/time for package version
|
12
|
+
* Google Closure Compiler for javascript compression
|
13
|
+
* Framework agnostic (adapters provided for Rails 2, Rails 3, and Sinatra)
|
14
|
+
|
15
|
+
<a name="installation"></a>
|
16
|
+
|
17
|
+
Installation
|
7
18
|
------------
|
8
19
|
|
20
|
+
### Install Gem
|
21
|
+
|
9
22
|
<pre>
|
10
23
|
gem install smart_asset
|
24
|
+
</pre>
|
25
|
+
|
26
|
+
### Rails 2
|
27
|
+
|
28
|
+
#### config/environment.rb
|
29
|
+
|
30
|
+
<pre>
|
31
|
+
config.gem 'smart_asset'
|
32
|
+
</pre>
|
33
|
+
|
34
|
+
### Rails 3
|
35
|
+
|
36
|
+
#### Gemfile
|
37
|
+
|
38
|
+
<pre>
|
39
|
+
gem 'smart_asset'
|
40
|
+
</pre>
|
41
|
+
|
42
|
+
### Sinatra
|
43
|
+
|
44
|
+
<pre>
|
45
|
+
require 'sinatra/base'
|
46
|
+
require 'smart_asset'
|
47
|
+
|
48
|
+
class Application < Sinatra::Base
|
49
|
+
include SmartAsset::Adapters::Sinatra
|
50
|
+
end
|
51
|
+
</pre>
|
52
|
+
|
53
|
+
Create Configuration File
|
54
|
+
-------------------------
|
55
|
+
|
56
|
+
### config/assets.yml
|
57
|
+
|
58
|
+
<pre>
|
59
|
+
javascripts:
|
60
|
+
package_1:
|
61
|
+
- jquery/jquery
|
62
|
+
- underscore
|
63
|
+
package_2:
|
64
|
+
- front_page
|
65
|
+
stylesheets:
|
66
|
+
package_1:
|
67
|
+
- blueprint/blueprint
|
68
|
+
- 960
|
69
|
+
package_2:
|
70
|
+
- front_page
|
71
|
+
</pre>
|
72
|
+
|
73
|
+
By default, SmartAsset will look for assets in <code>public/javascripts</code> and <code>public/stylesheets</code>.
|
74
|
+
|
75
|
+
Create Packaged Assets
|
76
|
+
----------------------
|
77
|
+
|
78
|
+
<code>cd</code> to your project and run
|
79
|
+
|
80
|
+
<pre>
|
81
|
+
smart_asset
|
82
|
+
</pre>
|
83
|
+
|
84
|
+
If your project is Git version controlled, only the assets that have changed are repackaged.
|
85
|
+
|
86
|
+
Otherwise, all packages generate every time.
|
87
|
+
|
88
|
+
Include Packages in Your Template
|
89
|
+
---------------------------------
|
90
|
+
|
91
|
+
<pre>
|
92
|
+
<%= javascript_include_merged :package_1, :package_2 %>
|
93
|
+
<%= stylesheet_link_merged :package_1, :package_2 %>
|
94
|
+
</pre>
|
95
|
+
|
96
|
+
Migrating from AssetPackager
|
97
|
+
----------------------------
|
98
|
+
|
99
|
+
* <code>rm vendor/plugins/asset\_packager</code>
|
100
|
+
* <a href="#installation">Install SmartAsset</a>
|
101
|
+
* Move <code>config/asset\_packages.yml</code> to <code>config/assets.yml</code>
|
102
|
+
* Instead of running <code>rake asset:packager:build_all</code>, run <code>smart\_asset</code>
|
103
|
+
|
104
|
+
Other Options
|
105
|
+
-------------
|
106
|
+
|
107
|
+
You may add extra options to your <code>config/assets.yml</code> file.
|
108
|
+
|
109
|
+
All the values below are the default values, excluding <code>append\_random</code> and <code>asset\_host</code>:
|
110
|
+
|
111
|
+
<pre>
|
112
|
+
# Append random numbers to script paths (defaults to true in development mode)
|
113
|
+
append_random: false
|
114
|
+
|
115
|
+
# Asset host URL (defaults to ActionController::Base.asset_host or nil)
|
116
|
+
asset_host:
|
117
|
+
production: http://assets%d.mydomain.com
|
118
|
+
|
119
|
+
# How many asset hosts you have (if asset_host defined with %d)
|
120
|
+
asset_host_count: 4
|
121
|
+
|
122
|
+
# Public directory
|
123
|
+
public: public
|
124
|
+
|
125
|
+
# Package destination directory (within the public directory)
|
126
|
+
destination:
|
127
|
+
javascripts: javascripts/packaged
|
128
|
+
stylesheets: stylesheets/packaged
|
129
|
+
|
130
|
+
# Asset source directories (within the public directory)
|
131
|
+
sources:
|
132
|
+
javascripts: javascripts
|
133
|
+
stylesheets: stylesheets
|
11
134
|
</pre>
|
@@ -1,5 +1,16 @@
|
|
1
|
-
|
2
|
-
SmartAsset
|
1
|
+
if Rails.root.nil?
|
2
|
+
class SmartAsset
|
3
|
+
class SmartAssetRailtie < Rails::Railtie
|
4
|
+
initializer "smart_asset_railtie.configure_rails_initialization" do
|
5
|
+
SmartAsset.env = Rails.env
|
6
|
+
SmartAsset.load_config(Rails.root)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
else
|
11
|
+
SmartAsset.env = Rails.env
|
12
|
+
SmartAsset.load_config(Rails.root)
|
13
|
+
end
|
3
14
|
|
4
15
|
ActionController::Base.send(:include, SmartAsset::Helper)
|
5
16
|
ActionController::Base.helper(SmartAsset::Helper)
|
data/lib/smart_asset/helper.rb
CHANGED
@@ -2,17 +2,19 @@ class SmartAsset
|
|
2
2
|
module Helper
|
3
3
|
|
4
4
|
def javascript_include_merged(*javascripts)
|
5
|
+
append = SmartAsset.append_random ? "?#{rand.to_s[2..-1]}" : ''
|
5
6
|
output = javascript_paths(*javascripts).collect { |js|
|
6
|
-
"<script src=\"#{SmartAsset.prepend_asset_host js}\"
|
7
|
+
"<script src=\"#{SmartAsset.prepend_asset_host js}#{append}\"></script>"
|
7
8
|
}.join("\n")
|
8
9
|
defined?(Rails) && Rails.version[0..0] == '3' ? output.html_safe : output
|
9
10
|
end
|
10
11
|
|
11
12
|
def stylesheet_link_merged(*stylesheets)
|
13
|
+
append = SmartAsset.append_random ? "?#{rand.to_s[2..-1]}" : ''
|
12
14
|
options = stylesheets.last.is_a?(::Hash) ? stylesheets.pop : {}
|
13
15
|
options[:media] ||= 'screen'
|
14
16
|
output = stylesheet_paths(*stylesheets).collect { |css|
|
15
|
-
"<link href=\"#{SmartAsset.prepend_asset_host css}\" media=\"#{options[:media]}\" rel=\"
|
17
|
+
"<link href=\"#{SmartAsset.prepend_asset_host css}#{append}\" media=\"#{options[:media]}\" rel=\"stylesheet\" />"
|
16
18
|
}.join("\n")
|
17
19
|
defined?(Rails) && Rails.version[0..0] == '3' ? output.html_safe : output
|
18
20
|
end
|
data/lib/smart_asset/version.rb
CHANGED
data/lib/smart_asset.rb
CHANGED
@@ -14,7 +14,7 @@ require 'smart_asset/version'
|
|
14
14
|
class SmartAsset
|
15
15
|
class <<self
|
16
16
|
|
17
|
-
attr_accessor :asset_host, :asset_counter, :cache, :config, :dest, :env, :envs, :pub, :root, :sources
|
17
|
+
attr_accessor :append_random, :asset_host, :asset_counter, :cache, :config, :dest, :env, :envs, :pub, :root, :sources
|
18
18
|
|
19
19
|
BIN = File.expand_path(File.dirname(__FILE__) + '/../bin')
|
20
20
|
CLOSURE_COMPILER = BIN + '/closure_compiler.jar'
|
@@ -39,7 +39,7 @@ class SmartAsset
|
|
39
39
|
files.each do |file|
|
40
40
|
if File.exists?("#{dir}/#{file}.#{ext}")
|
41
41
|
mod = `cd #{@root} && git log --pretty=format:%cd -n 1 --date=iso #{@config['public']}/#{@sources[type]}/#{file}.#{ext}`
|
42
|
-
if mod.strip.empty?
|
42
|
+
if mod.strip.empty? || mod.include?('command not found')
|
43
43
|
modified << Time.now.utc.strftime("%Y%m%d%H%M%S")
|
44
44
|
else
|
45
45
|
modified << Time.parse(mod).utc.strftime("%Y%m%d%H%M%S")
|
@@ -90,6 +90,9 @@ class SmartAsset
|
|
90
90
|
@root = File.expand_path(root)
|
91
91
|
@config = YAML::load(File.read("#{@root}/#{relative_config}"))
|
92
92
|
|
93
|
+
if @config['append_random'].nil?
|
94
|
+
@config['append_random'] = @env == 'development'
|
95
|
+
end
|
93
96
|
@config['asset_host_count'] ||= 4
|
94
97
|
@config['asset_host'] ||= ActionController::Base.asset_host rescue nil
|
95
98
|
@config['environments'] ||= %w(production)
|
@@ -110,6 +113,7 @@ class SmartAsset
|
|
110
113
|
end
|
111
114
|
end
|
112
115
|
|
116
|
+
@append_random = @config['append_random']
|
113
117
|
@asset_host = @config['asset_host']
|
114
118
|
@envs = @config['environments']
|
115
119
|
@sources = @config['sources']
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 4
|
9
|
+
version: 0.3.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Winton Welsh
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-12-
|
17
|
+
date: 2010-12-09 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|