Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7139

Show
Ignore:
Timestamp:
06/27/07 09:25:25 (1 year ago)
Author:
bitsweat
Message:

Generators: look for generators in all gems, not just those suffixed with _generator, in the gem's generators or rails_generators directory. Allow use of the rails_generators directory instead of the standard generators directory in plugins also. Closes #8730.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r7116 r7139  
    11*SVN* 
     2 
     3* Generators: look for generators in all gems, not just those suffixed with _generator, in the gem's generators or rails_generators directory. Allow use of the rails_generators directory instead of the standard generators directory in plugins also.  #8730 [Dr Nic, topfunky] 
    24 
    35* MySQL, PostgreSQL: database.yml defaults to utf-8.  #8701 [matt] 
  • trunk/railties/lib/rails_generator/lookup.rb

    r6101 r7139  
    4747    # A spec is not a generator:  it's a description of where to find 
    4848    # the generator and how to create it.  A source is anything that 
    49     # yields generators from #each.  PathSource and GemSource are provided. 
     49    # yields generators from #each.  PathSource and GemGeneratorSource are provided. 
    5050    module Lookup 
    5151      def self.included(base) 
     
    102102            sources << PathSource.new(:vendor, "#{::RAILS_ROOT}/vendor/generators") 
    103103            sources << PathSource.new(:plugins, "#{::RAILS_ROOT}/vendor/plugins/*/**/generators") 
     104            sources << PathSource.new(:plugins, "#{::RAILS_ROOT}/vendor/plugins/*/**/rails_generators") 
    104105          end 
    105106          sources << PathSource.new(:user, "#{Dir.user_home}/.rails/generators") 
    106           sources << GemSource.new if Object.const_defined?(:Gem) 
     107          if Object.const_defined?(:Gem) 
     108            sources << GemGeneratorSource.new 
     109            sources << GemPathSource.new 
     110          end 
    107111          sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/components") 
    108112        end 
     
    186190    end 
    187191 
    188  
    189     # GemSource hits the mines to quarry for generators.  The latest versions 
    190     # of gems named *_generator are selected. 
    191     class GemSource < Source 
     192    class AbstractGemSource < Source 
    192193      def initialize 
    193194        super :RubyGems 
    194195      end 
    195  
     196    end 
     197 
     198    # GemGeneratorSource hits the mines to quarry for generators.  The latest versions 
     199    # of gems named *_generator are selected. 
     200    class GemGeneratorSource < AbstractGemSource 
    196201      # Yield latest versions of generator gems. 
    197202      def each 
     
    206211    end 
    207212 
     213    # GemPathSource looks for generators within any RubyGem's /rails_generators/<generator_name>_generator.rb file. 
     214    class GemPathSource < AbstractGemSource 
     215      # Yield each generator within rails_generator subdirectories. 
     216      def each 
     217        generator_full_paths.each do |generator| 
     218          yield Spec.new(File.basename(generator).sub(/_generator.rb$/, ''), File.dirname(generator), label) 
     219        end 
     220      end 
     221 
     222      private 
     223        def generator_full_paths 
     224          @generator_full_paths ||= 
     225            Gem::cache.inject({}) do |latest, name_gem| 
     226              name, gem = name_gem 
     227              hem = latest[gem.name] 
     228              latest[gem.name] = gem if hem.nil? or gem.version > hem.version 
     229              latest 
     230            end.values.inject([]) do |mem, gem| 
     231              Dir[gem.full_gem_path + '/{rails_,}generators/**/*_generator.rb'].each do |generator| 
     232                mem << generator 
     233              end 
     234              mem 
     235            end 
     236        end 
     237    end 
     238 
    208239  end 
    209240end 
  • trunk/railties/lib/rails_generator/scripts.rb

    r4662 r7139  
    4444          def usage_message 
    4545            usage = "\nInstalled Generators\n" 
    46             Rails::Generator::Base.sources.each do |source| 
     46            Rails::Generator::Base.sources.inject({}) do |mem, source| 
    4747              label = source.label.to_s.capitalize 
    48               names = source.names 
     48              mem[label] ||= [] 
     49              mem[label] |= source.names 
     50              mem 
     51            end.each_pair do |label, names| 
    4952              usage << "  #{label}: #{names.join(', ')}\n" unless names.empty? 
    5053            end 
  • trunk/railties/lib/rails_generator/spec.rb

    r518 r7139  
    33    # A spec knows where a generator was found and how to instantiate it. 
    44    # Metadata include the generator's name, its base path, and the source 
    5     # which yielded it (PathSource, GemSource, etc.) 
     5    # which yielded it (PathSource, GemPathSource, etc.) 
    66    class Spec 
    77      attr_reader :name, :path, :source