Class: JAPR::Pipeline

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

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/japr/pipeline.rb', line 81

def initialize(manifest, prefix, source, destination, type, options = {})
  @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.



92
93
94
# File 'lib/japr/pipeline.rb', line 92

def assets
  @assets
end

#htmlObject (readonly)

Returns the value of attribute html.



92
93
94
# File 'lib/japr/pipeline.rb', line 92

def html
  @html
end

Class Method Details

.cacheObject

Cache processed pipelines



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

def cache
  @cache ||= {}
end

.clear_cacheObject

Empty cache



58
59
60
# File 'lib/japr/pipeline.rb', line 58

def clear_cache
  @cache = {}
end

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

Generate hash based on manifest



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/japr/pipeline.rb', line 5

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

.puts(message) ⇒ Object

Add prefix to output



75
76
77
# File 'lib/japr/pipeline.rb', line 75

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

.remove_staged_assets(source, config) ⇒ Object

Remove staged assets



63
64
65
66
67
68
69
70
71
72
# File 'lib/japr/pipeline.rb', line 63

def remove_staged_assets(source, config)
  config = DEFAULTS.merge(config)
  staging_path = File.join(source, config['staging_path'])
  FileUtils.rm_rf(staging_path)
  rescue Exception => e
    puts "Failed to remove staged assets: #{e.message}"

    # Re-raise the exception
    raise e
end

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

Run pipeline



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
43
44
45
46
47
48
49
50
# File 'lib/japr/pipeline.rb', line 18

def run(manifest, prefix, source, destination, tag, type, config)
  # Get hash for pipeline
  hash = hash(source, manifest, config)

  # Check if pipeline has been cached
  if cache.key?(hash)
    # Return cached pipeline and cached status
    return cache[hash], true
  else
    begin
      puts "Processing '#{tag}' manifest '#{prefix}'"

      # Create and process new pipeline
      pipeline = new(manifest, prefix, source, destination, type, config)
      pipeline.assets.each do |asset|
        puts "Saved '#{asset.filename}' to " \
             "'#{destination}/#{asset.output_path}'"
      end

      # Add processed pipeline to cache
      cache[hash] = pipeline

      # Return newly processed pipeline and cached status
      return pipeline, false
    rescue Exception => e
      # Add exception to cache
      cache[hash] = e

      # Re-raise the exception
      raise e
    end
  end
end