如何更改自定义Rails生成器的源? (雷神) [英] How to change source for a custom rails generator? (Thor)

查看:86
本文介绍了如何更改自定义Rails生成器的源? (雷神)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个自定义生成器,该生成器将生成一个新的Rails应用,并且我这样做是

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'

class AppBuilder < Rails::AppBuilder
  include Thor::Actions
  include Thor::Shell
  ...
end

问题是,如何添加新的源目录(然后由Thor::Actions#copy_fileThor::Actions#template等使用)?我在Thor的文档中看到Thor::Actions#source_paths保留了源代码(它是路径的数组),所以我尝试在类中覆盖它(因为我已经包含了Thor::Actions):

The problem is, how do I add a new source directory (which is then used by Thor::Actions#copy_file, Thor::Actions#template, and the others)? I saw in the Thor's documentation that Thor::Actions#source_paths holds the sources (it's an array of paths), so I tried overriding it inside my class (since I've included Thor::Actions):

def source_paths
  [File.join(File.expand_path(File.dirname(__FILE__)), "templates")] + super
end

为此,我想在源代码中添加./templates目录,同时仍保留Rails的目录(这就是+ super结尾的原因).但这不起作用,它仍将Rails的源路径列为唯一路径.

With this I wanted to add the ./templates directory in the sources, while still keeping the Rails' one (that's why the + super at the end). But it doesn't work, it still lists the Rails' source path as the only one.

我尝试浏览Rails的源代码,但是找不到Rails如何在源路径中放置他的目录.我真的很想知道:)

I tried browsing through the Rails' source code, but I couldn't find how Rails put his directory in the source paths. And I really want to know that :)

推荐答案

此方法有效:

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

module Thor::Actions
  def source_paths
    [MY_TEMPLATES]
  end
end

class AppBuilder < Rails::AppBuilder
  ...
end

我不明白为什么,但是我已经花了很多时间,所以我不在乎.

I don't understand why, but I've spent too much time on this already, so I don't care.

这篇关于如何更改自定义Rails生成器的源? (雷神)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆