Class: Blix::AssetManager

Inherits:
Object
  • Object
show all
Defined in:
lib/blix/assets/asset_manager.rb

Defined Under Namespace

Classes: AssetInfo

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.asset_rootObject



61
62
63
# File 'lib/blix/assets/asset_manager.rb', line 61

def asset_root
  @asset_root || 'assets'
end

.config_dirObject



50
51
52
# File 'lib/blix/assets/asset_manager.rb', line 50

def config_dir
  @config_dir || 'config/assets'
end

.path_root_lengthObject (readonly)

Returns the value of attribute path_root_length.



175
176
177
# File 'lib/blix/assets/asset_manager.rb', line 175

def path_root_length
  @path_root_length
end

Class Method Details

.asset_path(path, mode_production = false) ⇒ Object

caculate the correct asset path depending on the mode of execution



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 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_tag(partial_path, mode_production = false, opts = {}) ⇒ Object

write a complete asset tag html string



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# 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

.cacheObject

cache asset name store



135
136
137
# File 'lib/blix/assets/asset_manager.rb', line 135

def cache
  @cache ||= {}
end

.config_path(name) ⇒ Object

write the config info here



73
74
75
# File 'lib/blix/assets/asset_manager.rb', line 73

def config_path(name)
  File.join(config_dir, filename(name) + '.conf')
end

.dest_path(name) ⇒ Object

write the compiled asset here



68
69
70
# File 'lib/blix/assets/asset_manager.rb', line 68

def dest_path(name)
  File.join(asset_dir, filename(name))
end

.file_path(path) ⇒ Object

return the location of the compiled asset



200
201
202
203
204
205
206
207
208
# 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) ⇒ Object

the name of the file to write to



57
58
59
# File 'lib/blix/assets/asset_manager.rb', line 57

def filename(name)
  name
end

.full_path(path) ⇒ Object

calculate the full path of the asset



178
179
180
181
# 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) ⇒ Object

retrieve and cache the full asset name



140
141
142
143
144
145
146
147
148
149
# 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) ⇒ Object

retrieve and cache the full asset name



152
153
154
155
156
157
158
159
160
161
# 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) ⇒ Object

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



118
119
120
121
122
123
124
125
126
# 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_filestampObject

generate a unique suffix for the file



109
110
111
112
113
114
115
# 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 = {}) ⇒ Object

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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_rootObject



171
172
173
# File 'lib/blix/assets/asset_manager.rb', line 171

def path_root
  @path_root || '/'
end

.set_config(destination, name, hash, stamp, version = nil) ⇒ Object

set the config data



129
130
131
132
# 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) ⇒ Object



163
164
165
166
167
168
169
# 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 = {}) ⇒ Object

write an asset to the destination directory



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 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