Using a compass from Ruby (not a shell)

I am creating a script in Ruby where I would like to compile a single SCSS file using Compass. I am trying to make this as simple as possible and would like to avoid using the config.rb file. I just want to set up a couple of settings through direct Ruby and tell Compass to compile one SCSS file into a CSS file.

I know that this should be possible, but I could not find decent documentation on how to do this. Any help would be appreciated.

+8
ruby sass compass-sass
source share
2 answers

You are right, in fact there is no detailed documentation on how to use Compass from Ruby. This is unfortunate, but don't let the little details, like the documentation, stop us!

First try

When I was looking to do the same, I just poked a source around Compass and was able to put together this little Ruby script. At first glance it seems like a trick:

require 'compass' require 'sass/plugin' compiler = Compass::Compiler.new( # Compass working directory '.', # Input directory 'styles/scss', # Output directory 'styles/css', # Compass options { :style => :compressed } ) compiler.compile('test.scss', 'test.css') 

But, apparently, Compass has a bunch of default configuration settings that do not turn on automatically when the compiler constructor is called directly (of which SASS load_path is one). This can lead to errors when trying to import Compass and mixins functions, such as:

error: File to import was not found or unreadable: compass / css3

Compass <1.0.0 (aka "old way")

Here's how to call the compiler without overriding these defaults:

 require 'compass' Compass.add_configuration( { :project_path => '.', :sass_path => 'styles/scss', :css_path => 'styles/css' }, 'custom' # A name for the configuration, can be anything you want ) Compass.compiler.compile('test.scss', 'test.css') 

However, with Compass version 1.0.0, Compass.compiler deprecated in favor of Compass.sass_compiler , which leads to ...

Comps> = 1.0.0 (aka "new way")

Thanks to @philipp for finding how to use the new API , we can update this snippet again to work with Compass.sass_compiler :

 require 'compass' require 'compass/sass_compiler' Compass.add_configuration( { :project_path => '.', :sass_path => 'styles/scss', :css_path => 'styles/css' }, 'custom' # A name for the configuration, can be anything you want ) compiler = Compass.sass_compiler({ :only_sass_files => [ 'styles/scss/test.scss' ] }) compiler.compile! 
+9
source share

Just call the compilation method from the command line. You can specify each option there. To view all options, run compass help compile .

Below is an example. It will output the compiled css file to the same directory as the test.scss file.

 file_name = "absolute/path/to/test.scss" system "compass compile #{file_name} --sass-dir . --css-dir ." 

You can specify and interpolate as many parameters as you wish. Also check this to run commands in ruby:

Running command line commands in a Ruby script

0
source share

All Articles