require 'sprockets' require 'sprockets-sass' require 'sass' class SprocketsCompiler # This is pretty much copy/paste from https://github.com/stevehodgkiss/guard-sprockets2/blob/master/lib/guard/sprockets2/compiler.rb # The only method added/changed is self.sprockets() def initialize(options = {}) configure(options) @target = Pathname.new(@assets_path) end def clean FileUtils.rm_rf @assets_path, :secure => true end def compile @sprockets.send(:expire_index!) success = true @precompile.each do |path| @sprockets.each_logical_path do |logical_path| next unless path_matches?(path, logical_path) if asset = @sprockets.find_asset(logical_path) success = compile_asset(asset, false) success = compile_asset(asset, true) if @digest break unless success end end end success end protected def compile_asset(asset, digest) filename = digest ? @target.join(asset.digest_path) : @target.join(asset.logical_path) puts filename FileUtils.mkdir_p filename.dirname asset.write_to(filename) asset.write_to("#{filename}.gz") if @gz && filename.to_s =~ /\.(css|js)$/ true rescue => e puts unless ENV['GUARD_ENV'] == 'test' UI.error e.message.gsub(/^Error: /, '') false end def path_matches?(path, logical_path) if path.is_a?(Regexp) path.match(logical_path) else File.fnmatch(path.to_s, logical_path) end end def configure(options) @sprockets = options[:sprockets] @assets_path = options[:assets_path] @precompile = options[:precompile] @digest = options[:digest] @gz = options[:gz] set_defaults end def set_defaults if defined?(Rails) @sprockets ||= Rails.application.assets @assets_path ||= File.join(Rails.public_path, Rails.application.config.assets.prefix) @precompile ||= Rails.application.config.assets.precompile else @assets_path ||= "#{Dir.pwd}/public/assets" @precompile ||= [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ] end @digest = true if @digest.nil? @gz = true if @gz.nil? end def self.setup(base_dir) sprockets = ::Sprockets::Environment.new(base_dir) sprockets.append_path 'assets/img' sprockets.append_path 'assets/js' sprockets.append_path 'assets/css' sprockets.append_path 'assets/lib' default_assets_dir = File.dirname( File.expand_path( __FILE__ ) ) + '/..' sprockets.append_path default_assets_dir + '/assets/img' sprockets.append_path default_assets_dir + '/assets/js' sprockets.append_path default_assets_dir + '/assets/css' sprockets.append_path default_assets_dir + '/assets/lib' sprockets.append_path "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets" if ENV['RACK_ENV'] != 'development' require 'yui/compressor' require 'uglifier' sprockets.css_compressor = YUI::CssCompressor.new sprockets.js_compressor = Uglifier.new end return sprockets end end