2

我正在制作一个自定义生成器来生成一个新的 rails 应用程序,我这样做是这样的

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

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

问题是,我如何添加一个新的源目录(然后由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

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

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

4

4 回答 4

5

这有效:

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

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

class AppBuilder < Rails::AppBuilder
  ...
end

我不明白为什么,但我已经在这上面花了太多时间,所以我不在乎。

于 2012-03-30T13:57:11.253 回答
4

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

所以你在课堂上需要做的就是:

class NewgemGenerator < Thor::Group

  include Thor::Actions

  def source_paths
    ['/whatever', './templates']
  end

end

希望这可以帮助 :)

于 2012-03-30T09:30:11.447 回答
1

source_paths 方法在使用 AppBuilder 时不起作用。(这是使用 rails 模板的另一种选择)。我在这个类所在的 app_builder.rb 文件旁边有一个文件目录。我有这个工作,但似乎仍然应该有一个更优雅的方式。

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

然后在控制台上:

rails new my_app -b path_to_app_builder.rb

rails new这些点是必需的,因为在命令更改为新的应用程序目录(我认为)之后,ruby 文件“app_builder.rb”被吞并并评估。

于 2013-02-11T03:32:32.353 回答
0

这是一个旧帖子,但是,问题仍然存在,我花了一些时间来解决这个问题。如果有帮助,我在这里发表评论。

Rails 默认添加路径lib/templates,因此您可以通过将它们复制到此目录中来自定义任何模板。

检查正确的目录结构https://github.com/rails/rails/tree/v6.0.1/railties/lib/rails/generators/rails

虽然有微妙之处,但并不那么明显。

helper生成器为例,正如 Rails 官方文档中提到的那样,结构是

https://github.com/rails/rails/tree/v6.0.1/railties/lib/rails/generators/rails/helper

- helper
  - templates
    - helper.rb.tt
  - helper_generator.rb 

在这里,Railties期望在您的项目中找到什么:

- lib
  - templates 
    - helper
      - helper.rb

helper.rb的副本helper.rb.tt

最后一件事,如果您打算在其中使用它,Rails::Engine则必须告诉它加载该路径

# lib/blorgh/engine.rb 
module Blorgh
  class Engine < ::Rails::Engine
    isolate_namespace Blorgh
    config.generators.templates << File.expand_path('../templates', __dir__)
  end
end

真的希望可以帮助并为他人节省时间。

  1. https://github.com/rails/rails/blob/v6.0.1/railties/lib/rails/application/configuration.rb#L188
  2. https://guides.rubyonrails.org/generators.html#customizing-your-workflow-by-changeing-generators-templates
  3. https://github.com/rails/rails/blob/v6.0.1/railties/CHANGELOG.md
  4. https://guides.rubyonrails.org/engines.html
于 2019-12-03T16:48:57.560 回答