Created
April 18, 2016 18:24
-
-
Save potapuff/090b2da4a4156c1272430241cb70edc0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
VIEW_PATH = 'lib/assets' | |
namespace :static do | |
desc 'Render all resources' | |
task :publicate => :environment do | |
resources(VIEW_PATH).each do |src, dest| | |
html= controller.render_to_string(file:src, layout:'application') | |
dirname = File.dirname(dest) | |
unless File.directory?(dirname) | |
FileUtils.mkdir_p(dirname) | |
end | |
File.write(dest, html) | |
end | |
end | |
desc 'Remove all resources, generated by publicate task' | |
task :clean => :environment do | |
resources(VIEW_PATH).each do |src,dest| | |
FileUtils.remove_file(dest,true) | |
end | |
end | |
def resources search_path | |
search_path = search_path.to_s | |
files = Dir["#{search_path}/**/*"] | |
.select {|f| !File.directory? f} | |
.map{|x| [x, x.gsub(search_path, Rails.root.join('public').to_s)]} | |
Hash[files] | |
end | |
def controller | |
ApplicationController.new.tap do |controller| | |
controller.request = ActionDispatch::Request.new( { | |
'HTTP_HOST' => 'example.com', | |
'SCRIPT_NAME' => '', | |
'HTTPS' => Rails.env.development? ? 'off' : 'on', | |
'rack.input' => '' }) | |
controller.response = ActionDispatch::Response.new | |
controller.params = {} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment