Created
January 18, 2012 20:19
-
-
Save gardiner/1635301 to your computer and use it in GitHub Desktop.
Rakefile to build, preview and auto-rebuild haml+compass
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 'rake/clean' | |
include Rake::DSL | |
task :default => [:watch] | |
HAML = FileList['**/*.haml'] | |
HTML = HAML.ext('html') | |
SCSS = FileList['**/scss/*.scss'] | |
CSS = SCSS.gsub('scss', 'css').ext('css') | |
CLEAN.include HTML + CSS | |
task :compile => HTML + CSS | |
desc 'Generate markup and stylesheets and open browser preview' | |
task :preview, [:filename] => [:compile] do |t, args| | |
index = HTML.grep(/index/).first | |
args.with_defaults :filename => index | |
sh "( which gnome-open > /dev/null && gnome-open \"#{args[:filename]}\" ) || \ | |
( which open > /dev/null && open -g \"#{args[:filename]}\" )" | |
end | |
desc 'Watch the site and regenerate when it changes' | |
task :watch, [:filename] => [:preview] do |t, args| | |
require 'fssm' | |
FSSM.monitor do | |
path "#{File.dirname(__FILE__)}" do | |
glob '**/*.{haml,scss}' | |
update {|base, relative| refresh(args[:filename], relative)} | |
delete {|base, relative| refresh(args[:filename], relative)} | |
create {|base, relative| refresh(args[:filename], relative)} | |
end | |
end | |
end | |
# Refreshes the browser preview by calling rake preview. | |
# Passes a file argument to the preview task: | |
# If rake watch has been called with an explicit file name, | |
# that one is used. Otherwise the last changed haml file | |
# is used. | |
def refresh(filename, changed) | |
if filename.nil? | |
if changed.match /\.haml$/ | |
filename = changed.sub(/\.haml/, '.html') | |
$lastfilename = filename | |
else | |
filename = $lastfilename | |
end | |
end | |
sh "rake preview[\"#{filename}\"]" | |
end | |
rule '.html' => '.haml' do |t| | |
puts "Rebuilding #{t.name}" | |
libs = Dir.glob('**/*.rb').map{ |f| "-r \"#{f}\"" }.join(" ") | |
sh "haml #{libs} \"#{t.source}\" \"#{t.name}\"" | |
end | |
rule '.css' => lambda { |cssfile| source(cssfile) } do |t| | |
config = FileList['**/config.rb'].first | |
if config | |
Dir.chdir(File.dirname(config)) do | |
sh 'compass compile' | |
end | |
else | |
puts "Rebuilding #{t.name}" | |
sh "sass #{t.source} #{t.name}" | |
end | |
end | |
def source(cssfile) | |
SCSS.find { |f| File.basename(f, '.scss') == File.basename(cssfile, '.css') } | |
end |
Hi. I've tried to (somewhat) simplify and comment the code: https://gist.github.com/2830310 I don't know if you can achieve the same functionality completely without the preview task. Hope this helps... Ole.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey,
I've tried to make a rakefile off of this code which only watches a folder for changes in the .haml file and compiles it to .html. It does work but probably has code which is not needed (such as the :preview task perhaps?). If you want please take a look at it here: https://gist.github.com/2829808 .
Thanks for the hard work!
/C