Created
January 7, 2012 23:07
-
-
Save Arwid/1576435 to your computer and use it in GitHub Desktop.
Sprockets compilation of assets
This file contains 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
source 'http://rubygems.org' | |
gem 'sass' | |
gem 'coffee-script' | |
gem 'yui-compressor' | |
gem 'handlebars_assets' | |
gem 'sprockets' | |
gem 'rb-fsevent' |
This file contains 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
# Asset tasks | |
require 'rubygems' | |
require 'bundler' | |
require 'pathname' | |
require 'logger' | |
require 'fileutils' | |
require 'rb-fsevent' | |
# Require gems from Gemfile | |
Bundler.require | |
# Configuration | |
ROOT = Pathname(File.dirname(__FILE__)) | |
LOGGER = Logger.new(STDOUT) | |
BUNDLES = %w( app.js styles.css ) | |
BUILD_DIR = ROOT.join("public/assets") | |
SOURCE_DIR = ROOT.join("assets") | |
# register handlebar templates | |
Sprockets.register_engine '.hbs', HandlebarsAssets::TiltHandlebars | |
task :default => [:compile] | |
# Compile all assets | |
task :compile => :cleanup do | |
begin | |
# Create a new Sprockets::Environment instance, passing in some configurations | |
sprockets = Sprockets::Environment.new(ROOT) do |env| | |
env.logger = LOGGER | |
end | |
# Append the asset paths | |
sprockets.append_path(SOURCE_DIR.join('javascripts').to_s) | |
sprockets.append_path(SOURCE_DIR.join('stylesheets').to_s) | |
# Process and package the assets to the build directory | |
BUNDLES.each do |bundle| | |
assets = sprockets.find_asset(bundle) | |
# split on | |
prefix, basename = assets.pathname.to_s.split('/')[-2..-1] | |
realname = basename.split(".")[0..1].join(".") | |
# Add digest to filename in production | |
if ENV['env'] == 'production' | |
basename, ext = basename.split(".")[0..1] | |
realname = "#{basename}-#{assets.digest}.#{ext}" | |
end | |
# Write concatenated asset | |
FileUtils.mkpath BUILD_DIR | |
assets.write_to(BUILD_DIR.join(realname)) | |
# Write minified asset | |
if ENV['env'] == 'production' | |
Minifier.minify [:src => BUILD_DIR.join(realname), :dst => BUILD_DIR.join(realname)] | |
end | |
# If you want to reference a single CSS file (e.g. filename.css) instead of an entire bundle. Build a standalone package for each CSS source | |
# assets.to_a.each do |asset| | |
# # strip filename.css.foo.bar.css multiple extensions | |
# realname = asset.pathname.basename.to_s.split(".")[0..1].join(".") | |
# asset.write_to(BUILD_DIR.join(prefix, realname)) | |
# end | |
end | |
rescue Exception => e | |
puts e.message | |
end | |
end | |
# Watch source directory for file changes and compile | |
task :watch => :compile do | |
# options = {:latency => 4, :no_defer => true } | |
fsevent = FSEvent.new | |
fsevent.watch SOURCE_DIR.to_s do |directories| | |
puts "Detected change inside: #{directories.inspect}" | |
Rake::Task["compile"].reenable | |
Rake::Task["compile"].invoke | |
end | |
fsevent.run | |
end | |
# Cleanup asset directory | |
task :cleanup do | |
dirs = Dir.glob(File.join(BUILD_DIR.join("{*.js,*.css}"))) | |
dirs.each do |dir| | |
FileUtils.rm_rf dir | |
end | |
end | |
# Minify assets | |
module Minifier | |
def Minifier.minify(files) | |
files.each do |file| | |
cmd = "java -jar ../tools/yuicompressor/build/yuicompressor-2.4.6.jar #{file[:src]} -o #{file[:dst]} --charset utf-8" | |
puts cmd | |
ret = system(cmd) # *** SYSTEM RUN LOCAL COMMANDS *** | |
raise "Minification failed for #{file}" if !ret | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bundle
rake watch
rake compile env=production