Permalink
Browse files

Trying to build an automatic cache manifest generator

  • Loading branch information...
Jonathan Penn
Jonathan Penn committed Jan 2, 2012
1 parent 25831c0 commit 76fa7abe295fe38b9c1b7b92b0808a3e086b9227
View
@@ -11,13 +11,5 @@ group :assets do
gem 'yui-compressor'
end
-gem 'jquery-rails'
gem 'haml'
-# To use debugger
-# gem 'ruby-debug19', :require => 'ruby-debug'
-
-group :test do
- # Pretty printed test output
- gem 'turn', :require => false
-end
View
@@ -33,7 +33,6 @@ GEM
activesupport (= 3.1.1)
activesupport (3.1.1)
multi_json (~> 1.0)
- ansi (1.3.0)
arel (2.2.1)
builder (3.0.0)
coffee-rails (3.1.1)
@@ -49,9 +48,6 @@ GEM
haml (3.1.3)
hike (1.2.1)
i18n (0.6.0)
- jquery-rails (1.0.16)
- railties (~> 3.0)
- thor (~> 0.14)
json (1.6.1)
mail (2.3.0)
i18n (>= 0.4.0)
@@ -104,8 +100,6 @@ GEM
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
- turn (0.8.3)
- ansi
tzinfo (0.3.30)
uglifier (1.0.4)
execjs (>= 0.3.0)
@@ -119,9 +113,7 @@ PLATFORMS
DEPENDENCIES
coffee-rails
haml
- jquery-rails
rails
sass-rails
- turn
uglifier
yui-compressor
View
Binary file not shown.
Binary file not shown.
View
@@ -51,7 +51,5 @@ class Application < Rails::Application
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
-
- config.assets.paths << "#{Rails.root}/spec/javascripts"
end
end
@@ -27,4 +27,6 @@
# Expands the lines which load the assets
config.assets.debug = true
+
+ config.assets.paths << "#{Rails.root}/spec/javascripts"
end
@@ -21,7 +21,7 @@
config.assets.digest = false
# Defaults to Rails.root.join("public/assets")
- config.assets.manifest = Rails.root.join("public/assets")
+ # config.assets.manifest = Rails.root.join("public/assets")
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
View
@@ -1,43 +1,10 @@
-cache_version = ENV['CACHE_VERSION'] || 4
-do_cache = true
-CodeMash::Application.routes.draw do
+require 'cache_manifest'
- if do_cache
- match "/application.manifest" => Proc.new { |env|
- body = []
- body << "CACHE MANIFEST"
- body << "# v%s" % [cache_version]
- body << ""
- body << "/app.html"
- body << "/assets/application.css"
- body << "/assets/application.js"
- body << "/assets/room_map.jpg"
- body << "/assets/fav_on.png"
- body << "/assets/fav_off.png"
- body << "/assets/ajax-loader.gif"
- body << "/assets/dot.png"
- body << "/assets/jquery-mobile/ajax-loader.png"
- body << "/assets/jquery-mobile/icons-18-white.png"
- body << "/assets/jquery-mobile/icons-36-white.png"
- body << ""
- body << "NETWORK:"
- body << "*"
- body << ""
- [200, {"Content-Type" => "text/cache-manifest"}, [body.join("\n")]]
- }
- else
- match "/application.manifest" => Proc.new { |env|
- body = []
- body << "CACHE MANIFEST"
- body << "# v%s" % [Time.now.to_f]
- body << "NETWORK:\n*\n\n"
- [200, {"Content-Type" => "text/cache-manifest"}, [body.join("\n")]]
- }
- end
+CodeMash::Application.routes.draw do
+ match "/application.manifest" => CacheManifest.new(Rails.env)
match "/javascript/specs" => "specs#index"
-
match "/app.html" => "home#index"
-
root to: redirect('/app.html')
+
end
View
@@ -0,0 +1,49 @@
+class CacheManifest
+
+ def initialize environment
+ @environment = environment
+ end
+
+ def call env
+ [200, {"Content-Type" => "text/cache-manifest"}, [body]]
+ end
+
+ def body
+ @body ||= build
+ end
+
+ def build
+ <<EOS
+CACHE MANIFEST
+# v#{cache_version}
+
+#{cache_files.join("\n")}
+
+NETWORK:
+*
+
+EOS
+ end
+
+ def cache_files
+ @cache_files ||= asset_directories.map do |dir|
+ Dir[dir+"/**/*"].map do |file|
+ if File.file?(file)
+ "/assets" + file.sub(dir, '').gsub(/\.erb$/,'')
+ end
+ end
+ end.flatten.compact.sort
+ end
+
+ def asset_directories
+ @asset_directories ||= Rails.application.config.assets.paths
+ end
+
+ def production?; @environment == "production" end
+ def development?; @environment == "development" end
+
+ def cache_version
+ ENV['CACHE_VERSION'] || Time.now.to_f
+ end
+
+end

0 comments on commit 76fa7ab

Please sign in to comment.