Rake:在特定环境(开发/生产)中列出/删除自定义 rake 任务

Rake : List / Remove custom rake task in specific environment (development / production )

有没有办法让我的自定义 rake 任务只在 development 环境中列出,而不在 productionQA 中列出?

我的意思是,当我在 qaproduction 环境中调用 bundle exec rake -T 时,它不应列出我的自定义 rake tasks 但它应该在 development 中.

您可以将此添加到应用程序 Rakefile(在 MyRailsApp::Application.load_tasks 行之后):

if Rails.env == "production"
  # add here the tasks you want to disable
  tasks_to_disable = [
    "db:drop",
    "db:reset"
  ]
  tasks = Rake.application.instance_variable_get("@tasks")
  tasks.delete_if { |task_name, _| tasks_to_disable.include?(task_name) }
end