我怎样才能完成抽成任务
assets:precompile
可用于我的 Rails 2.3.14 应用程序?
最佳答案
如果你正在寻找 assets:precompile rake 任务的源代码,你可以在这里找到它:
https://github.com/rails/rails/blob/3-1-stable/actionpack/lib/sprockets/assets.rake
当您使用 sprockets 和 sprockets-helpers gem 将它复制到 rails 2.3.14 应用程序中的 lib/tasks 时,不要指望它会按原样运行。
[更新]
我做了一个简单的预编译器 rake 任务用于 rails 2.3.14(没有任何 javascript 压缩)。根据您的配置,您可能想要更改一些内容。仔细测试清理任务,因为它使用 rm_rf 命令;-)
BUILD_DIR = Rails.root.join("public/assets")
DIGEST = true
namespace :assets do
task :compile => :cleanup do
sprockets = Sprockets::Environment.new
sprockets.append_path 'app/assets/images'
sprockets.append_path 'app/assets/javascripts'
sprockets.append_path 'app/assets/stylesheets'
sprockets.each_logical_path do |logical_path|
if asset = sprockets.find_asset(logical_path)
target_filename = DIGEST ? asset.digest_path : asset.logical_path
prefix, basename = asset.pathname.to_s.split('/')[-2..-1]
FileUtils.mkpath BUILD_DIR.join(prefix)
filename = BUILD_DIR.join(target_filename)
puts "write asset: #{filename}"
asset.write_to(filename)
#asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
end
end
end
# Cleanup asset directory
task :cleanup do
dirs = Dir.glob(File.join(BUILD_DIR.join("{*}")))
dirs.each do |dir|
puts "removing: #{dir}"
FileUtils.rm_rf dir
end
end
end
[更新 #2]
我现在正在使用这种方法,效果很好: http://jaredonline.github.io/blog/2012/05/16/sprockets-2-with-rails-2-dot-3/
关于ruby-on-rails - Assets 管道/预编译 Assets 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9034415/