如何为位于lib/< plugin_name>目录中的插件实现生成器? [英] How to implement generators for a plugin located at the `lib/<plugin_name>` directory?
问题描述
我正在使用Ruby on Rails 3.2.2.我已经实现了Something
插件(它几乎是一个gem,但不是 一个gem),所有相关文件都位于lib/something
目录中.由于我想自动执行与该插件相关的代码生成,因此我想到了 Ruby on Rails Generators .因此,对于Something
插件,我正在寻找在lib/something
目录中实现我自己的生成器.
I am using Ruby on Rails 3.2.2. I have implemented a Something
plugin (it is almost a gem, but is not a gem) and all related files are in the lib/something
directory. Since I would like to automate code generation related to that plugin, I came up with Ruby on Rails Generators. So, for the Something
plugin, I am looking for implementing my own generators in the lib/something
directory.
我应该怎么做?处方是什么?也就是说,例如,应调用哪个rails generate
命令行以正确生成lib/something
目录中的所有所需文件?生成器仍然可以与插件( not gem)一起使用吗?关于这件事有什么建议?
How should I make that and what are prescriptions? That is, for example, what rails generate
command line should be invoked to properly generate all needed files in the lib/something
directory? generators would still work with plugins (not gem)? what are advices about this matter?
推荐答案
我会把它做成宝石.我已经使用gems生成了生成器,但是我不知道生成器是否仍然可以与插件一起使用.
I would make it a gem. I've made generators using gems, but I don't know if the generators would still work with plugins.
如果您在使用命令行时遇到困难,我想您不需要任何参数. (如果需要参数,我可以复制提供的模板,如果需要其他参数,我会迷路,因此我的建议仅限于非参数.)
If you are having difficulty with the command line, I am guessing that you don't need any argument. (If you need an argument, I could copy the provided templates, and if I needed some other argument I'd be lost, so my advise is limited to non-argument.)
我有一个生成器gem,它生成另一个gem所需的迁移文件.它检查具有给定根名(不带时间戳前缀)的迁移是否在db/migrate中,否则创建它.
I have a generator gem which generates migration files needed for another gem. It checks if the migration with a given root name (w/o the timestamp prefix) is in db/migrate, and otherwise creates it.
这是我的代码.我认为这个例子就是您需要的帮助.
Here is my code. I think this example is the help you need.
class ItrcClientFilesGenerator < Rails::Generators::Base
source_root(File.dirname(__FILE__) + "/../src")
desc "Generator to create migrations for needed db tables"
def create_itrc_client_files
prefix = DateTime.now.strftime("%Y%m%d%H%M")
existing_migrations =
Dir.glob("db/migrate/*itrc*").map do |path|
File.basename(path).gsub(/^\d*_/, '')
end
Dir.glob(File.dirname(__FILE__) + "/../src/*").sort.each_with_index do |src_filepath, index|
src_filename = File.basename(src_filepath)
unless existing_migrations.include?(src_filename.gsub(/^\d*_/, '')) then
this_prefix = "#{prefix}#{'%02i' % index}_"
dst_filename = src_filename.gsub(/^\d*_/, this_prefix)
copy_file(src_filename, "db/migrate/" + dst_filename)
end
end
end
end
这篇关于如何为位于lib/< plugin_name>目录中的插件实现生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!