4

How can I call compass in my ruby code to compile a project without calling the shell command?

I tried to adapt the solution from Using Compass from Ruby (not shell) but without success. My project structure looks like

assets/scss               (location of uncompiled project files)
assets/css                (location for compiled css)
assets/compass/config.cfg (the compass config file)

I have tried something like this

fixed_options = {
  :project_path => '/path/to/assets,
  :sass_path => 'scss',
  :css_path => 'css'
}
Compass.add_project_configuration '/path/to/assets/compass/config.rb'
Compass.add_configuration fixed_options, 'custom'
Compass.compiler.run

This works, but ONLY if i do by running irb while inside the project root /path/to/assets.

It seems like anything set in fixed_options overrides options in config.rb as desired (or they are merged, or there are two sets of options: it's a bit hard for me to tell), however the :project_path doesn't seem do do anything as compass only seems to care about the directory I run irb from.

Note: I have been trying use the output of Compass.compiler in irb to try and understand what is happening.

Community
  • 1
  • 1
Evan Sosenko
  • 167
  • 9
  • I would appreciate if whoever downvoted this answer could explain why so I can learn how I might write a better or more appropriate question. Thank you. – Evan Sosenko Dec 30 '12 at 07:03
  • It tells what you want and what you tried but does not ask anything. It is not a question. It is not clear what it asked. You need to ask a question on StackOverflow. – sawa Dec 30 '12 at 07:23
  • Thank you. I will rephrase this as a question, however I do not think this will really change the content of post. I do believe that others may benefit from an answer to this question since this could be used in any ruby project that needs to call compass and the documentation is not available. – Evan Sosenko Dec 30 '12 at 07:40

1 Answers1

3

Note: This is possibly not the answer you want as it involves the shell, but it possibly is the answer you want as it involves running a command once from the shell and from then on the assets are recompiled when they change. Maybe that will be enough for you.

The way I run compass is to use a Guardfile.

Here's the relevant part of my Gemfile (I'm using OSX):

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem "sass"  # sassy CSS
  gem "coffee-script" # destroy javascript!
  gem "guard" # file watcher with rules
  gem "guard-coffeescript" # regen coffee
  gem "guard-sass", :require => false # auto generate sass
  gem "rb-fsevent"
  gem "growl" # notifications for OSX
end

The Compass config file:

# Set this to the root of your project when deployed:
http_path = "/"
css_dir = "public/css"
sass_dir = "views/stylesheets"
images_dir = "public/images"
javascripts_dir = "public/js"
project_type = :stand_alone
output_style = :compact
line_comments = false
preferred_syntax = "scss"

In ./Guardfile

# This will generate stuff:

guard 'coffeescript', :input => "coffee", :output => 'app/public/js'

guard 'sass', :input => 'app/views/stylesheets', :output => 'app/public/css', :compass => true, :style => "compressed", :shallow => true

Then I run guard start in a terminal, and it will watch the files for changes and recompile on a change. I leave the terminal window open in the background so I can force recompiles too. YMMV.

ian
  • 12,003
  • 9
  • 51
  • 107