Rake 任务依赖于其他 Rake 任务

Rake task depend on other rake task

我有为 Git 提供功能的 rake 任务。我希望能够调用 rake git:pull,它应该识别出目录 @source_dir 不存在,然后它将在尝试 git:pull 之前调用 git:clone。可以将这样的依赖项添加到我的任务中吗?

namespace :git do
  desc "Download and create a copy of code from git server"
  task :clone do
    puts 'Cloning repository'.pink
    sh "git clone -b #{@git_branch} --single-branch #{@git_clone_url} #{@source_dir}"
    puts 'Clone complete'.green
  end

  desc "Fetch and merge from git server, using current checked out branch"
  task :pull do
    puts 'Pulling git'.pink
    sh "cd '#{@source_dir}'; git pull"
    puts 'Pulled'.green
  end

  desc "Shows status of all files in git repo"
  task :status do
    puts 'Showing `git status` of all source files'.pink
    sh "cd #{@source_dir} && git status --short"
  end
end

通常您只需像这样声明依赖项:

task :pull => :clone do
  # ...
end

或者在多个依赖的情况下:

task :status => [ :clone, :pull ] do
  # ...
end