Search code examples
clojureleiningen

Read project name and version from Leiningen project.clj file


How can I query the project group, name and version from a Leiningen project.clj file using only lein and simple command line utilities without modifying the project file?

I could not find anything useful in lein help, like executing code from the command line arguments.

I am thinking about writing a sed expression, but I am wondering if there is a simpler way using Leiningen.


Solution

  • This is fully(?) supported by Leiningen, sort of. In short:

    $ (lein update-in :version pr -- version ; lein update-in :name pr -- version) | cut -d '"' -f 2
    

    This works by abusing the update-in command to apply clojure.core/pr to the :version or :name project key. (You can see all project keys by using just a colon as the member to pr. The just-a-colon feature is described by lein help update-in.)

    Leiningen requires an actual command too, in addition to update-in. Because pr returns nil, I suppose the updates damage the project map and make it unsuitable for real work. The above demo sidesteps the problem by using the version command to print Leiningen's own version.

    Oddly, the printed version or name appears more than once; thus the cut command to take only the first quoted string on each line. We assume you haven't used quotes in your project name or version!

    You can stack update-in's on one command line, but doing so makes the name and version harder to parse from the output.