class Rake::SprocketsTask - sprockets-2.11.3 Documentation

# class Rake::SprocketsTask

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

### Attributes

assets[RW]

Array of asset logical paths to compile.

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

environment[W]
keep[RW]

Number of old assets to keep.

logger[RW]

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

    t.logger = Logger.new($stdout)

manifest[W]
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”.

output[RW]

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

    t.output = "./public/assets"

### Public Class Methods

new(name = :assets) { |self| ... } click to toggle source

    # File lib/rake/sprocketstask.rb, line 97
    def initialize(name = :assets)
      @name         = name
      @environment  = lambda { Sprockets::Environment.new(Dir.pwd) }
      @manifest     = lambda { Sprockets::Manifest.new(index, output) }
      @logger       = Logger.new($stderr)
      @logger.level = Logger::INFO
      @keep         = 2

      yield self if block_given?

      define
    end

### Public Instance Methods

define() click to toggle source

Define tasks

    # File lib/rake/sprocketstask.rb, line 111
    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)
        end
      end

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

environment() click to toggle source

`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

    # File lib/rake/sprocketstask.rb, line 31
    def environment
      if !@environment.is_a?(Sprockets::Base) && @environment.respond_to?(:call)
        @environment = @environment.call
      else
        @environment
      end
    end

index() click to toggle source

Returns cached indexed environment

    # File lib/rake/sprocketstask.rb, line 41
    def index
      @index ||= environment.index if environment
    end

log_level() click to toggle source

Returns logger level Integer.

    # File lib/rake/sprocketstask.rb, line 80
    def log_level
      @logger.level
    end

log_level=(level) click to toggle source

Set logger level with constant or symbol.

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

    # File lib/rake/sprocketstask.rb, line 89
    def log_level=(level)
      if level.is_a?(Integer)
        @logger.level = level
      else
        @logger.level = Logger.const_get(level.to_s.upcase)
      end
    end

manifest() click to toggle source

`Manifest` instance used for already compiled assets.

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

    # File lib/rake/sprocketstask.rb, line 49
    def manifest
      if !@manifest.is_a?(Sprockets::Manifest) && @manifest.respond_to?(:call)
        @manifest = @manifest.call
      else
        @manifest
      end
    end

### Private Instance Methods

with_logger() { || ... } click to toggle source

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

    # File lib/rake/sprocketstask.rb, line 141
    def with_logger
      if env = manifest.environment
        old_logger = env.logger
        env.logger = @logger
      end
      yield
    ensure
      env.logger = old_logger if env
    end