assets.rake 1.9 KB
Newer Older
1
namespace :assets do
2
  desc "Compile all the assets named in config.assets.precompile"
3 4 5
  task :precompile do
    # We need to do this dance because RAILS_GROUPS is used
    # too early in the boot process and changing here is already too late.
6 7 8
    if ENV["RAILS_GROUPS"].to_s.empty? || ENV["RAILS_ENV"].to_s.empty?
      ENV["RAILS_GROUPS"] ||= "assets"
      ENV["RAILS_ENV"]    ||= "production"
9
      ruby $0, *ARGV
10
    else
11
      require "fileutils"
12
      Rake::Task["tmp:cache:clear"].invoke
13
      Rake::Task["assets:environment"].invoke
J
Jon Leighton 已提交
14

15 16 17 18
      unless Rails.application.config.assets.enabled
        raise "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
      end

J
Jon Leighton 已提交
19 20
      # Ensure that action view is loaded and the appropriate sprockets hooks get executed
      ActionView::Base
21

J
Joshua Peek 已提交
22
      config = Rails.application.config
23 24 25
      config.assets.compile = true
      config.assets.digest = false if ENV["RAILS_ASSETS_NONDIGEST"]

J
Joshua Peek 已提交
26 27
      env    = Rails.application.assets

28
      target = File.join(Rails.public_path, config.assets.prefix)
29
      static_compiler = Sprockets::StaticCompiler.new(env, target, :digest => config.assets.digest)
30

31
      manifest = static_compiler.precompile(config.assets.precompile)
32 33
      manifest_path = config.assets.manifest || target
      FileUtils.mkdir_p(manifest_path)
34

35 36 37 38 39 40
      unless ENV["RAILS_ASSETS_NONDIGEST"]
        File.open("#{manifest_path}/manifest.yml", 'wb') do |f|
          YAML.dump(manifest, f)
        end
        ENV["RAILS_ASSETS_NONDIGEST"] = "true"
        ruby $0, *ARGV
41
      end
42
    end
43
  end
44 45

  desc "Remove compiled assets"
46
  task :clean => ['assets:environment', 'tmp:cache:clear'] do
J
Joshua Peek 已提交
47 48
    config = Rails.application.config
    public_asset_path = File.join(Rails.public_path, config.assets.prefix)
49
    rm_rf public_asset_path, :secure => true
50
  end
51 52 53 54 55

  task :environment do
    Rails.application.initialize!(:assets)
    Sprockets::Bootstrap.new(Rails.application).run
  end
56
end