November 5, 2011

Optional dependencies

Both uri-template and css-lite provide optional support for generating JavaScript templates via Parenscript. However, I did not want to make Parenscript a required dependency for either library.

Previously, this was implemented using the #+parenscript read-time conditional in the source files.

That worked ok if you loaded Parenscript before loading css-lite, but there were two problems:

  1. If you initially compiled css-lite without loading Parenscript first, you'd need to go back and re-compile css-lite by hand after loading Parenscript if you wanted the JavaScript output.
  2. If you loaded the css-lite fasls compiled with Parenscript into a fresh Lisp image without loading Parenscript first, you'd get an error.

Both of these error stem from the fact that ASDF didn't know anything about the optional Parenscript dependency.

Didier Verna has written about optional ASDF dependencies previously (make sure to read the asdf-devel thread on optional dependencies Didier links to if you're interested in this). In short, relying on ASDF's :weakly-depends-on seems quite hairy.

I think I found a simple alternate solution for uri-template that seems to work: put all the Parenscript-dependent code into one file, and then use read-time conditionals in the uri-template.asd list of files like so:

:components ((:file "package")
(:file "destructure-uri")
#+parenscript (:file "parenscript-implementation")

You can see the full implementation in the latest patch to uri-template.

Let me know if you have any ideas about this technique, or optional dependencies in general.


nathan said...

The only thing I do not like about this solution is that the ASDF system definition is now not a complete representation of the files contained in the project for the #-parenscript case.

Faré said...

The solution I recommended to the DWIM guys, who adopted it, is to create additional ASDF packages:


That's the only way that preserves the sanity of things, independently from the order in which you compile things, etc.

asd files are cheap.

Vladimir Sedach said...

With the hu.dwim approach, you'd then :require uri-template+parenscript instead of uri-template when you decide to have Parenscript support?