2011-09-20 03:12:29 -04:00
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
module Sprockets
|
|
|
|
class StaticCompiler
|
2011-10-04 18:05:01 -04:00
|
|
|
attr_accessor :env, :target, :paths
|
2011-09-20 03:12:29 -04:00
|
|
|
|
2011-10-04 18:05:01 -04:00
|
|
|
def initialize(env, target, paths, options = {})
|
2011-09-20 03:12:29 -04:00
|
|
|
@env = env
|
|
|
|
@target = target
|
2011-10-04 18:05:01 -04:00
|
|
|
@paths = paths
|
2011-09-20 03:12:29 -04:00
|
|
|
@digest = options.key?(:digest) ? options.delete(:digest) : true
|
2011-10-04 18:05:01 -04:00
|
|
|
@manifest = options.key?(:manifest) ? options.delete(:manifest) : true
|
|
|
|
@manifest_path = options.delete(:manifest_path) || target
|
2011-09-20 03:12:29 -04:00
|
|
|
end
|
|
|
|
|
2011-10-04 18:05:01 -04:00
|
|
|
def compile
|
2011-09-20 03:12:29 -04:00
|
|
|
manifest = {}
|
|
|
|
env.each_logical_path do |logical_path|
|
2011-10-04 18:05:01 -04:00
|
|
|
next unless compile_path?(logical_path)
|
2011-09-20 03:12:29 -04:00
|
|
|
if asset = env.find_asset(logical_path)
|
2011-10-04 18:05:01 -04:00
|
|
|
manifest[logical_path] = write_asset(asset)
|
2011-09-20 03:12:29 -04:00
|
|
|
end
|
|
|
|
end
|
2011-10-04 18:05:01 -04:00
|
|
|
write_manifest(manifest) if @manifest
|
2011-09-20 03:12:29 -04:00
|
|
|
end
|
|
|
|
|
2011-10-04 18:05:01 -04:00
|
|
|
def write_manifest(manifest)
|
|
|
|
FileUtils.mkdir_p(@manifest_path)
|
|
|
|
File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
|
|
|
|
YAML.dump(manifest, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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 filename.to_s =~ /\.(css|js)$/
|
|
|
|
end
|
2011-09-20 03:12:29 -04:00
|
|
|
end
|
|
|
|
|
2011-10-04 18:05:01 -04:00
|
|
|
def compile_path?(logical_path)
|
2011-09-20 03:12:29 -04:00
|
|
|
paths.each do |path|
|
2011-10-04 18:05:01 -04:00
|
|
|
case path
|
|
|
|
when Regexp
|
2011-09-20 03:12:29 -04:00
|
|
|
return true if path.match(logical_path)
|
2011-10-04 18:05:01 -04:00
|
|
|
when Proc
|
2011-09-20 03:12:29 -04:00
|
|
|
return true if path.call(logical_path)
|
|
|
|
else
|
|
|
|
return true if File.fnmatch(path.to_s, logical_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2011-10-04 18:05:01 -04:00
|
|
|
def path_for(asset)
|
|
|
|
@digest ? asset.digest_path : asset.logical_path
|
2011-09-20 03:12:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|