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

Ticket #8730: gem_generators2.patch

File gem_generators2.patch, 5.2 kB (added by nicwilliams, 1 year ago)

Replacement patch - lookup *generators changed to generators + rails_generators explicitly

  • CHANGELOG

    old new  
    11*SVN* 
    22 
     3* Plugin generators can be placed in /rails_generators folder, in addition to /generators folder; to allow RubyGems with rails_generators (see below) to be directly usable as plugins, and have their generators available. [Dr Nic, topfunky] 
     4 
     5* RubyGems can include Rails generators within /rails_generators/<generator_name>_generator.rb files (or /generators/... for generic generators), like you can do with plugins. GemSource renamed to GemGeneratorSource to free up meaningful namespace of Source subclasses [Dr Nic] 
     6 
    37* MySQL, PostgreSQL: database.yml defaults to utf-8.  #8701 [matt] 
    48 
    59* Added db:version to get the current schema number [via Err The Blog] 
  • lib/rails_generator/scripts.rb

    old new  
    4343 
    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 
    5154 
  • lib/rails_generator/lookup.rb

    old new  
    4646    # 
    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) 
    5252        base.extend(ClassMethods) 
     
    101101            sources << PathSource.new(:lib, "#{::RAILS_ROOT}/lib/generators") 
    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 
    109113 
     
    185189      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 
     196    end 
    195197 
     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 
    198203        Gem::cache.search(/_generator$/).inject({}) { |latest, gem| 
     
    205210      end 
    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          begin 
     226            Gem::cache.inject({}) do |latest, name_gem| 
     227              name, gem = name_gem 
     228              hem = latest[gem.name] 
     229              latest[gem.name] = gem if hem.nil? or gem.version > hem.version 
     230              latest 
     231            end.values.inject([]) do |mem, gem| 
     232              Dir[gem.full_gem_path + '/{rails_,}generators/**/*_generator.rb'].each do |generator| 
     233                mem << generator 
     234              end 
     235              mem 
     236            end 
     237          end 
     238      end 
     239    end 
     240 
    208241  end 
    209242end 
  • lib/rails_generator/spec.rb

    old new  
    22  module Generator 
    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 
    88