Manage dependencies in `project.clj` from the command line
Asked Answered
R

3

6

I would like to search and install clojure dependencies from the commandline.

Does there exists a tool/leiningen command/lein change script?/... that can:

  • search for clojure libraries online
  • rewrite project.clj to include the dependency (latest version)
  • update dependencies?

Much along the lines of npm install --save (--save-dev) and npm search, for those that are familiar with npm (JS/Node package manager).

(Maybe boot provides a more npm-like workflow?)

Rhatany answered 13/2, 2015 at 18:2 Comment(0)
K
6

You could find complete list of all Leiningen plugins at its Plugins Wiki page.

I just looked through Development Tools section and found two plugins that may interest you:

  • lein-plz - a Leiningen plugin for quickly adding dependencies to projects.
  • lein-ancient - a Leiningen plugin to check your project for outdated dependencies and plugins.

And here is an example of adding new dependencies with lein-plz (from its Readme):

$ lein plz add core.async cljs data.json

And an example of updating outdated dependencies with lein-ancient (from its Readme):

$ lein ancient upgrade-profiles [<options>]
Kristlekristo answered 13/2, 2015 at 18:47 Comment(1)
Why it doesn't built to lein? It seems to be an essential feature to me.Stlouis
U
3

I think lein-ancient is what your looking for. I've been using it for quite some time and found it pretty reliable. You can let it do the updates yourself or just get a report and then do it manually. You can also tell it if you just want production versions or are OK with snapshots etc.

It isn't perfect as it relies heavily on the information that developers provide regarding their libraries. I've run into very infrequent problems when developers have made changes and either not incremented version numbers or use an unusual version numbering scheme. However, it is rare.

Personally, I don't like updating lots of libraries all at once. This can make it difficult to identify and fix problems. Therefore, I tend to run lein ancient on the project and then decide whether I want to update all, some or none and then make the changes manually. It is often good to just get the report and then make a quiick check on the source site to see what may have changed either to make sure it isn't something which will break my code or to help reduce the search space should there be an issue. Nothing worse than upgrading a whole bunch of libs and then spending hours tracking down compile/run problems - tends to dampen your enthusiasm!

Utica answered 13/2, 2015 at 21:39 Comment(1)
Thanks, nice. My wording was a bit ambiguous, I wrote update when I meant "rewrite the project.clj file". Still your answer is interesting! ThanksRhatany
U
1

OK, based on clarifications in the comment, I would suggest looking at some of the clojure linters. While most focus on your project code tree, some will also examine your project.clj file. There is no one tool I can think off, but there are a number of separate tools which can help with little parts of what your after. You can use lein tasks and aliases to automate some of the process and if your using emacs, there are some add ons that will help a bit.

  • Eastwood: This one has a milestone to add functionality which will examine the :dependencies section in your project.clj and identify dependencies which are not being used. While not implemented yet, it is worth keeping an eye on and I think it is a useful plugin (written by the author of lein btw)

  • slamhound: This plugin will examine your namespace declarations and clean them up so that they only contain things which are required. A prerequisite for any tool which might be able to re-write your project.clj file IMO

  • kibit and bikeshed: these two will look for possible cleanup and code improvements to help keep your code consistent and more idomatic

Many of these types of tools can be useful, but their effectiveness can be limited by the 'quality' of your clojure code base. I find you get better results with such tools if your code is as consistent and idiomatic as possible. Running bikeshed -> kibit -> slamhound -> eastwood seems to provide good functionality for me.

If your using emacs, you might also find clj-refactor.el useful. Also note that lein does have a search facility - it can be slow if the indexes need to be updated, but after that is quite fast. However, it isn't that functional as a general search facility, but if your just looking to find the library name and version so that you can add them to your dependencies, it can be pretty useful - in the case when I'm looking for a library to solve a problem (as opposed to a specific known library when I just need to get the name and version number) I tend to just use google and some of the sites out there which collect info on available libs.

Utica answered 13/2, 2015 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.