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

Ticket #8730: gem_generators.patch

File gem_generators.patch, 5.3 kB (added by nicwilliams, 1 year ago)

Patch for railties gem (apply within /railties/ folder)

  • CHANGELOG

    old new  
    11*SVN* 
    22 
     3* Plugin generators can be placed in /rails_generators folder, in addition to /generators folder (in fact, any folder /*generators); to allow RubyGems with generators (see below) to be directly usable as plugins, and include generators. [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* Fixed that dispatcher preparation callbacks only run once in production mode.  Mock Routes.reload so that dispatcher preparation callback tests run. [Rick] 
    48 
    59* Fix syntax error in dispatcher than wrecked failsafe responses.  #8625 [mtitorenko] 
  • 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) 
     
    100100          if defined? ::RAILS_ROOT 
    101101            sources << PathSource.new(:lib, "#{::RAILS_ROOT}/lib/generators") 
    102102            sources << PathSource.new(:vendor, "#{::RAILS_ROOT}/vendor/generators") 
    103             sources << PathSource.new(:plugins, "#{::RAILS_ROOT}/vendor/plugins/*/**/generators") 
     103            sources << PathSource.new(:plugins, "#{::RAILS_ROOT}/vendor/plugins/*/**/*generators") 
    104104          end 
    105105          sources << PathSource.new(:user, "#{Dir.user_home}/.rails/generators") 
    106           sources << GemSource.new if Object.const_defined?(:Gem) 
     106          if Object.const_defined?(:Gem) 
     107            sources << GemGeneratorSource.new 
     108            sources << GemPathSource.new 
     109          end 
    107110          sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/components") 
    108111        end 
    109112 
     
    185188      end 
    186189    end 
    187190 
    188  
    189     # GemSource hits the mines to quarry for generators.  The latest versions 
    190     # of gems named *_generator are selected. 
    191     class GemSource < Source 
     191    class AbstractGemSource < Source 
    192192      def initialize 
    193193        super :RubyGems 
    194194      end 
     195    end 
    195196 
     197    # GemGeneratorSource hits the mines to quarry for generators.  The latest versions 
     198    # of gems named *_generator are selected. 
     199    class GemGeneratorSource < AbstractGemSource 
    196200      # Yield latest versions of generator gems. 
    197201      def each 
    198202        Gem::cache.search(/_generator$/).inject({}) { |latest, gem| 
     
    205209      end 
    206210    end 
    207211 
     212    # GemPathSource looks for generators within any RubyGem's /rails_generators/<generator_name>_generator.rb file. 
     213    class GemPathSource < AbstractGemSource 
     214      # Yield each generator within rails_generator subdirectories. 
     215      def each 
     216        generator_full_paths.each do |generator| 
     217          yield Spec.new(File.basename(generator).sub(/_generator.rb$/, ''), File.dirname(generator), label) 
     218        end 
     219      end 
     220 
     221      private 
     222      def generator_full_paths 
     223        @generator_full_paths ||=  
     224          begin 
     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    end 
     239 
    208240  end 
    209241end 
  • 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