Combining many rake tasks into one rake task

前端 未结 3 816
小蘑菇
小蘑菇 2021-02-02 10:16

Instead of running each rake task individually like this:

rake db:drop
rake db:create
rake db:migrate
rake db:load

I want to run one rake task

相关标签:
3条回答
  • 2021-02-02 10:50
    namespace :rebuild_dev do
     desc 'This rebuilds development db'
      task :clean_slate => :environment do 
        Rake::Task["db:drop"].invoke
        Rake::Task["db:create"].invoke
        Rake::Task["db:migrate"].invoke
        Rake::Task["db:load"].invoke
      end
    end
    
    0 讨论(0)
  • 2021-02-02 10:59

    You can do it with dependencies on a task with no body.

    desc 'This rebuilds development db'
    task :rebuild_dev => ["db:drop", "db:create", "db:migrate", "db:load"]
    
    0 讨论(0)
  • 2021-02-02 11:09

    You want invoke not execute. A little excerpt from my own code showing how to pass variables:

    namespace :clients do
    
      task :create, [:client] => ["clients:creation:checks"] do |t, args|
        Rake::Task["clients:creation:git"].invoke(client, password)
        Rake::Task["server:virtualhost:create"].invoke(client)
        Rake::Task["server:virtualhost:enable"].invoke(client)
        Rake::Task["server:reload"].invoke
        Rake::Task["db:roles:create"].invoke(client, password)
        Rake::Task["db:create"].invoke(client, client)
        Rake::Task["db:migrate"].invoke(client)
      end
    
    end
    

    Alternatively, you can make the task depend upon another task as I have done above with :create depending upon clients:creation:checks.

    Just to clarify, a namespace is for grouping tasks, so you must actually define the tasks within the namespace as I have above. You can't simply call tasks from within a namespace.

    So your code above should be:

    desc 'This rebuilds development db'
    task :rebuild_dev do
      Rake::Task["db:drop"].invoke
      Rake::Task["db:create"].invoke
      Rake::Task["db:migrate"].invoke
      Rake::Task["db:load"].invoke
    end
    
    0 讨论(0)
提交回复
热议问题