diff options
Diffstat (limited to 'actionpack/lib/sprockets')
-rw-r--r-- | actionpack/lib/sprockets/assets.rake | 109 | ||||
-rw-r--r-- | actionpack/lib/sprockets/helpers.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/sprockets/helpers/isolated_helper.rb | 13 | ||||
-rw-r--r-- | actionpack/lib/sprockets/helpers/rails_helper.rb | 13 | ||||
-rw-r--r-- | actionpack/lib/sprockets/railtie.rb | 8 | ||||
-rw-r--r-- | actionpack/lib/sprockets/static_compiler.rb | 49 |
6 files changed, 131 insertions, 64 deletions
diff --git a/actionpack/lib/sprockets/assets.rake b/actionpack/lib/sprockets/assets.rake index e29661e4e7..a5145080c2 100644 --- a/actionpack/lib/sprockets/assets.rake +++ b/actionpack/lib/sprockets/assets.rake @@ -1,60 +1,95 @@ +require "fileutils" + namespace :assets do + def ruby_rake_task(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 + ruby *args + end + + # We are currently running with no explicit bundler group + # and/or no explicit environment - we have to reinvoke rake to + # execute this task. + def invoke_or_reboot_rake_task(task) + if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty? + ruby_rake_task task + else + Rake::Task[task].invoke + end + end + desc "Compile all the assets named in config.assets.precompile" task :precompile do - # We need to do this dance because RAILS_GROUPS is used - # too early in the boot process and changing here is already too late. - if ENV["RAILS_GROUPS"].to_s.empty? || ENV["RAILS_ENV"].to_s.empty? - ENV["RAILS_GROUPS"] ||= "assets" - ENV["RAILS_ENV"] ||= "production" - ruby $0, *ARGV - else - require "fileutils" - Rake::Task["tmp:cache:clear"].invoke - Rake::Task["assets:environment"].invoke + invoke_or_reboot_rake_task "assets:precompile:all" + end + namespace :precompile do + def internal_precompile(digest=nil) unless Rails.application.config.assets.enabled - raise "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true" + warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true" + exit end - # Ensure that action view is loaded and the appropriate sprockets hooks get executed - ActionView::Base + # Ensure that action view is loaded and the appropriate + # sprockets hooks get executed + _ = ActionView::Base config = Rails.application.config config.assets.compile = true - config.assets.digest = false if ENV["RAILS_ASSETS_NONDIGEST"] - - env = Rails.application.assets - - # Always compile files and avoid use of existing precompiled assets - config.assets.compile = true + config.assets.digest = digest unless digest.nil? config.assets.digests = {} - target = File.join(Rails.public_path, config.assets.prefix) - static_compiler = Sprockets::StaticCompiler.new(env, target, :digest => config.assets.digest) + env = Rails.application.assets + target = File.join(Rails.public_path, config.assets.prefix) + compiler = Sprockets::StaticCompiler.new(env, + target, + config.assets.precompile, + :manifest_path => config.assets.manifest, + :digest => config.assets.digest, + :manifest => digest.nil?) + compiler.compile + end - manifest = static_compiler.precompile(config.assets.precompile) - manifest_path = config.assets.manifest || target - FileUtils.mkdir_p(manifest_path) + task :all do + Rake::Task["assets:precompile:primary"].invoke + # 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 - unless ENV["RAILS_ASSETS_NONDIGEST"] - File.open("#{manifest_path}/manifest.yml", 'wb') do |f| - YAML.dump(manifest, f) - end - ENV["RAILS_ASSETS_NONDIGEST"] = "true" - ruby $0, *ARGV - end + task :primary => ["assets:environment", "tmp:cache:clear"] do + internal_precompile + end + + task :nondigest => ["assets:environment", "tmp:cache:clear"] do + internal_precompile(false) end end desc "Remove compiled assets" - task :clean => ['assets:environment', 'tmp:cache:clear'] do - config = Rails.application.config - public_asset_path = File.join(Rails.public_path, config.assets.prefix) - rm_rf public_asset_path, :secure => true + task :clean do + invoke_or_reboot_rake_task "assets:clean:all" + end + + namespace :clean do + task :all => ["assets:environment", "tmp:cache:clear"] do + config = Rails.application.config + public_asset_path = File.join(Rails.public_path, config.assets.prefix) + rm_rf public_asset_path, :secure => true + end end task :environment do - Rails.application.initialize!(:assets) - Sprockets::Bootstrap.new(Rails.application).run + if Rails.application.config.assets.initialize_on_precompile + Rake::Task["environment"].invoke + else + Rails.application.initialize!(:assets) + Sprockets::Bootstrap.new(Rails.application).run + end end end diff --git a/actionpack/lib/sprockets/helpers.rb b/actionpack/lib/sprockets/helpers.rb index a952a55c5e..fee48386e0 100644 --- a/actionpack/lib/sprockets/helpers.rb +++ b/actionpack/lib/sprockets/helpers.rb @@ -1,5 +1,6 @@ module Sprockets module Helpers - autoload :RailsHelper, "sprockets/helpers/rails_helper" + autoload :RailsHelper, "sprockets/helpers/rails_helper" + autoload :IsolatedHelper, "sprockets/helpers/isolated_helper" end end diff --git a/actionpack/lib/sprockets/helpers/isolated_helper.rb b/actionpack/lib/sprockets/helpers/isolated_helper.rb new file mode 100644 index 0000000000..3adb928c45 --- /dev/null +++ b/actionpack/lib/sprockets/helpers/isolated_helper.rb @@ -0,0 +1,13 @@ +module Sprockets + module Helpers + module IsolatedHelper + def controller + nil + end + + def config + Rails.application.config.action_controller + end + end + end +end diff --git a/actionpack/lib/sprockets/helpers/rails_helper.rb b/actionpack/lib/sprockets/helpers/rails_helper.rb index c8c6c3ddd9..f866bc626e 100644 --- a/actionpack/lib/sprockets/helpers/rails_helper.rb +++ b/actionpack/lib/sprockets/helpers/rails_helper.rb @@ -8,9 +8,6 @@ module Sprockets def asset_paths @asset_paths ||= begin - config = self.config if respond_to?(:config) - config ||= Rails.application.config - controller = self.controller if respond_to?(:controller) paths = RailsHelper::AssetPaths.new(config, controller) paths.asset_environment = asset_environment paths.asset_digests = asset_digests @@ -65,6 +62,16 @@ module Sprockets end alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route + def javascript_path(source) + asset_path(source) + end + alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with an javascript_path named route + + def stylesheet_path(source) + asset_path(source) + end + alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with an stylesheet_path named route + private def debug_assets? compile_assets? && (Rails.application.config.assets.debug || params[:debug_assets]) diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb index 6b67fb1d2d..3d330bd91a 100644 --- a/actionpack/lib/sprockets/railtie.rb +++ b/actionpack/lib/sprockets/railtie.rb @@ -1,3 +1,5 @@ +require "action_controller/railtie" + module Sprockets autoload :Bootstrap, "sprockets/bootstrap" autoload :Helpers, "sprockets/helpers" @@ -8,13 +10,13 @@ module Sprockets # TODO: Get rid of config.assets.enabled class Railtie < ::Rails::Railtie - config.default_asset_host_protocol = :relative + config.action_controller.default_asset_host_protocol = :relative rake_tasks do load "sprockets/assets.rake" end - initializer "sprockets.environment", :group => :assets do |app| + initializer "sprockets.environment", :group => :all do |app| config = app.config next unless config.assets.enabled @@ -41,8 +43,8 @@ module Sprockets ActiveSupport.on_load(:action_view) do include ::Sprockets::Helpers::RailsHelper - app.assets.context_class.instance_eval do + include ::Sprockets::Helpers::IsolatedHelper include ::Sprockets::Helpers::RailsHelper end end 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 |