Class: SC::Helpers::HTML5Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/sproutcore/helpers/html5_manifest.rb

Overview

Creates an HTML5 manifest file for application caching

Instance Method Summary collapse

Instance Method Details

#build(dst_path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



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
# 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) ⇒ Object



44
45
46
# File 'lib/sproutcore/helpers/html5_manifest.rb', line 44

def joinlines(lines)
  lines.join("\n")
end

#readlines(src_path) ⇒ Object

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



50
51
52
53
54
55
56
# 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) ⇒ Object

writes the passed lines to the named file



37
38
39
40
41
42
# 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