Permalink
Browse files

move assets compiler configuration code back into rake task; reinstat…

…e dual tasks for compilation modes
  • Loading branch information...
1 parent 676cce2 commit a7744918c523d81a56b70867c5523ded5accb2ab @mjtko committed Oct 4, 2011
Showing with 65 additions and 62 deletions.
  1. +65 −25 actionpack/lib/sprockets/assets.rake
  2. +0 −37 actionpack/lib/sprockets/static_compiler.rb
@@ -3,7 +3,7 @@ require "fileutils"
namespace :assets do
desc "Compile all the assets named in config.assets.precompile"
task :precompile do
- Rake::Task["assets:internal:invoke"].invoke("compile[primary]")
+ Rake::Task["assets:internal:invoke"].invoke("compile:all")
end
desc "Remove compiled assets"
@@ -20,22 +20,6 @@ namespace :assets do
ruby *args
end
- def app
- @app ||= Rails.application
- end
-
- def assets
- @assets_config ||= app.config.assets
- end
-
- def compile(mode)
- Sprockets::StaticCompiler.compiler_for(assets, app.assets, mode).compile
- end
-
- def invoke_digestless?
- assets.digest && !assets.skip_digestless
- end
-
task :invoke, [:task] do |this, args|
task = args[:task]
if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty?
@@ -49,14 +33,70 @@ namespace :assets do
this.reenable
end
- task :compile, [:mode] => ["assets:internal:environment", "tmp:cache:clear"] do |t, args|
- mode = args[:mode] || 'primary'
- compile(mode)
- if mode == 'primary' && invoke_digestless?
- # We need to reinvoke in order to run the secondary asset
- # compilation run - a fresh Sprockets environment is
- # required in order to compile digestless assets.
- reinvoke_for("compile[digestless]")
+ def app
+ @app ||= Rails.application
+ end
+
+ def assets
+ @assets_config ||= app.config.assets
+ end
+
+ namespace :compile do
+ def mode=(mode)
+ unless [:primary, :digestless].include?(mode)
+ raise "Unknown asset compilation mode: #{mode}. Please use one of: primary, digestless"
+ end
+ @mode = ActiveSupport::StringInquirer.new(mode.to_s)
+ end
+
+ def mode
+ @mode ||= ActiveSupport::StringInquirer.new('primary')
+ end
+
+ def compile
+ unless assets.enabled
+ raise "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
+ end
+
+ # Ensure that action view is loaded and the appropriate
+ # sprockets hooks get executed
+ _ = ActionView::Base
+
+ assets.digest = false if mode.digestless?
+ assets.compile = true
+ assets.digests = {}
+
+ target = File.join(Rails.public_path, assets.prefix)
+ compiler = Sprockets::StaticCompiler.new(app.assets,
+ target,
+ assets.precompile,
+ :manifest_path => assets.manifest,
+ :digest => assets.digest,
+ :manifest => mode.primary?)
+ compiler.compile
+ end
+
+ def invoke_digestless?
+ assets.digest && !assets.skip_digestless
+ end
+
+ task :all => ["assets:internal:environment", "tmp:cache:clear"] do
+ Rake::Task["assets:internal:compile: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.
+ reinvoke_for("compile:digestless") if invoke_digestless?
+ end
+
+ task :primary => ["assets:internal:environment", "tmp:cache:clear"] do
+ compile
+ end
+
+ task :digestless => ["assets:internal:environment", "tmp:cache:clear"] do
+ self.mode = :digestless
+ compile
end
end
@@ -2,43 +2,6 @@
module Sprockets
class StaticCompiler
- MODES = ['primary', 'digestless']
- class << self
- def assert_valid_configuration(assets, mode)
- unless assets.enabled
- raise "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
- end
-
- unless MODES.include?(mode)
- raise "Unknown asset compilation mode: #{mode}. Please use one of #{MODES.join}"
- end
- end
-
- def configure_assets!(assets,mode)
- assets.digest = false if mode == 'digestless'
- assets.compile = true
- assets.digests = {}
- end
-
- def compiler_for(assets, env, mode = 'primary')
- assert_valid_configuration(assets, mode)
-
- # Ensure that action view is loaded and the appropriate
- # sprockets hooks get executed
- _ = ActionView::Base
-
- configure_assets!(assets,mode)
-
- target = File.join(Rails.public_path, assets.prefix)
- Sprockets::StaticCompiler.new(env,
- target,
- assets.precompile,
- :manifest_path => assets.manifest,
- :digest => assets.digest,
- :manifest => (mode == 'primary'))
- end
- end
-
attr_accessor :env, :target, :paths
def initialize(env, target, paths, options = {})

0 comments on commit a774491

Please sign in to comment.