【问题标题】:How to DRY Capistrano recipes?如何干燥 Capistrano 食谱?
【发布时间】:2011-09-15 10:16:57
【问题描述】:

我正在使用 Ruby on Rails 和 Capistrano gem。我想干燥(不要重复自己)Capistrano 食谱。

deploy.rb 文件中我有:

# 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

那么,我怎样才能干掉上面的代码,以免重复任务?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 capistrano recipe


    【解决方案1】:

    Capistrano 代码只是具有领域特定语言 (DSL) 的 ruby​​ 代码,因此您应该能够做到:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      相关资源
      最近更新 更多