0

我正在编写一个 Rails 生成器,它将文件/文件夹从我的 gem 的模板目录复制到应用程序的目录中。它在我运行时按预期工作,rails generate mygem:install但是当我尝试使用它来反转它时,rails destroy mygem:install它不会删除新创建的子文件夹。

模板文件夹

├── templates
│   ├── views
│   │   ├── about
│   │   │   ├── index.html.erb
│   │   ├── contact
│   │   │   ├── index.html.erb 

app 文件夹(生成后)

├── app
│   ├── views
│   │   ├── about
│   │   │   ├── index.html.erb
│   │   ├── contact
│   │   │   ├── index.html.erb 

app 文件夹(销毁后)

├── app
│   ├── views
│   │   ├── about
│   │   ├── contact

期望的结果

├── app
│   ├── views

我的 gem 的安装生成器

module Mygem
  module Generators
    class InstallGenerator < Rails::Generators::Base

      source_root File.expand_path('../templates', __FILE__)

      def copy_templates
        templates = Dir.glob("#{source_paths[0]}/*")
        directory(templates[0], "app/views/")
      end

    end
  end
end
4

1 回答 1

0

我遇到了同样的问题 - 通过将以下内容添加到我的 generator.rb 文件来解决

def clean_up
  case self.behavior
    when :revoke then `rm -rf path/to/directory/`
  end
end

您还可以:invoke选择指定仅在生成时发生的操作:

case self.behavior
  when :invoke then do_something
end

所以

# something_generator.rb

def generate_directory
  case self.behavior
  when :invoke
    `mkdir path/to/directory`
  when :revoke
    `rm -rf path/to/directory`
  end
end
于 2019-01-16T12:00:30.420 回答