Class: Sprocketize::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/sprocketize/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, target, paths, options = {}) ⇒ Compiler

Returns a new instance of Compiler.



7
8
9
10
11
12
13
14
15
# File 'lib/sprocketize/compiler.rb', line 7

def initialize(env, target, paths, options = {})
  @env = env
  @target = target
  @paths = paths
  @gzip = options.key?(:gzip) ? options.delete(:gzip) : false
  @digest = options.key?(:digest) ? options.delete(:digest) : false
  @manifest = options.key?(:manifest) ? options.delete(:manifest) : false
  @manifest_path = options.delete(:manifest_path) || target
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



5
6
7
# File 'lib/sprocketize/compiler.rb', line 5

def env
  @env
end

#pathsObject

Returns the value of attribute paths.



5
6
7
# File 'lib/sprocketize/compiler.rb', line 5

def paths
  @paths
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'lib/sprocketize/compiler.rb', line 5

def target
  @target
end

Instance Method Details

#compileObject



17
18
19
20
21
22
23
24
25
# File 'lib/sprocketize/compiler.rb', line 17

def compile
  manifest = {}
  env.each_logical_path do |logical_path|
    asset = env.find_asset(logical_path)
    next if asset.nil? || !compile_asset?(asset)
    manifest[logical_path] = write_asset(asset)
  end
  write_manifest(manifest) if @manifest
end

#compile_asset?(asset) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sprocketize/compiler.rb', line 43

def compile_asset?(asset)
  paths.each do |path|
    case path
    when Regexp
      return true if path.match(asset.pathname.to_s)
    when Proc
      return true if path.call(asset)
    else
      return true if File.fnmatch(path.to_s, asset.pathname.to_s)
    end
  end
  false
end

#path_for(asset) ⇒ Object



57
58
59
# File 'lib/sprocketize/compiler.rb', line 57

def path_for(asset)
  @digest ? asset.digest_path : asset.logical_path
end

#write_asset(asset) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/sprocketize/compiler.rb', line 34

def write_asset(asset)
  path_for(asset).tap do |path|
    filename = File.join(target, path)
    FileUtils.mkdir_p File.dirname(filename)
    asset.write_to(filename)
    asset.write_to("#{filename}.gz") if @gzip && filename.to_s =~ /\.(css|js)$/
  end
end

#write_manifest(manifest) ⇒ Object



27
28
29
30
31
32
# File 'lib/sprocketize/compiler.rb', line 27

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