Skip to content

Commit

Permalink
Merge pull request #3225 from mjtko/assets-compilation-refactoring-th…
Browse files Browse the repository at this point in the history
…e-sequel

assets rake task refactoring work - the sequel
  • Loading branch information
josevalim committed Oct 4, 2011
2 parents 4bc6e2f + 871cbae commit 0ad5040
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 40 deletions.
56 changes: 36 additions & 20 deletions actionpack/lib/sprockets/assets.rake
Expand Up @@ -2,16 +2,23 @@ require "fileutils"


namespace :assets do namespace :assets do
def ruby_rake_task(task) def ruby_rake_task(task)
args = [$0, task] env = ENV['RAILS_ENV'] || 'production'
groups = ENV['RAILS_GROUPS'] || 'assets'
args = [$0, task,"RAILS_ENV=#{env}","RAILS_GROUPS=#{groups}"]
args << "--trace" if Rake.application.options.trace args << "--trace" if Rake.application.options.trace
ruby *args ruby *args
end end


desc "Compile all the assets named in config.assets.precompile" desc "Compile all the assets named in config.assets.precompile"
task :precompile do task :precompile do
ENV["RAILS_GROUPS"] ||= "assets" if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty?
ENV["RAILS_ENV"] ||= "production" # We are currently running with no explicit bundler group
ruby_rake_task "assets:precompile:all" # and/or no explicit environment - we have to reinvoke rake to
# execute this task.
ruby_rake_task "assets:precompile:all"
else
Rake::Task["assets:precompile:all"].invoke
end
end end


namespace :precompile do namespace :precompile do
Expand All @@ -28,28 +35,32 @@ namespace :assets do
config = Rails.application.config config = Rails.application.config
config.assets.compile = true config.assets.compile = true
config.assets.digest = digest unless digest.nil? config.assets.digest = digest unless digest.nil?

config.assets.digests = {} config.assets.digests = {}


env = Rails.application.assets env = Rails.application.assets
target = File.join(Rails.public_path, config.assets.prefix) target = File.join(Rails.public_path, config.assets.prefix)
static_compiler = Sprockets::StaticCompiler.new(env, target, :digest => config.assets.digest) compiler = Sprockets::StaticCompiler.new(env,
static_compiler.precompile(config.assets.precompile) target,
config.assets.precompile,
:manifest_path => config.assets.manifest,
:digest => config.assets.digest,
:manifest => digest.nil?)
compiler.compile
end end


task :all do task :all do
Rake::Task["assets:precompile:digest"].invoke Rake::Task["assets:precompile:primary"].invoke
ruby_rake_task "assets:precompile:nondigest" # We need to reinvoke in order to run the secondary digestless
# asset compilation run - a fresh Sprockets environment is
# required in order to compile digestless assets as the
# environment has already cached the assets on the primary
# run.
ruby_rake_task "assets:precompile:nondigest" if Rails.application.config.assets.digest
end end


task :digest => ["assets:environment", "tmp:cache:clear"] do task :primary => ["assets:environment", "tmp:cache:clear"] do
manifest = internal_precompile 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
end end


task :nondigest => ["assets:environment", "tmp:cache:clear"] do task :nondigest => ["assets:environment", "tmp:cache:clear"] do
Expand All @@ -59,9 +70,14 @@ namespace :assets do


desc "Remove compiled assets" desc "Remove compiled assets"
task :clean do task :clean do
ENV["RAILS_GROUPS"] ||= "assets" if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty?
ENV["RAILS_ENV"] ||= "production" # We are currently running with no explicit bundler group
ruby_rake_task "assets:clean:all" # and/or no explicit environment - we have to reinvoke rake to
# execute this task.
ruby_rake_task "assets:clean:all"
else
Rake::Task["assets:clean:all"].invoke
end
end end


namespace :clean do namespace :clean do
Expand Down
49 changes: 29 additions & 20 deletions actionpack/lib/sprockets/static_compiler.rb
Expand Up @@ -2,41 +2,50 @@


module Sprockets module Sprockets
class StaticCompiler 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 @env = env
@target = target @target = target
@paths = paths
@digest = options.key?(:digest) ? options.delete(:digest) : true @digest = options.key?(:digest) ? options.delete(:digest) : true
@manifest = options.key?(:manifest) ? options.delete(:manifest) : true
@manifest_path = options.delete(:manifest_path) || target
end end


def precompile(paths) def compile
Rails.application.config.assets.digest = digest
manifest = {} manifest = {}

env.each_logical_path do |logical_path| 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) if asset = env.find_asset(logical_path)
manifest[logical_path] = compile(asset) manifest[logical_path] = write_asset(asset)
end end
end end
manifest write_manifest(manifest) if @manifest
end end


def compile(asset) def write_manifest(manifest)
asset_path = digest_asset(asset) FileUtils.mkdir_p(@manifest_path)
filename = File.join(target, asset_path) File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
FileUtils.mkdir_p File.dirname(filename) YAML.dump(manifest, f)
asset.write_to(filename) end
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/ end
asset_path
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 end


def precompile_path?(logical_path, paths) def compile_path?(logical_path)
paths.each do |path| paths.each do |path|
if path.is_a?(Regexp) case path
when Regexp
return true if path.match(logical_path) return true if path.match(logical_path)
elsif path.is_a?(Proc) when Proc
return true if path.call(logical_path) return true if path.call(logical_path)
else else
return true if File.fnmatch(path.to_s, logical_path) return true if File.fnmatch(path.to_s, logical_path)
Expand All @@ -45,8 +54,8 @@ def precompile_path?(logical_path, paths)
false false
end end


def digest_asset(asset) def path_for(asset)
digest ? asset.digest_path : asset.logical_path @digest ? asset.digest_path : asset.logical_path
end end
end end
end end

0 comments on commit 0ad5040

Please sign in to comment.