aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorMark J. Titorenko <mark@titorenko.net>2011-10-04 23:05:01 +0100
committerJosé Valim <jose.valim@gmail.com>2011-10-05 01:24:58 +0200
commitb41bd968c6734737d14c542c514b8deef30a3ffe (patch)
tree66e99259db1b284f85e9f4f43b21c7f5a69f56c8 /actionpack
parentdc85fc546fb9c40a610d3f3d029ccb3692aefc02 (diff)
downloadrails-b41bd968c6734737d14c542c514b8deef30a3ffe.tar.gz
rails-b41bd968c6734737d14c542c514b8deef30a3ffe.tar.bz2
rails-b41bd968c6734737d14c542c514b8deef30a3ffe.zip
assets compilation task refactoring
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/sprockets/assets.rake19
-rw-r--r--actionpack/lib/sprockets/static_compiler.rb49
2 files changed, 38 insertions, 30 deletions
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