require 'fileutils' namespace :assets do desc "Compile all the assets" task :precompile do Rake::Task["assets:precompile:all"].invoke end desc "Remove compiled assets" task :clean do puts "Removing compiled assets..." FileUtils.rm_rf(Dir.glob(File.join(_public_folder, '**', '*'))) puts "Done!" end namespace :precompile do task :all do puts "Compiling all assets..." Rake::Task["assets:precompile:css"].invoke Rake::Task["assets:precompile:js"].invoke Rake::Task["assets:precompile:img"].invoke Rake::Task["assets:precompile:font"].invoke puts "Done!" end task :css do _css_files.each do |css_file| _compile(css_file, false) _compile(css_file, true) end end task :js do _js_files.each do |js_file| _compile(js_file, false) _compile(js_file, true) end end task :img do _img_files.each do |img_file| _compile(img_file, false) end end task :font do _font_files.each do |font_file| _compile(font_file, false) end end end private def _public_folder File.join(ClassyAssets::Configuration.public_path, ClassyAssets::Configuration.asset_prefix) end def _assets_root File.join(ClassyAssets::Configuration.root_path, ClassyAssets::Configuration.asset_prefix) end def _asset_files(type) files = Dir.glob(File.join(_assets_root, type, '**', '*')) files.reject! {|f| File.directory?(f) } files.map! {|f| Pathname.new(f).basename.to_s } files.to_a end def _css_files _asset_files('stylesheets') end def _js_files _asset_files('javascripts') end def _img_files _asset_files('images') end def _font_files _asset_files('fonts') end def _path_to(filename) Pathname.new(File.join(_public_folder, filename)) end def _sprockets @sprockets ||= ClassyAssets::Configuration.sprockets end def _replace_ext(filename) filename.gsub!(/sass$|scss$/,'css') filename.gsub!(/coffee$/,'js') filename end def _compile(filename, compress = false) filename = _replace_ext(filename) asset = _sprockets[filename] if compress file = _path_to(filename + ".gz") else file = _path_to(filename) end FileUtils.mkdir_p file.dirname asset.write_to(file, { compress: compress }) puts " ✓ #{file.basename}" end end