用于预编译资产的自定义rake任务本地rails 3.2.8

时间:2013-01-04 16:46:10

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

我需要在我的本地计算机上预编译资产,然后使用capistrano进行部署,并预先编译资产。

我已添加到development.rb

config.assets.prefix = "/dev-assets"

另外,我已添加到application.rb

config.assets.initialize_on_precompile = false

而是手动执行rake assets:precompile,我希望从capistrano文件中自动完成此过程,清理资产,上传等等。我尝试过这个自定义任务

namespace :assets do
    task :precompile, :roles => :web, :except => { :no_release => true } do
      from = source.next_revision(current_revision)
      if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
        run_locally "bundle exec rake assets:precompile"
        run_locally "rsync -zvrh --progress -e 'ssh -i #{ssh_options[:keys][0]}' public/assets #{user}{server}:#{shared_path}"
        puts "cleaning up locally compiled assets"
        run_locally "bundle exec rake assets:clean"
      else
        puts "Skipping asset pre-compilation because there were no asset changes"
      end
    end
  end

但是我收到了一个错误:

/config/deploy.rb:73:in `block (3 levels) in load': undefined method `[]' for nil:NilClass (NoMethodError)

我怎样才能在本地和使用capistrano上传后对资产进行预编译?

2 个答案:

答案 0 :(得分:2)

问题得到解决:

这是我的自定义任务正常工作:

 namespace :assets do
    task :precompile, :roles => :web, :except => { :no_release => true } do
      from = source.next_revision(current_revision)
      if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
        run_locally("rm -rf public/assets/*") 
        run_locally "bundle exec rake assets:precompile"
        find_servers_for_task(current_task).each do |server|
         run_locally "rsync -vr --exclude='.DS_Store' public/assets #{user}@#{server.host}:#{shared_path}/"
        end
      else
        puts.info "Skipping asset pre-compilation because there were no asset changes"
      end
    end
  end

答案 1 :(得分:0)

看起来logger.info块中的else是个问题。

如果由于其他原因需要使用Capistrano的记录器,您可能需要手动初始化它,因为您实际上并未在服务器中运行。但是,打印到控制台可能更容易(正如您正在使用上面的其他消息

替换

logger.info "Skipping ..."

puts "Skipping ..."