Skip to content
This repository
Browse code

assets compilation task refactoring

  • Loading branch information...
commit 63bab287ec8a0789485bd90417f64f7e696d2898 1 parent 4bc6e2f
Mark J. Titorenko authored October 04, 2011
19  actionpack/lib/sprockets/assets.rake
@@ -28,12 +28,18 @@ namespace :assets do
28 28
       config = Rails.application.config
29 29
       config.assets.compile = true
30 30
       config.assets.digest  = digest unless digest.nil?
  31
+
31 32
       config.assets.digests = {}
32 33
 
33 34
       env    = Rails.application.assets
34 35
       target = File.join(Rails.public_path, config.assets.prefix)
35  
-      static_compiler = Sprockets::StaticCompiler.new(env, target, :digest => config.assets.digest)
36  
-      static_compiler.precompile(config.assets.precompile)
  36
+      compiler = Sprockets::StaticCompiler.new(env, 
  37
+                                               target,
  38
+                                               config.assets.precompile,
  39
+                                               :manifest_path => config.assets.manifest,
  40
+                                               :digest => config.assets.digest,
  41
+                                               :manifest => digest.nil?)
  42
+      compiler.compile
37 43
     end
38 44
 
39 45
     task :all do
@@ -42,14 +48,7 @@ namespace :assets do
42 48
     end
43 49
 
44 50
     task :digest => ["assets:environment", "tmp:cache:clear"] do
45  
-      manifest      = internal_precompile
46  
-      config        = Rails.application.config
47  
-      manifest_path = config.assets.manifest || File.join(Rails.public_path, config.assets.prefix)
48  
-      FileUtils.mkdir_p(manifest_path)
49  
-
50  
-      File.open("#{manifest_path}/manifest.yml", 'wb') do |f|
51  
-        YAML.dump(manifest, f)
52  
-      end
  51
+      internal_precompile
53 52
     end
54 53
 
55 54
     task :nondigest => ["assets:environment", "tmp:cache:clear"] do
49  actionpack/lib/sprockets/static_compiler.rb
@@ -2,41 +2,50 @@
2 2
 
3 3
 module Sprockets
4 4
   class StaticCompiler
5  
-    attr_accessor :env, :target, :digest
  5
+    attr_accessor :env, :target, :paths
6 6
 
7  
-    def initialize(env, target, options = {})
  7
+    def initialize(env, target, paths, options = {})
8 8
       @env = env
9 9
       @target = target
  10
+      @paths = paths
10 11
       @digest = options.key?(:digest) ? options.delete(:digest) : true
  12
+      @manifest = options.key?(:manifest) ? options.delete(:manifest) : true
  13
+      @manifest_path = options.delete(:manifest_path) || target
11 14
     end
12 15
 
13  
-    def precompile(paths)
14  
-      Rails.application.config.assets.digest = digest
  16
+    def compile
15 17
       manifest = {}
16  
-
17 18
       env.each_logical_path do |logical_path|
18  
-        next unless precompile_path?(logical_path, paths)
  19
+        next unless compile_path?(logical_path)
19 20
         if asset = env.find_asset(logical_path)
20  
-          manifest[logical_path] = compile(asset)
  21
+          manifest[logical_path] = write_asset(asset)
21 22
         end
22 23
       end
23  
-      manifest
  24
+      write_manifest(manifest) if @manifest
24 25
     end
25 26
 
26  
-    def compile(asset)
27  
-      asset_path = digest_asset(asset)
28  
-      filename = File.join(target, asset_path)
29  
-      FileUtils.mkdir_p File.dirname(filename)
30  
-      asset.write_to(filename)
31  
-      asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
32  
-      asset_path
  27
+    def write_manifest(manifest)
  28
+      FileUtils.mkdir_p(@manifest_path)
  29
+      File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
  30
+        YAML.dump(manifest, f)
  31
+      end
  32
+    end
  33
+
  34
+    def write_asset(asset)
  35
+      path_for(asset).tap do |path|
  36
+        filename = File.join(target, path)
  37
+        FileUtils.mkdir_p File.dirname(filename)
  38
+        asset.write_to(filename)
  39
+        asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
  40
+      end
33 41
     end
34 42
 
35  
-    def precompile_path?(logical_path, paths)
  43
+    def compile_path?(logical_path)
36 44
       paths.each do |path|
37  
-        if path.is_a?(Regexp)
  45
+        case path
  46
+        when Regexp
38 47
           return true if path.match(logical_path)
39  
-        elsif path.is_a?(Proc)
  48
+        when Proc
40 49
           return true if path.call(logical_path)
41 50
         else
42 51
           return true if File.fnmatch(path.to_s, logical_path)
@@ -45,8 +54,8 @@ def precompile_path?(logical_path, paths)
45 54
       false
46 55
     end
47 56
 
48  
-    def digest_asset(asset)
49  
-      digest ? asset.digest_path : asset.logical_path
  57
+    def path_for(asset)
  58
+      @digest ? asset.digest_path : asset.logical_path
50 59
     end
51 60
   end
52 61
 end

0 notes on commit 63bab28

Please sign in to comment.
Something went wrong with that request. Please try again.