Skip to content

Commit

Permalink
rake assets:precompile loads the application but does not initializ…
Browse files Browse the repository at this point in the history
…e it.

To the app developer, this means configuration add in
config/initializers/* will not be executed.

Plugins developers need to special case their initializers that are
meant to be run in the assets group by adding :group => :assets.

Conflicts:

	railties/CHANGELOG
	railties/test/application/assets_test.rb
  • Loading branch information
josevalim committed Sep 24, 2011
1 parent 096717e commit eb367af
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 110 deletions.
3 changes: 2 additions & 1 deletion actionpack/lib/sprockets/assets.rake
Expand Up @@ -11,8 +11,9 @@ namespace :assets do
ENV["RAILS_ENV"] ||= "production" ENV["RAILS_ENV"] ||= "production"
Kernel.exec $0, *ARGV Kernel.exec $0, *ARGV
else else
Rake::Task["environment"].invoke
Rake::Task["tmp:cache:clear"].invoke Rake::Task["tmp:cache:clear"].invoke
Rails.application.initialize!(:assets)
Sprockets::Bootstrap.new(Rails.application).run


# Ensure that action view is loaded and the appropriate sprockets hooks get executed # Ensure that action view is loaded and the appropriate sprockets hooks get executed
ActionView::Base ActionView::Base
Expand Down
65 changes: 65 additions & 0 deletions actionpack/lib/sprockets/bootstrap.rb
@@ -0,0 +1,65 @@
module Sprockets
class Bootstrap
def initialize(app)
@app = app
end

# TODO: Get rid of config.assets.enabled
def run
app, config = @app, @app.config
return unless app.assets

config.assets.paths.each { |path| app.assets.append_path(path) }

if config.assets.compress
# temporarily hardcode default JS compressor to uglify. Soon, it will work
# the same as SCSS, where a default plugin sets the default.
unless config.assets.js_compressor == false
app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) }
end

unless config.assets.css_compressor == false
app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) }
end
end

if config.assets.compile
app.routes.prepend do
mount app.assets => config.assets.prefix
end
end

if config.assets.digest
app.assets = app.assets.index
end
end

protected

def expand_js_compressor(sym)
case sym
when :closure
require 'closure-compiler'
Closure::Compiler.new
when :uglifier
require 'uglifier'
Uglifier.new
when :yui
require 'yui/compressor'
YUI::JavaScriptCompressor.new
else
sym
end
end

def expand_css_compressor(sym)
case sym
when :yui
require 'yui/compressor'
YUI::CssCompressor.new
else
sym
end
end
end
end
59 changes: 4 additions & 55 deletions actionpack/lib/sprockets/railtie.rb
@@ -1,5 +1,6 @@
module Sprockets module Sprockets
autoload :Helpers, "sprockets/helpers" autoload :Bootstrap, "sprockets/bootstrap"
autoload :Helpers, "sprockets/helpers"
autoload :LazyCompressor, "sprockets/compressors" autoload :LazyCompressor, "sprockets/compressors"
autoload :NullCompressor, "sprockets/compressors" autoload :NullCompressor, "sprockets/compressors"
autoload :StaticCompiler, "sprockets/static_compiler" autoload :StaticCompiler, "sprockets/static_compiler"
Expand All @@ -12,7 +13,7 @@ class Railtie < ::Rails::Railtie
load "sprockets/assets.rake" load "sprockets/assets.rake"
end end


initializer "sprockets.environment" do |app| initializer "sprockets.environment", :group => :assets do |app|
config = app.config config = app.config
next unless config.assets.enabled next unless config.assets.enabled


Expand Down Expand Up @@ -51,59 +52,7 @@ class Railtie < ::Rails::Railtie
# are compiled, and so that other Railties have an opportunity to # are compiled, and so that other Railties have an opportunity to
# register compressors. # register compressors.
config.after_initialize do |app| config.after_initialize do |app|
next unless app.assets Sprockets::Bootstrap.new(app).run
config = app.config

config.assets.paths.each { |path| app.assets.append_path(path) }

if config.assets.compress
# temporarily hardcode default JS compressor to uglify. Soon, it will work
# the same as SCSS, where a default plugin sets the default.
unless config.assets.js_compressor == false
app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) }
end

unless config.assets.css_compressor == false
app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) }
end
end

if config.assets.compile
app.routes.prepend do
mount app.assets => config.assets.prefix
end
end

if config.assets.digest
app.assets = app.assets.index
end
end end

protected
def expand_js_compressor(sym)
case sym
when :closure
require 'closure-compiler'
Closure::Compiler.new
when :uglifier
require 'uglifier'
Uglifier.new
when :yui
require 'yui/compressor'
YUI::JavaScriptCompressor.new
else
sym
end
end

def expand_css_compressor(sym)
case sym
when :yui
require 'yui/compressor'
YUI::CssCompressor.new
else
sym
end
end
end end
end end
12 changes: 12 additions & 0 deletions railties/CHANGELOG
Expand Up @@ -10,6 +10,18 @@


* Removed old 'config.paths.app.controller' API in favor of 'config.paths["app/controller"]' API. [Guillermo Iguaran] * Removed old 'config.paths.app.controller' API in favor of 'config.paths["app/controller"]' API. [Guillermo Iguaran]



*Rails 3.1.1

* `rake assets:precompile` loads the application but does not initialize it.

To the app developer, this means configuration add in
config/initializers/* will not be executed.

Plugins developers need to special case their initializers that are
meant to be run in the assets group by adding :group => :assets.


*Rails 3.1.0 (August 30, 2011)* *Rails 3.1.0 (August 30, 2011)*


* The default database schema file is written as UTF-8. [Aaron Patterson] * The default database schema file is written as UTF-8. [Aaron Patterson]
Expand Down
5 changes: 2 additions & 3 deletions railties/lib/rails/application.rb
Expand Up @@ -83,7 +83,6 @@ def require_environment! #:nodoc:
require environment if environment require environment if environment
end end



def reload_routes! def reload_routes!
routes_reloader.reload! routes_reloader.reload!
end end
Expand All @@ -92,9 +91,9 @@ def routes_reloader
@routes_reloader ||= RoutesReloader.new @routes_reloader ||= RoutesReloader.new
end end


def initialize! def initialize!(group=nil)
raise "Application has been already initialized." if @initialized raise "Application has been already initialized." if @initialized
run_initializers(self) run_initializers(group, self)
@initialized = true @initialized = true
self self
end end
Expand Down
16 changes: 8 additions & 8 deletions railties/lib/rails/application/bootstrap.rb
Expand Up @@ -7,21 +7,21 @@ class Application
module Bootstrap module Bootstrap
include Initializable include Initializable


initializer :load_environment_hook do end initializer :load_environment_hook, :group => :all do end


initializer :load_active_support do initializer :load_active_support, :group => :all do
require "active_support/all" unless config.active_support.bare require "active_support/all" unless config.active_support.bare
end end


# Preload all frameworks specified by the Configuration#frameworks. # Preload all frameworks specified by the Configuration#frameworks.
# Used by Passenger to ensure everything's loaded before forking and # Used by Passenger to ensure everything's loaded before forking and
# to avoid autoload race conditions in JRuby. # to avoid autoload race conditions in JRuby.
initializer :preload_frameworks do initializer :preload_frameworks, :group => :all do
ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks
end end


# Initialize the logger early in the stack in case we need to log some deprecation. # Initialize the logger early in the stack in case we need to log some deprecation.
initializer :initialize_logger do initializer :initialize_logger, :group => :all do
Rails.logger ||= config.logger || begin Rails.logger ||= config.logger || begin
path = config.paths["log"].first path = config.paths["log"].first
logger = ActiveSupport::BufferedLogger.new(path) logger = ActiveSupport::BufferedLogger.new(path)
Expand All @@ -41,7 +41,7 @@ module Bootstrap
end end


# Initialize cache early in the stack so railties can make use of it. # Initialize cache early in the stack so railties can make use of it.
initializer :initialize_cache do initializer :initialize_cache, :group => :all do
unless defined?(RAILS_CACHE) unless defined?(RAILS_CACHE)
silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) } silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) }


Expand All @@ -51,7 +51,7 @@ module Bootstrap
end end
end end


initializer :set_clear_dependencies_hook do initializer :set_clear_dependencies_hook, :group => :all do
ActionDispatch::Reloader.to_cleanup do ActionDispatch::Reloader.to_cleanup do
ActiveSupport::DescendantsTracker.clear ActiveSupport::DescendantsTracker.clear
ActiveSupport::Dependencies.clear ActiveSupport::Dependencies.clear
Expand All @@ -60,11 +60,11 @@ module Bootstrap


# Sets the dependency loading mechanism. # Sets the dependency loading mechanism.
# TODO: Remove files from the $" and always use require. # TODO: Remove files from the $" and always use require.
initializer :initialize_dependency_mechanism do initializer :initialize_dependency_mechanism, :group => :all do
ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load
end end


initializer :bootstrap_hook do |app| initializer :bootstrap_hook, :group => :all do |app|
ActiveSupport.run_load_hooks(:before_initialize, app) ActiveSupport.run_load_hooks(:before_initialize, app)
end end
end end
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/engine.rb
Expand Up @@ -537,12 +537,12 @@ def load_seed
end end
end end


initializer :load_environment_config, :before => :load_environment_hook do initializer :load_environment_config, :before => :load_environment_hook, :group => :all do
environment = paths["config/environments"].existent.first environment = paths["config/environments"].existent.first
require environment if environment require environment if environment
end end


initializer :append_assets_path do |app| initializer :append_assets_path, :group => :assets do |app|
app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories) app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories) app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
app.config.assets.paths.unshift(*paths["app/assets"].existent_directories) app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
Expand Down
8 changes: 6 additions & 2 deletions railties/lib/rails/initializable.rb
Expand Up @@ -21,6 +21,10 @@ def after
@options[:after] @options[:after]
end end


def belongs_to?(group)
@options[:group] == group || @options[:group] == :all
end

def run(*args) def run(*args)
@context.instance_exec(*args, &block) @context.instance_exec(*args, &block)
end end
Expand All @@ -44,10 +48,10 @@ def +(other)
end end
end end


def run_initializers(*args) def run_initializers(group=nil, *args)
return if instance_variable_defined?(:@ran) return if instance_variable_defined?(:@ran)
initializers.tsort.each do |initializer| initializers.tsort.each do |initializer|
initializer.run(*args) initializer.run(*args) if group.nil? || initializer.belongs_to?(group)
end end
@ran = true @ran = true
end end
Expand Down

0 comments on commit eb367af

Please sign in to comment.