class Yarrow::Assets::Pipeline

Processes static assets using Sprockets.

Attributes

append_paths[R]
assets[R]
bundles[R]
input_dir[R]
output_dir[R]

Public Class Methods

new(config) click to toggle source

@param config [Yarrow::Configuration]

# File lib/yarrow/assets/pipeline.rb, line 17
def initialize(config)
  raise Yarrow::ConfigurationError if config.assets.nil?

  @input_dir = config.assets.input_dir || default_input_dir

  if config.assets.output_dir
    @output_dir = config.assets.output_dir
  else
    @output_dir = config.output_dir || default_output_dir
  end

  @append_paths = []

  case config.assets.append_paths
  when Array
    @append_paths = config.assets.append_paths
  when '*'
    @append_paths = Dir[@input_dir + '/*'].select do |path|
      File.directory?(path)
    end.map do |path|
      File.basename(path)
    end
  when String
    @append_paths << config.assets.append_paths
  end
end

Public Instance Methods

compile(bundles = []) click to toggle source

Compiles an asset manifest and processed output files from the given input bundles. Also generates a manifest linking each output bundle to its given input name.

@param bundles [Array<String>]

# File lib/yarrow/assets/pipeline.rb, line 49
def compile(bundles = [])
  bundles.each do |bundle|          
    if bundle.include? '*'
      Dir["#{@input_dir}/#{bundle}"].each do |asset|
        logger.info "Compiling: #{asset}"
        manifest.compile(File.basename(asset))
      end
    else
      logger.info "Compiling: #{bundle}"
      manifest.compile(bundle)
    end
  end
end
copy(bundles = []) click to toggle source

Copy the given files to the output path without processing or renaming.

@param bundle [Array<String>]

# File lib/yarrow/assets/pipeline.rb, line 67
def copy(bundles = [])
  bundles.each do |bundle|
    FileUtils.cp_r "#{@input_dir}/#{bundle}", "#{@output_dir}/#{bundle}"
  end
end
environment() click to toggle source

Access instance of the Sprockets environment.

@return [Sprockets::Environment]

# File lib/yarrow/assets/pipeline.rb, line 90
def environment
  # TODO: decouple dependency on Sprockets
  @environment ||= create_environment
end
purge(keep = 2, age = 3600) click to toggle source

Purges redundant compiled assets from the output path.

@example Purge all assets except those created in the last 10 minutes

pipeline.purge(0, )

@param keep [Integer] Number of previous revisions to keep. Defaults to 2. @param age [Integer] Purge all assets older than this date. Defaults to 1 hour.

# File lib/yarrow/assets/pipeline.rb, line 81
def purge(keep = 2, age = 3600)
  # TODO: upgrade to Sprockets 3.0 to support the age arg
  manifest.clean(keep)
end

Private Instance Methods

create_environment() click to toggle source
# File lib/yarrow/assets/pipeline.rb, line 113
def create_environment
  environment = Sprockets::Environment.new(@input_dir)
  
  @append_paths.each do |path|
    environment.append_path path
  end

  environment
end
default_input_dir() click to toggle source
# File lib/yarrow/assets/pipeline.rb, line 97
def default_input_dir
  "#{Dir.pwd}/assets"
end
default_output_dir() click to toggle source
# File lib/yarrow/assets/pipeline.rb, line 101
def default_output_dir
  "#{Dir.pwd}/public/assets"
end
manifest() click to toggle source
# File lib/yarrow/assets/pipeline.rb, line 109
def manifest
  Sprockets::Manifest.new(environment, manifest_file_path)
end
manifest_file_path() click to toggle source
# File lib/yarrow/assets/pipeline.rb, line 105
def manifest_file_path
  "#{@output_dir}/manifest.json"
end