mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Generators are configured on initialization if RAILS_ENV=generators.
This commit is contained in:
parent
2699e9c2dd
commit
c9ea21717e
6 changed files with 169 additions and 94 deletions
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
attr_accessor :aliases, :options, :colorize_logging
|
||||
|
||||
def initialize
|
||||
@aliases, @options = {}, {}
|
||||
end
|
||||
|
||||
def aliases(values=nil)
|
||||
@aliases = values if values
|
||||
@aliases
|
||||
end
|
||||
|
||||
def options(values=nil)
|
||||
@options = values if values
|
||||
@options
|
||||
@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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue