astropanic
astropanic

Reputation: 10939

Asset pipeline / precompile assets task

How I can make the rake task

assets:precompile 

available into my rails 2.3.14 app ?

Upvotes: 2

Views: 1854

Answers (2)

Joris
Joris

Reputation: 617

If you are looking for the source code of the assets:precompile rake task, you can find it here:

https://github.com/rails/rails/blob/3-1-stable/actionpack/lib/sprockets/assets.rake

Don't expect it to run as-is when you copy it to your lib/tasks in your rails 2.3.14 app with the sprockets and sprockets-helpers gems.

[update]

I made a simple precompiler rake task for use in rails 2.3.14 (without any javascript compression). You might want to change some things, depending on your configuration. Test the clean up task carefully, because it use a rm_rf command ;-)

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

[update #2]

I am now using this approach, and that works fine: http://jaredonline.github.io/blog/2012/05/16/sprockets-2-with-rails-2-dot-3/

Upvotes: 4

John Feminella
John Feminella

Reputation: 311755

There isn't a straightforward approach. The asset pipeline relies on several pieces of architecture in Rails 3.1.x that aren't present in Rails 2.3.

You can try using the approach that Davis Frank outlines here, but be warned that it requires a number of steps.

Upvotes: 3

Related Questions