leiningen icon indicating copy to clipboard operation
leiningen copied to clipboard

Add `lein add <dependency>` command

Open danielcompton opened this issue 6 years ago • 8 comments

One of the nice things about the NPM/Yarn ecosystem is that it is easy to add a dependency to your project by copy/pasting a line from the project's README which is something like:

npm install --save lodash

This will install the package and add it to the project's package.json file. I wonder if something like this could be nice quality of life improvement for Leiningen users. As a sketch of how this could work:

lein add org.clojure/core.async

Leiningen would then look for the latest release version of the dependency, and update the user's project.clj with the new dependency added to the :dependencies vector. If you wanted to install the dependency to your :dev profile you could do something like

lein add org.clojure/core.async --profile :dev

and Leiningen would add the dependency. Because a project.clj is not pure data, it is probably tricky/impossible to handle this perfectly on 100% of existing project.clj files. However we could still provide a good experience for the 95% (99%?) case of a standard project.clj file.

I'm not aware of any projects that exist for rewriting a project.clj, but perhaps others are aware?

The main challenges I see are:

  • Being able to parse the project.clj file in such a way that you can add the dependency to the right spot in the textual file, and write the file back out again, preserving all of the other parts of the file. Something like this would be a lot easier to do if dependencies were specified in a data file, e.g. deps.edn.
  • Getting indentation right when writing back the dependency. If you were willing to apply a pretty printer to people's project.clj files then this wouldn't be too tricky, but I think that is probably a non starter.

What are people's thoughts on this? Does it seem like a useful kind of feature?

danielcompton avatar May 12 '18 11:05 danielcompton

I'm not aware of any projects that exist for rewriting a project.clj, but perhaps others are aware?

Actually this seems like a special case of lein change.

What about just adding an add-dep function which works with the existing change task?

technomancy avatar May 12 '18 16:05 technomancy

That sounds like what I was looking for. I’ll take a look at this.

danielcompton avatar May 12 '18 20:05 danielcompton

How should lein change be used to add dependencies to the project file from the command line?

carlosblanco avatar Oct 11 '19 19:10 carlosblanco

Here is an example:

lein change :dependencies conj '[org.clojars.benfb/gorilla-repl "0.6.0"]'

onetom avatar Dec 31 '19 05:12 onetom

+1

carina-akaia avatar Apr 04 '20 11:04 carina-akaia

The lein change proposal is lackluster at best. If I know the entire path AND version I want, I can just add them to the file.

I don't want to do lein change :dependencies conj '[org.clojars.benfb/gorilla-repl "0.6.0"]'

I want to do lein add gorilla-repl. This will then list the latest versions for me to choose.

lasiltan avatar Jul 12 '22 06:07 lasiltan

I agree that using conj with lein change as described above isn't ideal, but that is not what I suggested. I said that we should add a new add-dep function so that you can run lein change :dependencies add-dep gorilla-repl.

My point was that this does not belong in a new top-level task.

technomancy avatar Jul 12 '22 16:07 technomancy

+1 very convinient for people coming from other package managers.

kovan avatar Oct 02 '22 21:10 kovan