Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort dependencies in project.clj #121

Open
thumbnail opened this issue Feb 20, 2020 · 2 comments
Open

Sort dependencies in project.clj #121

thumbnail opened this issue Feb 20, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@thumbnail
Copy link
Member

thumbnail commented Feb 20, 2020

Context

Our lein-template adds ;; Please keep the dependencies sorted a-z. before the :dependencies-key in project.clj.

There's benefits for sorting this list (friction-free merges, improves readability, ..)

Task

We need a formatter to sort the dependency list

Acceptance criteria

dependency sorting:

(defproject corpus "unreleased"
  :dependencies [[c]
                 [a]])

should be formatted to

(defproject corpus "unreleased"
  :dependencies [[a]
                 [c]])

:exclusion sorting

(defproject corpus "unreleased"
  :dependencies [[a
                  :exclusions [c b]])

should be formatted to

(defproject corpus "unreleased"
  :dependencies [[a
                  :exclusions [b 
                               c]])

note the formatted newlines

Additional resources

inspiration for replacement of the :dependencies can be found here:

(speced/defn replace-ns-form!

sorting might be something like:

(let [deps '[[c] 
             [a :exclusions [c b]]]]
  (->> deps
       (mapv (fn [clause]
               (let [exclusion-index (.indexOf clause :exclusions)]
                 (if (> 0 exclusion-index)
                   clause
                   (update clause (inc exclusion-index) (comp vec sort))))))
       (sort-by first)
       (vec)))
=> [[a :exclusions [b c]] [c]]
@thumbnail thumbnail added the enhancement New feature or request label Feb 20, 2020
@vemv vemv changed the title sort dependencies in project.clj Sort dependencies in project.clj Feb 20, 2020
@vemv
Copy link
Contributor

vemv commented Feb 20, 2020

(speced/defn replace-ns-form!
...is quite reliable in part because ns forms are a quite limited "DSL" without a lot of room for variations, significant whitespace, metadata, comments, quoting/unquoting etc

For most other Clojure code I wouldn't be too trigger-happy with code rewriting, as we currently don't directly use rewrite-clj, so it's not a technique we master.

Eventually we might have some rewrite-clj helpers/abstraction, allowing us to implement small formatters easily and reliably.


If I polished my old https://github.com/reducecombine/autonewline, some of that code might serve as the mentioned abstraction. Also worth studying cljfmt of course

Also important to find the right rewrite-clj version - there's rewrite-clj, rewrite-cljs, and clj-kondo's vendored rewrite-clj which might be interesting

@vemv
Copy link
Contributor

vemv commented Jun 2, 2022

https://github.com/kkinnear/zprint/blob/1.2.3/doc/reference.md#sort-dependencies does this

(I'll add optional zprint support soonish)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants