Skip to content

Instantly share code, notes, and snippets.

@chrisross
Last active March 3, 2016 12:10
Show Gist options
  • Save chrisross/7854168 to your computer and use it in GitHub Desktop.
Save chrisross/7854168 to your computer and use it in GitHub Desktop.
A simple guardfile for a compass project, that updates each directory within the `sass_dir` with an index file called `_all.scss` in which there are imports for every other scss file in that folder making importation of all files simple. update sass_dir per project.
# Example Directory structure
# /path/to/project
# |───scss
# | |───library
# | | |───_all.scss <------- creates/updates this file
# | | |───_typography.scss
# | | |───_structure.scss
# | | └───_responsive.scss
# | |───modules
# | | |───_all.scss <------- and this file
# | | |───_lightbox.scss
# | | |───_navigation.scss
# | | |───_user-interface.scss
# | | └───_buttons.scss
# | |───_config.scss
# | |───_normalize.scss
# | |───_base.scss
# | |───_structure.scss
# | └───_typography.scss
# |───config.rb
# └───Guardfile
guard('compass') { watch %r{(.*)\.s[ac]ss$} }
# Needed for FileList Module in IndexUpdater
require 'rake'
# Mirror config.rb
SASS_DIR = 'scss'
SYNTAX = :scss
class SassIndexUpdater < Struct.new(:sass_dir, :preferred_syntax)
def run
Dir[ File.join(sass_dir,'*') ].map do |f|
update_dir(f) if File.directory?(f)
end
end
def update_dir(dir)
index = FileList[ File.join(dir, "*.#{preferred_syntax.to_s}") ]
index = index.exclude(/\_all\.#{preferred_syntax.to_s}$/)
imports = index.map do |f|
"@import \"#{File.basename(f, ".#{preferred_syntax.to_s}").gsub(/^\_/,'')}\";"
end
File.open( File.join(dir, "_all.#{preferred_syntax.to_s}"), 'w' ) do |f|
f.puts imports
end
end
end
# Update directories index sass file
guard 'shell' do
watch %r{(.*)\.s[ac]ss$} do |m|
SassIndexUpdater.new( SASS_DIR, SYNTAX ).run
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment