jan*_*o-m 2 ruby-on-rails generator thor
我正在制作一个自定义生成器,生成一个新的rails应用程序,我这样做
require 'thor'
require 'rails/generators/rails/app/app_generator'
class AppBuilder < Rails::AppBuilder
include Thor::Actions
include Thor::Shell
...
end
Run Code Online (Sandbox Code Playgroud)
问题是,我如何添加一个新的源目录(然后使用Thor::Actions#copy_file,Thor::Actions#template,和其他人)?我在Thor的文档中看到了Thor::Actions#source_paths保存源代码(它是一个路径数组),所以我尝试在我的课程中覆盖它(因为我已经包括在内Thor::Actions):
def source_paths
[File.join(File.expand_path(File.dirname(__FILE__)), "templates")] + super
end
Run Code Online (Sandbox Code Playgroud)
有了这个,我想./templates在源代码中添加目录,同时仍然保持Rails的一个(这就是+ super最后的原因).但它不起作用,它仍然将Rails的源路径列为唯一的路径.
我尝试浏览Rails的源代码,但我找不到Rails如何将他的目录放在源路径中.我真的想知道:)
Thor 将访问您的 source_paths 方法并将它们添加到默认值中:
# 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
Run Code Online (Sandbox Code Playgroud)
所以你在课堂上需要做的就是:
class NewgemGenerator < Thor::Group
include Thor::Actions
def source_paths
['/whatever', './templates']
end
end
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助 :)
这有效:
require 'thor'
require 'rails/generators/rails/app/app_generator'
module Thor::Actions
def source_paths
[MY_TEMPLATES]
end
end
class AppBuilder < Rails::AppBuilder
...
end
Run Code Online (Sandbox Code Playgroud)
我不明白为什么,但我已经花了太多时间在这上面,所以我不在乎.
| 归档时间: |
|
| 查看次数: |
1877 次 |
| 最近记录: |