# MasterView template engine rake tasks # load MasterView engine with the std config (uses app config settings when in rails app) mv_lib_dir_path = File.join( File.dirname(__FILE__), '../lib' ) MV_ADMIN_CONTROLLER_INSTALL_DIR_PATH = File.join( mv_lib_dir_path, 'masterview/extras' ) # ./app/views/masterview # doing load and require this way so that it works from both gems and plugin install $LOAD_PATH.push( mv_lib_dir_path ) require "masterview" MasterView.set_log_level('ERROR') namespace :mv do desc 'Display list and status about each MasterView template' task :list do template_specs, content_hash = MasterView::TemplateSpec.scan @template_specs_sorted = template_specs.sort @template_specs_sorted.each do |path, ts| puts "#{path}\t#{ts.status}\t#{ts.message}" end end desc 'Display list, status, and full details about each MasterView template' task :list_all do template_specs, content_hash = MasterView::TemplateSpec.scan @template_specs_sorted = template_specs.sort @template_specs_sorted.each do |path, ts| puts "#{path}\t#{ts.status}\t#{ts.message}" puts "\tgenerates: #{ts.gen_parts.join(', ')}\n\n" end end desc 'Rebuild/update template imports, specify TEMPLATE=foo/bar.html' task :rebuild do raise "Usage: rake mv:rebuild TEMPLATE=foo/bar.html" unless ENV['TEMPLATE'] path = ENV['TEMPLATE'] path = MasterView::IOMgr.template.cleanup_path_get_relative_pathname(path).to_s template_specs, content_hash = MasterView::TemplateSpec.scan template_spec = template_specs[path] raise 'Template '+path+' not found' unless template_spec if template_spec.rebuild_template(content_hash, :write_to_file => true) puts 'File '+path+' updated' puts 'Note: Backup was created in MasterView::IOMgr.backup location' if MasterView::IOMgr.backup else puts 'Identical content - no update needed for '+path end end desc 'Rebuild all outdated template imports' task :rebuild_all do files_rebuilt = [] MasterView::TemplateSpec.scan do |template_spec, content_hash| if template_spec.status == MasterView::TemplateSpec::Status::ImportsOutdated && template_spec.rebuild_template(content_hash, :write_to_file => true) files_rebuilt << template_spec.path end end files_rebuilt.each do |f| puts f+' updated' end unless files_rebuilt.empty? || MasterView::IOMgr.backup.nil? puts 'Note: Backups were created in MasterView::IOMgr.backup location' if MasterView::IOMgr.backup end end # checks app path first for views and files, then falls back to files in MV installation dir def find_path(path) mv_path = File.join( MV_ADMIN_CONTROLLER_INSTALL_DIR_PATH, path ) working_path = (File.exist?(path)) ? path : mv_path end desc 'Create shell template using layout of existing masterview template, requires TEMPLATE=foo/bar.html ACTION=list' task :copy_layout do raise "Usage: rake mv:copy_layout TEMPLATE=foo/bar.html ACTION=list" unless ENV['TEMPLATE'] && ENV['ACTION'] action_to_create = ENV['ACTION'] path = ENV['TEMPLATE'] path = MasterView::IOMgr.template.cleanup_path_get_relative_pathname(path).to_s empty_file_path = find_path('app/views/masterview/admin/empty.rhtml') empty_insert_erb = File.readlines(empty_file_path).join src_file = path dst_file = MasterView::TemplateSpec.create_empty_shell_for_action(src_file, action_to_create, empty_insert_erb, :write_to_file => true) puts dst_file+' created' end desc "Run parser manually on masterview html files to parse and generate the erb/rhtml files" task :parse do MasterView::IOMgr.template.find(:pattern => MasterView::TemplateFilenamePattern ) do |mio| puts 'Generated erb/rhtml files from '+mio.pathname.to_s MasterView::Parser.parse_mio(mio, MasterView::IOMgr.erb) end if MasterView::ConfigSettings.generate_rhtml_files puts 'Generated erb/rhtml output was created in app/views/** tree' else puts "\nNote: config.generate_rhtml_files was false, so rails reads erb/rhtml directly from MasterView, and thus files were parsed but not serialized to file system. Set config.generate_rhtml_files = true (in config/masterview/settings.rb) and rerun this command to generate rhtml to the file system" end end desc "View erb/rhtml that masterview generated - specify rhtml name like RHTML=product/_form.rhtml or RHTML=layouts/product.rhtml" task :view_rhtml do raise "Usage: rake mv:view_rhtml RHTML=product/_form.rhtml" unless ENV['RHTML'] rhtml_file = ENV['RHTML'] # make sure they are parsed string_hash_mio_tree = MasterView::MIO::StringHashMIOTree.new( {}, MasterView::ConfigSettings.generated_file_default_extension ) MasterView::IOMgr.template.find(:pattern => MasterView::TemplateFilenamePattern ) do |mio| MasterView::Parser.parse_mio(mio, string_hash_mio_tree) end f = string_hash_mio_tree.path(rhtml_file) raise "RHTML ("+rhtml_file+") does not exist. Use rake mv:list_all to find correct rhtml file to view" unless f.exist? puts f.read end end