Created
August 24, 2011 23:33
-
-
Save peterkappus/1169586 to your computer and use it in GitHub Desktop.
Haml/Sass builder script (great for Wordpress themes & other HTML-like templates)
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
require 'digest/md5' | |
=begin | |
About: | |
This script automatically runs on a 2-second loop, searches the current directory for haml & sass files that have been modified since the last iteration and builds them as needed into a file with the same name minus the .haml/sass extension. I wrote it so I could use Haml & Sass to develop Wordpress themes. | |
How-to: | |
1) Name your source files with the extension you'd like them to have, then add ".haml" or ".sass" as appropriate... | |
For example: index.php.haml and styles.css.sass | |
2) Open a terminal window in your development directory and run "ruby build.rb" | |
3) Tweak your haml & sass, refresh your PHP app (ie your Wordpress site) & Enjoy! | |
Have fun, | |
Peter Kappus | |
August 2011 | |
http://www.peterkappus.com | |
=end | |
def generate(type,options="") | |
Dir["*.#{type}"].each do |f| | |
newname = f.gsub(/.#{type}/,'') | |
note = "THIS FILE MODIFIED VIA THE BUILDER SCRIPT... modify #{f}.#{type} file instead." | |
#brittle solution for making it a comment in the appropriate language | |
if(type=="sass") then | |
note = "/* #{note}*/" | |
else | |
note = "<?php # #{note}?>" | |
end | |
#hash the contents of the file to see if it's changed since our last build | |
latest_md5 = Digest::MD5.hexdigest(File.read(f)) | |
#if so, do a rebuild | |
if(latest_md5 != @hashes[f]) then | |
File.open(newname, "w") do |fw| | |
fw.write note + "\n" + `#{type} #{options} #{f}` | |
puts "generating new version of #{newname}" | |
end | |
@hashes[f] = latest_md5 | |
end | |
end | |
end | |
@hashes = Hash.new | |
while(true) do | |
generate("haml","--no-escape-attrs") | |
generate("sass") | |
sleep 2 | |
puts Time.now.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment