Skip to content

Instantly share code, notes, and snippets.

@thinkstylestudio
Forked from peterkappus/haml-sass builder
Created July 7, 2012 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkstylestudio/3068056 to your computer and use it in GitHub Desktop.
Save thinkstylestudio/3068056 to your computer and use it in GitHub Desktop.
Haml/Sass builder script (great for Wordpress themes & other HTML-like templates)
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