Created
May 30, 2013 06:44
-
-
Save chrisross/5676103 to your computer and use it in GitHub Desktop.
This pair of files are a crude attempt at providing a backup method of compiling static projects' source files (HAML and Sass) using the ease of rake tasking from the commandline.
This file contains 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 | |
require 'yaml' | |
# Load _config.yaml file | |
Conf = YAML.load_file File.join(Dir.pwd, 'rake_config.yaml') | |
# Paths | |
ROOT = Conf['root'] == '/' ? File.expand_path(File.dirname(__FILE__)) : Conf['root'] | |
SRC_DIR = File.join ROOT, (Conf['source'] || 'src') | |
SITE_DIR = File.join ROOT, (Conf['output'] || 'site') | |
CSS_DIR, JS_DIR, IMAGES_DIR = %w{css js images}.map do |i| | |
File.join SITE_DIR, (Conf["#{i}_dir'"] || i) | |
end | |
# Manually create output dir, as later file writing/creation will not do so | |
[SITE_DIR, CSS_DIR, JS_DIR, IMAGES_DIR].each do |dir_path| | |
Dir::mkdir dir_path unless File.directory? dir_path | |
end | |
namespace :compile do | |
desc 'Compile both haml and sass from source directory to output directory' | |
task 'all' do | |
compile_haml | |
compile_sass | |
end | |
desc 'Compile sitemap' | |
task 'sitemap' do | |
compile_haml 'sitemap.html.haml' | |
end | |
desc 'Compile sass source directory to output directory' | |
task 'sass' do | |
compile_sass | |
end | |
desc 'Compile haml source directory to output directory' | |
task 'haml' do | |
compile_haml | |
end | |
def compile_sass(globs=nil) | |
require 'sass' | |
if Conf['sass'].nil? | |
raise Exception.new('Conf file not loaded') | |
else | |
# Glean options from Conf | |
sass_ext = Conf['sass']['extension'] || '.scss' | |
sass_exc = Conf['sass']['exclude'] || /^[^\_]/ | |
sass_options = Conf['sass_options'] ? Conf['sass_options'].dup : {} | |
sass_options[:syntax] ||= sass_ext.to_sym | |
sass_options[:load_paths] ||= %w{scss sass}.map {|i| File.join SRC_DIR,i } | |
sass_options[:style] ||= Conf['minified'] ? :compressed : Conf['sass']['style'] || :expanded | |
globs ||= ["scss/*#{sass_ext}"].map! {|i| File.join SRC_DIR, i} | |
Sass::Script::Number.precision = Conf['sass']['precision'] || 7 | |
FileList.new(globs).each do |filename| | |
if !sass_exc.match(File.basename(filename)).nil? && | |
filename =~ /([^\/]+)(#{sass_ext})$/ | |
begin | |
outfile = File.join(CSS_DIR, "#{$1}.css") | |
unless File.exists?(outfile) && File.ctime(outfile) == File.ctime(filename) | |
STDOUT.puts "Compiling #{filename} --> #{outfile}" | |
File.open(outfile, 'w') do |f| | |
f.write Sass::Engine.new(File.read(filename), sass_options).render | |
end | |
end | |
rescue => e | |
STDOUT.puts e.message | |
next | |
end | |
else | |
STDOUT.puts "Sass file in glob failed test: #{filename}" | |
end | |
end | |
end | |
rescue => e | |
STDOUT.puts e.message | |
end | |
def compile_haml(globs=nil) | |
require 'haml' | |
%w{haml_helpers file_helpers}.each do |i| | |
require File.join SRC_DIR, 'helpers',"#{i}.rb" | |
end | |
if Conf['haml'].nil? | |
raise Exception.new('Conf file not loaded') | |
else | |
# Glean options from Conf | |
haml_ext = Conf['haml']['extension'] || '.haml' | |
haml_exc = Conf['haml']['exclude'] || /^[^\_]/ | |
haml_options = Conf['haml_options'] ? Conf['haml_options'].dup : {} | |
haml_options[:ugly] ||= Conf['minified'] || false | |
globs ||= ["haml/*#{haml_ext}"].map! {|i| File.join SRC_DIR, i} | |
FileList.new(globs).each do |filename| | |
if !haml_exc.match(File.basename(filename)).nil? && | |
filename =~ /([^\/]+)#{haml_ext}$/ | |
begin | |
outfile = File.join(SITE_DIR,"#{$1}.html") | |
unless File.exists?(outfile) && File.ctime(outfile) == File.ctime(filename) | |
STDOUT.puts "Compiling #{filename} --> #{outfile}" | |
File.open(outfile, 'w') do |f| | |
f.write Haml::Engine.new(File.read(filename), haml_options).render | |
end | |
end | |
rescue => e | |
STDOUT.puts e.message | |
next | |
end | |
else | |
STDOUT.puts "Haml file in glob failed test: #{filename}" | |
end | |
end | |
end | |
rescue => e | |
STDOUT.puts e.message | |
end | |
end |
This file contains 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
--- # Rake compiler tasks for haml and sass | |
root: / # defaults to Dir.pwd | |
source: src # defaults to src | |
output: site # defaults to site | |
css_dir: css # defaults to css | |
minified: false # defaults to false | |
haml: | |
extension: .haml # defaults to .haml | |
sass: | |
extension: .scss # defaults to .scss | |
style: expanded | |
precision: 7 # defaults to 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment