Class: Guard::Sprockets

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/sprockets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sprockets

Returns a new instance of Sprockets.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

Instance Attribute Details

#asset_pathsObject (readonly)

Returns the value of attribute asset_paths.



10
11
12
# File 'lib/guard/sprockets.rb', line 10

def asset_paths
  @asset_paths
end

#destinationObject (readonly)

Returns the value of attribute destination.



10
11
12
# File 'lib/guard/sprockets.rb', line 10

def destination
  @destination
end

#root_fileObject (readonly)

Returns the value of attribute root_file.



10
11
12
# File 'lib/guard/sprockets.rb', line 10

def root_file
  @root_file
end

#sprocketsObject (readonly)

Returns the value of attribute sprockets.



10
11
12
# File 'lib/guard/sprockets.rb', line 10

def sprockets
  @sprockets
end

Instance Method Details

#run_allObject



57
58
59
# File 'lib/guard/sprockets.rb', line 57

def run_all
  run_on_changes([])
end

#run_on_changes(paths) ⇒ Object



61
62
63
64
65
66
67
68
69
# 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

#startObject



49
50
51
52
53
54
55
# 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