我需要在我的本地机器上预编译资产,并在使用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上传后?
问题已解决:
这是我的自定义任务工作正常:
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
看起来else
块中的logger.info
是问题所在。
如果您由于其他原因需要在Capistrano中使用日志记录器,您可能需要手动初始化它,因为您实际上并没有在服务器中运行。但是直接打印到控制台可能更容易(就像您对
上面的其他消息所做的那样)。代替logger.info "Skipping ..."
puts "Skipping ..."