Class: Rake::SprocketsTask

Inherits:
TaskLib
  • Object
show all
Defined in:
lib/rake/sprocketstask.rb

Overview

Simple Sprockets compilation Rake task macro.

Rake::SprocketsTask.new do |t|
  t.environment = Sprockets::Environment.new
  t.output      = "./public/assets"
  t.assets      = %w( application.js application.css )
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :assets) {|_self| ... } ⇒ SprocketsTask

Returns a new instance of SprocketsTask.

Yields:

  • (_self)

Yield Parameters:



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rake/sprocketstask.rb', line 102

def initialize(name = :assets)
  @name         = name
  @environment  = lambda { Sprockets::Environment.new(Dir.pwd) }
  @manifest     = lambda { Sprockets::Manifest.new(cached, output) }
  @logger       = Logger.new($stderr)
  @logger.level = Logger::INFO
  @keep         = 2
  @age          = 3600

  yield self if block_given?

  define
end

Instance Attribute Details

#ageObject

Assets created within this age will be kept. See Sprockets::Manifest#clean for more information.



76
77
78
# File 'lib/rake/sprocketstask.rb', line 76

def age
  @age
end

#assetsObject

Array of asset logical paths to compile.

t.assets = %w( application.js jquery.js application.css )


70
71
72
# File 'lib/rake/sprocketstask.rb', line 70

def assets
  @assets
end

#environmentObject

‘Environment` instance used for finding assets.

You’ll most likely want to reassign ‘environment` to your own.

Rake::SprocketsTask.new do |t|
  t.environment = Foo::Assets
end


32
33
34
35
36
37
38
# File 'lib/rake/sprocketstask.rb', line 32

def environment
  if !@environment.is_a?(Sprockets::Base) && @environment.respond_to?(:call)
    @environment = @environment.call
  else
    @environment
  end
end

#keepObject

Minimum number of old assets to keep. See Sprockets::Manifest#clean for more information.



73
74
75
# File 'lib/rake/sprocketstask.rb', line 73

def keep
  @keep
end

#loggerObject

Logger to use during rake tasks. Defaults to using stderr.

t.logger = Logger.new($stdout)


82
83
84
# File 'lib/rake/sprocketstask.rb', line 82

def logger
  @logger
end

#manifestObject

‘Manifest` instance used for already compiled assets.

Will be created by default if an environment and output directory are given



51
52
53
54
55
56
57
# File 'lib/rake/sprocketstask.rb', line 51

def manifest
  if !@manifest.is_a?(Sprockets::Manifest) && @manifest.respond_to?(:call)
    @manifest = @manifest.call
  else
    @manifest
  end
end

#nameObject

Name of the task. Defaults to “assets”.

The name will also be used to suffix the clean and clobber tasks, “clean_assets” and “clobber_assets”.



22
23
24
# File 'lib/rake/sprocketstask.rb', line 22

def name
  @name
end

#outputObject

Directory to write compiled assets too. As well as the manifest file.

t.output = "./public/assets"


64
65
66
# File 'lib/rake/sprocketstask.rb', line 64

def output
  @output
end

Instance Method Details

#cachedObject Also known as: index

Returns cached cached environment



42
43
44
# File 'lib/rake/sprocketstask.rb', line 42

def cached
  @cached ||= environment.cached if environment
end

#defineObject

Define tasks



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rake/sprocketstask.rb', line 117

def define
  desc name == :assets ? "Compile assets" : "Compile #{name} assets"
  task name do
    with_logger do
      manifest.compile(assets)
    end
  end

  desc name == :assets ? "Remove all assets" : "Remove all #{name} assets"
  task "clobber_#{name}" do
    with_logger do
      manifest.clobber
    end
  end

  task clobber: ["clobber_#{name}"]

  desc name == :assets ? "Clean old assets" : "Clean old #{name} assets"
  task "clean_#{name}" do
    with_logger do
      manifest.clean(keep, age)
    end
  end

  task clean: ["clean_#{name}"]
end

#log_levelObject

Returns logger level Integer.



85
86
87
# File 'lib/rake/sprocketstask.rb', line 85

def log_level
  @logger.level
end

#log_level=(level) ⇒ Object

Set logger level with constant or symbol.

t.log_level = Logger::INFO
t.log_level = :debug


94
95
96
97
98
99
100
# File 'lib/rake/sprocketstask.rb', line 94

def log_level=(level)
  if level.is_a?(Integer)
    @logger.level = level
  else
    @logger.level = Logger.const_get(level.to_s.upcase)
  end
end