1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added lookup to generators.

This commit is contained in:
José Valim 2009-07-03 12:10:09 +02:00
parent 3bf45890b3
commit d1c404ee90
18 changed files with 103 additions and 95 deletions

View file

@ -12,10 +12,62 @@ require 'rails/version' unless defined?(Rails::VERSION)
require 'generators/base'
require 'generators/named_base'
require 'generators/active_record' # We will need ActionORM from ActiveRecord, but just it.
module Rails
module Generators
# Remove builtin generators.
mattr_accessor :load_path
# Generators load paths. First search on generators in the RAILS_ROOT, then
# look for them in rails generators.
#
def self.load_path
@@load_path ||= begin
paths = []
paths << File.expand_path(File.join(File.dirname(__FILE__), "generators"))
paths << File.join(RAILS_ROOT, "lib", "generators") if defined?(RAILS_ROOT)
paths
end
end
load_path # Cache load paths
# Receives paths in an array and tries to find generators for it in the load
# path.
#
def self.lookup(attempts)
generators_path = []
# Traverse attempts into directory lookups. For example:
#
# rails:generators:model
#
# Becomes:
#
# generators/rails/model/model_generator.rb
# generators/rails/model_generator.rb
# generators/model_generator.rb
#
attempts.each do |attempt|
paths = attempt.gsub(':generators:', ':').split(':')
paths << "#{paths.last}_generator.rb"
until paths.empty?
generators_path << File.join(*paths)
paths.delete_at(-1) unless paths.delete_at(-2)
end
end
generators_path.uniq!
generators_path.each do |generator_path|
self.load_path.each do |path|
Dir[File.join(path, generator_path)].each do |file|
require file
end
end
end
end
# Keep builtin generators in an Array[Array[group, name]].
#
def self.builtin
Dir[File.dirname(__FILE__) + '/generators/*/*'].collect do |file|
@ -53,17 +105,18 @@ module Rails
def self.find_by_namespace(name, base=nil, context=nil)
name, attempts = name.to_s, []
attempts << "#{base}:generators:#{name}" if base && name.count(':') == 0
attempts << "#{name}:generators:#{context}" if context && name.count(':') == 0
if name.count(':') == 0
attempts << "#{base}:generators:#{name}" if base
attempts << "#{name}:generators:#{context}" if context
end
attempts << name.sub(':', ':generators:') if name.count(':') == 1
attempts << name
attempts.each do |namespace|
klass = Thor::Util.find_by_namespace(namespace)
return klass if klass
unless klass = find_many_by_namespace(attempts)
lookup(attempts)
klass = find_many_by_namespace(attempts)
end
nil
klass
end
# Show help message with available generators.
@ -87,11 +140,6 @@ module Rails
# commands.
#
def self.invoke(namespace, args=ARGV, behavior=:invoke)
# Load everything right now ...
builtin.each do |group, name|
require "generators/#{group}/#{name}/#{name}_generator"
end
if klass = find_by_namespace(namespace, "rails")
args << "--help" if klass.arguments.any? { |a| a.required? } && args.empty?
klass.start args, :behavior => behavior
@ -99,6 +147,17 @@ module Rails
puts "Could not find generator #{namespace}."
end
end
protected
def self.find_many_by_namespace(attempts)
attempts.each do |namespace|
klass = Thor::Util.find_by_namespace(namespace)
return klass if klass
end
nil
end
end
end

View file

@ -3,7 +3,6 @@ require 'generators/actions'
module Rails
module Generators
DEFAULTS = {
:actions => [],
:fixture => true,
:force_plural => false,
:helper => true,
@ -147,7 +146,7 @@ module Rails
# end
#
class_eval <<-METHOD, __FILE__, __LINE__
def invoke_for_#{name}
def hook_for_#{name}
return unless options[#{name.inspect}]
klass_name = options[#{name.inspect}]

View file

@ -1,7 +1,7 @@
module Rails
module Generators
class ControllerGenerator < NamedBase
argument :actions, :type => :array, :default => DEFAULTS[:actions], :banner => "action action"
argument :actions, :type => :array, :default => [], :banner => "action action"
check_class_collision :suffix => "Controller"
def create_controller_files

View file

@ -1,51 +1,44 @@
module Rails
module Generators
class PluginGenerator < NamedBase
class_option :with_tasks, :type => :boolean, :aliases => "-r", :default => false,
class_option :tasks, :type => :boolean, :aliases => "-t", :default => false,
:desc => "When supplied creates tasks base files."
class_option :with_generator, :type => :boolean, :aliases => "-g", :default => false,
class_option :generator, :type => :boolean, :aliases => "-g", :default => false,
:desc => "When supplied creates generator base files."
check_class_collision
def create_root
self.root = File.expand_path("vendor/plugins/#{file_name}", root)
empty_directory '.' if behavior == :invoke
FileUtils.cd(root)
end
def create_root_files
%w(README MIT-LICENSE Rakefile init.rb install.rb uninstall.rb).each do |file|
template file
end
directory '.', plugin_dir, false # non-recursive
end
def create_lib_files
directory 'lib'
directory 'lib', plugin_dir('lib'), false # non-recursive
end
hook_for :test_framework
def create_tasks_files
return unless options[:with_tasks]
directory 'tasks'
return unless options[:tasks]
directory 'tasks', plugin_dir('tasks')
end
def create_generator_files
return unless options[:with_generator]
directory 'generators'
return unless options[:generator]
directory 'generators', plugin_dir('generators')
end
protected
def plugin_dir(join=nil)
if join
File.join(plugin_dir, join)
else
"vendor/plugins/#{file_name}"
end
end
# Work around for generator to work on revoke. If we remove the root
# folder at the beginning, it will raise an error since FileUtils.cd
# will move to a non-existent folder.
#
def remove_on_revoke
return unless behavior == :revoke
FileUtils.cd("../../..")
empty_directory "vendor/plugins/#{file_name}"
end
end
end
end

View file

@ -7,7 +7,7 @@ module Rails
base.invoke controller, [ base.name.pluralize, base.options[:actions] ]
end
class_option :actions, :type => :array, :banner => "ACTION ACTION",
class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [],
:desc => "Actions for the resource controller"
class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"

View file

@ -6,7 +6,7 @@ module TestUnit
check_class_collision :suffix => "Test"
def create_test_files
directory 'test'
directory 'test', "vendor/plugins/#{file_name}/test"
end
end
end

View file

@ -1,12 +1,6 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/erb/controller/controller_generator'
require 'generators/rails/controller/controller_generator'
require 'generators/rails/helper/helper_generator'
require 'generators/test_unit/controller/controller_generator'
require 'generators/test_unit/helper/helper_generator'
ObjectController = Class.new
class ControllerGeneratorTest < GeneratorsTestCase
@ -21,8 +15,11 @@ class ControllerGeneratorTest < GeneratorsTestCase
end
def test_check_class_collision
Object.send :const_set, :ObjectController, Class.new
content = capture(:stderr){ run_generator ["object"] }
assert_match /The name 'ObjectController' is either already used in your application or reserved/, content
ensure
Object.send :remove_const, :ObjectController
end
# No need to spec content since it's already spec'ed on helper generator.

View file

@ -1,5 +1,6 @@
require 'test/unit'
require 'fileutils'
require 'mocha'
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
require 'generators'

View file

@ -1,7 +1,6 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/rails/helper/helper_generator'
require 'generators/test_unit/helper/helper_generator'
ObjectHelper = Class.new
AnotherObjectHelperTest = Class.new

View file

@ -1,8 +1,6 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/erb/mailer/mailer_generator'
require 'generators/rails/mailer/mailer_generator'
require 'generators/test_unit/mailer/mailer_generator'
class MailerGeneratorTest < GeneratorsTestCase

View file

@ -1,6 +1,5 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/active_record/migration/migration_generator'
require 'generators/rails/migration/migration_generator'
class MigrationGeneratorTest < GeneratorsTestCase

View file

@ -1,8 +1,6 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/active_record/model/model_generator'
require 'generators/rails/model/model_generator'
require 'generators/test_unit/model/model_generator'
class ModelGeneratorTest < GeneratorsTestCase

View file

@ -1,8 +1,6 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/active_record/observer/observer_generator'
require 'generators/rails/observer/observer_generator'
require 'generators/test_unit/observer/observer_generator'
class ObserverGeneratorTest < GeneratorsTestCase

View file

@ -1,7 +1,6 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/rails/plugin/plugin_generator'
require 'generators/test_unit/plugin/plugin_generator'
class PluginGeneratorTest < GeneratorsTestCase
@ -32,12 +31,12 @@ class PluginGeneratorTest < GeneratorsTestCase
end
def test_creates_tasks_if_required
run_generator ["plugin_fu", "--with-tasks"]
run_generator ["plugin_fu", "--tasks"]
assert_file "vendor/plugins/plugin_fu/tasks/plugin_fu_tasks.rake"
end
def test_creates_generator_if_required
run_generator ["plugin_fu", "--with-generator"]
run_generator ["plugin_fu", "--generator"]
assert_file "vendor/plugins/plugin_fu/generators/plugin_fu/templates"
flag = /class PluginFuGenerator < Rails::Generators::NamedBase/

View file

@ -2,18 +2,6 @@ require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/rails/resource/resource_generator'
# Model
require 'generators/active_record/model/model_generator'
require 'generators/rails/model/model_generator'
require 'generators/test_unit/model/model_generator'
# Controller
require 'generators/erb/controller/controller_generator'
require 'generators/rails/controller/controller_generator'
require 'generators/rails/helper/helper_generator'
require 'generators/test_unit/controller/controller_generator'
require 'generators/test_unit/helper/helper_generator'
class ResourceGeneratorTest < GeneratorsTestCase
def setup

View file

@ -1,11 +1,6 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/active_record'
require 'generators/erb/scaffold/scaffold_generator'
require 'generators/rails/helper/helper_generator'
require 'generators/rails/scaffold_controller/scaffold_controller_generator'
require 'generators/test_unit/scaffold/scaffold_generator'
require 'generators/test_unit/helper/helper_generator'
class ScaffoldControllerGeneratorTest < GeneratorsTestCase

View file

@ -1,20 +1,6 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/active_record'
require 'generators/rails/scaffold/scaffold_generator'
require 'generators/rails/stylesheets/stylesheets_generator'
# Model
require 'generators/active_record/model/model_generator'
require 'generators/rails/model/model_generator'
require 'generators/test_unit/model/model_generator'
# Controller
require 'generators/erb/scaffold/scaffold_generator'
require 'generators/rails/scaffold_controller/scaffold_controller_generator'
require 'generators/rails/helper/helper_generator'
require 'generators/test_unit/scaffold/scaffold_generator'
require 'generators/test_unit/helper/helper_generator'
class ScaffoldGeneratorTest < GeneratorsTestCase

View file

@ -1,6 +1,5 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/active_record/session_migration/session_migration_generator'
require 'generators/rails/session_migration/session_migration_generator'
class SessionMigrationGeneratorTest < GeneratorsTestCase