123456789_123456789_123456789_123456789_123456789_

Class: Rake::SprocketsTask

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Rake::TaskLib
Instance Chain:
self, Rake::TaskLib
Inherits: Rake::TaskLib
  • Object
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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

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

Yields:

  • (_self)

Yield Parameters:

  • _self (SprocketsTask)

    the object that the method was called on

[ GitHub ]

  
# 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

#age (rw)

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

[ GitHub ]

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

attr_accessor :age

#assets (rw)

Array of asset logical paths to compile.

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

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

attr_accessor :assets

#environment (rw)

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
[ GitHub ]

  
# 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

#environment=(value) (rw)

[ GitHub ]

  
# File 'lib/rake/sprocketstask.rb', line 39

attr_writer :environment

#keep (rw)

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

[ GitHub ]

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

attr_accessor :keep

#log_level (rw)

Returns logger level Integer.

[ GitHub ]

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

def log_level
  @logger.level
end

#log_level=(level) (rw)

Set logger level with constant or symbol.

t.log_level = Logger::INFO
t.log_level = :debug
[ GitHub ]

  
# 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

#logger (rw)

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

t.logger = Logger.new($stdout)
[ GitHub ]

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

attr_accessor :logger

#manifest (rw)

Manifest instance used for already compiled assets.

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

[ GitHub ]

  
# 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

#manifest=(value) (rw)

[ GitHub ]

  
# File 'lib/rake/sprocketstask.rb', line 58

attr_writer :manifest

#name (rw)

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”.

[ GitHub ]

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

attr_accessor :name

#output (rw)

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

t.output = "./public/assets"
[ GitHub ]

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

attr_accessor :output

Instance Method Details

#cached Also known as: #index

Returns cached cached environment

[ GitHub ]

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

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

#define

Define tasks

[ GitHub ]

  
# 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

#index

Alias for #cached.

[ GitHub ]

  
# File 'lib/rake/sprocketstask.rb', line 45

alias_method :index, :cached

#with_logger (private)

Sub out environment logger with our rake task logger that writes to stderr.

[ GitHub ]

  
# File 'lib/rake/sprocketstask.rb', line 147

def with_logger
  if env = manifest.environment
    old_logger = env.logger
    env.logger = @logger
  end
  yield
ensure
  env.logger = old_logger if env
end