Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one compile CoffeeScript during the build phase of a Python packages' distutils installation?

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?

like image 270
fish2000 Avatar asked Nov 05 '22 00:11

fish2000


2 Answers

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.

like image 114
Adam Siegel Avatar answered Nov 09 '22 08:11

Adam Siegel


You could have a pre-commit hook* that compiles coffescript into javascript.

So everytime you commit a change in the coffescript, the javascript version is updated.

*pre commit hook: the way to do it depends on the VCS you use, and depends on you using a sane VCS.

like image 22
jpic Avatar answered Nov 09 '22 08:11

jpic