How can one compile CoffeeScript during the build phase of a Python packages' distutils installation?
coffeescript, distutils, django, pypi, python
Solution
You can roll this into a custom manage.py command. See the official Django documentation here this way the script will be run everytime the server is run always resulting in a clean build of your js.
Problem
I'm working on a Python Django package whose front-end components employ a bit of CoffeeScript. Right now, I have a rather brain-dead external script that takes care of the CoffeeScript compilation. It simply runs a `coffee` compile command for every *.coffee file in a src/coffee/ directory and stores the output in src/static/js -- this similar to how `python ./setup.py build_ext --inplace` stores a C extension's build files in the development source tree. That works for now, but it's pretty cheesy -- it forces a flat directory structure, and modifies the files in src/static (which is the opposite of what "static" implies). I want to be maximally pythonic about things, so I looked into modifying `distutils.ccompiler.CCompiler` to run `coffee` as a subcommand of the setup.py "build_ext" subcommand -- I was envisioning the ability to do things like this: ``` % python ./setup.py build_coffee % python ./setup.py build_coffee --inplace % python ./setup.py build_ext --inplace # implying 'build_coffee --inplace' ``` ... but I found distutils' compiler API to be way too focussed on C compilation nuances that have no analog in this case e.g. preprocessing, linking, etc. I also looked at Cython's code (specifically at Cython's `CCompiler` subclass, which preprocesses .pyx files into .c source) but this looked similarly specialized, and not so appropriate for this case. Does anyone have a good solution for compiling CoffeeScript with a distutils setup.py script? Or, barring that, a good alternative suggestion?