Created
September 26, 2010 18:16
-
-
Save voldy/598170 to your computer and use it in GitHub Desktop.
Sprockets Watch
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
#!/usr/bin/env ruby | |
# | |
# sprockets watch | |
# ------------------------------------------------------ | |
# inspired by Compass from Chris Eppstein | |
# minify and concat code from Mark Story | |
# put together by Kjell Bublitz | |
# using Sprockets! | |
# | |
# sources: | |
# http://chriseppstein.github.com/blog/2009/09/07/building-a-jekyll-watcher-with-fssm/ | |
# http://mark-story.com/posts/view/creating-deployment-ready-javascript-with-sprockets | |
# ------------------------------------------------------ | |
# | |
# usage: | |
# 1) create a structure like "js/src", "js/dist". | |
# 2) copy this rakefile to "js/" | |
# 3) start watching "js/src" for changes by calling "rake watch" | |
# 4) work in in "js/src". | |
# any change will trigger the build process and create "build.js" in "dist" | |
# | |
# ------------------------------------------------------ | |
# | |
# installation (if you dont have them already.): | |
# sudo gem install sprockets | |
# sudo gem install jsmin | |
# sudo gem install ttilley-fssm | |
# | |
# ------------------------------------------------------ | |
# | |
# options: | |
# while i find the style to write them ugly, these may still come handy. | |
# you can specify the src & dist folder (path or dir-name) and the build filename | |
# | |
# rake watch[SOURCE-FOLDER, BUILD-FOLDER, FILE-NAME] | |
# | |
# | |
# defaults (if no options given): | |
# ./src | |
# ./dist | |
# build | |
# | |
# to change these you can call rake like this: | |
# rake watch[source,deploy,site] | |
# | |
# which would translate to: | |
# ./source | |
# ./deploy | |
# site | |
# | |
# please note that you have to omit the file extension in the filename argument. | |
# given the "site" example above, the filename would become "site.js" and "site-min.js" automatically. | |
# | |
# as said you can use full paths too: | |
# rake watch[webroot/js/app,webroot/js/build] | |
# rake watch[../sources,js] | |
# | |
# ------------------------------------------------------ | |
# | |
# enjoy! | |
require 'rubygems' | |
require 'Sprockets' | |
require 'Jsmin' | |
def rebuild(relative, srcFolder, distFolder, distFilename) | |
puts ">>> Change Detected to: #{srcFolder}/#{relative}" | |
concatFile = "#{distFolder}/#{distFilename}.js" | |
minifiedFile = "#{distFolder}/#{distFilename}-min.js" | |
secretary = Sprockets::Secretary.new( | |
:asset_root => '.', | |
:load_path => ['.'], | |
:source_files => [ | |
"#{srcFolder}/*.js" | |
], | |
:strip_comments => true | |
) | |
concatenation = secretary.concatenation | |
gluedFiles = concatenation.to_s | |
concatenation.save_to(concatFile) | |
File.open(minifiedFile, 'w') { |file| file.write(JSMin.minify(gluedFiles)) } | |
puts "#{concatFile} updated." | |
end | |
desc "Watch the src folder and concat and minify sources through sprockets" | |
task :watch, :src_folder, :dist_folder, :filename do |t, args| | |
require 'fssm' | |
srcFolder = args.src_folder.nil? ? "./src" : args.src_folder | |
distFolder = args.dist_folder.nil? ? "./dist" : args.dist_folder | |
distFilename = args.filename.nil? ? "build" : args.filename | |
puts ">>> Watching #{srcFolder} for changes <<<" | |
FSSM.monitor(srcFolder, '**/*.js') do | |
update {|base, relative| rebuild(relative, srcFolder, distFolder, distFilename)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment