Capistrano:deploy.rb文件重构 [英] Capistrano: deploy.rb file refactoring
问题描述
我的deploy.rb中有以下代码
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
我认为保持我的deploy.rb文件整洁是一个好主意,我尝试将上述代码移至config下的capistrano_utilities.rb。我正在使用Rails应用程序。然后我将以下代码行添加到deploy.rb
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")
现在我遇到以下错误。
undefined method `namespace' for main:Object (NoMethodError)
deploy.rb中self的值为Capistrano :: Configuration。而capistrano_utilities中self的值为Main。因此,我理解了为什么会出现名称空间方法错误。解决此问题的方法是什么?
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?
推荐答案
在您的 config / deploy.rb $ c中$ c>,请尝试
加载
,而不是要求
。另外,capistrano已经像您在 RAILS_ROOT
一样运行,因此无需使用 __ FILE __
:
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"
在capistrano配置文件中,重新定义了 load
以将另一个配置文件加载到当前配置中。当传递路径时,它实际上会调用 load_from_file
(由capistrano定义的私有方法),该函数仅从磁盘和 instance_eval $中读取文件。 c $ c>是。
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.
这篇关于Capistrano:deploy.rb文件重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!