Using Compass from Ruby (Not Shell)

Using Compass from Ruby (not shell)

You're right, there's not really any comprehensive documentation on how to use Compass from Ruby. This is unfortunate, but let's not let little details like documentation stop us!

A First Attempt

When I was looking to do the same thing, I just poked around the Compass source and was able put together this little Ruby script. At first glance it seems to do the 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 options that aren't automatically included when invoking the compiler constructor directly (of which the SASS load_path is one). This can lead to errors when trying to import Compass functions and mixins, such as:

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

Compass <1.0.0 (a.k.a. "the old way")

Here's how to call the compiler without overriding those 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, as of Compass version 1.0.0, Compass.compiler has been deprecated in favor of Compass.sass_compiler, leading to...

Compass >=1.0.0 (a.k.a. "the new way")

With 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!

Compass mixins not found when using directly from ruby

Make sure that you check out the answers on the below post. Between my findings posted here and the suggestions made on the other post, the question is answered.

Using Compass from Ruby (not shell)

Compass from Ruby—SassCompiler not found

After digging a bit into the source, I finally found it!

require 'compass/sass_compiler'

is the missing line!

The final line to run the compilation looks like that:

Compass.sass_compiler.compile!

Thats it.

Btw.: the Compass.sass_compiler method accepts some options (source) which are handed over to the compiler, but using Compass.add_configuration as above does the same Job.

I hope somebody can use this Information, happy compiling!

EDIT

Due to the comments here the complete code that works for my project. It is included in a build script, the following lines are from the initialisation:

require 'compass'
require 'compass/sass_compiler'

Compass.add_configuration({
:project_path => _(),
:output_style => :expanded,
:cache_path => '<path to cache>',
:http_fonts_path => '../fonts',
:fonts_dir => '<relative path to fonts>',
:sass_path => '<path to the scss files>',
:css_path => '<path fot the compiled css>',
:http_images_path => '../img',
:images_path => '<path to images>'
},'custom-name')

And these line run in each compilation:

compiler = Compass.sass_compiler({
:only_sass_files => [
'<path to scss file to compile>'
]})
compiler.compile!

To get an overview of all possible options I recommend a look at the source and at the official documentation of sass and compass.

compass not detecting new ruby version

It turns out that I have an older version of ruby that is installed inside /usr/bin/ruby which is called by compass instead of the new one at /usr/local/bin/ruby.. So I just deleted the old one and updated the ruby symlink to the new version.

Can't use compass after installing it

I asked for help on the official compass Github issue's page and got the answer for this problem.

The GitHub issue is right here.

How to fix this:

When installing compass (v0.12.2), it will download the latest sass v3.3.0.rc3 (cause it's a dependency), but compass won't work with the latest sass. You either upgrade compass to the latest alpha version (0.13.*) or downgrade sass to 3.2.*. I choose the last one:

gem uninstall sass --version 3.3.0.rc.3
gem install sass --version 3.2.18
compass -v # Working :)

Make sure you use the version you got installed. Find out by typing this:

sass --version

How to Configure Ruby to use compass for Sench-touch on Windows

First you need to install Ruby if you do not have it already. Get the latest Windows installer at http://www.rubyinstaller.org.

The installer should automatically add the Ruby folder to your PATH environment variable, so you can run Ruby commands from the command line. Once Ruby is installed, open a command line interface (cmd.exe) and install the required Compass packages using the 'gem' command from Ruby. Do this with the following three commands:

gem install haml
gem install haml-edge
gem install compass

After the packages install, type compass version to see if it installed correctly. You should see something like "Compass 0.11.7 (Antares) Copyright (c) 2008-2012 Chris Eppstein..."

If the gem and compass commands do not do anything, ensure that your Ruby folder is in your path. Check this tutorial if you need help.

The specifics for your config.rb depend on how you have your system and projects set up. Most of the configuration deals with the folder structure of you project. I'll refer you to the Sencha documentation for this:
- Mastering the Compass/SASS Setup with Sencha Touch
- An Introduction to Theming Sencha Touch

Once your config.rb is set up, you can run compass compile in your .scss folder to build the css files.

Compass Installed on WebSpace: Command not found

Your .gem location is not appended to your environmental variable. As seen from the output echo $PATH it isn't appended to your $PATH environmental variable.

Try appending it to your $PATH variable

export GEM_HOME=/my/home/dir/.gem/bin
export GEM_PATH=/my/home/dir/.gem:/usr/lib/ruby/gems/1.8

PATH=$PATH:GEM_HOME:GEM_PATH
export PATH

Finally do a source ~/.bashrc

Update

Appending the correct executable path to the $PATH environment variable will make your executable available within the shell environment.

In this specific scenario the executable location is /my/home/dir/.gem/bin



Related Topics



Leave a reply



Submit