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

后端 未结 4 1968
遥遥无期
遥遥无期 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:43

    The source_paths method doesn't work when using AppBuilder. (which is another option to using rails templates). I have a files directory next to the app_builder.rb file that this class is in. I have this working, though it seems there should still be a more elegant way.

    tree .
    |-- app_builder.rb
    |-- files
         `-- Gemfile
    
    class AppBuilder < Rails::AppBuilder
    
      def initialize generator
        super generator
        path = File.expand_path( File.join( '..', File.dirname( __FILE__ )) )
        source_paths << path
      end
    
      def gemfile
        copy_file 'files/Gemfile', 'Gemfile'
      end
    

    and then on the console:

    rails new my_app -b path_to_app_builder.rb

    The dots are required since the ruby file 'app_builder.rb' is slurped up and eval'd after the rails new command changes into the new app directory (I think).

提交回复
热议问题