July 4, 2011
Automating Compass and CoffeeScript with Virtualenv(wrapper)
By Issac Kelly
Compass and CoffeeScript are tools I've been hearing a ton about lately. If you'd like to read more about those, check other sources. I'm going to show you how I got them working with Virtualenv and Django, but it should apply to pretty much any project, python or not (although virtualenv is made in python, with python in mind, it could be really useful for, say PHP or JS, and maybe Ruby, but I'm less familiar with the tools available there)
Moving on.
Virtualenv has a concept of hooks, places where you can write user scripts when you activate or deactivate your virtual environment. I keep one virtualenv per project and you should too.
In my postactivate script, I check to see if the coffeescript and compass processes are running by checking a 'watching.pid' file. If the watching.pid file is empty, I put the current shell's PID there, and run my `watch_coffee_sass` script.
`watch_coffee_sass` is another script (which I put into my source repo/bin folder, notice the path hacks) which simply starts the coffee and compass processes. It's pointed a a typical django.contrib.staticfiles directory structure (project/static/sass, project/static/coffeescript) and compiles those files on the fly.
In my predeactivate script, I check working.pid for the current shell pid, if they match, I close down the compass and coffeescript processes.
It's pretty simple, and has one drawback that I haven't quite sorted out yet (my bash is weak!): Closing the shell without deactivating leaves your virtualenv leaves your watching.pid file in a broken state.
Here's the code: