Skip to content

Commit 3824ba1

Browse files
committed
Initial.
0 parents  commit 3824ba1

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Why you want it
2+
---------------
3+
4+
$ bundle exec rake assets:precompile:profile
5+
application.js 6663ms
6+
new_project.js 367ms
7+
vendor.js 966ms
8+
application.css 8579ms
9+
styleguide-extras.css 271ms
10+
styleguide.css 429ms
11+
Compilation finished in 17765ms.
12+
13+
How to use it
14+
-------------
15+
16+
# Gemfile
17+
gem 'rails-asset_profile', github: 'nadarei/rails-asset_profile', branch: 'master'
18+
19+
License
20+
-------
21+
22+
MIT.
23+

lib/rails/asset_profile.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Rails
2+
module AssetProfile
3+
class Engine < Rails::Engine
4+
5+
end
6+
end
7+
end

lib/tasks/assets.rake

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
desc "Compile all assets named in config.assets.precompile, and show breakdown of their compilation speed"
2+
task :'assets:precompile:profile' => ['assets:environment'] do
3+
module Sprockets
4+
class StaticCompiler
5+
# Re-implement #compile to measure the time it takes.
6+
def compile
7+
time, result = measure { do_compile }
8+
puts "Compilation finished in %ims." % [time]
9+
result
10+
end
11+
12+
# Re-implement #compile to measuge the time and print it.
13+
def do_compile
14+
manifest = {}
15+
env.each_logical_path do |logical_path|
16+
next unless compile_path?(logical_path)
17+
18+
show = show_message?(logical_path)
19+
print "%-60s" % [logical_path] if show
20+
21+
time, _ = measure {
22+
if asset = env.find_asset(logical_path)
23+
write_asset_to(asset, asset.logical_path)
24+
write_asset_to(asset, asset.digest_path) if @digest
25+
manifest[logical_path] = path_for(asset)
26+
end
27+
}
28+
29+
puts "%ims" % [time] if show
30+
end
31+
32+
write_manifest(manifest) if @manifest
33+
end
34+
35+
# Re-implement write_asset to write to digest and local path simultaneously
36+
def write_asset_to(asset, path)
37+
filename = File.join(target, path)
38+
FileUtils.mkdir_p File.dirname(filename)
39+
asset.write_to(filename)
40+
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
41+
path
42+
end
43+
44+
private
45+
46+
def show_message?(path)
47+
return true if ENV['VERBOSE'] || ENV['verbose']
48+
path =~ /\.(css|js)$/
49+
end
50+
51+
def measure(&blk)
52+
t = Time.now
53+
output = yield
54+
[((Time.now - t) * 1000).to_i, output]
55+
end
56+
end
57+
end
58+
59+
Rake::Task[:'assets:precompile:primary'].invoke
60+
end

rails-asset_profile.gemspec

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Gem::Specification.new do |s|
2+
s.name = "rails-asset_profile"
3+
s.version = "0.0.1"
4+
s.summary = "Profiled asset compilation for Rails"
5+
s.description = "Shows you stats on how long each asset compiles."
6+
s.authors = ["Rico Sta. Cruz"]
7+
s.email = ["[email protected]"]
8+
s.homepage = "http://github.com/nadarei/rails-asset_profile"
9+
s.files = `git ls-files`.strip.split("\n")
10+
s.executables = Dir["bin/*"].map { |f| File.basename(f) }
11+
12+
s.add_dependency "rails", "~> 3.2.0"
13+
end

0 commit comments

Comments
 (0)