Class: BPM::Pipeline

Inherits:
Sprockets::Environment
  • Object
show all
Defined in:
lib/bpm/pipeline.rb

Overview

A BPM package-aware asset pipeline. Asset lookup respects package.json directory configurations as well as loading preprocessors, formats, and postprocessors from the package config.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, mode = :debug, include_preview = false) ⇒ Pipeline

Pass in the project you want the pipeline to manage.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bpm/pipeline.rb', line 29

def initialize(project, mode = :debug, include_preview = false)

  @project = project
  @mode    = mode
  @plugin_js = {}

  # Create a pipeline for each package.  Will be used for searching.
  @package_pipelines = project.local_deps.map do |pkg|
    BPM::PackagePipeline.new self, pkg
  end
  @package_pipelines << BPM::PackagePipeline.new(self, project)

  project_path = project.root_path

  super project_path

  # Unregister built-in processors.  We want most things served by the
  # pipeline directly to just pass through.  (package pipelines do the
  # processing)
  %w(text/css application/javascript).each do |kind|
    unregister_processor kind, Sprockets::DirectiveProcessor
  end
  unregister_postprocessor 'application/javascript', Sprockets::SafetyColons

  # configure search paths
  append_path project.assets_root
  append_path project.preview_root if include_preview
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



25
26
27
# File 'lib/bpm/pipeline.rb', line 25

def mode
  @mode
end

#package_pipelinesObject (readonly)

Returns the value of attribute package_pipelines.



26
27
28
# File 'lib/bpm/pipeline.rb', line 26

def package_pipelines
  @package_pipelines
end

#projectObject (readonly)

Returns the value of attribute project.



24
25
26
# File 'lib/bpm/pipeline.rb', line 24

def project
  @project
end

Instance Method Details

#attributes_for(path) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/bpm/pipeline.rb', line 66

def attributes_for(path)
  if path.to_s[File.join(project.root_path, BPM_DIR)] ||  !Pathname.new(path).absolute?
    return super(path)
  end

  pipeline = pipeline_for path
  pipeline ? pipeline.attributes_for(path) : super(path)
end

#build_asset(logical_path, pathname, options) ⇒ Object

Detect whenever we are asked to build some of the magic files and swap in a custom asset type that can generate the contents.



97
98
99
100
101
102
103
104
105
# File 'lib/bpm/pipeline.rb', line 97

def build_asset(logical_path, pathname, options)
  if magic_paths.include? pathname.to_s
    BPM::GeneratedAsset.new(self, logical_path, pathname, options)
  elsif pipeline = pipeline_for(pathname)
    pipeline.build_asset logical_path, pathname, options
  else
    super logical_path, pathname, options
  end
end

#buildable_assetsObject

Returns an array of all the buildable assets in the current directory. These are the assets that will be built when you compile the project.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bpm/pipeline.rb', line 114

def buildable_assets
  # make sure the logical_path can be used to simply build into the
  # assets directory when we are done
  ret = project.buildable_asset_filenames mode

  # Add in the static assets that we just need to copy
  project.build_settings(mode).each do |target_name, opts|
    next unless opts.is_a? Array
    opts.each do |dir_name|

      dep = project.local_deps.find { |dep| dep.name == target_name }
      dep = project if project.name == target_name

      dir_paths = File.join(dep.root_path, dir_name)
      if File.directory? dir_paths
        dir_paths = Dir[File.join(dir_paths, '**', '*')]
      else
        dir_paths = [dir_paths]
      end

      dir_paths.each do |dir_path|
        if File.exist?(dir_path) && !File.directory?(dir_path)
          ret << File.join(target_name, dir_path[dep.root_path.size+1..-1])
        end
      end
    end
  end

  ret.sort.map { |x| find_asset x }.compact
end

#find_assetObject

MEGAHAX!!!!



150
151
152
153
154
155
# File 'lib/bpm/pipeline.rb', line 150

def find_asset(*)
  asset = super
  # The way BPM is set up we need to call build_source
  asset.send(:build_source) if asset.respond_to?(:build_source, true)
  asset
end

#indexObject

Index is for caching, but it causes us problem, we don’t need the caching



159
160
161
# File 'lib/bpm/pipeline.rb', line 159

def index
  self
end

#magic_pathsObject

Paths to files that should be built.



108
109
110
# File 'lib/bpm/pipeline.rb', line 108

def magic_paths
  @magic_paths ||= build_magic_paths
end

#pipeline_for(path) ⇒ Object

determines the proper pipeline for the path



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

def pipeline_for(path)
  return nil if magic_paths.include?(path)
  package_pipelines.find do |cur_pipeline|
    path.to_s[cur_pipeline.package.root_path.to_s]
  end
end

#plugin_js_for(logical_path) ⇒ Object



145
146
147
# File 'lib/bpm/pipeline.rb', line 145

def plugin_js_for(logical_path)
  @plugin_js[logical_path] ||= build_plugin_js(logical_path)
end

#resolve(logical_path, options = {}, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bpm/pipeline.rb', line 75

def resolve(logical_path, options={}, &block)

  magic_path = magic_paths.find do |path|
    path =~ /#{Regexp.escape logical_path.to_s}(\..+)?$/
  end

  package_name = logical_path.to_s.sub(/#{Regexp.escape File::SEPARATOR}.+/,'')
  pipeline = package_pipelines.find do |cur_pipeline|
    cur_pipeline.package_name == package_name
  end

  if pipeline && magic_path.nil?
    logical_path = logical_path.to_s[package_name.size+1..-1]
    pipeline.resolve Pathname.new(logical_path), options, &block
  else
    super logical_path, options, &block
  end

end