Class: Yarrow::Assets::Pipeline

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/yarrow/assets/pipeline.rb

Overview

Processes static assets using Sprockets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(config) ⇒ Pipeline

Returns a new instance of Pipeline.

Parameters:

Raises:



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
# 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

Instance Attribute Details

#append_pathsObject (readonly)

Returns the value of attribute append_paths.



13
14
15
# File 'lib/yarrow/assets/pipeline.rb', line 13

def append_paths
  @append_paths
end

#assetsObject (readonly)

Returns the value of attribute assets.



13
14
15
# File 'lib/yarrow/assets/pipeline.rb', line 13

def assets
  @assets
end

#bundlesObject (readonly)

Returns the value of attribute bundles.



13
14
15
# File 'lib/yarrow/assets/pipeline.rb', line 13

def bundles
  @bundles
end

#input_dirObject (readonly)

Returns the value of attribute input_dir.



13
14
15
# File 'lib/yarrow/assets/pipeline.rb', line 13

def input_dir
  @input_dir
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



13
14
15
# File 'lib/yarrow/assets/pipeline.rb', line 13

def output_dir
  @output_dir
end

Instance Method Details

#compile(bundles = []) ⇒ Object

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.

Parameters:

  • bundles (Array<String>) (defaults to: [])


49
50
51
52
53
54
55
56
57
58
59
60
61
# 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 = []) ⇒ Object

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

Parameters:

  • bundle (Array<String>)


67
68
69
70
71
# 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

#environmentSprockets::Environment

Access instance of the Sprockets environment.

Returns:

  • (Sprockets::Environment)


90
91
92
93
# File 'lib/yarrow/assets/pipeline.rb', line 90

def environment
  # TODO: decouple dependency on Sprockets
  @environment ||= create_environment
end

#purge(keep = 2, age = 3600) ⇒ Object

Purges redundant compiled assets from the output path.

Examples:

Purge all assets except those created in the last 10 minutes

pipeline.purge(0, )

Parameters:

  • keep (Integer) (defaults to: 2)

    Number of previous revisions to keep. Defaults to 2.

  • age (Integer) (defaults to: 3600)

    Purge all assets older than this date. Defaults to 1 hour.



81
82
83
84
# 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