aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-07-08 12:19:17 +0200
committerJosé Valim <jose.valim@gmail.com>2009-07-08 12:53:45 +0200
commitc9ea21717eefb9e9b49891c519cc4d121ef7bb74 (patch)
treea7901541789ab022074cf043fd764a4992455b7f /railties
parent2699e9c2ddff13c5e19e1c95e838fa6d9a965c34 (diff)
downloadrails-c9ea21717eefb9e9b49891c519cc4d121ef7bb74.tar.gz
rails-c9ea21717eefb9e9b49891c519cc4d121ef7bb74.tar.bz2
rails-c9ea21717eefb9e9b49891c519cc4d121ef7bb74.zip
Generators are configured on initialization if RAILS_ENV=generators.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/generators.rb91
-rw-r--r--railties/lib/generators/base.rb35
-rw-r--r--railties/lib/generators/rails/app/app_generator.rb1
-rw-r--r--railties/lib/initializer.rb10
-rw-r--r--railties/lib/rails/configuration.rb39
-rw-r--r--railties/test/initializer_test.rb87
6 files changed, 169 insertions, 94 deletions
diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb
index dd61a686c2..a4ce92e04b 100644
--- a/railties/lib/generators.rb
+++ b/railties/lib/generators.rb
@@ -10,7 +10,6 @@ rescue LoadError
end
$:.unshift(File.dirname(__FILE__))
-require 'rails/version' unless defined?(Rails::VERSION)
# TODO Use vendored Thor
require 'rubygems'
@@ -22,6 +21,46 @@ require 'generators/named_base'
module Rails
module Generators
+ DEFAULT_ALIASES = {
+ :actions => '-a',
+ :fixture_replacement => '-r',
+ :orm => '-o',
+ :resource_controller => '-c',
+ :scaffold_controller => '-c',
+ :stylesheets => '-y',
+ :test_framework => '-t',
+ :template_engine => '-e'
+ }
+
+ DEFAULT_OPTIONS = {
+ :fixture => true,
+ :force_plural => false,
+ :helper => true,
+ :layout => true,
+ :migration => true,
+ :orm => 'active_record',
+ :resource_controller => 'controller',
+ :scaffold_controller => 'scaffold_controller',
+ :singleton => false,
+ :stylesheets => true,
+ :test_framework => 'test_unit',
+ :template_engine => 'erb',
+ :timestamps => true
+ }
+
+ def self.aliases
+ @@aliases ||= DEFAULT_ALIASES.dup
+ end
+
+ def self.options
+ @@options ||= DEFAULT_OPTIONS.dup
+ end
+
+ # Remove the color from output.
+ #
+ def self.no_color!
+ Thor::Base.shell = Thor::Shell::Basic
+ end
# Generators load paths used on lookup. The lookup happens as:
#
@@ -48,21 +87,7 @@ module Rails
paths
end
end
- load_path # Cache load paths
-
- # Keep builtin generators in an Array[Array[group, name]].
- #
- def self.builtin
- Dir[File.dirname(__FILE__) + '/generators/*/*'].collect do |file|
- file.split('/')[-2, 2]
- end
- end
-
- # Remove the color from output.
- #
- def self.no_color!
- Thor::Base.shell = Thor::Shell::Basic
- end
+ load_path # Cache load paths. Needed to avoid __FILE__ pointing to wrong paths.
# Receives a namespace and tries different combinations to find a generator.
#
@@ -106,6 +131,19 @@ module Rails
nil
end
+ # Receives a namespace, arguments and the behavior to invoke the generator.
+ # It's used as the default entry point for generate, destroy and update
+ # commands.
+ #
+ def self.invoke(namespace, args=ARGV, config={})
+ if klass = find_by_namespace(namespace, "rails")
+ args << "--help" if klass.arguments.any? { |a| a.required? } && args.empty?
+ klass.start args, config
+ else
+ puts "Could not find generator #{namespace}."
+ end
+ end
+
# Show help message with available generators.
#
def self.help
@@ -137,19 +175,6 @@ module Rails
puts "Others: #{others.join(', ')}." unless others.empty?
end
- # Receives a namespace, arguments and the behavior to invoke the generator.
- # It's used as the default entry point for generate, destroy and update
- # commands.
- #
- def self.invoke(namespace, args=ARGV, config={})
- if klass = find_by_namespace(namespace, "rails")
- args << "--help" if klass.arguments.any? { |a| a.required? } && args.empty?
- klass.start args, config
- else
- puts "Could not find generator #{namespace}."
- end
- end
-
protected
# Return all defined namespaces.
@@ -158,6 +183,14 @@ module Rails
Thor::Base.subclasses.map(&:namespace)
end
+ # Keep builtin generators in an Array[Array[group, name]].
+ #
+ def self.builtin
+ Dir[File.dirname(__FILE__) + '/generators/*/*'].collect do |file|
+ file.split('/')[-2, 2]
+ end
+ end
+
# Receives namespaces in an array and tries to find matching generators
# in the load path. Each path is traversed into directory lookups. For
# example:
diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb
index 0d2d165741..dcc210240e 100644
--- a/railties/lib/generators/base.rb
+++ b/railties/lib/generators/base.rb
@@ -2,33 +2,6 @@ require 'generators/actions'
module Rails
module Generators
- DEFAULTS = {
- :fixture => true,
- :force_plural => false,
- :helper => true,
- :layout => true,
- :migration => true,
- :orm => 'active_record',
- :resource_controller => 'controller',
- :scaffold_controller => 'scaffold_controller',
- :singleton => false,
- :stylesheets => true,
- :test_framework => 'test_unit',
- :template_engine => 'erb',
- :timestamps => true
- }
-
- ALIASES = {
- :actions => '-a',
- :fixture_replacement => '-r',
- :orm => '-o',
- :resource_controller => '-c',
- :scaffold_controller => '-c',
- :stylesheets => '-y',
- :test_framework => '-t',
- :template_engine => '-e'
- }
-
class Error < Thor::Error
end
@@ -137,7 +110,7 @@ module Rails
names.each do |name|
defaults = if options[:type] == :boolean
{ }
- elsif [true, false].include?(options.fetch(:default, DEFAULTS[name]))
+ elsif [true, false].include?(options.fetch(:default, Rails::Generators.options[name]))
{ :banner => "" }
else
{ :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" }
@@ -212,12 +185,12 @@ module Rails
end
end
- # Make class option aware of DEFAULTS and ALIASES.
+ # Make class option aware of Rails::Generators.options and Rails::Generators.aliases.
#
def self.class_option(name, options) #:nodoc:
options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc)
- options[:aliases] = ALIASES[name] unless options.key?(:aliases)
- options[:default] = DEFAULTS[name] unless options.key?(:default)
+ options[:aliases] = Rails::Generators.aliases[name] unless options.key?(:aliases)
+ options[:default] = Rails::Generators.options[name] unless options.key?(:default)
super(name, options)
end
diff --git a/railties/lib/generators/rails/app/app_generator.rb b/railties/lib/generators/rails/app/app_generator.rb
index 1971d150c8..dc1d360e3f 100644
--- a/railties/lib/generators/rails/app/app_generator.rb
+++ b/railties/lib/generators/rails/app/app_generator.rb
@@ -1,5 +1,6 @@
require 'digest/md5'
require 'active_support/secure_random'
+require 'rails/version' unless defined?(RAILS::VERSION)
module Rails::Generators
class AppGenerator < Base
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb
index f0fb78c8f4..fa7d2d5249 100644
--- a/railties/lib/initializer.rb
+++ b/railties/lib/initializer.rb
@@ -568,4 +568,14 @@ Run `rake gems:install` to install the missing gems.
ActiveSupport::Dependencies.unhook!
end
end
+
+ # Load generators if RAILS_ENV == "generators"
+ Initializer.default.add :initialize_generators do
+ if RAILS_ENV == "generators"
+ require 'generators'
+ Rails::Generators.no_color! unless config.generators.colorize_logging
+ Rails::Generators.aliases.merge! config.generators.aliases
+ Rails::Generators.options.merge! config.generators.options
+ end
+ end
end
diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb
index 4a5fe4da7d..1a8f261e6f 100644
--- a/railties/lib/rails/configuration.rb
+++ b/railties/lib/rails/configuration.rb
@@ -252,21 +252,25 @@ module Rails
# Holds generators configuration:
#
- # config.generators.orm :datamapper
- # config.generators.test_framework :rspec
- # config.generators.template_engine :haml
+ # config.generators.orm = :datamapper
+ # config.generators.test_framework = :rspec
+ # config.generators.template_engine = :haml
#
# A block can also be given for less verbose configuration:
#
# config.generators do |g|
- # g.orm :datamapper
- # g.test_framework :datamapper
- # g.template_engine :haml
+ # g.orm = :datamapper
+ # g.test_framework = :datamapper
+ # g.template_engine = :haml
# end
#
# You can also configure/override aliases:
#
- # config.generators.aliases :test_framework => "-w"
+ # config.generators.aliases = :test_framework => "-w"
+ #
+ # Finally, to disable color in console, do:
+ #
+ # config.generators.colorize_logging = false
#
def generators
@generators ||= Generators.new
@@ -278,22 +282,19 @@ module Rails
end
class Generators #:nodoc:
- def initialize
- @aliases, @options = {}, {}
- end
+ attr_accessor :aliases, :options, :colorize_logging
- def aliases(values=nil)
- @aliases = values if values
- @aliases
- end
-
- def options(values=nil)
- @options = values if values
- @options
+ def initialize
+ @aliases, @options, @colorize_logging = {}, {}, true
end
def method_missing(method, *args, &block)
- @options[method.to_sym] = args.first
+ method = method.to_s
+ if method.gsub!(/=$/, '')
+ @options[method.to_sym] = args.first
+ else
+ super(method.to_sym, *args, &block)
+ end
end
end
end
diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb
index da78647ab3..0a423b4da2 100644
--- a/railties/test/initializer_test.rb
+++ b/railties/test/initializer_test.rb
@@ -1,5 +1,6 @@
require 'abstract_unit'
require 'initializer'
+require 'generators'
require 'action_view'
require 'action_mailer'
@@ -10,8 +11,19 @@ module Rails
def self.configuration
Rails::Configuration.new
end
+
+ module Generators
+ def self.clear_aliases!
+ @aliases = nil
+ end
+
+ def self.clear_options!
+ @@options = nil
+ end
+ end
end
+
class ConfigurationMock < Rails::Configuration
attr_reader :environment_path
@@ -279,32 +291,77 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase
end
class InitializerGeneratorsTests < Test::Unit::TestCase
- def test_generators_empty_aliases_and_options
- assert_equal({}, Rails::Configuration.new.generators.aliases)
- assert_equal({}, Rails::Configuration.new.generators.options)
+
+ def setup
+ @old_env_value = RAILS_ENV.dup
+ @configuration = Rails::Configuration.new
+ @initializer = Rails::Initializer.default
+ @initializer.config = @configuration
+ end
+
+ def test_generators_default_values
+ assert_equal(true, @configuration.generators.colorize_logging)
+ assert_equal({}, @configuration.generators.aliases)
+ assert_equal({}, @configuration.generators.options)
end
def test_generators_set_options
- config = Rails::Configuration.new
- config.generators.orm :datamapper
- config.generators.test_framework :rspec
- assert_equal({ :orm => :datamapper, :test_framework => :rspec }, config.generators.options)
+ @configuration.generators.orm = :datamapper
+ @configuration.generators.test_framework = :rspec
+ assert_equal({ :orm => :datamapper, :test_framework => :rspec }, @configuration.generators.options)
end
def test_generators_set_aliases
- config = Rails::Configuration.new
- config.generators.aliases :test_framework => "-w"
- assert_equal({ :test_framework => "-w" }, config.generators.aliases)
+ @configuration.generators.aliases = { :test_framework => "-w" }
+ assert_equal({ :test_framework => "-w" }, @configuration.generators.aliases)
end
def test_generators_with_block
- config = Rails::Configuration.new
- config.generators do |g|
- g.orm :datamapper
- g.test_framework :rspec
+ @configuration.generators do |g|
+ g.orm = :datamapper
+ g.test_framework = :rspec
end
- assert_equal({ :orm => :datamapper, :test_framework => :rspec }, config.generators.options)
+ assert_equal({ :orm => :datamapper, :test_framework => :rspec }, @configuration.generators.options)
end
+
+ def test_generators_aliases_and_options_on_initialization
+ @configuration.generators.aliases = { :test_framework => "-w" }
+ @configuration.generators.orm = :datamapper
+ @configuration.generators.test_framework = :rspec
+
+ RAILS_ENV.replace "generators"
+ @initializer.run(:initialize_generators)
+
+ assert_equal :rspec, Rails::Generators.options[:test_framework]
+ assert_equal "-w", Rails::Generators.aliases[:test_framework]
+ end
+
+ def test_generators_no_color_on_initialization
+ @configuration.generators.colorize_logging = false
+ RAILS_ENV.replace "generators"
+ @initializer.run(:initialize_generators)
+ assert_equal Thor::Base.shell, Thor::Shell::Basic
+ end
+
+ def test_generators_raise_no_method_error_non_setters
+ assert_raise NoMethodError do
+ @configuration.generators.foo
+ end
+ end
+
+ def test_generators_are_not_invoked_with_other_environments
+ @configuration.generators.test_framework = :rspec
+ @initializer.run(:initialize_generators)
+ assert_equal "test_unit", Rails::Generators.options[:test_framework]
+ end
+
+ protected
+
+ def teardown
+ RAILS_ENV.replace @old_env_value
+ Rails::Generators.clear_aliases!
+ Rails::Generators.clear_options!
+ end
end
class InitializerSetupI18nTests < Test::Unit::TestCase