提交 c9ea2171 编写于 作者: J José Valim

Generators are configured on initialization if RAILS_ENV=generators.

上级 2699e9c2
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
end end
$:.unshift(File.dirname(__FILE__)) $:.unshift(File.dirname(__FILE__))
require 'rails/version' unless defined?(Rails::VERSION)
# TODO Use vendored Thor # TODO Use vendored Thor
require 'rubygems' require 'rubygems'
...@@ -22,6 +21,46 @@ ...@@ -22,6 +21,46 @@
module Rails module Rails
module Generators 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: # Generators load paths used on lookup. The lookup happens as:
# #
...@@ -48,21 +87,7 @@ def self.load_path ...@@ -48,21 +87,7 @@ def self.load_path
paths paths
end end
end end
load_path # Cache load paths load_path # Cache load paths. Needed to avoid __FILE__ pointing to wrong 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
# Receives a namespace and tries different combinations to find a generator. # Receives a namespace and tries different combinations to find a generator.
# #
...@@ -106,6 +131,19 @@ def self.find_by_namespace(name, base=nil, context=nil) ...@@ -106,6 +131,19 @@ def self.find_by_namespace(name, base=nil, context=nil)
nil nil
end 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. # Show help message with available generators.
# #
def self.help def self.help
...@@ -137,19 +175,6 @@ def self.help ...@@ -137,19 +175,6 @@ def self.help
puts "Others: #{others.join(', ')}." unless others.empty? puts "Others: #{others.join(', ')}." unless others.empty?
end 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 protected
# Return all defined namespaces. # Return all defined namespaces.
...@@ -158,6 +183,14 @@ def self.namespaces ...@@ -158,6 +183,14 @@ def self.namespaces
Thor::Base.subclasses.map(&:namespace) Thor::Base.subclasses.map(&:namespace)
end 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 # Receives namespaces in an array and tries to find matching generators
# in the load path. Each path is traversed into directory lookups. For # in the load path. Each path is traversed into directory lookups. For
# example: # example:
......
...@@ -2,33 +2,6 @@ ...@@ -2,33 +2,6 @@
module Rails module Rails
module Generators 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 class Error < Thor::Error
end end
...@@ -137,7 +110,7 @@ def self.hook_for(*names, &block) ...@@ -137,7 +110,7 @@ def self.hook_for(*names, &block)
names.each do |name| names.each do |name|
defaults = if options[:type] == :boolean 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 => "" } { :banner => "" }
else else
{ :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" } { :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" }
...@@ -212,12 +185,12 @@ def self.remove_hook_for(*names) ...@@ -212,12 +185,12 @@ def self.remove_hook_for(*names)
end end
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: def self.class_option(name, options) #:nodoc:
options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc) options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc)
options[:aliases] = ALIASES[name] unless options.key?(:aliases) options[:aliases] = Rails::Generators.aliases[name] unless options.key?(:aliases)
options[:default] = DEFAULTS[name] unless options.key?(:default) options[:default] = Rails::Generators.options[name] unless options.key?(:default)
super(name, options) super(name, options)
end end
......
require 'digest/md5' require 'digest/md5'
require 'active_support/secure_random' require 'active_support/secure_random'
require 'rails/version' unless defined?(RAILS::VERSION)
module Rails::Generators module Rails::Generators
class AppGenerator < Base class AppGenerator < Base
......
...@@ -568,4 +568,14 @@ def self.run(initializer = nil, config = nil) ...@@ -568,4 +568,14 @@ def self.run(initializer = nil, config = nil)
ActiveSupport::Dependencies.unhook! ActiveSupport::Dependencies.unhook!
end end
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 end
...@@ -252,21 +252,25 @@ def reload_plugins? ...@@ -252,21 +252,25 @@ def reload_plugins?
# Holds generators configuration: # Holds generators configuration:
# #
# config.generators.orm :datamapper # config.generators.orm = :datamapper
# config.generators.test_framework :rspec # config.generators.test_framework = :rspec
# config.generators.template_engine :haml # config.generators.template_engine = :haml
# #
# A block can also be given for less verbose configuration: # A block can also be given for less verbose configuration:
# #
# config.generators do |g| # config.generators do |g|
# g.orm :datamapper # g.orm = :datamapper
# g.test_framework :datamapper # g.test_framework = :datamapper
# g.template_engine :haml # g.template_engine = :haml
# end # end
# #
# You can also configure/override aliases: # 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 def generators
@generators ||= Generators.new @generators ||= Generators.new
...@@ -278,22 +282,19 @@ def generators ...@@ -278,22 +282,19 @@ def generators
end end
class Generators #:nodoc: class Generators #:nodoc:
def initialize attr_accessor :aliases, :options, :colorize_logging
@aliases, @options = {}, {}
end
def aliases(values=nil) def initialize
@aliases = values if values @aliases, @options, @colorize_logging = {}, {}, true
@aliases
end
def options(values=nil)
@options = values if values
@options
end end
def method_missing(method, *args, &block) 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 end
end end
......
require 'abstract_unit' require 'abstract_unit'
require 'initializer' require 'initializer'
require 'generators'
require 'action_view' require 'action_view'
require 'action_mailer' require 'action_mailer'
...@@ -10,8 +11,19 @@ module Rails ...@@ -10,8 +11,19 @@ module Rails
def self.configuration def self.configuration
Rails::Configuration.new Rails::Configuration.new
end end
module Generators
def self.clear_aliases!
@aliases = nil
end
def self.clear_options!
@@options = nil
end
end
end end
class ConfigurationMock < Rails::Configuration class ConfigurationMock < Rails::Configuration
attr_reader :environment_path attr_reader :environment_path
...@@ -279,32 +291,77 @@ def load_plugins! ...@@ -279,32 +291,77 @@ def load_plugins!
end end
class InitializerGeneratorsTests < Test::Unit::TestCase class InitializerGeneratorsTests < Test::Unit::TestCase
def test_generators_empty_aliases_and_options
assert_equal({}, Rails::Configuration.new.generators.aliases) def setup
assert_equal({}, Rails::Configuration.new.generators.options) @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 end
def test_generators_set_options def test_generators_set_options
config = Rails::Configuration.new @configuration.generators.orm = :datamapper
config.generators.orm :datamapper @configuration.generators.test_framework = :rspec
config.generators.test_framework :rspec assert_equal({ :orm => :datamapper, :test_framework => :rspec }, @configuration.generators.options)
assert_equal({ :orm => :datamapper, :test_framework => :rspec }, config.generators.options)
end end
def test_generators_set_aliases def test_generators_set_aliases
config = Rails::Configuration.new @configuration.generators.aliases = { :test_framework => "-w" }
config.generators.aliases :test_framework => "-w" assert_equal({ :test_framework => "-w" }, @configuration.generators.aliases)
assert_equal({ :test_framework => "-w" }, config.generators.aliases)
end end
def test_generators_with_block def test_generators_with_block
config = Rails::Configuration.new @configuration.generators do |g|
config.generators do |g| g.orm = :datamapper
g.orm :datamapper g.test_framework = :rspec
g.test_framework :rspec
end end
assert_equal({ :orm => :datamapper, :test_framework => :rspec }, config.generators.options) assert_equal({ :orm => :datamapper, :test_framework => :rspec }, @configuration.generators.options)
end 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 end
class InitializerSetupI18nTests < Test::Unit::TestCase class InitializerSetupI18nTests < Test::Unit::TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册