Created
January 31, 2011 16:11
-
-
Save kurbmedia/804267 to your computer and use it in GitHub Desktop.
watchr script to minify js files on save
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
somepackage: | |
- somefile.js | |
anotherpackage: | |
- somefile.js |
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 | |
# encoding: utf-8 | |
# | |
# Run me with: | |
# | |
# $ watchr minify.watchr | |
# -------------------------------------------------- | |
# Convenience Methods | |
# -------------------------------------------------- | |
begin | |
require 'watchr' | |
require 'yaml' | |
@compiler = "/usr/local/gcc-compiler.jar" | |
@manifest = YAML.load(File.open("javascripts/manifest.yml")) | |
puts @manifest.inspect | |
rescue Exception => e | |
puts e.message | |
end | |
def all_js_files | |
Dir['javascripts/**/*.js'] | |
end | |
def compile_package(package) | |
libs = @manifest[package[1]] | |
if libs.nil? or libs.empty? | |
puts "Sorry, thanks for playing, but there were no matches for #{package}" | |
else | |
sources = libs.map{ |lib| "--js javascripts/#{package[1]}/#{lib}" }.join(' ') | |
puts 'Minifying...' | |
puts "java -jar #{@compiler} #{sources} --js_output_file javascripts/#{package[1]}.min.js" | |
puts `java -jar #{@compiler} #{sources} --js_output_file javascripts/#{package[1]}.min.js` | |
puts 'Done.' | |
end | |
end | |
# -------------------------------------------------- | |
# Watchr Rules | |
# -------------------------------------------------- | |
watch('^javascripts/(.*)/(.*)\.js'){ |m| compile_package(m) } | |
# -------------------------------------------------- | |
# Signal Handling | |
# -------------------------------------------------- | |
Signal.trap 'INT' do | |
puts " quitting....FINE!." | |
exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment