Skip to content

Commit

Permalink
update cached admin.js in rake radiant:update
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Apr 6, 2010
1 parent e7c4aee commit 3816f81
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -2,6 +2,7 @@


=== Edge === Edge


* Cache admin.js in update task [Janne Asmala]
* Finally rename User.protected_attributes to unprotected_attributes [Jim Gay] * Finally rename User.protected_attributes to unprotected_attributes [Jim Gay]
* Support i18n [Keith Bingman, many others] * Support i18n [Keith Bingman, many others]
* Only run migrate and update tasks with script/extension if they exist [John Muhl, Jim Gay] * Only run migrate and update tasks with script/extension if they exist [John Muhl, Jim Gay]
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -5,6 +5,7 @@ core:


=== Edge === Edge


* Janne Asmala
* Keith Bingman * Keith Bingman
* Dirk Kelly * Dirk Kelly
* Anna billstrom * Anna billstrom
Expand Down
22 changes: 21 additions & 1 deletion lib/task_support.rb
Expand Up @@ -29,5 +29,25 @@ def config_import(path = "#{Rails.root}/config/radiant_config.yml", clear = nil)
puts "No file exists at #{path}" puts "No file exists at #{path}"
end end
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 end
8 changes: 7 additions & 1 deletion lib/tasks/framework.rake
Expand Up @@ -63,7 +63,7 @@ unless File.directory? "#{RAILS_ROOT}/app"


desc "Update configs, scripts, sass, stylesheets and javascripts from Radiant." desc "Update configs, scripts, sass, stylesheets and javascripts from Radiant."
task :update do 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['ONLY'].split(',') if ENV['ONLY']
tasks = tasks - ENV['EXCEPT'].split(',') if ENV['EXCEPT'] tasks = tasks - ENV['EXCEPT'].split(',') if ENV['EXCEPT']
tasks.each do |task| tasks.each do |task|
Expand Down Expand Up @@ -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"]] copy_javascripts[RAILS_ROOT + '/public/javascripts/admin/', Dir["#{File.dirname(__FILE__)}/../../public/javascripts/admin/*.js"]]
end 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" desc "Update config/boot.rb from your current radiant install"
task :configs do task :configs do
require 'erb' require 'erb'
Expand Down
31 changes: 31 additions & 0 deletions spec/lib/task_support_spec.rb
Expand Up @@ -39,4 +39,35 @@
Radiant::Config.to_hash.should == YAML.load(YAML.load_file(@yaml_file)) Radiant::Config.to_hash.should == YAML.load(YAML.load_file(@yaml_file))
end end
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 end

0 comments on commit 3816f81

Please sign in to comment.