class Blix::AssetManager

Constants

AssetInfo

Attributes

asset_root[W]
config_dir[W]
path_root_length[R]

Public Class Methods

asset_path(path, mode_production = false) click to toggle source

caculate the correct asset path depending on the mode of execution

# File lib/blix/assets/asset_manager.rb, line 184
def asset_path(path, mode_production = false)
  partial_path = full_path(path)
  if mode_production
    asset_name = File.basename(partial_path)
    dir_name = File.dirname(partial_path)
    if dir_name == '.'
      AssetManager.get_asset_name(asset_name)
    else
      File.join(dir_name, AssetManager.get_asset_name(asset_name))
    end
  else
    partial_path
  end
end
asset_root() click to toggle source
# File lib/blix/assets/asset_manager.rb, line 61
def asset_root
  @asset_root || 'assets'
end
asset_tag(partial_path, mode_production = false, opts = {}) click to toggle source

write a complete asset tag html string

# File lib/blix/assets/asset_manager.rb, line 211
def asset_tag(partial_path, mode_production = false, opts = {})
  name = File.basename(partial_path)
  type = File.extname(partial_path)
  inline = opts.delete(:inline)
  opts_str = opts.to_a.map { |v| "#{v[0]}=\"#{v[1]}\"" }.join(' ')

  if type == '.css'
    if mode_production && inline
      content = cache['///' + name] ||= File.read(file_path(partial_path))
      %(<style type="text/css" #{opts_str} >\n#{content}</style>)
    else
      %(<link rel="stylesheet" href="#{asset_path(partial_path, mode_production)}" type="text/css" #{opts_str} >)
    end
  elsif type == '.js'
    if mode_production && inline
      # insert js here if in production mode
      content = cache['///' + name] ||= File.read(file_path(partial_path))
      %(<script>\n#{content}</script>)
    else
      %(<script src="#{asset_path(partial_path, mode_production)}" type="text/javascript"></script>)
    end
  else
    raise "unknown tag for file extension:#{partial_path}"
  end
end
cache() click to toggle source

cache asset name store

# File lib/blix/assets/asset_manager.rb, line 135
def cache
  @cache ||= {}
end
config_dir() click to toggle source
# File lib/blix/assets/asset_manager.rb, line 50
def config_dir
  @config_dir || 'config/assets'
end
config_path(name) click to toggle source

write the config info here

# File lib/blix/assets/asset_manager.rb, line 73
def config_path(name)
  File.join(config_dir, filename(name) + '.conf')
end
dest_path(name) click to toggle source

write the compiled asset here

# File lib/blix/assets/asset_manager.rb, line 68
def dest_path(name)
  File.join(asset_dir, filename(name))
end
file_path(path) click to toggle source

return the location of the compiled asset

# File lib/blix/assets/asset_manager.rb, line 200
def file_path(path)
  name = File.basename(path)
  config = get_config(name)
  raise "ERROR : config file for asset:#{name} not found !!" unless config

  ext = File.extname(name)[1..-1]
  base = name.split('.')[0]
  config[:destination] + '/' + base + '-' + (config[:version] || config[:stamp]) + '.' + ext
end
filename(name) click to toggle source

the name of the file to write to

# File lib/blix/assets/asset_manager.rb, line 57
def filename(name)
  name
end
full_path(path) click to toggle source

calculate the full path of the asset

# File lib/blix/assets/asset_manager.rb, line 178
def full_path(path)
  path = path[1..-1] if path[0, 1] == '/'
  path_root + path
end
get_asset_name(name) click to toggle source

retrieve and cache the full asset name

# File lib/blix/assets/asset_manager.rb, line 140
def get_asset_name(name)
  cache[name] ||= begin
    config = get_config(name)
    raise "ERROR : config file for asset:#{name} not found !!" unless config

    ext = File.extname(name)[1..-1]
    base = name.split('.')[0]
    base + '-' + (config[:version] || config[:stamp]) + '.' + ext
  end
end
get_asset_version_name(name) click to toggle source

retrieve and cache the full asset name

# File lib/blix/assets/asset_manager.rb, line 152
def get_asset_version_name(name)
  cache[name] ||= begin
    config = get_config(name)
    raise "ERROR : config file for asset:#{name} not found !!" unless config

    ext = File.extname(name)[1..-1]
    base = name.split('.')[0]
    base + '-' + config[:version] + '.' + ext
  end
end
get_config(name) click to toggle source

get config data from file or nil if file does not exist

# File lib/blix/assets/asset_manager.rb, line 118
def get_config(name)
  return nil unless File.exist? config_path(name)

  data = File.read config_path(name)
  parts = data.split('|')
  out = { :hash => parts[1], :stamp => parts[0], :destination => parts[3] }
  out[:version] = parts[2] unless parts[2].empty?
  out
end
get_filestamp() click to toggle source

generate a unique suffix for the file

# File lib/blix/assets/asset_manager.rb, line 109
def get_filestamp
  now = Time.now
  str = '%X' % now.to_i
  str += '%X' % now.usec
  str += '%X' % rand(9999)
  str
end
if_modified(destination, name, data, opts = {}) { |info| ... } click to toggle source

yield the old and new name to a block if the asset has been modified

# File lib/blix/assets/asset_manager.rb, line 79
def if_modified(destination, name, data, opts = {})
  new_hash = Digest::MD5.hexdigest data

  ext = File.extname(name)[1..-1]
  base = name.split('.')[0]
  confname = base + '.' + ext
  config = get_config(confname)

  if !config ||
     (config[:hash] != new_hash || (config[:destination] != destination)) ||
     opts[:version] && (opts[:version] != config[:version])

    stamp = get_filestamp
    info = AssetInfo.new
    info.newname = "#{base}-#{opts[:version] || stamp}.#{ext}"
    info.oldname = config && "#{base}-#{config[:version] || config[:stamp]}.#{ext}"
    set_config(destination, confname, new_hash, stamp, opts[:version])
    yield(info)

  elsif opts[:rewrite] # rewrite the same data to the same asset as before

    info = AssetInfo.new
    info.newname = config && "#{base}-#{config[:version] || config[:stamp]}.#{ext}"
    info.oldname = nil
    yield(info)

  end
end
path_root() click to toggle source
# File lib/blix/assets/asset_manager.rb, line 171
def path_root
  @path_root || '/'
end
set_config(destination, name, hash, stamp, version = nil) click to toggle source

set the config data

# File lib/blix/assets/asset_manager.rb, line 129
def set_config(destination, name, hash, stamp, version = nil)
  File.write(config_path(name), stamp + '|' + hash + '|' + version.to_s + '|' + destination)
  true
end
set_path_root(root) click to toggle source
# File lib/blix/assets/asset_manager.rb, line 163
def set_path_root(root)
  root = root.to_s
  root = '/' + root if root[0, 1] != '/'
  root += '/' if root[-1, 1] != '/'
  @path_root = root
  @path_root_length = @path_root.length - 1
end
write_asset(destination, name, str, opts = {}) click to toggle source

write an asset to the destination directory

# File lib/blix/assets/asset_manager.rb, line 238
def write_asset(destination, name, str, opts = {})
  opts = opts.dup
  opts[:rewrite] = true unless opts.key?(:rewrite)
  raise "asset destination directory must exist:#{destination}" unless Dir.exist?(destination)

  puts "#{red('WARNING !')} Use a relative path to the destination directory" if destination[0] == '/' # with colour red
  if_modified(destination, name, str, opts) do |a|
    outfile = File.join(destination, a.newname)
    File.write outfile, str
    if a.oldname && (a.oldname != a.newname)
      oldfilename = File.join(destination, a.oldname)
      File.unlink oldfilename if File.exist?(oldfilename)
    end
    outfile
  end
end

Private Class Methods

red(text) click to toggle source

escape text for printing in red on console.

# File lib/blix/assets/asset_manager.rb, line 258
def red(text)
  "\e[31m#{text}\e[0m"
end