Skip to content
Browse files

update cached admin.js in rake radiant:update

  • Loading branch information...
1 parent e7c4aee commit 3816f8117b60b4b34d58d892423f4beab09341a7 @saturnflyer saturnflyer committed Apr 5, 2010
Showing with 61 additions and 2 deletions.
  1. +1 −0 CHANGELOG
  2. +1 −0 CONTRIBUTORS
  3. +21 −1 lib/task_support.rb
  4. +7 −1 lib/tasks/framework.rake
  5. +31 −0 spec/lib/task_support_spec.rb
View
1 CHANGELOG
@@ -2,6 +2,7 @@
=== Edge
+* Cache admin.js in update task [Janne Asmala]
* Finally rename User.protected_attributes to unprotected_attributes [Jim Gay]
* Support i18n [Keith Bingman, many others]
* Only run migrate and update tasks with script/extension if they exist [John Muhl, Jim Gay]
View
1 CONTRIBUTORS
@@ -5,6 +5,7 @@ core:
=== Edge
+* Janne Asmala
* Keith Bingman
* Dirk Kelly
* Anna billstrom
View
22 lib/task_support.rb
@@ -29,5 +29,25 @@ def config_import(path = "#{Rails.root}/config/radiant_config.yml", clear = nil)
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
+end
View
8 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'
View
31 spec/lib/task_support_spec.rb
@@ -39,4 +39,35 @@
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

0 comments on commit 3816f81

Please sign in to comment.
Something went wrong with that request. Please try again.