如何创建rake任务
assets:precompile 是否可用于我的rails 2.3.14应用程序?
发布于 2012-02-10 06:43:47
如果你正在寻找assets:预编译rake任务的源代码,你可以在这里找到它:
https://github.com/rails/rails/blob/3-1-stable/actionpack/lib/sprockets/assets.rake
当你在你的rails 2.3.14应用程序中使用链轮和链轮-帮助器gem将它复制到你的lib/task中时,不要期望它会按原样运行。
更新
我创建了一个简单的预编译器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/
发布于 2012-01-27 22:10:34
没有一种直接的方法。资产管道依赖于Rails 3.1.x中的几个架构,而这些架构在Rails 2.3中并不存在。
您可以尝试使用的方法,但需要注意的是,它需要许多步骤。
https://stackoverflow.com/questions/9034415
复制相似问题