How to change source for a custom rails generator? (Thor)

后端 未结 4 1946
遥遥无期
遥遥无期 2020-12-21 15:18

I\'m making a custom generator that generates a new rails app, and I do it like this

require \'thor\'
require \'rails/generators/rails/app/app_generator\'

c         


        
4条回答
  •  不思量自难忘°
    2020-12-21 15:53

    Thor will access your source_paths method and add these to the defaults:

      # Returns the source paths in the following order:
      #
      #   1) This class source paths
      #   2) Source root
      #   3) Parents source paths
      #
      def source_paths_for_search
        paths = []
        paths += self.source_paths
        paths << self.source_root if self.source_root
        paths += from_superclass(:source_paths, [])
        paths
      end
    

    So all you need to do in your class is:

    class NewgemGenerator < Thor::Group
    
      include Thor::Actions
    
      def source_paths
        ['/whatever', './templates']
      end
    
    end
    

    Hope this helps :)

提交回复
热议问题