Class: BPM::GeneratedAsset

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

Direct Known Subclasses

PluginAsset

Constant Summary collapse

FORMAT_METHODS =
{
  'text/css' => ['css'],
  'application/javascript' => ['lib']
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generating_assetObject



12
13
14
# File 'lib/bpm/pipeline/generated_asset.rb', line 12

def self.generating_asset
  @generating_asset && @generating_asset.last
end

.pop_generating_assetObject



21
22
23
# File 'lib/bpm/pipeline/generated_asset.rb', line 21

def self.pop_generating_asset
  @generating_asset && @generating_asset.pop
end

.push_generating_asset(asset) ⇒ Object



16
17
18
19
# File 'lib/bpm/pipeline/generated_asset.rb', line 16

def self.push_generating_asset(asset)
  @generating_asset ||= []
  @generating_asset.push asset
end

Instance Method Details

#build_settingsObject



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

def build_settings
  ret = environment.project.build_settings[asset_name]
  (ret && ret['bpm:settings']) || {}
end

#minify_as_jsObject



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
# File 'lib/bpm/pipeline/generated_asset.rb', line 30

def minify_as_js
  project = environment.project
  minifier_name = project.minifier_name asset_name
  minifier_name = minifier_name.keys.first if minifier_name

  if minifier_name && content_type == 'application/javascript'
    pkg = project.package_from_name minifier_name
    if pkg.nil?
      raise MinifierNotFoundError.new(minifier_name)
    end
    minifier_plugin_name = pkg.provided_minifier
    if minifier_plugin_name.nil?
      raise MinifierNotFoundError.new(minifier_name)
    end
    plugin_ctx = environment.plugin_js_for minifier_plugin_name

    plugin_ctx += <<-end_eval
      ; // Safety
      CTX.minify = function(body) { return BPM_PLUGIN.minify(body); };
    end_eval

  end
  #Return a default (noop) minifier if none if defined      
  plugin_ctx = "CTX.minify=function(body) { return body; }" unless plugin_ctx;
  plugin_ctx
end

#minify_body(data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bpm/pipeline/generated_asset.rb', line 57

def minify_body(data)
  project = environment.project
  minifier_name = project.minifier_name asset_name
  minifier_name = minifier_name.keys.first if minifier_name

  if minifier_name && content_type == 'application/javascript'
    pkg = project.package_from_name minifier_name
    if pkg.nil?
      raise MinifierNotFoundError.new(minifier_name)
    end

    minifier_plugin_name = pkg.provided_minifier
    if minifier_plugin_name.nil?
      raise MinifierNotFoundError.new(minifier_name)
    end

    plugin_ctx = environment.plugin_js_for minifier_plugin_name

    # slice out the header at the top - we don't want the minifier to
    # touch it.
    header   = data.match /^(\/\* ====.+====\*\/)$/m
    if header
      header = header[0] + "\n"
      data   = data[header.size..-1]
    end

    plugin_ctx += <<-end_eval
      ; // Safety
      CTX = #{BPM::PluginContext.new(pkg).to_json};
      CTX.minify = function(body){ return body; };
      DATA = #{data.to_json};
    end_eval

    ctx = BPM.compile_js(plugin_ctx);
    data = ctx.eval("BPM_PLUGIN.minify(DATA, CTX)")

    data = header+data if header
  end
  data
end