开发者

How to DRY Capistrano recipes?

开发者 https://www.devze.com 2023-04-05 14:11 出处:网络
I am using Ruby on Rails and the Capistrano gem. I would like to DRY (Don\'t Repeat Yourself) a Capistr开发者_运维百科ano recipe.

I am using Ruby on Rails and the Capistrano gem. I would like to DRY (Don't Repeat Yourself) a Capistr开发者_运维百科ano recipe.

In the deploy.rb file I have:

# First task
task :first_task do
  ...

  run "cd #{current_path}; #{try_sudo} chmod -R 666 /log/production.log"
end

# Second task
task :second_task do
  run "..."

  # The following code is equal to that stated in the first task.
  run "cd #{current_path}; #{try_sudo} chmod -R 666 /log/production.log"
end

So, how can I DRY the above code so to have not duplicated tasks?


Capistrano code is just ruby code with a Domain Specific Language (DSL), so you should be able to do:

def chmod_log
  run "cd #{current_path}; #{try_sudo} chmod -R 666 /log/production.log"
end

# First task
task :first_task do
  ...

  chmod_log
end

# Second task
task :second_task do
  run "..."

  # The following code is equal to that stated in the first task.
  chmod_log
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号