Created
April 3, 2010 07:16
-
-
Save asmala/354217 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 6309caf9f1d5445989e2b1c1b246d7929f36c3f1 Mon Sep 17 00:00:00 2001 | |
From: Janne Asmala <janne@asmala.name> | |
Date: Sat, 3 Apr 2010 10:14:40 +0300 | |
Subject: [PATCH] Added rake task, including specs, to pre-cache admin UI Javascripts when updating Radiant assets. This removes a pain encountered when deploying on read-only filesystems such as the one used on Heroku. | |
--- | |
lib/task_support.rb | 22 +++++++++++++++++++++- | |
lib/tasks/framework.rake | 8 +++++++- | |
spec/lib/task_support_spec.rb | 31 +++++++++++++++++++++++++++++++ | |
3 files changed, 59 insertions(+), 2 deletions(-) | |
diff --git a/lib/task_support.rb b/lib/task_support.rb | |
index 147ae70..bbaad39 100644 | |
--- a/lib/task_support.rb | |
+++ b/lib/task_support.rb | |
@@ -29,5 +29,25 @@ class TaskSupport | |
puts "No file exists at #{path}" | |
end | |
end | |
+ | |
+ # Write the combined content of files in dir into cache_file in the same dir. | |
+ # | |
+ def cache_files(dir, files, cache_file) | |
+ cache_content = files.collect { |f| | |
+ File.read(File.join(dir, f)) }.join("\n\n") | |
+ | |
+ cache_path = File.join(dir, cache_file) | |
+ rm(cache_path) if File.exists?(cache_path) | |
+ File.open(cache_path, "w+") { |f| f.write(cache_content) } | |
+ end | |
+ | |
+ # Reads through the layout file and returns an array of JS filenames | |
+ # | |
+ def find_admin_js | |
+ layout = File.join(RADIANT_ROOT, 'app', 'views', 'layouts', 'application.html.haml') | |
+ js_regexp = /javascript_include_tag %w\((.*)\), :cache => 'admin\/all/ | |
+ files = File.open(layout) { |f| f.read.match(js_regexp)[1].split } | |
+ files.collect { |f| f.split('/').last + '.js' } | |
+ end | |
end | |
-end | |
\ No newline at end of file | |
+end | |
diff --git a/lib/tasks/framework.rake b/lib/tasks/framework.rake | |
index 664410d..3650a97 100644 | |
--- a/lib/tasks/framework.rake | |
+++ b/lib/tasks/framework.rake | |
@@ -63,7 +63,7 @@ unless File.directory? "#{RAILS_ROOT}/app" | |
desc "Update configs, scripts, sass, stylesheets and javascripts from Radiant." | |
task :update do | |
- tasks = %w{scripts javascripts configs images sass stylesheets} | |
+ tasks = %w{scripts javascripts configs images sass stylesheets cached_assets} | |
tasks = tasks & ENV['ONLY'].split(',') if ENV['ONLY'] | |
tasks = tasks - ENV['EXCEPT'].split(',') if ENV['EXCEPT'] | |
tasks.each do |task| | |
@@ -104,6 +104,12 @@ unless File.directory? "#{RAILS_ROOT}/app" | |
copy_javascripts[RAILS_ROOT + '/public/javascripts/admin/', Dir["#{File.dirname(__FILE__)}/../../public/javascripts/admin/*.js"]] | |
end | |
+ desc "Update the cached assets for the admin UI" | |
+ task :cached_assets do | |
+ dir = File.join(Rails.root, 'public', 'javascripts', 'admin') | |
+ TaskSupport.cache_files(dir, TaskSupport.find_admin_js, 'all.js') | |
+ end | |
+ | |
desc "Update config/boot.rb from your current radiant install" | |
task :configs do | |
require 'erb' | |
diff --git a/spec/lib/task_support_spec.rb b/spec/lib/task_support_spec.rb | |
index 7bdb965..773f2c8 100644 | |
--- a/spec/lib/task_support_spec.rb | |
+++ b/spec/lib/task_support_spec.rb | |
@@ -39,4 +39,35 @@ describe TaskSupport do | |
Radiant::Config.to_hash.should == YAML.load(YAML.load_file(@yaml_file)) | |
end | |
end | |
+ | |
+ describe "self.cache_files" do | |
+ before do | |
+ @files = [ 'a.txt', 'b.txt' ] | |
+ @dir = "#{Rails.root}/tmp/cache_files_test" | |
+ @cache_file = 'all.txt' | |
+ | |
+ FileUtils.mkdir_p(@dir) | |
+ FileUtils.rm_rf(File.join(@dir, '*.txt')) | |
+ @files.each do |f_name| | |
+ File.open(File.join(@dir, f_name), "w+") do |f| | |
+ f.write("Contents of '#{f_name}'") | |
+ end | |
+ end | |
+ end | |
+ | |
+ it "should create a cache file containing the contents of the specified files" do | |
+ TaskSupport.cache_files(@dir, @files, @cache_file) | |
+ cache_path = File.join(@dir, @cache_file) | |
+ File.should exist(cache_path) | |
+ File.read(cache_path).should == "Contents of 'a.txt'\n\nContents of 'b.txt'" | |
+ end | |
+ end | |
+ | |
+ describe "self.find_admin_js" do | |
+ it "should return an array of JS files" do | |
+ js_files = TaskSupport.find_admin_js | |
+ js_files.should_not be_empty | |
+ js_files.each { |f| f.should =~ /^[^\/]+.js$/ } | |
+ end | |
+ end | |
end | |
\ No newline at end of file | |
-- | |
1.6.1.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment