rails_asset_packager 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/CHANGELOG +7 -0
- data/Gemfile +4 -0
- data/README +174 -0
- data/Rakefile +2 -0
- data/lib/jsmin.rb +211 -0
- data/lib/rails_asset_packager/asset_package.rb +211 -0
- data/lib/rails_asset_packager/asset_package_helper.rb +39 -0
- data/lib/rails_asset_packager/railtie.rb +13 -0
- data/lib/rails_asset_packager/version.rb +3 -0
- data/lib/rails_asset_packager.rb +3 -0
- data/lib/tasks/asset_packager_tasks.rake +90 -0
- data/rails/init.rb +1 -0
- data/rails_asset_packager.gemspec +21 -0
- data/test/asset_package_helper_development_test.rb +102 -0
- data/test/asset_package_helper_production_test.rb +142 -0
- data/test/asset_packager_test.rb +91 -0
- data/test/asset_packages.yml +20 -0
- data/test/assets/javascripts/application.js +2 -0
- data/test/assets/javascripts/bar.js +4 -0
- data/test/assets/javascripts/controls.js +815 -0
- data/test/assets/javascripts/dragdrop.js +913 -0
- data/test/assets/javascripts/effects.js +958 -0
- data/test/assets/javascripts/foo.js +4 -0
- data/test/assets/javascripts/prototype.js +2006 -0
- data/test/assets/stylesheets/bar.css +16 -0
- data/test/assets/stylesheets/foo.css +16 -0
- data/test/assets/stylesheets/header.css +16 -0
- data/test/assets/stylesheets/screen.css +16 -0
- data/test/assets/stylesheets/subdir/bar.css +16 -0
- data/test/assets/stylesheets/subdir/foo.css +16 -0
- metadata +133 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
------------------------------------------------------------------------
|
2
|
+
v0.1.0 | plainprogrammer | 2011-01-16
|
3
|
+
|
4
|
+
* Converted project into Gem
|
5
|
+
* Added task to support S3 distribution (http://www.blog.bridgeutopiaweb.com/post/package-assets-for-rails-3-and-upload-to-amazon-s3/)
|
6
|
+
* Added handling of other static assets when pushing to S3.
|
7
|
+
------------------------------------------------------------------------
|
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
= RailsAssetPackager
|
2
|
+
|
3
|
+
Rails Asset Packager provides an easy way to combine and compress stylesheets
|
4
|
+
and Javascript files to improve performance for production environments. The
|
5
|
+
packager also provides a convenient way to push those packed files, along with
|
6
|
+
any other static assets to Amazon S3.
|
7
|
+
|
8
|
+
== Description
|
9
|
+
|
10
|
+
When it comes time to deploy your new web application, instead of
|
11
|
+
sending down a dozen JavaScript and CSS files full of formatting
|
12
|
+
and comments, this Rails plugin makes it simple to merge and
|
13
|
+
compress JavaScript and CSS down into one or more files, increasing
|
14
|
+
speed and saving bandwidth.
|
15
|
+
|
16
|
+
When in development, it allows you to use your original versions
|
17
|
+
and retain formatting and comments for readability and debugging.
|
18
|
+
|
19
|
+
This code is released under the MIT license (like Ruby). You're free
|
20
|
+
to rip it up, enhance it, etc. And if you make any enhancements,
|
21
|
+
I'd like to know so I can add them back in. Thanks!
|
22
|
+
|
23
|
+
== Credit
|
24
|
+
|
25
|
+
This gem is derived directly from the work of Scott Becker and Katherine G Pe
|
26
|
+
on the AssetPackager plugin for Rails.
|
27
|
+
|
28
|
+
== Key Features
|
29
|
+
|
30
|
+
* Merges and compresses JavaScript and CSS when running in production.
|
31
|
+
* Uses uncompressed originals when running in development.
|
32
|
+
* Can generate packages on demand in production
|
33
|
+
* Provides Rake tasks to pre-package and push static resources to Amazon S3
|
34
|
+
|
35
|
+
== Components
|
36
|
+
|
37
|
+
* Rake tasks for managing packages and pushing to S3
|
38
|
+
* Helper functions for including these JavaScript and CSS files in your views.
|
39
|
+
* YAML configuration file for mapping JavaScript and CSS files to packages.
|
40
|
+
* Section in YAML configuration for declaring folders of static assets to be
|
41
|
+
pushed to Amazon S3.
|
42
|
+
* Rake Task for auto-generating the YAML file from your existing JavaScript files.
|
43
|
+
|
44
|
+
== Updates
|
45
|
+
|
46
|
+
January 2011:
|
47
|
+
* Ported to be packaged as a Gem
|
48
|
+
* Leveraging Railties capabilities in Rails 3
|
49
|
+
* Added S3 publishing support
|
50
|
+
|
51
|
+
== How to Use:
|
52
|
+
|
53
|
+
1. Add the gem to your Gemfile
|
54
|
+
gem "rails_asset_packager"
|
55
|
+
|
56
|
+
2. Run the rake task "asset:packager:create_yml" to generate the /config/asset_packages.yml
|
57
|
+
file the first time. You will need to reorder files under 'base' so dependencies are loaded
|
58
|
+
in correct order. Feel free to rename or create new file packages.
|
59
|
+
|
60
|
+
IMPORTANT: JavaScript files can break once compressed if each statement doesn't end with a semi-colon.
|
61
|
+
The minifier puts multiple statements on one line, so if the semi-colon is missing, the statement may no
|
62
|
+
longer makes sense and cause a syntax error.
|
63
|
+
|
64
|
+
== Examples of config/asset_packages.yml
|
65
|
+
|
66
|
+
Example from a fresh rails app after running the rake task. (Stylesheets is blank because a
|
67
|
+
default rails app has no stylesheets yet.):
|
68
|
+
|
69
|
+
---
|
70
|
+
javascripts:
|
71
|
+
- base:
|
72
|
+
- prototype
|
73
|
+
- effects
|
74
|
+
- dragdrop
|
75
|
+
- controls
|
76
|
+
- application
|
77
|
+
stylesheets:
|
78
|
+
- base: []
|
79
|
+
uploads:
|
80
|
+
- images
|
81
|
+
|
82
|
+
Multiple packages:
|
83
|
+
|
84
|
+
---
|
85
|
+
javascripts:
|
86
|
+
- base:
|
87
|
+
- prototype
|
88
|
+
- effects
|
89
|
+
- controls
|
90
|
+
- dragdrop
|
91
|
+
- application
|
92
|
+
- secondary:
|
93
|
+
- foo
|
94
|
+
- bar
|
95
|
+
stylesheets:
|
96
|
+
- base:
|
97
|
+
- screen
|
98
|
+
- header
|
99
|
+
- secondary:
|
100
|
+
- foo
|
101
|
+
- bar
|
102
|
+
uploads:
|
103
|
+
- images
|
104
|
+
|
105
|
+
3. Run the rake task "asset:packager:build_all" to generate the compressed, merged versions
|
106
|
+
for each package. Whenever you rearrange the yaml file, you'll need to run this task again.
|
107
|
+
|
108
|
+
Merging and compressing is expensive, so this is something we want to do once, not every time
|
109
|
+
your app starts. Thats why its a rake task. You can run this task via Capistrano when deploying
|
110
|
+
to avoid an initially slow request the first time a page is generated.
|
111
|
+
|
112
|
+
Note: The package will be generated on the fly if it doesn't yet exist, so you don't *need*
|
113
|
+
to run the rake task when deploying, its just recommended for speeding up initial requests.
|
114
|
+
|
115
|
+
4. Run the rake task "rake asset:cache:production" to push your static assets and compressed
|
116
|
+
packages up to Amazon S3.
|
117
|
+
|
118
|
+
Note: You need to have a file at config/s3.yml that defines your Amazon S3 access credentials.
|
119
|
+
|
120
|
+
5. Use the helper functions whenever including these files in your application. See below for examples.
|
121
|
+
|
122
|
+
6. Potential warning: css compressor function currently removes CSS comments. This might blow
|
123
|
+
away some CSS hackery. To disable comment removal, comment out /lib/synthesis/asset_package.rb line 176.
|
124
|
+
|
125
|
+
== JavaScript Examples
|
126
|
+
|
127
|
+
Example call (based on above /config/asset_packages.yml):
|
128
|
+
<%= javascript_include_merged :base %>
|
129
|
+
|
130
|
+
In development, this generates:
|
131
|
+
<script type="text/javascript" src="/javascripts/prototype.js?1228027240"></script>
|
132
|
+
<script type="text/javascript" src="/javascripts/effects.js?1228027240"></script>
|
133
|
+
<script type="text/javascript" src="/javascripts/controls.js?1228027240"></script>
|
134
|
+
<script type="text/javascript" src="/javascripts/dragdrop.js?1228027240"></script>
|
135
|
+
<script type="text/javascript" src="/javascripts/application.js?1228027240"></script>
|
136
|
+
|
137
|
+
In production, this generates:
|
138
|
+
<script type="text/javascript" src="/javascripts/base_packaged.js?123456789"></script>
|
139
|
+
|
140
|
+
== Stylesheet Examples
|
141
|
+
|
142
|
+
Example call:
|
143
|
+
<%= stylesheet_link_merged :base %>
|
144
|
+
|
145
|
+
In development, this generates:
|
146
|
+
<link href="/stylesheets/screen.css?1228027240" media="screen" rel="Stylesheet" type="text/css" />
|
147
|
+
<link href="/stylesheets/header.css?1228027240" media="screen" rel="Stylesheet" type="text/css" />
|
148
|
+
|
149
|
+
In production this generates:
|
150
|
+
<link href="/stylesheets/base_packaged.css?1228027240" media="screen" rel="Stylesheet" type="text/css" />
|
151
|
+
|
152
|
+
== Different CSS Media
|
153
|
+
|
154
|
+
All options for stylesheet_link_tag still work, so if you want to specify a different media type:
|
155
|
+
<%= stylesheet_link_merged :secondary, 'media' => 'print' %>
|
156
|
+
|
157
|
+
== Rake tasks
|
158
|
+
|
159
|
+
rake asset:packager:build_all # Merge and compress assets
|
160
|
+
rake asset:packager:create_yml # Generate asset_packages.yml from existing assets
|
161
|
+
rake asset:packager:delete_all # Delete all asset builds
|
162
|
+
rake asset:cache:s3 # update s3
|
163
|
+
rake asset:cache:production # cache assets and update s3 for production
|
164
|
+
|
165
|
+
== License
|
166
|
+
Copyright (c) 2006-2008 Scott Becker - http://synthesis.sbecker.net
|
167
|
+
Copyright (c) 2011 James Thompson - james@plainprograms.com
|
168
|
+
Contact via Github for change requests, etc.
|
169
|
+
|
170
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
171
|
+
|
172
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
173
|
+
|
174
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/jsmin.rb
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# jsmin.rb 2007-07-20
|
3
|
+
# Author: Uladzislau Latynski
|
4
|
+
# This work is a translation from C to Ruby of jsmin.c published by
|
5
|
+
# Douglas Crockford. Permission is hereby granted to use the Ruby
|
6
|
+
# version under the same conditions as the jsmin.c on which it is
|
7
|
+
# based.
|
8
|
+
#
|
9
|
+
# /* jsmin.c
|
10
|
+
# 2003-04-21
|
11
|
+
#
|
12
|
+
# Copyright (c) 2002 Douglas Crockford (www.crockford.com)
|
13
|
+
#
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
15
|
+
# this software and associated documentation files (the "Software"), to deal in
|
16
|
+
# the Software without restriction, including without limitation the rights to
|
17
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
18
|
+
# of the Software, and to permit persons to whom the Software is furnished to do
|
19
|
+
# so, subject to the following conditions:
|
20
|
+
#
|
21
|
+
# The above copyright notice and this permission notice shall be included in all
|
22
|
+
# copies or substantial portions of the Software.
|
23
|
+
#
|
24
|
+
# The Software shall be used for Good, not Evil.
|
25
|
+
#
|
26
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
27
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
28
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
29
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
30
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
31
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
32
|
+
# SOFTWARE.
|
33
|
+
|
34
|
+
# Ruby 1.9 Compatibility Fix - the below isAlphanum uses Fixnum#ord to be compatible with Ruby 1.9 and 1.8.7
|
35
|
+
# Fixnum#ord is not found by default in 1.8.6, so monkey patch it in:
|
36
|
+
if RUBY_VERSION == '1.8.6'
|
37
|
+
class Fixnum; def ord; return self; end; end
|
38
|
+
end
|
39
|
+
|
40
|
+
EOF = -1
|
41
|
+
$theA = ""
|
42
|
+
$theB = ""
|
43
|
+
|
44
|
+
# isAlphanum -- return true if the character is a letter, digit, underscore,
|
45
|
+
# dollar sign, or non-ASCII character
|
46
|
+
def isAlphanum(c)
|
47
|
+
return false if !c || c == EOF
|
48
|
+
return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
|
49
|
+
(c >= 'A' && c <= 'Z') || c == '_' || c == '$' ||
|
50
|
+
c == '\\' || c[0].ord > 126)
|
51
|
+
end
|
52
|
+
|
53
|
+
# get -- return the next character from stdin. Watch out for lookahead. If
|
54
|
+
# the character is a control character, translate it to a space or linefeed.
|
55
|
+
def get()
|
56
|
+
c = $stdin.getc
|
57
|
+
return EOF if(!c)
|
58
|
+
c = c.chr
|
59
|
+
return c if (c >= " " || c == "\n" || c.unpack("c") == EOF)
|
60
|
+
return "\n" if (c == "\r")
|
61
|
+
return " "
|
62
|
+
end
|
63
|
+
|
64
|
+
# Get the next character without getting it.
|
65
|
+
def peek()
|
66
|
+
lookaheadChar = $stdin.getc
|
67
|
+
$stdin.ungetc(lookaheadChar)
|
68
|
+
return lookaheadChar.chr
|
69
|
+
end
|
70
|
+
|
71
|
+
# mynext -- get the next character, excluding comments.
|
72
|
+
# peek() is used to see if a '/' is followed by a '/' or '*'.
|
73
|
+
def mynext()
|
74
|
+
c = get
|
75
|
+
if (c == "/")
|
76
|
+
if(peek == "/")
|
77
|
+
while(true)
|
78
|
+
c = get
|
79
|
+
if (c <= "\n")
|
80
|
+
return c
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
if(peek == "*")
|
85
|
+
get
|
86
|
+
while(true)
|
87
|
+
case get
|
88
|
+
when "*"
|
89
|
+
if (peek == "/")
|
90
|
+
get
|
91
|
+
return " "
|
92
|
+
end
|
93
|
+
when EOF
|
94
|
+
raise "Unterminated comment"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
return c
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
# action -- do something! What you do is determined by the argument: 1
|
104
|
+
# Output A. Copy B to A. Get the next B. 2 Copy B to A. Get the next B.
|
105
|
+
# (Delete A). 3 Get the next B. (Delete B). action treats a string as a
|
106
|
+
# single character. Wow! action recognizes a regular expression if it is
|
107
|
+
# preceded by ( or , or =.
|
108
|
+
def action(a)
|
109
|
+
if(a==1)
|
110
|
+
$stdout.write $theA
|
111
|
+
end
|
112
|
+
if(a==1 || a==2)
|
113
|
+
$theA = $theB
|
114
|
+
if ($theA == "\'" || $theA == "\"")
|
115
|
+
while (true)
|
116
|
+
$stdout.write $theA
|
117
|
+
$theA = get
|
118
|
+
break if ($theA == $theB)
|
119
|
+
raise "Unterminated string literal" if ($theA <= "\n")
|
120
|
+
if ($theA == "\\")
|
121
|
+
$stdout.write $theA
|
122
|
+
$theA = get
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
if(a==1 || a==2 || a==3)
|
128
|
+
$theB = mynext
|
129
|
+
if ($theB == "/" && ($theA == "(" || $theA == "," || $theA == "=" ||
|
130
|
+
$theA == ":" || $theA == "[" || $theA == "!" ||
|
131
|
+
$theA == "&" || $theA == "|" || $theA == "?" ||
|
132
|
+
$theA == "{" || $theA == "}" || $theA == ";" ||
|
133
|
+
$theA == "\n"))
|
134
|
+
$stdout.write $theA
|
135
|
+
$stdout.write $theB
|
136
|
+
while (true)
|
137
|
+
$theA = get
|
138
|
+
if ($theA == "/")
|
139
|
+
break
|
140
|
+
elsif ($theA == "\\")
|
141
|
+
$stdout.write $theA
|
142
|
+
$theA = get
|
143
|
+
elsif ($theA <= "\n")
|
144
|
+
raise "Unterminated RegExp Literal"
|
145
|
+
end
|
146
|
+
$stdout.write $theA
|
147
|
+
end
|
148
|
+
$theB = mynext
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# jsmin -- Copy the input to the output, deleting the characters which are
|
154
|
+
# insignificant to JavaScript. Comments will be removed. Tabs will be
|
155
|
+
# replaced with spaces. Carriage returns will be replaced with linefeeds.
|
156
|
+
# Most spaces and linefeeds will be removed.
|
157
|
+
def jsmin
|
158
|
+
$theA = "\n"
|
159
|
+
action(3)
|
160
|
+
while ($theA != EOF)
|
161
|
+
case $theA
|
162
|
+
when " "
|
163
|
+
if (isAlphanum($theB))
|
164
|
+
action(1)
|
165
|
+
else
|
166
|
+
action(2)
|
167
|
+
end
|
168
|
+
when "\n"
|
169
|
+
case ($theB)
|
170
|
+
when "{","[","(","+","-"
|
171
|
+
action(1)
|
172
|
+
when " "
|
173
|
+
action(3)
|
174
|
+
else
|
175
|
+
if (isAlphanum($theB))
|
176
|
+
action(1)
|
177
|
+
else
|
178
|
+
action(2)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
else
|
182
|
+
case ($theB)
|
183
|
+
when " "
|
184
|
+
if (isAlphanum($theA))
|
185
|
+
action(1)
|
186
|
+
else
|
187
|
+
action(3)
|
188
|
+
end
|
189
|
+
when "\n"
|
190
|
+
case ($theA)
|
191
|
+
when "}","]",")","+","-","\"","\\", "'", '"'
|
192
|
+
action(1)
|
193
|
+
else
|
194
|
+
if (isAlphanum($theA))
|
195
|
+
action(1)
|
196
|
+
else
|
197
|
+
action(3)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
else
|
201
|
+
action(1)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
ARGV.each do |anArg|
|
208
|
+
$stdout.write "// #{anArg}\n"
|
209
|
+
end
|
210
|
+
|
211
|
+
jsmin
|
@@ -0,0 +1,211 @@
|
|
1
|
+
module RailsAssetPackager
|
2
|
+
class AssetPackage
|
3
|
+
|
4
|
+
@asset_base_path = "#{Rails.root}/public"
|
5
|
+
@asset_packages_yml = File.exists?("#{Rails.root}/config/asset_packages.yml") ? YAML.load_file("#{Rails.root}/config/asset_packages.yml") : nil
|
6
|
+
|
7
|
+
# singleton methods
|
8
|
+
class << self
|
9
|
+
attr_accessor :asset_base_path,
|
10
|
+
:asset_packages_yml
|
11
|
+
|
12
|
+
attr_writer :merge_environments
|
13
|
+
|
14
|
+
def merge_environments
|
15
|
+
@merge_environments ||= ["production"]
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_path(path)
|
19
|
+
/^(?:(.*)\/)?([^\/]+)$/.match(path).to_a
|
20
|
+
end
|
21
|
+
|
22
|
+
def find_by_type(asset_type)
|
23
|
+
asset_packages_yml[asset_type].map { |p| self.new(asset_type, p) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_by_target(asset_type, target)
|
27
|
+
package_hash = asset_packages_yml[asset_type].find {|p| p.keys.first == target }
|
28
|
+
package_hash ? self.new(asset_type, package_hash) : nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def find_by_source(asset_type, source)
|
32
|
+
path_parts = parse_path(source)
|
33
|
+
package_hash = asset_packages_yml[asset_type].find do |p|
|
34
|
+
key = p.keys.first
|
35
|
+
p[key].include?(path_parts[2]) && (parse_path(key)[1] == path_parts[1])
|
36
|
+
end
|
37
|
+
package_hash ? self.new(asset_type, package_hash) : nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def targets_from_sources(asset_type, sources)
|
41
|
+
package_names = Array.new
|
42
|
+
sources.each do |source|
|
43
|
+
package = find_by_target(asset_type, source) || find_by_source(asset_type, source)
|
44
|
+
package_names << (package ? package.current_file : source)
|
45
|
+
end
|
46
|
+
package_names.uniq
|
47
|
+
end
|
48
|
+
|
49
|
+
def sources_from_targets(asset_type, targets)
|
50
|
+
source_names = Array.new
|
51
|
+
targets.each do |target|
|
52
|
+
package = find_by_target(asset_type, target)
|
53
|
+
source_names += (package ? package.sources.collect do |src|
|
54
|
+
package.target_dir.gsub(/^(.+)$/, '\1/') + src
|
55
|
+
end : target.to_a)
|
56
|
+
end
|
57
|
+
source_names.uniq
|
58
|
+
end
|
59
|
+
|
60
|
+
def build_all
|
61
|
+
asset_packages_yml.keys.each do |asset_type|
|
62
|
+
asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).build } unless asset_type == 'uploads'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def delete_all
|
67
|
+
asset_packages_yml.keys.each do |asset_type|
|
68
|
+
asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).delete_previous_build } unless asset_type == 'uploads'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_yml
|
73
|
+
unless File.exists?("#{Rails.root}/config/asset_packages.yml")
|
74
|
+
asset_yml = Hash.new
|
75
|
+
|
76
|
+
asset_yml['javascripts'] = [{"base" => build_file_list("#{Rails.root}/public/javascripts", "js")}]
|
77
|
+
asset_yml['stylesheets'] = [{"base" => build_file_list("#{Rails.root}/public/stylesheets", "css")}]
|
78
|
+
asset_yml['uploads'] = ["images"]
|
79
|
+
|
80
|
+
File.open("#{Rails.root}/config/asset_packages.yml", "w") do |out|
|
81
|
+
YAML.dump(asset_yml, out)
|
82
|
+
end
|
83
|
+
|
84
|
+
log "config/asset_packages.yml example file created!"
|
85
|
+
log "Please reorder files under 'base' so dependencies are loaded in correct order."
|
86
|
+
else
|
87
|
+
log "config/asset_packages.yml already exists. Aborting task..."
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
# instance methods
|
94
|
+
attr_accessor :asset_type, :target, :target_dir, :sources
|
95
|
+
|
96
|
+
def initialize(asset_type, package_hash)
|
97
|
+
target_parts = self.class.parse_path(package_hash.keys.first)
|
98
|
+
@target_dir = target_parts[1].to_s
|
99
|
+
@target = target_parts[2].to_s
|
100
|
+
@sources = package_hash[package_hash.keys.first]
|
101
|
+
@asset_type = asset_type
|
102
|
+
@asset_path = "#{self.class.asset_base_path}/#{@asset_type}#{@target_dir.gsub(/^(.+)$/, '/\1')}"
|
103
|
+
@extension = get_extension
|
104
|
+
@file_name = "#{@target}_packaged.#{@extension}"
|
105
|
+
@full_path = File.join(@asset_path, @file_name)
|
106
|
+
end
|
107
|
+
|
108
|
+
def package_exists?
|
109
|
+
File.exists?(@full_path)
|
110
|
+
end
|
111
|
+
|
112
|
+
def current_file
|
113
|
+
build unless package_exists?
|
114
|
+
|
115
|
+
path = @target_dir.gsub(/^(.+)$/, '\1/')
|
116
|
+
"#{path}#{@target}_packaged"
|
117
|
+
end
|
118
|
+
|
119
|
+
def build
|
120
|
+
delete_previous_build
|
121
|
+
create_new_build
|
122
|
+
end
|
123
|
+
|
124
|
+
def delete_previous_build
|
125
|
+
File.delete(@full_path) if File.exists?(@full_path)
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
def create_new_build
|
130
|
+
new_build_path = "#{@asset_path}/#{@target}_packaged.#{@extension}"
|
131
|
+
if File.exists?(new_build_path)
|
132
|
+
log "Latest version already exists: #{new_build_path}"
|
133
|
+
else
|
134
|
+
File.open(new_build_path, "w") {|f| f.write(compressed_file) }
|
135
|
+
log "Created #{new_build_path}"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def merged_file
|
140
|
+
merged_file = ""
|
141
|
+
@sources.each {|s|
|
142
|
+
File.open("#{@asset_path}/#{s}.#{@extension}", "r") { |f|
|
143
|
+
merged_file += f.read + "\n"
|
144
|
+
}
|
145
|
+
}
|
146
|
+
merged_file
|
147
|
+
end
|
148
|
+
|
149
|
+
def compressed_file
|
150
|
+
case @asset_type
|
151
|
+
when "javascripts" then compress_js(merged_file)
|
152
|
+
when "stylesheets" then compress_css(merged_file)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def compress_js(source)
|
157
|
+
jsmin_path = File.dirname(__FILE__) + '/..'
|
158
|
+
tmp_path = "#{Rails.root}/tmp/#{@target}_packaged"
|
159
|
+
|
160
|
+
# write out to a temp file
|
161
|
+
File.open("#{tmp_path}_uncompressed.js", "w") {|f| f.write(source) }
|
162
|
+
|
163
|
+
# compress file with JSMin library
|
164
|
+
`ruby #{jsmin_path}/jsmin.rb <#{tmp_path}_uncompressed.js >#{tmp_path}_compressed.js \n`
|
165
|
+
|
166
|
+
# read it back in and trim it
|
167
|
+
result = ""
|
168
|
+
File.open("#{tmp_path}_compressed.js", "r") { |f| result += f.read.strip }
|
169
|
+
|
170
|
+
# delete temp files if they exist
|
171
|
+
File.delete("#{tmp_path}_uncompressed.js") if File.exists?("#{tmp_path}_uncompressed.js")
|
172
|
+
File.delete("#{tmp_path}_compressed.js") if File.exists?("#{tmp_path}_compressed.js")
|
173
|
+
|
174
|
+
result
|
175
|
+
end
|
176
|
+
|
177
|
+
def compress_css(source)
|
178
|
+
source.gsub!(/\s+/, " ") # collapse space
|
179
|
+
source.gsub!(/\/\*(.*?)\*\//, "") # remove comments - caution, might want to remove this if using css hacks
|
180
|
+
source.gsub!(/\} /, "}\n") # add line breaks
|
181
|
+
source.gsub!(/\n$/, "") # remove last break
|
182
|
+
source.gsub!(/ \{ /, " {") # trim inside brackets
|
183
|
+
source.gsub!(/; \}/, "}") # trim inside brackets
|
184
|
+
source
|
185
|
+
end
|
186
|
+
|
187
|
+
def get_extension
|
188
|
+
case @asset_type
|
189
|
+
when "javascripts" then "js"
|
190
|
+
when "stylesheets" then "css"
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def log(message)
|
195
|
+
self.class.log(message)
|
196
|
+
end
|
197
|
+
|
198
|
+
def self.log(message)
|
199
|
+
puts message
|
200
|
+
end
|
201
|
+
|
202
|
+
def self.build_file_list(path, extension)
|
203
|
+
re = Regexp.new(".#{extension}\\z")
|
204
|
+
file_list = Dir.new(path).entries.delete_if { |x| ! (x =~ re) }.map {|x| x.chomp(".#{extension}")}
|
205
|
+
# reverse javascript entries so prototype comes first on a base rails app
|
206
|
+
file_list.reverse! if extension == "js"
|
207
|
+
file_list
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module RailsAssetPackager
|
2
|
+
module AssetPackageHelper
|
3
|
+
|
4
|
+
def should_merge?
|
5
|
+
AssetPackage.merge_environments.include?(Rails.env)
|
6
|
+
end
|
7
|
+
|
8
|
+
def javascript_include_merged(*sources)
|
9
|
+
options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
|
10
|
+
|
11
|
+
if sources.include?(:defaults)
|
12
|
+
sources = sources[0..(sources.index(:defaults))] +
|
13
|
+
['prototype', 'effects', 'dragdrop', 'controls'] +
|
14
|
+
(File.exists?("#{Rails.root}/public/javascripts/application.js") ? ['application'] : []) +
|
15
|
+
sources[(sources.index(:defaults) + 1)..sources.length]
|
16
|
+
sources.delete(:defaults)
|
17
|
+
end
|
18
|
+
|
19
|
+
sources.collect!{|s| s.to_s}
|
20
|
+
sources = (should_merge? ?
|
21
|
+
AssetPackage.targets_from_sources("javascripts", sources) :
|
22
|
+
AssetPackage.sources_from_targets("javascripts", sources))
|
23
|
+
|
24
|
+
sources.collect {|source| javascript_include_tag(source, options) }.join("\n")
|
25
|
+
end
|
26
|
+
|
27
|
+
def stylesheet_link_merged(*sources)
|
28
|
+
options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
|
29
|
+
|
30
|
+
sources.collect!{|s| s.to_s}
|
31
|
+
sources = (should_merge? ?
|
32
|
+
AssetPackage.targets_from_sources("stylesheets", sources) :
|
33
|
+
AssetPackage.sources_from_targets("stylesheets", sources))
|
34
|
+
|
35
|
+
sources.collect { |source| stylesheet_link_tag(source, options) }.join("\n")
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module RailsAssetPackager
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer "RailsAssetPackager.initialize" do
|
6
|
+
ActionView::Base.send :include, RailsAssetPackager::AssetPackageHelper
|
7
|
+
end
|
8
|
+
|
9
|
+
rake_tasks do
|
10
|
+
load "tasks/asset_packager_tasks.rake"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|