Capistrano: deploy.rb file refactoring

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

I have following code in my deploy.rb

namespace :app do   desc "copies the configuration frile from ~/shared/config/*.yml to ~/config"   task :copy_config_files,:roles => :app do     run "cp -fv #{deploy_to}/shared/config/hoptoad.rb #{release_path}/config/initializers"     run "cp -fv #{deploy_to}/shared/config/app_config.yml #{release_path}/config/app_config.yml"   end end 

I thought it would be a good idea to keep my deploy.rb file clean and I attempted to move above code to capistrano_utilities.rb under config. I am using Rails application. And I added following line of code to deploy.rb

require File.expand_path(File.dirname(__FILE__) + "/../lib/capistrano_utilities") 

Now I am getting following error.

undefined method `namespace' for main:Object (NoMethodError) 

The value of self in the deploy.rb is Capistrano::Configuration . While the value of self in capistrano_utilities is Main. So I understand why I am getting namespace method error. What is the fix for this problem?

回答1:

In your config/deploy.rb, try load instead of require. Also, capistrano already runs as if you're at the RAILS_ROOT, so there's no need to use __FILE__:

load "lib/capistrano_utilities" 

In a capistrano config file, load is redefined to load another configuration file into the current configuration. When passing a path to it, it actually calls load_from_file (a private method defined by capistrano) that just reads the file from disk and instance_eval's it.



回答2:

Check your Capfile on Rails.root. if you use capistrano 3, you see this line;

Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }

Now, put your file on "lib/capistrano/tasks/capistrano_utilities.cap" and it will be loaded.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!