Class: Yescode::AssetCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/yescode/asset_compiler.rb

Class Method Summary collapse

Class Method Details

.compile(assets, ext) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yescode/asset_compiler.rb', line 6

def compile(assets, ext)
  # read contents of each file into array of strings
  combined = StringIO.open do |s|
    assets[ext].each do |filename|
      source = File.join(".", "app", ext, filename)
      s.puts File.read(source)
    end

    s.string
  end

  # hash the contents of each concatenated asset
  hash = Digest::SHA1.hexdigest combined

  # use hash to bust the cache
  name = "#{hash}.#{ext}"
  filename = File.join(".", "public", ext, name)
  FileUtils.mkdir_p(File.join(".", "public", ext))

  # output asset path for browser
  # returns string of written filename
  File.write(filename, combined)

  name
end