简体   繁体   中英

Capistrano: deploy.rb file refactoring

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM