|
| 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 |
0 commit comments