Class: JAPR::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/japr/pipeline.rb

Overview

The pipeline itself, the run method is where it all happens rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest, prefix, source, destination, type, options = {}) ⇒ Pipeline

Initialize new pipeline rubocop:disable Metrics/ParameterLists



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/japr/pipeline.rb', line 88

def initialize(manifest, prefix, source, destination, type, options = {})
  # rubocop:enable Metrics/ParameterLists
  @manifest = manifest
  @prefix = prefix
  @source = source
  @destination = destination
  @type = type
  @options = JAPR::DEFAULTS.merge(options)

  process
end

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



100
101
102
# File 'lib/japr/pipeline.rb', line 100

def assets
  @assets
end

#destinationObject (readonly)

Returns the value of attribute destination.



100
101
102
# File 'lib/japr/pipeline.rb', line 100

def destination
  @destination
end

#htmlObject (readonly)

Returns the value of attribute html.



100
101
102
# File 'lib/japr/pipeline.rb', line 100

def html
  @html
end

Class Method Details

.cacheObject

Cache processed pipelines



49
50
51
# File 'lib/japr/pipeline.rb', line 49

def cache
  @cache ||= {}
end

.clear_cacheObject

Empty cache



54
55
56
# File 'lib/japr/pipeline.rb', line 54

def clear_cache
  @cache = {}
end

.hash(source, manifest, options = {}) ⇒ Object

Generate hash based on manifest



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/japr/pipeline.rb', line 10

def hash(source, manifest, options = {})
  options = DEFAULTS.merge(options)
  begin
    Digest::MD5.hexdigest(YAML.safe_load(manifest).map! do |path|
      "#{path}#{File.mtime(File.join(source, path)).to_i}"
    end.join.concat(options.to_s))
  rescue StandardError => e
    puts "Failed to generate hash from provided manifest: #{e.message}"
    raise e
  end
end

.puts(message) ⇒ Object

Add prefix to output



66
67
68
# File 'lib/japr/pipeline.rb', line 66

def puts(message)
  $stdout.puts("Asset Pipeline: #{message}")
end

.remove_staged_assets(source, config) ⇒ Object

Remove staged assets



59
60
61
62
63
# File 'lib/japr/pipeline.rb', line 59

def remove_staged_assets(source, config)
  config = DEFAULTS.merge(config)
  staging_path = File.join(source, config['staging_path'])
  FileUtils.rm_rf(staging_path)
end

.run(manifest, prefix, source, destination, tag, type, config) ⇒ Object

Run the pipeline This is called from JAPR::LiquidBlockExtensions.render or, to be more precise, from JAPR::CssAssetTag.render and JAPR::JavaScriptAssetTag.render rubocop:disable Metrics/ParameterLists



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/japr/pipeline.rb', line 27

def run(manifest, prefix, source, destination, tag, type, config)
  # rubocop:enable Metrics/ParameterLists
  # Get hash for pipeline
  hash = hash(source, manifest, config)

  # Check if pipeline has been cached
  return cache[hash], true if cache.key?(hash)

  begin
    puts "Processing '#{tag}' manifest '#{prefix}'"
    pipeline = new(manifest, prefix, source, destination, type, config)
    process_pipeline(hash, pipeline)
  rescue StandardError => e
    # Add exception to cache
    cache[hash] = e

    # Re-raise the exception
    raise e
  end
end