[英]Capistrano: deploy.rb file refactoring
I have following code in my deploy.rb 我在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. 我认为保持deploy.rb文件干净是个好主意,我试图在config下将代码移到capistrano_utilities.rb之上。 I am using Rails application.
我正在使用Rails应用程序。 And I added following line of code to deploy.rb
我将以下代码行添加到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 . deploy.rb中self的值是Capistrano :: Configuration。 While the value of self in capistrano_utilities is Main.
虽然capistrano_utilities中self的值是Main。 So I understand why I am getting namespace method error.
所以我理解为什么我得到名称空间方法错误。 What is the fix for this problem?
这个问题的解决方法是什么?
In your config/deploy.rb
, try load
instead of require
. 在
config/deploy.rb
,尝试load
而不是require
。 Also, capistrano already runs as if you're at the RAILS_ROOT
, so there's no need to use __FILE__
: 此外,capistrano已经运行,就像你在
RAILS_ROOT
,因此不需要使用__FILE__
:
load "lib/capistrano_utilities"
In a capistrano config file, load
is redefined to load another configuration file into the current configuration. 在一个Capistrano的配置文件,
load
被重新定义到另一个配置文件加载到当前配置。 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. 在向它传递路径时,它实际上调用
load_from_file
(由capistrano定义的私有方法),它只从磁盘读取文件,而instance_eval
从中读取文件。
Check your Capfile on Rails.root. 检查Rails.root上的Capfile。 if you use capistrano 3, you see this line;
如果你使用capistrano 3,你会看到这一行;
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. 现在,将您的文件放在“lib / capistrano / tasks / capistrano_utilities.cap”上,它将被加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.