class Guard::Sprockets

Attributes

asset_paths[R]
destination[R]
root_file[R]
sprockets[R]

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/guard/sprockets.rb, line 12
def initialize(options = {})
  super

  @options     = options
  @asset_paths = Array(@options[:asset_paths] || 'app/assets/javascripts')
  @destination = @options[:destination] || 'public/javascripts'
  @root_file   = Array(@options[:root_file])
  @keep_paths  = @options[:keep_paths] || false

  @sprockets = ::Sprockets::Environment.new
  @asset_paths.each { |p| @sprockets.append_path(p) }
  @root_file.each { |f| @sprockets.append_path(Pathname.new(f).dirname) }

  if @options.delete(:minify)
    begin
      require 'uglifier'
      @sprockets.js_compressor = ::Uglifier.new
      UI.info 'Sprockets will compress JavaScript output.'
    rescue LoadError => ex
      UI.error "minify: Uglifier cannot be loaded. No JavaScript compression will be used.\nPlease include 'uglifier' in your Gemfile."
      UI.debug ex.message
    end        
  end
  
  if @options.delete(:css_minify)
    begin
      require 'yui/compressor'
      @sprockets.css_compressor = YUI::CssCompressor.new
      UI.info 'Sprockets will compress CSS output.'
    rescue LoadError => ex
      UI.error "minify: yui-compressor cannot be loaded. No CSS compression will be used.\nPlease include 'yui-compressor' in your Gemfile."
      UI.debug ex.message
    end      
  end
          
end

Public Instance Methods

run_all() click to toggle source
# File lib/guard/sprockets.rb, line 57
def run_all
  run_on_changes([])
end
run_on_changes(paths) click to toggle source
# File lib/guard/sprockets.rb, line 61
def run_on_changes(paths)
  paths = @root_file unless @root_file.empty?

  success = true
  paths.each do |file|
    success &= sprocketize(file)
  end
  success
end
start() click to toggle source
# File lib/guard/sprockets.rb, line 49
def start
   UI.info 'Guard::Sprockets is ready and waiting for some file changes...'
   UI.debug "Guard::Sprockets.asset_paths = #{@asset_paths.inspect}" unless @asset_paths.empty?
   UI.debug "Guard::Sprockets.destination = #{@destination.inspect}"

   run_all
end

Private Instance Methods

sprocketize(path) click to toggle source
# File lib/guard/sprockets.rb, line 73
def sprocketize(path)
  path = Pathname.new(path)

  output_filename = if @keep_paths
    # retain the relative directories of assets to the asset directory
    parent_paths = @asset_paths.find_all { |p| path.to_s.start_with?(p) }.collect { |p| Pathname.new(p) }
    relative_paths = parent_paths.collect { |p| path.relative_path_from(p) }
    relative_path = relative_paths.min_by { |p| p.to_s.size }

    without_preprocessor_extension(relative_path.to_s)
  else
    without_preprocessor_extension(path.basename.to_s)
  end

  output_path = Pathname.new(File.join(@destination, output_filename))

  UI.info "Sprockets will compile #{output_filename}"

  FileUtils.mkdir_p(output_path.parent) unless output_path.parent.exist?
  output_path.open('w') do |f|
    f.write @sprockets[output_filename]
  end

  UI.info "Sprockets compiled #{output_filename}"
  Notifier.notify "Sprockets compiled #{output_filename}"
rescue ExecJS::ProgramError => ex
  UI.error "Sprockets failed compiling #{output_filename}"
  UI.error ex.message
  Notifier.notify "Sprockets failed compiling #{output_filename}!", priority: 2, image: :failed

  false
end
without_preprocessor_extension(filename) click to toggle source
# File lib/guard/sprockets.rb, line 106
def without_preprocessor_extension(filename)
  filename.gsub(/^(.*\.(?:js|css))\.[^.]+(\.erb)?$/, '\1')
end