aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/sprockets/assets.rake
blob: b7e8f39b43639a238e083e4624a318b8fde4b112 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace :assets do
  desc "Compile all the assets named in config.assets.precompile"
  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.
    if ENV["RAILS_GROUPS"].to_s.empty? || ENV["RAILS_ENV"].to_s.empty?
      ENV["RAILS_GROUPS"] ||= "assets"
      ENV["RAILS_ENV"]    ||= "production"
      Kernel.exec $0, *ARGV
    else
      Rake::Task["environment"].invoke
      Rake::Task["tmp:cache:clear"].invoke

      # Ensure that action view is loaded and the appropriate sprockets hooks get executed
      ActionView::Base

      # Always compile files
      Rails.application.config.assets.compile = true

      # Always ignore asset host
      Rails.application.config.action_controller.asset_host = nil

      config = Rails.application.config
      env    = Rails.application.assets
      target = Pathname.new(File.join(Rails.public_path, config.assets.prefix))
      manifest = {}
      manifest_path = config.assets.manifest || target

      config.assets.precompile.each do |path|
        env.each_logical_path do |logical_path|
          if path.is_a?(Regexp)
            next unless path.match(logical_path)
          elsif path.is_a?(Proc)
            next unless path.call(logical_path)
          else
            next unless File.fnmatch(path.to_s, logical_path)
          end

          if asset = env.find_asset(logical_path)
            asset_path = config.assets.digest ? asset.digest_path : logical_path
            manifest[logical_path] = asset_path
            filename = target.join(asset_path)

            mkdir_p filename.dirname
            asset.write_to(filename)
            asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
            asset.write_to(target.join(logical_path))
          end
        end
      end

      File.open("#{manifest_path}/manifest.yml", 'wb') do |f|
        YAML.dump(manifest, f)
      end
    end
  end

  desc "Remove compiled assets"
  task :clean => [:environment, 'tmp:cache:clear'] do
    config = Rails.application.config
    public_asset_path = File.join(Rails.public_path, config.assets.prefix)
    rm_rf public_asset_path, :secure => true
  end
end