Configure Compass to compile CSS with a specific name and versioning

There are many configuration options for your config.rb file. However, i wanted my main.scss to compile to something like main.v.1.0.1.css automatically without me having to type anything in the command line, besides compass watch or compass compile.

I needed it to be quick and easy to use. I did not want it to produce a different random name for my css file every time i compiled my scss file, simply because i was still in production and this would fill up my css folder with too many junk files.

So i came up with this:

on_stylesheet_saved do |file|
  if File.exists?(file)
    filename = File.basename(file, File.extname(file))
    File.rename(file, css_dir + "/" + filename + ".v.1.0.1" + File.extname(file))
  end
end

You simply add this block of code at the end of your config.rb file.

What it does: every time a css file is compiled, it renames it to main.v.1.0.1.css by  adding the .v.1.0.1 at the end of its name.

So every time you need a different version of your CSS file, you can change the .v.1.0.1 to .v.1.0.2 or whatever best suits your needs.

Of course every time you make such a change, you will have to update your html code:

<link rel="stylesheet" type="text/css" media="all" href="styles/css/main.css" />