class SC::Helpers::HTML5Manifest

Creates an HTML5 manifest file for application caching

Public Instance Methods

build(dst_path) click to toggle source
# File lib/sproutcore/helpers/html5_manifest.rb, line 14
def build(dst_path)
  @files = []

  @files << "CACHE MANIFEST\n# List of all resources required by this project\n"

  path = dst_path.split('/tmp/build')

  inspect_files(path[0] + '/tmp/build', path[1])

  networks = $to_html5_manifest_networks
  if networks
    @files << "\n\nNETWORK:"
    networks.each do |network|
      @files << network
    end
    @files << "\n"
  end

  manifest_path = dst_path.sub('index.html', '') + 'manifest.appcache'
  writelines manifest_path, @files
end
inspect_files(base_path, dst_path) click to toggle source
# File lib/sproutcore/helpers/html5_manifest.rb, line 58
def inspect_files(base_path, dst_path)
  path = base_path + dst_path
  content = readlines(path)

  content.each do |line|
    line.scan(/['"]([^\s]+?(?=\.(css|js|png|gif|jpg|jpeg))\.\2)['"]/i) do |x, y, z|
      file_location = x
      # in case of hyperdomaining, strip off the http part and then look
      # for the file
      if x[0,4] == 'http'
        file_location = '/' + x.gsub(/https?\:\/\/.*?\//, '')
      end

      next unless File.exist?(base_path + file_location)

      if !@files.include?(x)
        @files << x
      end

      if y == 'css' || y == 'js'
        inspect_files(base_path, file_location)
      end

    end
  end

end
joinlines(lines) click to toggle source
# File lib/sproutcore/helpers/html5_manifest.rb, line 44
def joinlines(lines)
  lines.join("\n")
end
readlines(src_path) click to toggle source

Reads the lines from the source file. If the source file does not exist, returns empty array.

# File lib/sproutcore/helpers/html5_manifest.rb, line 50
def readlines(src_path)
  if File.exist?(src_path) && !File.directory?(src_path)
    File.readlines(src_path)
  else
    []
  end
end
writelines(dst_path, lines) click to toggle source

writes the passed lines to the named file

# File lib/sproutcore/helpers/html5_manifest.rb, line 37
def writelines(dst_path, lines)
  FileUtils.mkdir_p(File.dirname(dst_path))
  f = File.open(dst_path, 'w')
  f.write joinlines(lines)
  f.close
end