简体   繁体   中英

custom rake task for precompile assets locally rails 3.2.8

I need precompile assets on my local machine and after make deploy with capistrano with assets precompiled.

I have added to development.rb :

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

also, I have added to application.rb

config.assets.initialize_on_precompile = false

Instead execute rake assets:precompile manually, I want make this process from capistrano file automated, clean assets, upload...etc. I have tried with this custom task

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

But I get a error:

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

How can I precompile assets on local and after upload with capistrano?

The problem was fixed:

This is my custom task is working fine:

 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

It looks like the logger.info in the else block is the issue.

If you need a logger in Capistrano for other reasons, you may need to initialize it manually because your not actually running in the server. But it might be easier to just print to the console (as you're doing with your other messages above

Replace

logger.info "Skipping ..."

with

puts "Skipping ..."

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