From b41bd968c6734737d14c542c514b8deef30a3ffe Mon Sep 17 00:00:00 2001 From: "Mark J. Titorenko" Date: Tue, 4 Oct 2011 23:05:01 +0100 Subject: assets compilation task refactoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/lib/sprockets/assets.rake | 19 ++++++----- actionpack/lib/sprockets/static_compiler.rb | 49 +++++++++++++++++------------ 2 files changed, 38 insertions(+), 30 deletions(-) (limited to 'actionpack/lib/sprockets') diff --git a/actionpack/lib/sprockets/assets.rake b/actionpack/lib/sprockets/assets.rake index 7ab079c626..8f993b0abb 100644 --- a/actionpack/lib/sprockets/assets.rake +++ b/actionpack/lib/sprockets/assets.rake @@ -28,12 +28,18 @@ namespace :assets do config = Rails.application.config config.assets.compile = true config.assets.digest = digest unless digest.nil? + config.assets.digests = {} env = Rails.application.assets target = File.join(Rails.public_path, config.assets.prefix) - static_compiler = Sprockets::StaticCompiler.new(env, target, :digest => config.assets.digest) - static_compiler.precompile(config.assets.precompile) + compiler = Sprockets::StaticCompiler.new(env, + target, + config.assets.precompile, + :manifest_path => config.assets.manifest, + :digest => config.assets.digest, + :manifest => digest.nil?) + compiler.compile end task :all do @@ -42,14 +48,7 @@ namespace :assets do end task :digest => ["assets:environment", "tmp:cache:clear"] do - manifest = internal_precompile - config = Rails.application.config - manifest_path = config.assets.manifest || File.join(Rails.public_path, config.assets.prefix) - FileUtils.mkdir_p(manifest_path) - - File.open("#{manifest_path}/manifest.yml", 'wb') do |f| - YAML.dump(manifest, f) - end + internal_precompile end task :nondigest => ["assets:environment", "tmp:cache:clear"] do diff --git a/actionpack/lib/sprockets/static_compiler.rb b/actionpack/lib/sprockets/static_compiler.rb index 4a0078be46..32a9d66e6e 100644 --- a/actionpack/lib/sprockets/static_compiler.rb +++ b/actionpack/lib/sprockets/static_compiler.rb @@ -2,41 +2,50 @@ require 'fileutils' module Sprockets class StaticCompiler - attr_accessor :env, :target, :digest + attr_accessor :env, :target, :paths - def initialize(env, target, options = {}) + def initialize(env, target, paths, options = {}) @env = env @target = target + @paths = paths @digest = options.key?(:digest) ? options.delete(:digest) : true + @manifest = options.key?(:manifest) ? options.delete(:manifest) : true + @manifest_path = options.delete(:manifest_path) || target end - def precompile(paths) - Rails.application.config.assets.digest = digest + def compile manifest = {} - env.each_logical_path do |logical_path| - next unless precompile_path?(logical_path, paths) + next unless compile_path?(logical_path) if asset = env.find_asset(logical_path) - manifest[logical_path] = compile(asset) + manifest[logical_path] = write_asset(asset) end end - manifest + write_manifest(manifest) if @manifest end - def compile(asset) - asset_path = digest_asset(asset) - filename = File.join(target, asset_path) - FileUtils.mkdir_p File.dirname(filename) - asset.write_to(filename) - asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/ - asset_path + def write_manifest(manifest) + FileUtils.mkdir_p(@manifest_path) + File.open("#{@manifest_path}/manifest.yml", 'wb') do |f| + YAML.dump(manifest, f) + end + end + + def write_asset(asset) + path_for(asset).tap do |path| + filename = File.join(target, path) + FileUtils.mkdir_p File.dirname(filename) + asset.write_to(filename) + asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/ + end end - def precompile_path?(logical_path, paths) + def compile_path?(logical_path) paths.each do |path| - if path.is_a?(Regexp) + case path + when Regexp return true if path.match(logical_path) - elsif path.is_a?(Proc) + when Proc return true if path.call(logical_path) else return true if File.fnmatch(path.to_s, logical_path) @@ -45,8 +54,8 @@ module Sprockets false end - def digest_asset(asset) - digest ? asset.digest_path : asset.logical_path + def path_for(asset) + @digest ? asset.digest_path : asset.logical_path end end end -- cgit v1.2.3